KEMBAR78
Program Codes For Internal Assessment (Class 9) | PDF | Parameter (Computer Programming) | Data Type
0% found this document useful (0 votes)
963 views16 pages

Program Codes For Internal Assessment (Class 9)

The document contains 11 code examples that demonstrate various Java programming concepts such as: 1) Calculating the area of a circle and volume of a cylinder 2) Converting between Celsius and Fahrenheit temperatures 3) Swapping the values of two variables 4) Finding the real and imaginary roots of a quadratic equation 5) Checking if a number is even or odd 6) Determining if a character is uppercase, lowercase, or non-alphabetic 7) Identifying if a character is a vowel or consonant 8) Calculating the factorial of a number 9) Determining if a number is prime or composite 10) Generating terms of the Fibonacci sequence Each example includes the

Uploaded by

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

Program Codes For Internal Assessment (Class 9)

The document contains 11 code examples that demonstrate various Java programming concepts such as: 1) Calculating the area of a circle and volume of a cylinder 2) Converting between Celsius and Fahrenheit temperatures 3) Swapping the values of two variables 4) Finding the real and imaginary roots of a quadratic equation 5) Checking if a number is even or odd 6) Determining if a character is uppercase, lowercase, or non-alphabetic 7) Identifying if a character is a vowel or consonant 8) Calculating the factorial of a number 9) Determining if a number is prime or composite 10) Generating terms of the Fibonacci sequence Each example includes the

Uploaded by

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

Program Codes

1 Write a program to calculate of area of a circle.


public class area_circle
{
public static void main(String args[])
{
double r=5;
double area=3.14*r*r;
System.out.println("Area of circle = "+area);
}
}

Output:
Area of circle = 78.5

Variable and Description


Name of the variable Data Type Purpose/Description
r double Radius of circle
area double Area of circle

2 Write a program to calculate volume of a cylinder


public class vol_cylinder
{
public static void main(String args[])
{
double r=5,h=7;
double vol=3.14*r*r*h;
System.out.println("Volume of cylinder = "+vol);
}
}

Output:
Volume of cylinder = 549.5

Variable and Description


Name of the variable Data Type Purpose/Description
r double Radius of cylinder
h double Height of cylinder
vol double Volume of cylinder
3 Write a program to accept temperature in celcius as parameter and convert temperature to Fahrenheit
public class pass_parameter
{
public static void main(double c)
{
double f;
f=9.0*c/5.0+32;
System.out.println(c+" degree C = "+f+" degree F");
}
}
Output:
-40.0 degree C = -40.0 degree F

Variable and Description


Name of the variable Data Type Purpose/Description
c double Temperature in celcius
f double Temperature in fahrenheit
4 Write a program to accept two variables as parameter and swap their values.
public class swap_variables
{
public static void main(int a,int b)
{
int temp;
System.out.println("Values before swap");
System.out.println("Value of a = "+a);
System.out.println("Value of b = "+b);
temp=a;
a=b;
b=temp;
System.out.println("Values after swap");
System.out.println("Value of a = "+a);
System.out.println("Value of b = "+b);
}
}

Output:
Values before swap
Value of a = 7
Value of b = 100
Values after swap
Value of a = 100
Value of b = 7

Variable and Description


Name of the variable Data Type Purpose/Description
a int One variable storing value
b int Another variable storing value
temp int Temporary variable
5 Write a program to accept the value of a,b,c of a quadratic equation through Scanner class and display the real roots
and for imaginary roots, display proper messages.
import java.util.*;
public class quadriac_roots
{
public static void main(String args[])
{
int a,b,c; double r1,r2;
Scanner sc=new Scanner(System.in);
System.out.println("Enter value of a, b and c");
a=sc.nextInt();
b=sc.nextInt();
c=sc.nextInt();
int d=b*b-4*a*c;
if(d>=0)
{
System.out.println("Roots are real.......");
r1=(-b+Math.sqrt(d))/(2.0*a);
r2=(-b-Math.sqrt(d))/(2.0*a);
System.out.println("Roots are ("+r1+","+r2+")");
}
else
System.out.println("Roots are imaginary......");
}
}

Output:
Enter value of a, b and c
1
-4
4
Roots are real.......
Roots are (2.0,2.0)

Variable and Description


Name of the variable Data Type Purpose/Description
a int Value of a in ax2+bx+c=0
b int Value of b in ax2+bx+c=0
c int Value of c in ax2+bx+c=0
d int Discriminant b2-4ac
r1 double 1st real root
r2 double 2nd real root
6 Write a program to accept a number using InputStreamReader class and display whether the even number or odd
number.
import java.io.*;
public class even_odd
{
public static void main(String args[]) throws IOException
{
InputStreamReader ir=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(ir);
int n;
System.out.println("Enter the number : ");
n=Integer.parseInt(br.readLine());
if(n%2==0)
System.out.println(n+" is even.......");
else
System.out.println(n+" is odd........");
}
}
Enter the number :
3
3 is odd........

Variable and Description


Name of the variable Data Type Purpose/Description
n int The number that is checked whether it even or odd
7 Write a program and accept a letter and check whether the accepted character is in uppercase or in lowercase.
import java.util.*;
public class case_check
{
public static void main(String args[])
{
char ch;
Scanner sc=new Scanner(System.in);
System.out.println("Enter the character : ");
ch=sc.next().charAt(0);
if(ch>='A' && ch<='Z')
System.out.println(ch+ "is in uppercase");
else
if(ch>='a' && ch<='z')
System.out.println(ch+ " is in lowercase");
else
System.out.println(ch+ " is non-alphabet");
}
}

Output:
Enter the character :
a
a is in lowercase

Variable and Description


Name of the variable Data Type Purpose/Description
ch char It is character accepted by user

8 Write a program to accept a letter and check whether is a vowel or consonant. (Use switch case)
import java.util.*;
public class vowel_consonant
{
public static void main(String args[])
{
char ch;
Scanner sc=new Scanner(System.in);
System.out.println("Enter the character : ");
ch=sc.next().charAt(0);
if((ch>='a' && ch<='z') || (ch>='A' && ch<='Z'))
{
switch(ch)
{
case 'a':
case 'A':
case 'e':
case 'E':
case 'i':
case 'I':
case 'o':
case 'O':
case 'u':
case 'U': System.out.println(ch+" is vowel");
break;
default: System.out.println(ch+" is consonant");
}
}
}
}

Output:
Enter the character :
c
c is consonant
Enter the character :
A
A is vowel

Variable and Description


Name of the variable Data Type Purpose/Description
ch char It is character accepted by user
9 Write a program to accept a number and display factorial of number
import java.util.*;
public class factorial
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int n,i,fact=1;
System.out.println("Enter the number : ");
n=sc.nextInt();
for(i=1;i<=n;i++)
fact*=i;
System.out.println("Factorial of "+n+" = "+fact);
}
}

Enter the number :


5
Factorial of 5 = 120

Variable and Description


Name of the variable Data Type Purpose/Description
i Int Variable whose values from 1 to n
n Int The number whose factorial is to calculated
fact int Storing factorial
10 Write a program to accept a number and display whether the number is a prime number or composite number.
import java.util.*;
public class prime_composite
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int n,i,c=0;
System.out.println("Enter the number : ");
n=sc.nextInt();
if(n==1)
System.out.println(n+" is neither prime nor composite");
else
{
for(i=1;i<=n;i++)
if(n%i==0)
c++;
if(c==2)
System.out.println(n+" is prime");
else
System.out.println(n+" is composite");
}
}
}
Enter the number :
5
5 is prime
Variable and Description
Name of the variable Data Type Purpose/Description
i int Variable whose value changes from 1 to n
n int Number which is checked whether it is prime or composite
c int Counting number of factors
11 Write a program to display n number of terms of Fibonacci series.
import java.util.*;
public class fibonacci
{
public static void main(String args[])
{
int n,f1=0,f2=1,f3,i;
Scanner sc=new Scanner(System.in);
System.out.println("Enter the no. of terms to be displayed : ");
n=sc.nextInt();
System.out.println("Fibonacci series :-");
System.out.print(f1+","+f2+",");
for(i=3;i<=n;i++)
{
f3=f1+f2;
System.out.print(f3+",");
f1=f2;
f2=f3;
}
}
}

Output:
Enter the no. of terms to be displayed :
10
Fibonacci series :-
0,1,1,2,3,5,8,13,21,34,

Variable and Description


Name of the variable Data Type Purpose/Description
n Int No. of Fibonacci terms
i Int Variable changing from 2 to n
f1 Int 1st term of series
f2 Int 2nd term of series
f3 int 3rd term of series (adding previous 2 terms)
12 Write a program to accept a number and check whether the number is palindrome number of not.
import java.util.*;
public class palindrome
{
public static void main(String args[])
{
int n,n1, digit,rev=0;
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number : ");
n=sc.nextInt();
n1=n;
while(n1>0)
{
digit=n1%10;
n1/=10;
rev=rev*10+digit;
}
if(rev==n)
System.out.println(n+" is palindrome");
else
System.out.println(n+" is not palindrome");
}
}

Output:
Enter the number :
123
123 is not palindrome

Variable and Description


Name of the variable Data Type Purpose/Description
n Int Number which is checked whether palindrome or not
n1 Int Temporary variable storing value of n
digit Int Digits of the number accepted
rev int Storing the number after reversing
13 Write a program and accept a number and check whether it is a BUZZ number or not.
import java.util.*;
public class BUZZ
{
public static void main(String args[])
{
int n;
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number : ");
n=sc.nextInt();
if(n%10==7 || n%7==0)
System.out.println(n+" is buzz number");
else
System.out.println(n+" is not buzz number");
}
}
Output:
Enter the number :
127
127 is buzz number

Variable and Description


Name of the variable Data Type Purpose/Description
n int The number which is accepted and checked whether is a buzz no.
14 Write a program to accept a number and display whether the number is a spy number or not.
import java.util.*;
public class spy_number
{
public static void main(String args[])
{
int n,n1,sum=0,prod=1,digit;
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number : ");
n=sc.nextInt();
n1=n;
while(n1>0)
{
digit=n1%10;
sum+=digit;
prod*=digit;
n1/=10;
}
if(sum==prod)
System.out.println(n+" is spy number");
else
System.out.println(n+" is not spy number");
}
}
Output:
Enter the number :
1124
1124 is spy number

Variable and Description


Name of the variable Data Type Purpose/Description
n int Number which is checked whether it is a spy number
n1 Int Temporary variable storing n
sum Int It stores the sum of digits
prod Int It stores the product of digits
digit int It stores the digit in number
15 Write a program to accept a number and display whether the number is an Armstrong number or not.
import java.util.*;
public class armstrong_number
{
public static void main(String args[])
{
int n,n1,sum=0,digit,c=0;
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number : ");
n=sc.nextInt();
n1=n;
while(n1>0)
{
c++;
n1/=10;
}
n1=n;
while(n1>0)
{
digit=n1%10;
sum+=Math.pow(digit,c);
n1/=10;
}
if(sum==n)
System.out.println(n+" is armstrong number");
else
System.out.println(n+" is not armstrong number");
}
}
Output:
Enter the number :
407
407 is armstrong number

Variable and Description


Name of the variable Data Type Purpose/Description
n Int Number which is checked whether Armstrong or not
n1 Int Temporary variable storing value of n
c Int Counting how many digits are there in number
digit Int Digits of the number
sum int Getting the sum of digits to the power c
16 Write a menu driven program to display any two of the triangle patterns.
a) 1 b) 1
22 12
333 123
4444 1234
55555 12345

import java.util.*;
public class triangle_pattern1
{
public static void main(String args[])
{
char choice;
int i,j;
Scanner sc=new Scanner(System.in);
System.out.println("a- 1st pattern b-2nd pattern");
System.out.println("Enter your choice: ");
choice=sc.next().charAt(0);
switch(choice)
{
case 'a': for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
System.out.print(i+" ");
System.out.println();
}
break;
case 'b': for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
System.out.print(j+" ");
System.out.println();
}
break;
default: System.out.println("Invalid choice......");
}
}
}

a- 1st pattern b-2nd pattern


Enter your choice:
a
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
Variable and Description
Name of the variable Data Type Purpose/Description
choice char Variable accepting choice from user from two options
i Int Control variable referring row
j int Control variable referring column
17 Write a menu driven program to display any two of the triangle patterns.
1) 2)
1 *
01 *#
101 *#*
0101 *#*#
10101 *#*#*
import java.util.*;
public class triangle_pattern2
{
public static void main(String args[])
{
int choice;
int i,j,k;
Scanner sc=new Scanner(System.in);
System.out.println("1- 1st pattern 2-2nd pattern");
System.out.println("Enter your choice: ");
choice=sc.nextInt();
switch(choice)
{
case 1: for(i=1;i<=5;i++)
{
for(j=1,k=i;j<=i;j++,k++)
if(k%2==0)
System.out.print("0 ");
else
System.out.print("1 ");
System.out.println();
}
break;
case 2: for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
if(j%2==0)
System.out.print("# ");
else
System.out.print("* ");
System.out.println();
}
break;
default: System.out.println("Invalid choice......");
}
}
}
Output:
1- 1st pattern 2-2nd pattern
Enter your choice:
1
1
01
101
0101
10101
1- 1st pattern 2-2nd pattern
Enter your choice:
2
*
*#
*#*
*#*#
*#*#*

Variable and Description


Name of the variable Data Type Purpose/Description
choice int Variable accepting choice from user from two options
i Int Control variable referring row
j int Control variable referring column
18 Write a menu driven program to display the sum of series:
(a) 1+4+....................+200
1 2 3 19
(b) + + +… … … … … … …+
2 3 4 20

import java.util.*;
public class series1
{
public static void main(String args[])
{
char choice;
int i;
Scanner sc=new Scanner(System.in);
System.out.println("a- 1+4+......+200 2-1/2+2/3+.....+19/20");
System.out.println("Enter your choice: ");
choice=sc.next().charAt(0);
switch(choice)
{
case 'a': int s=0;
for(i=1;i<=200;i+=3)
s+=i;
System.out.println("1+4+.....+200 = "+s);
break;
case 'b': double s1=0;
for(i=1;i<=19;i++)
s1+=((i*1.0)/(i+1));
System.out.println("1/2+2/3+......+19/20 ="+s1);
break;
default: System.out.println("Invalid choice......");
}
}
}
Output:
Enter your choice:
b
1/2+2/3+......+19/20 =16.40226034285632

Variable and Description


Name of the variable Data Type Purpose/Description
choice char Variable accepting choice from user from two options
i int Control variable referring row
s int It stores the overall sum
s1 double It stores the intermediate sum
19 x x x x x
Write a program to display the series S= + + + +… … … … … … …+
2 5 8 11 20
public class series2
{
public static void main(int x)
{
double s=0;int i;
for(i=2;i<=20;i+=3)
s+=((x*1.0)/i);
System.out.println("Series summation = "+s);
}
}
Output:
Series summation = 2.192322383498854
Variable and Description
Name of the variable Data Type Purpose/Description
x int Variable accepting any numeric value
i int Variable storing value from 2 to 20, incremented by 3
s double Storing the summation of terms
20 1 1 1 1 1
Write a program to display the series + + + + … … … …+
2 3 5 7 29
public class series_prime
{
public static void main(String args[])
{
int c,i,j;
double s=0;
for(i=2;i<=29;i++)
{
c=0;
for(j=1;j<=i;j++)
if(i%j==0) c++;
if(c==2)
{
s+=(1.0/i);
System.out.print("1/"+i+"+");
}
}
System.out.print("\nSun of series : "+s);
}
}
Output:
1/2+1/3+1/5+1/7+1/11+1/13+1/17+1/19+1/23+1/29+
Sum of series : 1.5334387718720317

Name of the variable Data Type Purpose/Description


x int Variable accepting any numeric value
i Int Variable storing value from 2 to 29, incremented by 1
j int Variable storing value from 1 to i
c int Counting no. of factors
s double Storing the summation of terms
21 1 1 1 1
Write a program to display the series S=1+ + + +… … … … … … …+
2! 3 ! 4 ! n!
import java.util.*;
public class series_sumFactorial
{
public static void main(String args[])
{
int f,i,j,n;
double s=0;
Scanner sc=new Scanner(System.in);
System.out.println("Enter value of n : ");
n=sc.nextInt();
for(i=1;i<=n;i++)
{
f=1;
for(j=1;j<=i;j++)
f*=j;
s+=(1.0/f);
}
System.out.print("Sum of series : "+s);
}
}
Enter value of n :
4
Sum of series : 1.7083333333333335

Name of the variable Data Type Purpose/Description


n int Variable accepting any numeric value
i int Variable storing value from 1 to n, incremented by 1
j int Variable storing value from 1 to i, incremented by 1
f int Storing factorial of i
s int Storing the summation
22 Write a program to display the series 1+(1+2)+(1+2+3)+................+(1+2+......+n)
import java.util.*;
public class series
{
public static void main(String args[])
{
int i,j,s=0,s1,n;
Scanner sc=new Scanner(System.in);
System.out.println("Enter value of n : ");
n=sc.nextInt();
for(i=1;i<=n;i++)
{
s1=0;
for(j=1;j<=i;j++)
s1+=j;
s+=s1;
}
System.out.println("1+(1+2)+…. : "+s);
}
}

Output:
Enter value of n :
3
1+(1+2)+……..: 10

Name of the variable Data Type Purpose/Description


n int Variable accepting any numeric value
i int Variable storing value from 1 to n, incremented by 1
j int Variable storing value from 1 to i, incremented by 1
s1 int Stores the sum of j values
s int Storing overall summation
23 Write a program to display the series 1+(1*2)+(1*2*3)+(1*2*3*4)+.............+(1*2*3*........n)
import java.util.*;
public class series
{
public static void main(String args[])
{
int i,j,s=0,s1,n;
Scanner sc=new Scanner(System.in);
System.out.println("Enter value of n : ");
n=sc.nextInt();
for(i=1;i<=n;i++)
{
s1=1;
for(j=1;j<=i;j++)
s1*=j;
s+=s1;
}
System.out.println("1+(1*2)+…. : "+s);
}
}

Output:
Enter value of n :
3
1+(1*2)+……..: 9

Name of the variable Data Type Purpose/Description


n int Variable accepting any numeric value
i int Variable storing value from 1 to n, incremented by 1
j int Variable storing value from 1 to i, incremented by 1
s1 int Stores the multiplication of j values
s int Storing overall summation
24 Write a menu driven program to display any one of the pattern:
1. 5 4 3 2 1 2. 1
5432 2 3
543 4 5 6
54 7 8 9 10
5 11 12 13 14 15
import java.util.*;
public class triangle_pattern3
{
public static void main(String args[])
{
int choice;
int i,j,k;
Scanner sc=new Scanner(System.in);
System.out.println("1- 1st pattern 2-2nd pattern");
System.out.println("Enter your choice: ");
choice=sc.nextInt();
switch(choice)
{
case 1: for(i=5;i>=1;i--)
{
for(j=1,k=5;j<=i;j++,k--)
System.out.print(k+ " ");
System.out.println();
}
break;
case 2: for(i=1,k=1;i<=5;i++)
{
for(j=1;j<=i;j++)
System.out.print(k++ +"\t");
System.out.println();
}
break;
default: System.out.println("Invalid choice......");
}
}
}

Output:
1- 1st pattern 2-2nd pattern
Enter your choice:
1
54321
5432
543
54
5

1- 1st pattern 2-2nd pattern


Enter your choice:
2
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15

Name of the variable Data Type Purpose/Description


choice int Value accepted for choice
i int Variable storing value from 1 to n, incremented by 1
j int Variable storing value from 1 to i, incremented by 1
k int k variable assigns integer value based on question

You might also like