IX CLASS RECORD PROGRAMS

ORDER OF THE PROGRAMS:
Experiment no
Order
Program Headings
1
DuckNumber
DuckNumber
2
Isfibonacci
To check whether number exists in Fibonacci series or not
3.
Special
Special  Number
4
Switch
Menu driven program  for Perfect or Palindrome number t
5
Encode
Encoding  String
6
RemoveDupchar
Removing duplicate characters in  string
7
revwords
Reverse words in a String
8
Short_long_word
Displaying shortest and longest word in a String 
9
Pattern
Pattern-2
10
Triangle
Pattern-1
11
Linear Search
Linear Search
12
BinarySearch
Binary Search
13
Selection
SelectionSort
14
City
Displays STD  code for entered city
15
Electricity
Generating electricity bill
16
Choice
Function overloading-1
17
Generation
FunctionOverloding -2
18
Stringpro
Program using parameterized constructor
19
Series
 Constructor Overloading
20
Taximeter
Program using default constructor


***Everyone should follow this order while writing the programs.
For Example : This is a reference example for
“How to present programs in your RECORD”
****Left Side page****
****Right Side Page****
Question :

The class  inputs a number and checks if it a DUCK Number or not



Variable Descripition:

variables
Data Types
Description
duck
boolean
Stores true when 0 present in that digit.
temp
int
A copy of original value is maintained in this variable and works with the logic
Lastdigit
int
Store the last digit
num
int
Original value accepted from user








Sample Input:
Enter a number : 73
Sample Output:
73 is not a DUCK Number.

Sample Input:
Enter a number : 110
Sample Output:
110 is a DUCK Number.
DUCK NUMBER

import java.util.*;
public class DuckNumber
{
    public static void main(String                               args[])
    {
        Scanner ob=new                               Scanner(System.in);
        System.out.println("Enter             the number to be checked");
        int num=ob.nextInt(); 
                              //Accepting the                                                      input.
        boolean duck=false;
        int temp=num;//storing a safe                                                            copy
        while(temp!=0)
        {
            int lastdigit= temp%10;
                      //Extracting the Last Digit
            if(lastdigit==0) 
                      //checking zero in the                                given number
            {
                duck=true;
                break;
            }
            temp=temp/10;
        }
        if(duck==true)
        {                                      System.out.println(num+"            is a Duck Number.");
              //printing the output
        }
        else
        {           
          System.out.println(num+"
          is  not a Duck Number.");              //printing the output
        }
    }
}

·           Question should be placed in the left hand side page as shown above.
·           Whatever variables are present in your Programs, you have to mention variable description for your program on your left hand side Page(after the question) as shown above.
·           Sample input and corresponding Sample Output should be mentioned on the Left hand Page, Wherever program ends, in the right side  page.
(For Suppose: If Program ends in the 3rd Page (right side), You have to mention the Sample Input and Output on the 3rd Page Left side .
·           On the right side, You have mention the Program Heading as shown above.
·           After Heading on Right side, You have to write the Program as shown above.
·           Along with the Programs, You have to Mention Atleast 6 Comment Lines regarding the Program as shown above.
·           You should write your Record with Black Ink Pen.
·           For presenting Side-Heading & Comment Lines, Use different Colour Pens.
·           Cover Your Record with Purple Colour Chart and also Cover a Transparent Sheet on it.
·           Present Your Record Properly.
·            
1
DuckNumber
Question :
The class  inputs a number and checks if it a DUCK Number or not

import java.util.*;
public class DuckNumber
{
    public static void main(String args[])
    {
        Scanner ob=new Scanner(System.in);
        System.out.println("Enter the number to be checked.");
        int num=ob.nextInt();

        boolean duck=false;
        int temp=num;
        while(temp!=0)
        {
            int lastdigit=temp%10;
            if(lastdigit==0)
            {
                duck=true;
                break;
            }
            temp=temp/10;
        }
        if(duck==true)
        {
            System.out.println(num+" is a Duck Number.");
        }
        else
        {
            System.out.println(num+" is not a Duck Number.");
        }
    }
}


Sample Output:
Enter a number : 73
73 is not a DUCK Number.
Enter a number : 110
110 is a DUCK Number.



********These Should be placed on Left Hand side Page*********
2
To check whether number exists in Fibonacci series or not
Question :
The class IsFibonacci inputs a number and checks whether it belongs to the Fibonacci Series or not

import java.io.*;
class IsFibonacci
{
   public static void main(String args[])throws IOException
   {
     BufferedReader br=new BufferedReader(new                                                              InputStreamReader(System.in));
     System.out.print("Enter a number : "); // Inputting a number
     int n = Integer.parseInt(br.readLine());
     if(n<0)
        System.out.println("Kindly enter a positive number.");
     else
     {
         int a=0, b=1 ,c=0;/* 'a' is the 1st term, 'b' is the 2nd                                  term and 'c' is the 3rd term 'c'                                    stores the last generated term of the                                Fibonacci series */
     
          while(c<n) // Loop goes on till the 3rd term is less than                          the given number
          {
              c = a + b; // Generating the terms of Fibonacci Series
              a = b;
              b = c;
          }
     
          /* When the control comes out of the while loop, either               the 3rd term is equal to the number or greater than it */
        
           if(c==n) // If the last term = number, then it belongs                                                    to Fibonacci Series
           System.out.println("Output : The number belongs to                                                     Fibonacci Series");
           else
           System.out.println("Output :The number does not                                           belong  to Fibonacciseries" );
     }
   }
}
Output:
1. Enter a number : 377
Output : The number belongs to Fibonacci Series.

2. Enter a 2 digit number : 100
Output : The number does not belong to Fibonacci Series.

3.
Special  Number

Question:
Write a program to input a number and print whether the number is a special or not. a number is said to be a special number,if the sum of the factorial of the digits of the number is same as the original number.e.g:145=1!+4!+5!=1+24+120=145

import java.io.*;
import java.util.*;
public class Special
{
    public static void main(String args[])throws IOException
    {
        int m,n,i,d,f=1,s=0;
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter a number:");
        n=sc.nextInt();
        m=n;
        while(m!=0)
        {
            d=m%10;
            for(i=1;i<=d;i++)
            f=f*i;
            s=s+f;
            f=1;
            m=m/10;
        }
        if(s==n)
        {
            System.out.println("The sum of the factorial of the digits="+s);
            System.out.println(n+"is a special number:");
        }
        else
        {
            System.out.println("The sum of factorial of digits ="+s);
            System.out.println(n+"is not  special number");
        }
    }
}
  Sample output:
      The sum of the factorial of the digits=145
      145is a special number:       
Variable description:
 Variable    Data type   Description
     n          int      to accept a number.       
     m          int      to copy thevalue of n.
     i          int       loop variable
     d          int       to extract each digit from the number.
     f          int      to find the factorial of each digit.
     s          int      to store the sum of factorial of each digit
                                                         


4
Menu driven program  for Perfect or Palindrome number t
Question :
Write a menu driven class to accept a number from the user and check
whether it is a palindrome or a perfect number.
(i) Palindrome number :(A number is Palindrome which when read in reverse order is same as in the right order)
      Example: 11,101,151,etc.
(ii) Perfect number : (A number is called a perfect number if it is equal to the sum of its factors other than the number itself.)
      Example: 6=1+2+3


    import java.io.*;
    public class Switch
    {
        public static void main(String args[])throws IOException
        {
            int i,n,p,d,r=0,ch,s=0;
            BufferedReader br= new BufferedReader(new                                                     InputStreamReader(System.in));
            System.out.println("Enter 1 to checkPalindrome number");
            System.out.println("Enter 2 to check Perfect number :");
            System.out.println("Enter your choice: ");
            ch=Integer.parseInt(br.readLine());
            switch(ch)
            {
                case 1:
                System.out.println("Enter a number: ");
                n=Integer.parseInt(br.readLine());
                p=n;
                do
                {
                    d=n%10;
                    r=r*10+d;
                    n=n/10;
                }
                while(n!=0);
                if(r==p)
                System.out.println("It is a palindrome number:");
                else
                System.out.println("It is not  palindrome number;");
                break;
                case 2:
                System.out.println("Enter a number:");
                n=Integer.parseInt(br.readLine());
                for(i=1;i<n;i++)
                {
                    if(n%i==0)
                    s=s+i;
                }
                if(s==n)
                System.out.println("It is a perfect number");
                else
                System.out.println("It is not a perfect number");
                break;
                default:
                System.out.println("It is a wrong choice");
            }
        }
    }
Variable description:
 Variable    Data type   Description
     n         int       to accept a number.       
     i         int       to run the loop.
     p         int       to store the number
     d         int       to storethe remainder with the loop
     r         int       to store the reversed number
    ch         int       to enter the choice
     s         int       to store the sum of the factors of  number

       

5
Encoding  String

Question :Write a program to accept a String and display the new String
after encoding the given string.One of the simplest way is to replace each letter  by a letter at fixed distance ahead or behind in the alphabet,where the alphabet is assumed to wrap around(i.e.,'A' follows 'Z').In our program encodig=2 means each  character moves two characters ahead.

import java.io.*;
public class Encode
{
 public static void main(String args[])throws IOException
 {
     BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
     int i,a,c=0;
     String str;
     char ch,chr=0;
     System.out.println("Enter your string to be coded");
     str=br.readLine();
     a=str.length();
     System.out.println("Encoded :2");
     System.out.println("Encoded string:");
     for(i=0;i<a;i++)
     {
         ch=str.charAt(i);
         if((ch>='a'&&ch<='x')||(ch>='A'&&ch<='x'))
         c=(int)(ch+2);
             if((ch>='y'&&ch<='z')||(ch>='Y'&&ch<='Z'))
         c=(int)(ch-24);
         chr=(char)(c);
             System.out.print(chr);
      }
    }
}
Variable description:
 Variable    Data type   Description
     i         int         to runthe  loop for (a-1)times    
     a        int         to calculate the length of the string
     c        int         to store the ASCII value of encoded character
   str      string        to accept the string
    ch       char         to accept one character with the loop
   chr       char         to store the encoded character
Sample Output:
Enter your string to be coded
COMPUTERS
Encoded :2
Encoded string:
EQORWVGTU

     

Comments