0% found this document useful (0 votes)
15 views

Class 9 - Computer Programming

Uploaded by

rajshreeg259
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Class 9 - Computer Programming

Uploaded by

rajshreeg259
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

CLASS IX : COMPUTER PROGRAMMING

Question 1.
Write a program in Java to accept a two digit-number. Add the sum of its digits to the product
of its digits. If the value is equal to the input number, display the message "Special 2-digit
Number"; otherwise display message "Not Special 2-digit Number"
Consider the number 59.
sum of digits =5+9=14
Product of its digits =5x9= 45
Sum of the sum of digits and product of digits= 14+45=59
PROGRAM:
import java.util.*; //IMPORTING PACKAGES

public class Special2_Digit_Number


{ //Declaring class
public static void main(String args[])
{// Defining main function
Scanner sc= new Scanner(System.in);
//Preparing for input.
int n1,d, n2, s=0 , p=1 , sum;
//Declaring and initializing variables
System.out.println(" Input a number");
//Asking for input
n1= sc.nextInt(); // Taking input
if(n1>=10&&n1<=99) //Comparing
{
n2=n1; // Assigning
while(n2>0) // Checking entered number n2 is zero or not
{// Start of while loop
d=n2%10; // Finding remainder
s=s+d; // Finding sum of digit
p=p*d; // Finding product OF Digit
n2/=10; // Finding quotient value
}// End of while loop

sum= s+p; // Finding sum of sum of digits and product of digits


if(n1==sum) // Comparing
System.out.println(n1+" is a Special- 2 Digit Number");
else
System.out.println(n1+" is not a Special- 2 Digit Number");
}
else
{
System.out.println(n1+" is not a Two Digit Number");
}
}// End of main()
} //End of class

VARIABLE DESCRIPTION
VARIABLE NAME DATA TYPE PURPOSE / DESCRIPTION
n1 int To input a number
n2 int To assign value to variable
d int To find and store remainder
s int To find and store sum of digits
p int To find and store product of digits
sum int To find and store the sum value
Output :

Input a number
49
49 is a Special- 2 Digit Number
Input a number
87
87 is not a Special- 2 Digit Number

Question 2.
Write a Menu driven program in Java using switch break statement to find the following options for
a circle i) Calculate Diameter
ii)Calculate Circumference
iii) Calculate area
PROGRAM :
import java.util.*;//Importing Packages
public class MENU_CIRCLE
{ //Declaring class
public static void main( String args[])
{//Defining main()
Scanner sc=new Scanner(System.in);
// Preparing for Input
int ch;
double r;
System.out.println("Circle Menu :");
System.out.println("1. Calculate Diameter");
System.out.println("2. Calculate Circumference");
System.out.println("3. Calculate area");
System.out.println("Enter Your Choice(1-3):");//Creating Menu
ch=sc.nextInt();//Taking Input
switch(ch)
{//start of switch break statement
case 1: System.out.println("Enter the radius:");
r= sc.nextDouble();
double dia=2*r;//Finding diameter
System.out.println("Diameter of the circle is :"+dia);
break;//Transfer the control
case 2: System.out.println("Enter the radius:");
r= sc.nextDouble();
double cir =2*Math.PI*r; // Finding Circumference
System.out.println("Circumference of the circle is:"+cir);
break;
case 3: System.out.println("Enter the radius:");
r= sc.nextDouble();
double area =Math.PI*Math.pow(r,2); //Finding area
System.out.println("Area of the circle is:"+area);
break; //Transfer the control
default : System.out.println("Invalid Choice");
}//End of switch break statement
}//End of main()
}//End of class
VARIABLE DESCRIPTION
VARIABLE NAMES DATA TYPE DESCRIPTION
ch int To input the choice.
r double To input the radius of the circle.
dia double To find and store the diameter value .
cir double To Find and store the circumference value.
area double To find and store the area value.

OUTPUT :

Circle Menu :
1. Calculate Diameter
2. Calculate Circumference
3. Calculate area
Enter Your Choice(1-3):
1
Enter the radius:
2.5
Diameter of the circle is :5.0
Circle Menu :
1. Calculate Diameter
2. Calculate Circumference
3. Calculate area
Enter Your Choice(1-3):
3
Enter the radius:
3.5
Area of the circle is:38.48451000647496
Question 3.
Write a program in Java to input a number and display each digit of that number in different line.
Sample Input: 892
Sample Output: 2
9
8
PROGRAM :
import java.util.*;
public class Display_digit
{
public static void main( String args[])
{
Scanner sc= new Scanner(System.in);
int m, n1, n2 ,n3, d, len=0;
System.out.println( "Input a number");
n1= sc.nextInt();// Taking input
n2=n3=n1;
while(n1>0)
{
len++;// len=len+1 Finding total number of digits
n1/=10;
}
System.out.println("The digits are:\n");
while(len!=0)
{//Start of while loop
d=n2%10;// Extracting digits
System.out.println(d);// Displaying the digit
n2/=10;
len=len-1;// Update expression
}//End of while loop
}//End of main()
}//End of class

OUTPUT : Input a number


892
The digits are:

2
9
8
Input a number
345
The digits are:

5
4
3
Question 4.

Write a program in Java to print the following format:


*
**
***
****
*****
******

PROGRAM :
public class Pattern_12
{// Declaring class
public static void main( String args[])
{
System.out.println(" Pattern ");
int x,y;//Declaring variables
for(x=1;x<=6;x++)
{//Start of outer for loop
for(y=1;y<=x;y++)
{//Start of inner for loop
System.out.print("*"+" ");
//Displaying output
}//End of inner for loop
System.out.println();
//Giving line break
}//End of outer for loop

}//End of main()
}//End of class

OUTPUT :

*
**
***
****
*****
******

VARIABLE DESCRIPTION:
VARIABLE NAMES DATATYPE DESCRIPTION
x int outer for loop variable /counter variable
y int inner for loop variable / counter variable
Question 5.
Write a program in Java to print the following format :
*****
****
***
**
*
*

PROGRAM :

public class Pattern_6


{
public static void main( String args[])
{
System.out.println(" Pattern ");
int x,y;//Declaring variables
for(x=5;x>=1;x--)
{//Start of outer for loop
for(y=1;y<=x;y++)
{//Start of inner for loop
System.out.print("*"+" ");
//Displaying output
}//End of inner for loop
System.out.println();
//Giving line break
}//End of outer for loop
}//End of main()
}//End of class

OUTPUT :
*****
****
***
**
*
*

VARIABLE DESCRIPTION:
VARIABLE NAMES DATATYPE DESCRIPTION
x int outer for loop variable /counter variable
y int inner for loop variable / counter variable

You might also like