ICSE 2013 Computer Application Q9 Solution

ICSE 2013 Computer Application Q9 Solution

ICSE 2013 Computer Application Q9 Solution

Q. Using the switch statement, write a menu driven program:
(i) To check and display whether the number input by the user is a composite number or not (A number is said to be a composite, if it has one or more than one factors excluding 1 and the number itself).
Example: 4, 6, 8, 9 …
(ii) To find the smallest digit of an integer that is input:
Sample input: 6524
Sample output: Smallest digit is 2

ICSE 2013

Solution
import java.io.*;
/**
 *  class MenuDriven 
 *
  */
public class MenuDriven
{
    public static void main(String args[])throws IOException
    {
        InputStreamReader in=new InputStreamReader(System.in);
        BufferedReader br = new BufferedReader(in);
        int n, count, ch, small, a, i;
        // For menu
        System.out.println("Enter 1 for Composite number");
        System.out.println("Enter 2 for Smallest digit search");
        System.out.print("Enter your choice :");
        ch=Integer.parseInt(br.readLine());     // enter menu option
        switch(ch)
        {
            case 1:                                           // for checking composite number
                count=0;
                System.out.print("Enter a number :");
                n= Integer.parseInt(br.readLine());
                for(i=2; i<=n/2; i++)
                {
                    if(n % i == 0)
                     count++;
                }
                if(count>0)
                 System.out.println("Entered number is Composite number");
                else
                 System.out.println("Entered number is Composite number");
                break;
            case 2:                                        // for checking smallest digit from a number
                System.out.print("Enter a number :");
                n= Integer.parseInt(br.readLine());
                small=99;
                while(n!=0)
                {
                    a = n % 10;
                    if(a < small)
                    {
                        small = a;
                    }
                    n = n / 10;
                }
                System.out.println("Smallest number is " + small);
                break;
            default:
                  System.out.println("Wrong choice");
        }
    }
               
}

Output:
Enter 1 for Composite number
Enter 2 for Smallest digit search
Enter your choice :1
Enter a number :27
Entered number is Composite number

Enter 1 for Composite number
Enter 2 for Smallest digit search
Enter your choice :2
Enter a number :78653
Smallest number is 3

Important java Program

Important java Program

Important java Program

Java programming Example


Question-1.

Write a program using a switch case to find the factor and factorial of a given number. 

If the choice is 1 find Factor and 2 find Factorial of the input number.
Solution:
import java.util.*;
public class Menu
{
    void main()
    {
        int ch,f,i,n;
        Scanner sc=new Scanner(System.in);
        System.out.println("1. Factor 2. Factorial:");
        ch=sc.nextInt();
        switch(ch)
        {
            case 1:
            System.out.println("Enter a number to print Factor:");
            n=sc.nextInt();
            for(i=1;i<=n;i++)
            {
                if(n%i==0)
                {
                    System.out.print(i+" ");
                }
            }
            break;
            case 2:
            System.out.println("Enter a number to print Factorial:");
            n=sc.nextInt();
            f=1;
            for(i=1;i<=n;i++)
            {
                f=f*i;
            }
            System.out.println("Factorial="+f);
            break;
            default:
            System.out.println("Wrong Choice");
        }
    }
}
Question-2.

Write a program to print the following pattern up to n lines.

A
AB
ABC
ABCD
Upto n number[1>=n<=26]
Solution:
import java.util.*;
public class LetterPattern
{
    void main()
    {
        int i,n,j;
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter number 1-26 only");
        n=sc.nextInt();
        if(n>=1 && n<=26)
        {
            for(i=1;i<=n;i++)
            {
                for(j=65;j<=64+i;j++)
                {
                    System.out.print((char)j);
                }
                System.out.println();
            }
        }
        else
        {
            System.out.println("Pattern will not print");
        }
    }
}
Question-3.

Write a program to print the Fibonacci series up to n term.

Solution:
import java.util.*;
public class Fibonacci
{
    void main()
    {
        int f1,f2,fib,i,n;
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter a number:");
        n=sc.nextInt();
        f1=fib=0;
        f2=1;
        for(i=0;i<n;i++)
        {
            f1=f2;
            f2=fib;
            fib=f1+f2;
            System.out.print(fib+" ");
        }
    }
}
Question-4.

Write a program to input a number and find whether it is a Neon Number or not. 

A number is said to be a Neon Number. If it is equal to sum of digits of Square number.
92-> 81->8+1=9
Solution:
import java.util.*;
public class NeonNumber
{
    void main()
    {
        int n,m,a,s=0;
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter a number:");
        n=sc.nextInt();
        m=n*n;
        while(m!=0)
        {
            a=m%10;
            s=s+a;
            m=m/10;
        }
        if(s==n)
            System.out.println("It is a Neon Number");
        else
            System.out.println("It is not a Neon Number");
    }

}

Menu driven program in java with do-while loop

import java.util.*;
public class MenuDrivenWithDoWhileLoop
{
    static void menu()  // Method
    {        
        System.out.println("1. For Prime Number");
        System.out.println("2. For Sum of digits of a Givem number");
        System.out.println("3. For Factorial of a number");
        System.out.println("4. For Exit------->");
    }
    public static void main(String args[])
    {
        // Local variables
        int ch, num, m, sum=0, f, i, key=0;
        Scanner sc=new Scanner(System.in);
        // Do loop started
        do{
            menu();// call the menu method
            System.out.println("Enter your choice----->");
            ch=sc.nextInt();
            System.out.println("Enter a number:");
            num=sc.nextInt();
            switch(ch)
            {
                case 1:
                f=1;
                for(i=2;i<num;i++)
                {
                    if(num%i==0)
                    {
                        f=0; //Flag Variable
                        break;
                    }
                }
                if(f==1)
                    System.out.println("Prime Number");
                else
                    System.out.println("Not Prime Number");
                System.out.println("Press any key to continue");
                key=sc.nextInt();
                System.out.println("\f"); // form feed escap sequence
                break;
                case 2:
                while(num!=0)
                {
                    m=num%10;
                    sum=sum+m;
                    num=num/10;
                }
                System.out.println("Sum="+sum);
                System.out.println("Press (0-9) key to continue");
                key=sc.nextInt();
                System.out.println("\f");
                break;                
                case 3:
                f=1;
                for(i=1; i<=num; i++)
                {
                    f=f*i;
                }
                System.out.println("Factorial="+f);
                System.out.println("Press (0-9) key to continue");
                key=sc.nextInt();
                System.out.println("\f");
                break;
                case 4:
                System.exit(0);
                default:
                System.out.println("Wrong Choice");
                System.out.println("Press (0-9) key to continue");
                key=sc.nextInt();
                System.out.println("\f");
            }
        }while(ch != 4);
    }
}

area of a circle in java

area of a circle in java

Java program  to calculate  area of  a circle


Write a program to calculate the area of a circle by entering the radius. Formula: area=PI*r*r.

Logic:
To calculate the area of a circle we use formula area= 22/7 * r*r. For this, we input the radius value and PI value from Math.PI.

/*
        Calculate Circle Area using Java Example
        This Calculate Circle Area using Java Example shows how to calculate
        area of a circle using its radius. 
        This program will help for ICSE, ISC and BCA students
*/
 import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
 public class CalculateCircleAreaExample
{
         public static void main(String[] args)
        {
              
                int radius = 0;                            //Local variable
                System.out.println("Please enter radius of a circle");
              
                try
                {
                        //get the radius from a console
                     InputStreamReader in=new InputStreamReader(System.in);
                      BufferedReader br = new BufferedReader(in);
                        radius = Integer.parseInt(br.readLine());
                }
                //if an invalid value was entered
                catch(NumberFormatException ne)
                {
                        System.out.println("Invalid radius value" + ne);
                        System.exit(0);
                }
                catch(IOException ioe)
                {
                        System.out.println("IO Error :" + ioe);
                        System.exit(0);
                }
              
                /*
                 * Area of a circle is
                 * Formula area=pi * r * r
                 * where r is a radius of a circle.
                 */
              
                // NOTE: use Math.PI constant to get the value of Pi
                double area = Math.PI * radius * radius;
               // Print the value of the area of a circle
                System.out.println("Area of a circle is " + area);
        }
}


Output:
Please enter radius of a circle
12

Area of a circle is 452.3893421169302

Sl. No.
Variable Name
Data Type
Purpose
1
radius
double
Store the radius value
2
area
double
Store the area of a circle
3
main()
static void
Main function
4
in
InputStreamReader
To instantiate the InputStreamReader class which exists in java.io package
5
br
BufferedReader
To instantiate the BufferedReader class which exists in java.io package using the above variable as the passing parameter


ICSE Computer Question Answer

ICSE Computer Question Answer

ICSE Computer Question Answer

Q. a) State the difference between token and identifier, variable.
Ans. A token is the smallest individual unit in a program for e.g.: Keyword, Identifiers, Literals etc. where as an identifier is the name given to different parts of a program e.g. variable, functions, classes etc.
Variable: A memory location that stores a value is known as variable. E.g. int a;
    b) What is variable. Give an example.
Ans. Place holder of data in computer memory. E.g. Int a,b,c;
Q. Define Byte code and JVM.
Ans. Byte code is the compiled format for Java programs. Once a Java program has been converted to byte code, it can be transferred across a network and executed by Java Virtual Machine (JVM). Byte code files generally have a .class extension
Ans: JVM is a platform-independent execution environment that converts Java byte code into machine language and executes it. Most programming languages compile source code directly into machine code that is designed to run on a specific microprocessor architecture or operating system, such as Windows or UNIX. A JVM -- a machine within a machine -- mimics a real Java processor, enabling Java byte code to be executed as actions or operating system calls on any processor regardless of the operating system.
Q. Example the term Object and Class using an example. 
Ans. Object: An instance of a class called Object. Table is an instance of class Furniture.
Class: Blue print of an object is called Class. Example, mango, apple and orange are members of the class fruit.
Q. Name four OOP’S principles.
Ans. Abstraction, Encapsulation, Polymorphism, Inheritance
Abstraction: Abstraction refers to the act of representating essential features without including the background details or explanations.
Encapsulation: The wrapping up of data and methods into a single unit is called Encapsulation.
Polymorphism: polymorphism refers to a programming language's ability to process objects differently depending on their data type or class. More specifically, it is the ability to redefine methods for derived classes. For example, given a base class shape, polymorphism enables the programmer to define different area methods for any number of derived classes, such as circles, rectangles and triangles. No matter what shape an object is, applying the area method to it will return the correct results.
Inheritance: Inheritance is the process by which objects of one class acquire the properties of objects of anther class. Different kinds of objects often have a certain amount in common with each other. Mountain bikes, road bikes, and tandem bikes, for example, all share the characteristics of bicycles (current speed, current pedal cadence, current gear). Yet each also defines additional features that make them different: tandem bicycles have two seats and two sets of handlebars; road bikes have drop handlebars; some mountain bikes have an additional chain ring, giving them a lower gear ratio.
Q. Mention any two attributes required for class declaration.
Ans: Variables and function or state and behaviour.
Q. Differentiate between base and derived class.
The base class is a class whose properties will be inherited and the derived class is a class which inherits the base class. E.g. Of base class is Super and derived class is sub.
Q. What is data types?
A data type in a programming language is a set of data with values having predefined characteristics. Examples of data types are: int, float,char etc.
The following chart summarizes the default values for the above data types.
Data Type
Default Value (for fields)
byte
0
short
0
int
0
long
0L
float
0.0f
double
0.0d
char
'\u0000'
String (or any object)  
null
boolean
false


Java project class IX Maria Day School

Java project class IX Maria Day School

Q1. Write a program to find net pay where basic  salary taken as input.
da= 25% of basic salary
ta= 2% basic salary
pf= 15% basic salary
gross pay= basic salary+da+ta
net pay=gross pay – pf
Solution:
import java.util.*;
//Start of clas Employee
public class Employee
{
    public static void main(String args[])
    {
        //Local variables
        double bs,da,ta,pf,gp,np;
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter Basic salary:");
        bs=sc.nextDouble();
        da=bs*25.0/100.0;
        ta=bs*2.0/100.0;
        pf=bs*15.0/100.0;
        gp=bs+da+ta;
        np=gp-pf;
        System.out.println("Gross payment:"+gp);
        System.out.println("Net Payment :"+np);
    }//end of main
}//end of class

Q2. Write a program to input radius and find volume and surface area of a Sphere.
[ sa=4  π r2      v=4/3 π r3 ]
Solution
import java.util.*;
//Start of clas Sphere
public class Sphere
{
    public static void main(String args[])
    {
        //Local variables
        double v,sa,r,pi=314;
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter Radius of Sphere:");
        r=sc.nextDouble();
        sa=4*pi*r*r;
        v=(double)4/3*pi*r*r*r;
        System.out.println("Surface area="+sa);
        System.out.println("Volume  ="+v);
    }
}end of the class







Q3. Write a program to accept a year and check whether the year is Leap year or not. If the year in century then it is divisible by 400. If it is non-century year then it is divisible by 4 but not 100.
Solution:
import java.util.*;
//Start of clas Leap Year
public class LeapYear
{
    public static void main(String args[])
    {
        //Local variables
        int year;
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter year :");
        year=sc.nextInt();
        if ((year % 4 == 0) && year % 100 != 0)
        {
            System.out.println(year + " is a leap year.");
        }
        else if ((year % 4 == 0) && (year % 100 == 0) && (year % 400 == 0))
        {
            System.out.println(year + " is a leap year.");
        }
        else
        {
            System.out.println(year + " is not a leap year.");
        }
    }

} end of the class