Java Program Class 12 IT/Computer Science
Java Program Class 12 IT/Computer Science
Contents Page
1. Write a Program to find out sum of two numbers.
2. Write a Program to find out Area & Perimeter of a Circle.
3. Write a Program to Swap/ Exchange the value of two variables without using 3 rd
variables.
4. Write a Program to Swap/ Exchange the value of two variables without using 3 rd
variables & Arithmetic Operators.
5. Write a Program to check the greater number between two numbers.
6. Write a Program to check the greater number between two numbers using
ternary operator.
7. Write a Program to check the greatest number among three numbers.
8. Write a Program to check the greatest number among three numbers using
ternary operator.
9. Write a Program to check the given number is even or odd.
10. Write a Program to prepare students mark sheet.
11. Write a Program to prepare Grade Sheet using Switch Case.
12. Write a Program to find out sum of any 10 numbers.
13. Write a Program to find out factorial of a number.
14. Write a Program to print Fibonacci Series.
15. Write a Program to Reverse a Number.
16. Write a Program to check the given number is Palindrome or not.
17. Write a Program to find out the sum of the digits of a Number.
18. Write a Program to check the given number is Armstrong or not.
19. Write a Program to check the given number is Perfect or not.
20. Write a Program to check the given number is Prime or not.
21. Write a Program to check the given number is Strong Number or not.
****************
3. Write a Program to swap the value of two variables without using third
variable.
import java.util.Scanner;
public class Swap {
public static void main(String[] args) {
int x,y;
Scanner sc=new Scanner(System.in);
System.out.println("\n Input any Two Numbers:");
x=sc.nextInt();
y=sc.nextInt();
System.out.println("\n Before Swap A:"+x+"\t B:"+y);
x=x+y;
y=x-y;
x=x-y;
System.out.println("\n After Swap A:"+x+"\t B:"+y);
}
}
4. Write a Program to swap the value of two variables without using third
variable & Arithmetic Operator.
import java.util.Scanner;
public class Swap1 {
public static void main(String[] args) {
int x,y;
Scanner sc=new Scanner(System.in);
System.out.println("\n Input any Two Numbers:");
x=sc.nextInt();
y=sc.nextInt();
System.out.println("\n Before Swap A:"+x+"\t B:"+y);
x=x^y;
y=x^y;
x=x^y;
System.out.println("\n After Swap A:"+x+"\t B:"+y);
}
}
6. Write a Program to check the greater number between two numbers using
ternary operator.
import java.util.Scanner;
public class greater1 {
public static void main(String[] args) {
int a,b,c;
Scanner sc=new Scanner(System.in);
System.out.println("\nInput any Two Numbers: ");
a=sc.nextInt();
b=sc.nextInt();
c=(a>b)?a:b;
System.out.println(c+" is Greater");
}
}
7. Write a Program to check the greatest number among three numbers using
nested if.
import java.util.Scanner;
public class greatest {
public static void main(String[] args) {
int a,b,c;
Scanner sc=new Scanner(System.in);
System.out.println("\nInput any Three Numbers: ");
a=sc.nextInt();
b=sc.nextInt();
c=sc.nextInt();
if(a>b)
{
if(a>c)
{
System.out.println(a+" is Greater");
}
else
{
System.out.println(c+" is Greater");
}
}
else
{
if(b>c)
{
System.out.println(b+" is Greater");
}
else
{
System.out.println(c+" is Greater");
}
}
}
}
8. Write a Program to check the greatest number among three numbers using
ternary operator.
import java.util.Scanner;
public class greatest1 {
public static void main(String[] args) {
int a,b,c,d;
Scanner sc=new Scanner(System.in);
}
else if(per>=33)
{
System.out.println("\n Division: Third");
}
else
{
System.out.println("\n Division: Fail");
}
}
}
11. Write a Program to prepare Grade Sheet using Switch Case.
import java.util.Scanner;
public class swth {
public static void main(String[] args) {
int m;
Scanner sc=new Scanner(System.in);
System.out.println("\n Enter Mark");
m=sc.nextInt();
m=m/10;
switch(m)
{
case 9:
case 8:
System.out.println("\n A+");
break;
case 7:
case 6:
System.out.println("\n A");
break;
case 5:
System.out.println("\n B");
break;
case 4:
case 3:
System.out.println("\n C");
break;
default:
System.out.println("\n D");
}
} }
12. Write a Program to find out sum of any 10 numbers.
import java.util.Scanner;
public class summ {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int m,i=1,sum=0;
System.out.println("\n Enter any 10 Numbers:");
while(i<=10)
{
m=sc.nextInt();
sum=sum+m;
i++;
}
System.out.println("\n Sum:"+ sum);
} }
System.out.print("\t"+z);
x=y;
y=z;
i++;
}
}
15. Write a Program to Reverse a Number.
import java.util.Scanner;
public class Rev {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n,rem,rv=0,m;
System.out.println("\n Enter a Number:");
n=sc.nextInt();
m=n;
while(n>0)
{
rem=n%10;
rv=(rv*10)+rem;
n=n/10;
}
System.out.println("Reverse of "+m+" is: "+rv);
}
}
16. Write a Program to check the given number is Palindrome or not.
import java.util.Scanner;
public class Pali {
public static void main(String[] args) {
n=sc.nextInt();
m=n;
while(n>0)
{
rem=n%10;
sum=sum+rem;
n=n/10;
}
System.out.println("Sum of Digits of "+m+" is: "+sum);
}
}
18. Write a Program to check the given number is Armstrong or not.
import java.util.Scanner;
public class Armstrong {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n,rem,sum=0,m;
System.out.println("\n Enter a Number:");
n=sc.nextInt();
m=n;
while(n>0)
{
rem=n%10;
sum=sum+(rem*rem*rem);
n=n/10;
}
if(sum==m)
{
{
System.out.println(n+" is not a Perfect Number");
}
} }
20. Write a Program to check the given number is Prime or not.
import java.util.Scanner;
public class Prime {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n,i,c=0;
System.out.println("\n Enter a Number:");
n=sc.nextInt();
for(i=1;i<=n;i++)
{
if(n%i==0)
{
c++;
}
}
if(c==2)
{
System.out.println(n+" is a Prime Number");
}
else
{
System.out.println(n+" is a Non-Prime Number");
}
} }
21. Write a Program to check the given number is Strong Number or not.
import java.util.Scanner;
public class Strong {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n,m,rem,i,f,fact=0;
System.out.println("\n Enter a Number:");
n=sc.nextInt();
m=n;
while(n>0)
{
rem=n%10;
f=1;
for(i=1;i<=rem;i++)
{
f=f*i;
}
fact=fact+f;
n=n/10;
}
if(m==fact)
{
System.out.println(m+" is a Strong Number");
}
else
{
System.out.println(m+" is a Not a Strong Number");
}
}
}
Java Program Page 19
Java Program Part-I
for(j=1;j<=i;j++)
{
System.out.print(j+" ");
}
System.out.println();
}
}
}
24. Write a Program to Print following Patterns.
1
22
333
4444
55555
public class Pattern3 {
public static void main(String[] args) {
int i,j;
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
System.out.print(i+" ");
}
System.out.println();
}
}
}
System.out.print(j+" ");
}
System.out.println();
}
}
}
27. Write a Program to Print following Patterns (Water Image).
55555
4444
333
22
1
public class Pattern6 {
public static void main(String[] args) {
int i,j;
for(i=5;i>=1;i--)
{
for(j=1;j<=i;j++)
{
System.out.print(i+" ");
}
System.out.println();
}
}
}
28. Write a Program to Print following Patterns (Mirror Image).
*
**
***
****
*****
}
for(j=1;j<=i;j++)
{
System.out.print(j);
}
System.out.println();
}
}
}
30. Write a Program to Print following Patterns (Mirror Image).
1
22
333
4444
55555
public class Mirror2 {
public static void main(String[] args) {
int i,j,k;
for(i=1;i<=5;i++)
{
for(k=i;k<5;k++)
{
System.out.print(" ");
}
for(j=1;j<=i;j++)
{
System.out.print(i);
}
System.out.println();
} }
A[i]=sc.nextInt();
sum=sum+A[i];
}
System.out.println("Entered Numbers are : ");
for(i=0;i<10;i++)
{
System.out.print(A[i]+"\t");
}
System.out.println("\nSum : "+sum);
}
}
33. Write a Program to find out Maximum Number with in a series.
import java.util.Scanner;
public class Max_Array {
public static void main(String[] args) {
int [] A=new int[10];
int i,Max;
Scanner sc=new Scanner(System.in);
System.out.println("Input any 10 Numbers: ");
for(i=0;i<10;i++)
{
A[i]=sc.nextInt();
}
Max=A[0];
for(i=0;i<10;i++)
{
if(Max<A[i])
Max=A[i];
}
System.out.println("Entered Numbers are : ");
for(i=0;i<10;i++)
{
System.out.print(A[i]+"\t");
}
System.out.println("\nMaximum Number: "+Max);
}
}
34. Write a Program to find out Minimum Number with in a series.
import java.util.Scanner;
public class Min_Arr {
public static void main(String[] args) {
int [] A=new int[10];
int i,Min;
Scanner sc=new Scanner(System.in);
System.out.println("Input any 10 Numbers: ");
for(i=0;i<10;i++)
{
A[i]=sc.nextInt();
}
Min=A[0];
for(i=0;i<10;i++)
{
if(Min>A[i])
Min=A[i];
}
System.out.println("Entered Numbers are : ");
for(i=0;i<10;i++)
{
System.out.print(A[i]+"\t");
}
System.out.println("\nMinimum Number: "+Min);
}
}
35. Write a Program to sort a list in ascending order using Bubble sort.
import java.util.Scanner;
public class Sort_Bub {
public static void main(String[] args) {
int [] A=new int[10];
int i,j,tmp;
Scanner sc=new Scanner(System.in);
System.out.println("Input any 10 Numbers: ");
for(i=0;i<10;i++)
{
A[i]=sc.nextInt();
}
System.out.println("\nList Before Sorting : ");
for(i=0;i<10;i++)
{
System.out.print(A[i]+"\t");
}
for(i=0;i<10;i++)
{
for(j=0;j<9;j++)
{
if(A[j]>A[j+1])
{
tmp=A[j];
A[j]=A[j+1];
A[j+1]=tmp;
}
}
}
System.out.println("List After Sorting : ");
for(i=0;i<10;i++)
{
System.out.print(A[i]+"\t");
}
}
}
36. Write a Program to sort a list in ascending order using Selection sort.
import java.util.Scanner;
public class Sort_Sel {
public static void main(String[] args) {
int [] A=new int[10];
int i,j,tmp;
Scanner sc=new Scanner(System.in);
System.out.println("Input any 10 Numbers: ");
for(i=0;i<10;i++)
{
A[i]=sc.nextInt();
}
System.out.println("\nList Before Sorting : ");
for(i=0;i<10;i++)
{
System.out.print(A[i]+"\t");
}
for(i=0;i<9;i++)
{
for(j=i+1;j<10;j++)
{
if(A[i]>A[j])
{
tmp=A[i];
A[i]=A[j];
A[j]=tmp;
}
}
}
System.out.println("\nList After Sorting : ");
for(i=0;i<10;i++)
{
System.out.print(A[i]+"\t");
}
}
}
37. Write a Program to Add two matrices.
import java.util.Scanner;
public class Matrixadd {
public static void main(String[] args) {
int [][] A=new int[3][3];
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
System.out.print(C[i][j]+"\t");
}
System.out.println();
}
}
}
38. Write a Program to multiply two matrices.
import java.util.Scanner;
public class Mat_mul {
public static void main(String[] args) {
int [][] A=new int[3][3];
int [][] B=new int[3][3];
int [][] C=new int[3][3];
int i,j,k;
Scanner sc=new Scanner(System.in);
System.out.println("Input any 9 Numbers for Matrix A: ");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
A[i][j]=sc.nextInt();
}
}
System.out.println("Input any 9 Numbers for Matrix B: ");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
B[i][j]=sc.nextInt();
}
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
C[i][j]=0;
for(k=0;k<3;k++)
{
C[i][j]=C[i][j]+A[i][k]*B[k][j];
}
}
}
System.out.println("\nMatrix-C\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
System.out.print(C[i][j]+"\t");
}
System.out.println();
}
} }
System.out.print(B[i][j]+"\t");
}
System.out.println();
}
}
}
40. Write a Program to search a number with in a list.
import java.util.Scanner;
public class Search {
public static void main(String[] args) {
int [] A=new int[10];
int i,n,c=0;
Scanner sc=new Scanner(System.in);
System.out.println("Input any 10 Numbers: ");
for(i=0;i<10;i++)
{
A[i]=sc.nextInt();
}
System.out.println("\nInput a Number for Search : ");
n=sc.nextInt();
for(i=0;i<10;i++)
{
if(A[i]==n)
c++;
}
if(c>0)
{
System.out.println(n+ " is exists in the list for "+ c +" times.");
}
else
{
System.out.println(n+ "is not exists in the list. ");
}
}
************************