0% found this document useful (0 votes)
24 views79 pages

Half Yearly Computer Project 11

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)
24 views79 pages

Half Yearly Computer Project 11

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/ 79

HALF YEARLY COMPUTER PROJECT

Shashank Mishra

11A

21/08/2023

11th Grade Computer Science


Program 1: To display n terms of Fibonacci series
Algorithm:
Start
Step 1: initialise i,a-0,b=1,c,n
Step 2: ask user to input number of n terms
Step 3: for(i=1;i<=n;i++){
Step 4: System.out.println(a); //Repeat step 4 to 7
Step 5: c=a+b;
Step 6: a=b;
Step 7: b=c;}
Stop

Program:
import java.util.Scanner;
class fibonacci{
Scanner sc = new Scanner(System.in);
void display(){
int i,a=0,b=1,c,n;
System.out.println("Enter number of terms");
n = sc.nextInt();
for(i=1;i<=n;i++){
System.out.println(a);
c= a+b;
a=b;
b=c;
}
}
}

Output:
Enter number of terms
3
0
1
1
Variable List:
Data Name Data Type Description
i Integer Loop variable
a Integer Calculating variables
b Integer Calculating variables
c Integer Calculating variables
n Integer Number of terms

Program 2: Perfect number


Algorithm:
Start
Step1: ask for a number from user
Step 2: for(i=1;i<n;i++){ // repeat Step 3 to Step 5
Step 3: if(n%i==0)
Step 4: s=s+1
Step 5: if(s==n), then it is a perfect number, otherwise it is not a
perfect number
Stop

Program:
import java.util.Scanner;
class perfect{
Scanner sc = new Scanner (System.in);
void display(){
System.out.println("Enter a number to check");
int n = sc.nextInt();
int s=0;
for(int i=1;i<n;i++){
if(n%i==0)
s=s+i;
}
if (s==n)
System.out.println("Number is perfect");
else
System.out.println("Number is not perfect");
}
}
}

Output:
Enter a number to check
6
Number is perfect

Variable List:
Data Name Data Type Description
i Integer Loop variable
n Integer Number to be checked
s Integer Check variable

Program 3: Prime Number


Algorithm:
Start
Step 1: initialise n,i,c=0;
Step 2: ask user to input a number n
Step 3: for(i=1;i<=n;i++){
Step 4: if(n%i==0)//Repeat step 4 to 7
Step 5: c++
Step 6: if(c==2), then entered number is prime otherwise its not
prime
Stop

Program:
import java.util.Scanner;
class prime{
Scanner sc = new Scanner (System.in);
void display(){
System.out.println("Enter a number to check");
int n = sc.nextInt();
int c=0;
for(int i=1;i<=n;i++){
if(n%i==0)
c++;
}
if (c==2)
System.out.println("Number is prime");
else
System.out.println("Number is not prime");
}
}

Output:
Enter a number to check
11
Number is prime
Enter a number to check
4
Number is not prime
Variable List:
Data Name Data Type Description
i Integer Loop variable
n Integer Number to be checked
c Integer Check variable

Program 4: Conversion from decimal to binary


Algorithm:
Start
Step 1: initialise int n; String b; int d;
Step 2: ask user to input a number n
Step 3: while(n>0) // repeat step 4 to Step 6
Step 4: int d = n%2;
Step 5: b = d+b;
Step 6: n=n/2;
Step 7: print b;
Stop

Program:
import java.util.Scanner;
class convert1{
Scanner sc = new Scanner (System.in);
void display(){
System.out.println("Enter decimal number:");
int n = sc.nextInt();
String b="";
while(n>0){
int d = n%2;
b = d+b;
n=n/2;
}
System.out.println(b);
}
}

Output:
Enter decimal number:
22
=10110

Variable List:
Data Name Data Type Description
n Integer Decimal number to be
converted to binary
b String Stores binary equivalent
d Integer Converts into binary

Program 5: Conversion from decimal to octal


Algorithm:
Start
Step 1: initialise int n; String b; int d;
Step 2: ask user to input a number n
Step 3: while(n>0) repeat Step 4 to Step 6
Step 4: int d = n%8;
Step 5: b = d+b;
Step 6: n=n/8;
Step 7: print b;
Stop

Program:
import java.util.Scanner;
class convert2{
Scanner sc = new Scanner (System.in);
void display(){
System.out.println("Enter decimal number:");
int n = sc.nextInt();
String b="";
while(n>0){
int d = n%8;
b = d+b;
n=n/8;
}
System.out.println(b);
}
}
Output:
Enter decimal number:
23
=27

Variable List:
Data Name Data Type Description
n Integer Decimal number to be
converted to octal
b String Stores octal equivalent
d Integer Converts into octal

Program 6: Conversion from decimal to hexa decimal


Algorithm:
Start
Step 1: initialise int n; String b; int d;
Step 2: ask user to input a number n
Step 3: while(n>0)
Step 4: int d = n%16; //repeat step 4 to step 7
Step 5:if(d>9)
Step 6: b= (char) (55+d)+b;
Step 7: else, b =d+b;
Step 8: print b
Stop

Program:
import java.util.Scanner;
class convert3{
Scanner sc = new Scanner (System.in);
void display(){
System.out.println("Enter decimal number:");
int n = sc.nextInt();
String b="";
while(n>0){
int d = n%16;
if(d>9)
b = (char)(55+d)+b;
else
b=d+b
}
System.out.println(b);
}
}

Output:
Enter decimal number:
12
=C

Variable List:
Data Name Data Type Description
n Integer Decimal number to be
converted to hexa
decimal
b String Stores hexa decimal
equivalent
d Integer Converts into hexa
decimal

Program 7: Sum of prime digits


Algorithm:
Start
Step 1: input n
Step 2: initialise n to 0
Step 3: d=0,c=0;
Step 4: while n>0 //repeat step 5 to step 8
Step 5:d=n%10
Step 6: if(d==2or d==3 or d==5 or d==7)
Step 7: s=s+d
Step 8: n=n/10
Step 9: print s
Stop

Program:
import java.util.Scanner;
class sumPrime{
Scanner sc = new Scanner(System.in);
void main(){
System.out.println("Enter number of prime digits");
int n= sc.nextInt();
int s=0;
int d=0,c=0;
while(n>0){
d= n%10;
if(d==2||d==3||d==5||d==7){
s=s+d;
}
n=n/10;
}
System.out.println(s);
}
}

Output:
Enter number of prime digits
347
=10

Variable List:
Data Name Data Type Description
n Integer Entered number
s Integer Sum of prime digits
d Integer Stores digits of number
Program 8: Sum of even digits
Algorithm:
Start
Step 1: input n
Step 2: initialise s to 0
Step 3: d=0;
Step 4: while n>0 //repeat step 5 to step 8
Step 5: d=n%10
Step 6: if(d%2==0)
Step 7: s=s+d
Step 8: n=n/10
Step 9: print s
Stop

Program:
import java.util.Scanner;
class sumEven{
Scanner sc = new Scanner(System.in);
void main(){
System.out.println("Enter a number");
int n= sc.nextInt();
int s=0;
int d=0;
while(n>0){
d= n%10;
if(d%2==0){
s=s+d;
}
n=n/10;
}
System.out.println(s);
}
}

Output:
Enter a number
452
=6

Variable List:
Data Name Data Type Description
n Integer Entered number
s Integer Sum of even digits
d Integer Stores digits of number

Program 9: Sum of Even, Product of Odd


Algorithm:
Start
Step 1: int esum =0
Step 2: int product = 1
Step 3: enter number of integers
Step 4: int n
Step 5: int a[] = new int[n]
Step 6: enter numbers
Step 7: for(int i=0;i<n;i++)
Step 8: accept number
Step 9: if(a[i]==0)
Step 10: break;
Step 11: if(a[i]%2==0)
Step12: Calculate the sum of even numbers
Step 13: else
Step 14: Calculate the product of odd numbers
Step 15: print sum of even numbers
Step 16: print product of odd numbers
Stop

Program:
import java.util.Scanner;
class loop3{
Scanner sc = new Scanner(System.in);
void display(){
int esum =0;
int product =1;
System.out.println("Enter the numberof integers to be
entered");
int n = sc.nextInt();
int a[]= new int[n];
System.out.println("Enter numbers");
for(int i=0;i<n;i++){
a[i] = sc.nextInt();
if(a[i]==0){
break;
}
if(a[i]%2==0)
esum = a[i]+esum;
else
product = product*a[i];
}
System.out.println("Sum of even numbers are:"+esum);
System.out.println("Product of odd numbers are:"+product);
}
}

Output:
Enter the numberof integers to be entered
4
Enter numbers
2
3
4
5
Sum of even numbers are:6
Product of odd numbers are:15

Variable List:
Data Name Data Type Description
esum Integer Stores sum of even no.
product Integer Stores product of odd no.
n Integer Stores number of values
a[] Integer Stores all the numbers
i Integer Loop variable

Program 10: Printing of fractional numbers


Algorithm:
Start
Step 1: double i
Step2: for(i=2.0;i<30.0;i=i+0.5)
Step 3: print i
Stop

Program:
class fraction{
void main(){
double i;
for(i=2.0;i<=30.0;i=i+0.5)
System.out.println(i);
}
}

Output:
2.0 2.5 3.0 3.5 4.0 4.5 5.0 5.5 6.0 6.5 7.0 7.5 8.0 8.5 9.0 9.5 10.0 10.5 11.0 11.5 12.0 12.5
13.0 13.5 14.0 14.5 15.0 15.5 16.0 16.5 17.0 17.5 18.0 18.5 19.0 19.5 20.0 20.5 21.0 21.5
22.0 22.5 23.0 23.5 24.0 24.5 25.0 25.5 26.0 26.5 27.0 27.5 28.0 28.5 29.0 29.5 30.0

Variable List:
Data Name Data Type Description
i Double Loop variable

Program 11: Count of integers


Algorithm:
Start
Step 1: int db =0,tp=0,ot=0
Step2: enter number of integers
Step 3: int n = sc.next Int();
Step 4: int a[] = newint[n]
Step 5: enter numbers
Step 6: for(int I =0;i<n;i++) // repeat step 6 to step 17
Step 7: a[i] = sc.nextInt();
Step 8: if(a[i]==0)
Step 9: break;
Step 10: if(a[i]>9&&a[i]<100)
Step 11: db++
Step 12: if(a[i]>99&&a[i]<1000)
Step 13: tp++
Step 14: else
Step 15: ot++
Step 16: print count of double digit integers, triple digit integers and
other integers
Stop

Program:
import java.util.*;
class loop{
Scanner sc = new Scanner(System.in);
void display(){
int db =0,tp =0,ot=0;
System.out.println("Enter te number of integers to be entered");
int n = sc.nextInt();
int a[]= new int[n];
System.out.println("Enter numbers");
for(int i =0;i<n;i++){
a[i] = sc.nextInt();
if(a[i]==0)
break;
if(a[i]>9&&a[i]<100)
db++;
if(a[i]>99&&a[i]<1000)
tp++;
else
ot++;
}
System.out.println("Number of double digits are:"+db);
System.out.println("Number of triple digits are:"+tp);
System.out.println("Number of other integers are:"+ot);
}
}

Output:
Enter the number of integers to be entered
3
Enter numbers
2
34
209
Number of double digits are: 1
Number of triple digits are: 1
Number of other integers are: 1

Variables List
Data Name Data Type Description
db Integer Stores no. of double
digits number
tp Integer Stores no. of triple digits
number
ot Integer Stores no. of other
integers
a[] Integer Stores all the numbers
n Integer Stores number of
integers
i Integers Loop variable

Program 12: Removing even digits form the number


Algorithm:
Start
Step 1: accept a number
Step 2: int n,ne;
Step 3: while n>0
Step 4: int d = n%10
Step 5: if d%2==0;
Else{
ne = ne*10+d;
n=n/10

Program:
import java.util.Scanner;
class ok{
Scanner sc = new Scanner(System.in);
void main(){
System.out.println("Enter a number");
int n = sc.nextInt();
int ne =0;
while (n>0){
int d =n%10;
if(d%2==0){}
else{
ne = ne*10+d;
}
n= n/10;
}
System.out.println(ne);
}
}

Output:
Enter a number
234
3
Variables List
Data Name Data Type Description
db Integer Stores no. of double
digits number
tp Integer Stores no. of triple digits
number
ot Integer Stores no. of other
integers

Program 13: Evil number


Algorithm:
Start
Step 1: input a number
Step2: find binary equivalent
Step 3: d=0,c=0,s= “”
Step 4: while n>0
Step 5: d = n%2
Step 6: if(d==1)
Step 7: c++
Step 8: s= d+s
Step 9: n=n/2
Step 10: print s
Step 11: if(c%2==0)
Step 12: it is an evil number
Step 13: else
Step 14: it is not even
Stop

Program:
import java.util.*;
class evil{
Scanner sc = new Scanner(System.in);
void display(){
System.out.println("Enter a positve whole number");
int n = sc.nextInt();
int d=0,c=0;
String s= "";
while (n>0){
d = d%2;
if(d==1){
c++;
}
s=d+s;
n=n/2;
}
System.out.println("Binary equivalent is"+s);
System.out.println("Number of ones are:"+c);
if(c%2==0)
System.out.println("It is an evil number");
else
System.out.println("It is not an evil number");
}
}

Output:
Enter a positive whole number
15
Binary Equivalents is 1111
No. of one s: 4
It is an evil number

Program 14: reverse a number


Algorithm:
Start
Step 1: input n
Step2: s=0 and p = n
Step 3: while(n>0) //repeat step 4 to step 6
Step 4: d=n%10
Step 5: s=s*10+d
Step 6: =n/10
Step 7: if(s==p)
Step 8: print ”It is palindromic number”

Program:
import java.util.Scanner;
class palindrome{
Scanner sc = new Scanner(System.in);
void display(){
System.out.println("Enter a number");
int n = sc.nextInt();
int s =0;
int p=n;
int d =0;
while(n>0){
d=n%10;
s = s*10+d;
n= n/10;
}
if(s==p)
System.out.println("It is palandromic number:");
else
System.out.println("It is not palandromic number:");
}
}

Output:
Enter a number
123456789
It is not palindromic number:

Variables List
Data Name Data Type Description
p Integer Stores palindromic
number
n Integer Stores original number
s Integer Check variable
d Integers Used for extracting digit

Program 15: check if the number is prime or not


Algorithm:
Start
Step 1: initialise n,i,c=0;
Step 2: ask user to input a number n
Step 3: for(i=1;i<=n;i++){
Step 4: if(n%i==0)//Repeat step 4 to 7
Step 5: c++
Step 6: if(c==2), then entered number is prime otherwise its not
prime
Stop

Program:
import java.util.Scanner;
class prime{
Scanner sc = new Scanner (System.in);
void display(){
System.out.println("Enter a number to check");
int n = sc.nextInt();
int c=0;
for(int i=1;i<=n;i++){
if(n%i==0)
c++;
}
if (c==2)
System.out.println("Number is prime");
else
System.out.println("Number is not prime");
}
}

Output:
Enter a number to check
11
Number is prime
Enter a number to check
4
Number is not prime

Variable List:
Data Name Data Type Description
i Integer Loop variable
n Integer Number to be checked
c Integer Check variable

Program 16: check if the number is perfect or not


Algorithm:
Start
Step1: ask for a number from user
Step 2: for(i=1;i<n;i++){ // repeat Step 3 to Step 5
Step 3: if(n%i==0)
Step 4: s=s+1
Step 5: if(s==n), then it is a perfect number, otherwise it is not a
perfect number
Stop

Program:
import java.util.Scanner;
class perfect{
Scanner sc = new Scanner (System.in);
void display(){
System.out.println("Enter a number to check");
int n = sc.nextInt();
int s=0;
for(int i=1;i<n;i++){
if(n%i==0)
s=s+i;
}
if (s==n)
System.out.println("Number is perfect");
else
System.out.println("Number is not perfect");
}
}
}

Output:
Enter a number to check
6
Number is perfect

Variable List:
Data Name Data Type Description
i Integer Loop variable
n Integer Number to be checked
s Integer Check variable

Program 17: Automorphic


Algorithm:
Start
Step1: input n
Step 2: t=n
Step 3: c=0
Step 4: while n>0 // repeat Step 5 to Step 6
Step 5: n=n/10
Step 6: c=c+1
Step 7: v= t*t
Step 8: if(t==v%10to the power c) then it is automorphic else it is not
automorphic
Stop

Program:
import java.util.Scanner;
class automorphic{
Scanner sc = new Scanner(System.in);
void display(){
System.out.println("Enter a number");
int n = sc.nextInt();
int t =n;
int c=0;
int v=1;
while(n>0){
n= n/10;
c=c+1;
v=t*t;
}
if(t==v%(Math.pow(10,c))){
System.out.println("It is automorphic");
}
else
System.out.println("It is not automorphic");
}
}

Output:
Enter a number
345
It is not automorphic

Variable List:
Data Name Data Type Description
t Integer Copy variable
n Integer Number to be checked
c Integer Helps to verify the number
v Integer Helps to verify the number

Program 18: Twin Prime


Algorithm:
Start
Step1: input n,m
Step 2: i=1c=0
Step 3: while(i<n) // repeat Step 4 to Step 5
Step 4: if(n%i==2), then c++
Step 5: i=i+1
Step 6: j=1,d=0
Step 7: while(j<=m) //repeat Step 8 and Step 9
Step 8: if(m%i==0) then d++
Step 9: j=j+1
Step 10: if(c==2 and d==2) then they are called ”twin prime”
Step 11: else print not twin prime
Stop

Program:
import java.util.Scanner;
class twinPrime{
Scanner sc = new Scanner(System.in);
void display(){
System.out.println("Enter a number");
int n = sc.nextInt();
System.out.println("Enter another number");
int m = sc.nextInt();
int i =1,c=0,j=1,d=0;
while(i<n){
if(n%i==2){
c++;
}
i=i+1;
}
while(j<=m){
if(m%i==0){
d++;
}
j= j+1;
}
if(c==2&&d==2){
System.out.println("Twin prime");
}
else
System.out.println("Not twin prime");
}
}

Output:
Enter a number
45
Enter another number
67
Not twin prime

Variable List:
Data Name Data Type Description
nd
m Integer 2 number entered
n Integer 1st number entered
i Integer Loop Variable
j Integer Loop variable
c Integer Check variable
d Integer Check variable

Program 19: Fascinating Number


Algorithm:
Start
Step 1: Get an integer number either by initialization or by user input
Step 2: Check if that is a three digit number
Step 3: Multiply the number by two and three
Step 4: Concatenate both the products with the number itself
Step 5: Now find if all the digits from 1 to 9 are present in the
number. If it does then it is a fascinating number.
Stop
Program:
import java.util.*;
class fascinating{
Scanner sc = new Scanner(System.in);
void display(){
System.out.print("Enter a three digit number: ");
int n = sc.nextInt();
if(n<100){
System.out.println("Invalid input");
System.exit(0);
}
int n1= n*1;
int n2= n*2;
int n3= n*3;
String k = "";
k= n1+""+n2+""+n3;
int fl=0;
int x,i;
char c;
for(c ='1';c<='9';c++){
x =0;
for(i=0;i<k.length();i++){
if(k.charAt(i)==c){
x=x+1;
}
}
if(x>1){
fl=1;
break;
}
}
if(fl==0){
System.out.println("It is a fascinating number");
}
else
System.out.println("It is not a fascinating number");
}
}

Output:
Enter a three digit number: 327
It is a fascinating number

Variable List:
Data Name Data Type Description
n1 Integer Multiplied number
with 1
n Integer number entered
n2 Integer Multiplied number
with 2
n3 Integer Multiplied number
with 3
k String Concatenated number
fl Integer Check variable
i Integer Loop variable
x Integer Check Variable
c Integer Check Variable

Program 20: Smith Number


Algorithm:
Start
Step 1: input n
Step 2: f=2
Step 3: s=0
Step 4: while f<=n
Step 5: if n%f==0 then
Step 6: n=n/f
Step 7: While f>0
Step 8: d= f%10
Step 9: s=s+d
Step 10: f=f/10
Step 11: f=1
Step 12: f=f+1
Step 13: print s
Stop

Program:
import java.util.Scanner;
class factor{
Scanner sc = new Scanner(System.in);
void main(){
System.out.println("Enter a number");
int n = sc.nextInt();
int f=2;
int s=0;
while (f<=n){
if(n%f==0){
System.out.println(f+" is a factor of "+n);
n=n/f;
while(f>0){
int d=f%10;
s=s+d;
f= f/10;
}
f=1;
}
f=f+1;
}
System.out.println(s);
}
}
Output:
Enter a number
666
2 is a factor of 666
3 is a factor of 333
3 is a factor of 111
37 is a factor of 37
18
Since sum of the digits is equal to the sum of the digit of prime
factors, 666 is a Smith number

Variable List:
Data Name Data Type Description
f Integer Sum of prime digits
n Integer number entered
s Integer Sum of digits of number
d Integer Used for extracting digits

Program 21: Triangular Number


Algorithm:
Start
Step 1: Enter a number and store it in n
Step 2: initialise s to 0
Step 3: for(int i =1;i<=n;i++)
Step 4: s+=i
Step 5: print s
Stop

Program:
import java.util.Scanner;
class triangle{
Scanner sc = new Scanner(System.in);
void main(){
System.out.println("Enter a number");
int n = sc.nextInt();
int s=2;
for(int i =1;i<=n;i++){
s+=i;
System.out.println(s);
}
}
}

Output:
Enter a number
5
=1
3
6
10
15
Variable List:
Data Name Data Type Description
n Integer Number upto which
the pattern is formed
s Integer These are triangular
numbers
i Integer Loop variable

Program 22: Dudeney number


Algorithm:
Start
Step 1: Enter a number and store it in n
Step 2: initialise s to 0
Step 3: int t =n
Step 4: int d=0
Step 5: while t>0
Step 6: d = t%10;
Step 7: s = s+d
Step 8: t=t/10
Step 9: if s==n^3, then it is a dudeny number otherwise it is not a
dudeny number
Stop

Program:
import java.util.Scanner;
class dudeney{
Scanner sc = new Scanner(System.in);
void main(){
System.out.println("Enter a number");
int n = sc.nextInt();
int s=0;
int t=n;
int d=0;
while(t>0){
d=t%10;
s=s+d;
t = t/10;
}
if(s==Math.cbrt(n))
System.out.println("It is a dudeny number");
else
System.out.println("It is not a dudeny number");
}
}

Output:
Enter a number
512
It is a dudeny number
Data Name Data Type Description
n Integer Number to be
checked
s Integer Use for digit extraction
d Integer Use for digit extraction
t Integer It is copy variable

Program 23: DeciOct


Algorithm:
Start
Step 1: initialise n and oct to 0
Step 2: accept n
Step 3: int t =n
Step 4: int s=0
Step 5: while t>0
Step 6: d = t%8;
Step 7: s = s*10+d
Step 8: t=t/8
Step 9: while(s>d)
Step 10: oct = oct*10+(s%10)
Step 11: s=s/10
Step 12: call deci_oct(), then print decimal and octal values

Program:
import java.util.Scanner;
class DeciOct{
int n, oct =0;
DeciOct(){
n=0;
oct=0;
}
void gentnum(){
Scanner sc = new Scanner(System.in);
System.out.println("Enter n");
n = sc.nextInt();
}
void deci_oct(){
int t=n;
int s =0;
while(t>0){
int d = t%8;
s = s*d;
t = t/8;
}
while(s>d){
oct = oct*10+(a%10);
s= s/10;
}
}
void show(){
deci_oct();
System.out.println("The decimal value is "+s);
System.out.println("The octal value is "+oct);
}

Output:
Enter n
12
The decimal value is 12
The octal value is C
Variable List
Data Name Data Type Description
n Integer Number to be
checked
Oct Integer Use for digit extraction
t Integer Used for converting to
octal
s Integer Used for converting to
octal
d Integer Used for converting to
octal

Program 24: Disarium


Algorithm:
Start
Step 1:input n
Step 2: t=n
Step 3: c=0
Step 4:while t>0 rep. 5 and 6
Step 5: t = t/10
Step 6: c=c+1
Step 7: t=n
Step 8: s=0
Step 9: while t>0
Step 10: d=t%10
Step 4: s=s+d^c
Step 4: t = t/10
Step 4: c=c-1
Step 4: if s==n then

Program:
import java.util.Scanner;
class disarium{
Scanner sc = new Scanner(System.in);
void display(){
System.out.println("Enter a number");
int n = sc.nextInt();
int t =n;
int c =0;
while(t>0){
t=t/10;
c=c+1;
}
t=n;
int s=0;
while(t>0){
int d = t%10;
s = s+d^c;
t = t/10;
c=c-1;
}
if(s==n)
System.out.println("It is a disarium number");
}
}

Output:
Enter a number
89
It is a disarium number

Variable List
Data Name Data Type Description
n Integer Number to be
checked
t Integer Copy variable
c Integer Used for digit extraction
s Integer Check variable
d Integer Used for digit extraction

Program 25: Evil number


Algorithm:
Start
Step 1: input a number
Step2: find binary equivalent
Step 3: d=0,c=0,s= “”
Step 4: while n>0
Step 5: d = n%2
Step 6: if(d==1)
Step 7: c++
Step 8: s= d+s
Step 9: n=n/2
Step 10: print s
Step 11: if(c%2==0)
Step 12: it is an evil number
Step 13: else
Step 14: it is not even
Stop

Program:
import java.util.*;
class evil{
Scanner sc = new Scanner(System.in);
void display(){
System.out.println("Enter a positve whole number");
int n = sc.nextInt();
int d=0,c=0;
String s= "";
while (n>0){
d = d%2;
if(d==1){
c++;
}
s=d+s;
n=n/2;
}
System.out.println("Binary equivalent is"+s);
System.out.println("Number of ones are:"+c);
if(c%2==0)
System.out.println("It is an evil number");
else
System.out.println("It is not an evil number");
}
}

Output:
Enter a positive whole number
15
Binary Equivalents is 1111
No. of one s: 4
It is an evil number

Program 26: Factorial


Algorithm:
Start
Step 1: initialise n to 0
Step 2: accept a number
Step 3: int t=n
Step 4: call input() in void fact()
Step 5: int pr =1
Step 6: for(int i=1;i<=n;i++)
Step 7: pr = pr*i
Step 8: call fact() in void display()
Step 9: print number and its factorial
Stop

Program:
import java.util.Scanner;
class num{
Scanner sc = new Scanner(System.in);
int n;
num(){
n=0;
}
void input (){
System.out.println(“Enter a number”);
n= sc.nextInt();
}
int t=n;
void fact(){
input();
int pr=1;
for(int i=1;i<=n;i++){
pr=pr*i;
}
void display(){
fact();
System.out.println(“The number is ”+n);
System.out.println(“Its factorial is ”+pr);
}
}
}

Output:
Enter a number
5
The number is 5
Its factorial is 120

Variables List
Data Name Data Type Description
n Integer Number to be
accepted
t Integer Copy variable
i Integer Loop variable
pr Integer Stores factorial

Program 27: Sumproduct


Algorithm:
Start
Step 1: initialise n to 0
Step 2: int p=1,f=1;
Step 3: accept n
Step 4: int t=n
Step 5: int sum(int v){
}
Step 6: v=0
Step 7: for(int i=0;i<=t,i++){
}
Step 8: int d= t%10;
Step 9: u = v+d;
Step 10: t=t/10;
Step 11: return v
Step 12: find the product and then call sum and product functions in
void check and then print if f==n then number is sumproduct
otherwise not.
Stop

Program:
import java.util.Scanner;
class sumproduct(){
Scanner sc = new Scanner(System.in);
Int n;
sumproduct (){
n =0;}
int p=1,f=1;
void readreadnum(){
System.out.println(“Enter a number”)
n= sc.nextInt();
}
int t=n;
int sum(int v){
v=o;
for(int I =0;i<=t;i++){
int d = t%10
u = v+d;
t=t/10;
}
return u;
}
int product(){
for(int i =0;i<=t;i++){
int d = t%10;
p=p*d;
t=t/10;
}
return p;
}
void check(){
sum();
product();
f=u*p;
if(f==n)
System.out.println(‘’Number is simproduct”)
Else
System.out.println(“Number is not sumproduct”)
}
}
Output:
Enter a number
144
Number is sumproduct

Program 28: Sum of Even, Product of Odd


Algorithm:
Start
Step 1: int esum =0
Step 2: int product = 1
Step 3: enter number of integers
Step 4: int n
Step 5: int a[] = new int[n]
Step 6: enter numbers
Step 7: for(int i=0;i<n;i++)
Step 8: accept number
Step 9: if(a[i]==0)
Step 10: break;
Step 11: if(a[i]%2==0)
Step12: Calculate the sum of even numbers
Step 13: else
Step 14: Calculate the product of odd numbers
Step 15: print sum of even numbers
Step 16: print product of odd numbers
Stop

Program:
import java.util.Scanner;
class loop3{
Scanner sc = new Scanner(System.in);
void display(){
int esum =0;
int product =1;
System.out.println("Enter the numberof integers to be
entered");
int n = sc.nextInt();
int a[]= new int[n];
System.out.println("Enter numbers");
for(int i=0;i<n;i++){
a[i] = sc.nextInt();
if(a[i]==0){
break;
}
if(a[i]%2==0)
esum = a[i]+esum;
else
product = product*a[i];
}
System.out.println("Sum of even numbers are:"+esum);
System.out.println("Product of odd numbers are:"+product);
}
}

Output:
Enter the numberof integers to be entered
4
Enter numbers
2
3
4
5
Sum of even numbers are:6
Product of odd numbers are:15

Variable List:
Data Name Data Type Description
esum Integer Stores sum of even no.
product Integer Stores product of odd no.
n Integer Stores number of values
a[] Integer Stores all the numbers
i Integer Loop variable

Program 29: Armstrong Number


Algorithm:
1. Start
2. Accept a number in n
3. Store length in len
4. Set b to n, s = 0
5. While b not equal to 0
6. s = s + b mod 10 to the power len
7. b = b / 10
8. End while
9. If s equal to n then yes else no
10. End
Program:
import java.util.Scanner;

public class Armstrong {

static boolean isArmstrong(int n) {

int b = n;

int len = Integer.toString(n).length();

int s = 0;

while(b != 0) {

s = s + (int)(Math.pow(b%10, len));

b /= 10;

return s == n;

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.println("Enter a number = ");

int n = sc.nextInt();

if(isArmstrong(n))

System.out.println("YES");

else

System.out.println("NO");
}

Output:
Enter a number =
153
Yes

Variables List:
Variable Name Data Type Description

b int backup

n int Stores the actual number

s int Manipulated sum

len int Stores the length of the n

Program 30: Pattern

Algorithm:
1. Start
2. Take a string w
3. For i = 1 to length of string print substring of w from 0 to i each on new
line
4. end

Code:
import java.util.Scanner;

public class StringPatter {

public static void main(String[] args) {


Scanner sc = new Scanner(System.in);

System.out.println("Enter a word = ");

String w = sc.next();

for(int i = 1; i <= w.length(); ++i){

System.out.println(w.substring(0, i));

Data
Variable Name Description
Type

sentence String Stores the sentence

output String Stores the final output

word String Stores the word to search for

replacement String Stores the replacement word

token String Stores the token split

Program 31: Replace word


import java.util.*;

public class WordReplace {

static Scanner sc = new Scanner(System.in);

public static void main(String[] args) {

System.out.println("Enter a sentence");

String sentence = sc.nextLine().toUpperCase();

System.out.println("Enter word to search for = ");

String word = sc.next().toUpperCase();


System.out.println("Enter word to replace with = ");

String replacement = sc.next().toUpperCase();

String output = "" ;

StringTokenizer st = new StringTokenizer(sentence);

while(st.hasMoreTokens()) {

String token = st.nextToken();

if(token.equalsIgnoreCase(word)) {

output += replacement + " ";

} else {

output += token + " ";

System.out.println("After replacement = " + output);

Data
Variable Name Description
Type

sentence String Stores the sentence

output String Stores the final output

word String Stores the word to search for

replacement String Stores the replacement word

token String Stores the token split

Program 32: check if the number is prime or not


Algorithm:
Start
Step 1: initialise n,i,c=0;
Step 2: ask user to input a number n
Step 3: for(i=1;i<=n;i++){
Step 4: if(n%i==0)//Repeat step 4 to 7
Step 5: c++
Step 6: if(c==2), then entered number is prime otherwise its not
prime
Stop

Program:
import java.util.Scanner;
class prime{
Scanner sc = new Scanner (System.in);
void display(){
System.out.println("Enter a number to check");
int n = sc.nextInt();
int c=0;
for(int i=1;i<=n;i++){
if(n%i==0)
c++;
}
if (c==2)
System.out.println("Number is prime");
else
System.out.println("Number is not prime");
}
}

Output:
Enter a number to check
11
Number is prime
Enter a number to check
4
Number is not prime

Variable List:
Data Name Data Type Description
i Integer Loop variable
n Integer Number to be checked
c Integer Check variable
Program 33: Major Diagonal sum
Algorithm:
1. Start
2. Input length l and breadth b
3. Create a 2D array of l x b dimensions
4. Set s = 0
5. For i = 0 to l
6. For j = 0 to b
7. When i equal to j add the j-th element of the i-th row to s
8. End j
9. End i
10.Print s
11.end

Code:
import java.util.Scanner;
public class DiagonalSum {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.println("Enter the length and breadth of the matrix = ");

int[][] a = new int[sc.nextInt()][sc.nextInt()];

for(int i = 0; i < a.length; ++i){

for(int j = 0; j < a[0].length; ++j){

System.out.println("Enter an integer = ");

a[i][j] = sc.nextInt();

int s = 0;

for(int i = 0; i < a.length; ++i){

for(int j = 0; j < a[0].length; ++j){

if(i==j) s = s + a[i][j];

System.out.println("Sum = " + s);

Data
Variable Name Description
Type

a int[][] Stores the matrix

i int Outer loop variable

j int Inner loop variable

Stores sum of major


s int
diagonal
Program 34: Automorphic
Algorithm:
Start
Step1: input n
Step 2: t=n
Step 3: c=0
Step 4: while n>0 // repeat Step 5 to Step 6
Step 5: n=n/10
Step 6: c=c+1
Step 7: v= t*t
Step 8: if(t==v%10to the power c) then it is automorphic else it is not
automorphic
Stop

Program:
import java.util.Scanner;
class automorphic{
Scanner sc = new Scanner(System.in);
void display(){
System.out.println("Enter a number");
int n = sc.nextInt();
int t =n;
int c=0;
int v=1;
while(n>0){
n= n/10;
c=c+1;
v=t*t;
}
if(t==v%(Math.pow(10,c))){
System.out.println("It is automorphic");
}
else
System.out.println("It is not automorphic");
}
}

Output:
Enter a number
345
It is not automorphic

Variable List:
Data Name Data Type Description
t Integer Copy variable
n Integer Number to be checked
c Integer Helps to verify the number
v Integer Helps to verify the number
Program 35: Twin Prime
Algorithm:
Start
Step1: input n,m
Step 2: i=1c=0
Step 3: while(i<n) // repeat Step 4 to Step 5
Step 4: if(n%i==2), then c++
Step 5: i=i+1
Step 6: j=1,d=0
Step 7: while(j<=m) //repeat Step 8 and Step 9
Step 8: if(m%i==0) then d++
Step 9: j=j+1
Step 10: if(c==2 and d==2) then they are called ”twin prime”
Step 11: else print not twin prime
Stop

Program:
import java.util.Scanner;
class twinPrime{
Scanner sc = new Scanner(System.in);
void display(){
System.out.println("Enter a number");
int n = sc.nextInt();
System.out.println("Enter another number");
int m = sc.nextInt();
int i =1,c=0,j=1,d=0;
while(i<n){
if(n%i==2){
c++;
}
i=i+1;
}
while(j<=m){
if(m%i==0){
d++;
}
j= j+1;
}
if(c==2&&d==2){
System.out.println("Twin prime");
}
else
System.out.println("Not twin prime");
}
}

Output:
Enter a number
45
Enter another number
67
Not twin prime

Variable List:
Data Name Data Type Description
nd
m Integer 2 number entered
n Integer 1st number entered
i Integer Loop Variable
j Integer Loop variable
c Integer Check variable
d Integer Check variable

Program 36: Printing of fractional numbers


Algorithm:
Start
Step 1: double i
Step2: for(i=2.0;i<30.0;i=i+0.5)
Step 3: print i
Stop

Program:
class fraction{
void main(){
double i;
for(i=2.0;i<=30.0;i=i+0.5)
System.out.println(i);
}
}

Output:
2.0 2.5 3.0 3.5 4.0 4.5 5.0 5.5 6.0 6.5 7.0 7.5 8.0 8.5 9.0 9.5 10.0 10.5 11.0 11.5 12.0 12.5
13.0 13.5 14.0 14.5 15.0 15.5 16.0 16.5 17.0 17.5 18.0 18.5 19.0 19.5 20.0 20.5 21.0 21.5
22.0 22.5 23.0 23.5 24.0 24.5 25.0 25.5 26.0 26.5 27.0 27.5 28.0 28.5 29.0 29.5 30.0

Variable List:
Data Name Data Type Description
i Double Loop variable

Program 37: Count of integers


Algorithm:
Start
Step 1: int db =0,tp=0,ot=0
Step2: enter number of integers
Step 3: int n = sc.next Int();
Step 4: int a[] = newint[n]
Step 5: enter numbers
Step 6: for(int I =0;i<n;i++) // repeat step 6 to step 17
Step 7: a[i] = sc.nextInt();
Step 8: if(a[i]==0)
Step 9: break;
Step 10: if(a[i]>9&&a[i]<100)
Step 11: db++
Step 12: if(a[i]>99&&a[i]<1000)
Step 13: tp++
Step 14: else
Step 15: ot++
Step 16: print count of double digit integers, triple digit integers and
other integers
Stop

Program:
import java.util.*;
class loop{
Scanner sc = new Scanner(System.in);
void display(){
int db =0,tp =0,ot=0;
System.out.println("Enter te number of integers to be entered");
int n = sc.nextInt();
int a[]= new int[n];
System.out.println("Enter numbers");
for(int i =0;i<n;i++){
a[i] = sc.nextInt();
if(a[i]==0)
break;
if(a[i]>9&&a[i]<100)
db++;
if(a[i]>99&&a[i]<1000)
tp++;
else
ot++;
}
System.out.println("Number of double digits are:"+db);
System.out.println("Number of triple digits are:"+tp);
System.out.println("Number of other integers are:"+ot);
}
}

Output:
Enter the number of integers to be entered
3
Enter numbers
2
34
209
Number of double digits are: 1
Number of triple digits are: 1
Number of other integers are: 1

Variables List
Data Name Data Type Description
db Integer Stores no. of double
digits number
tp Integer Stores no. of triple digits
number
ot Integer Stores no. of other
integers
a[] Integer Stores all the numbers
n Integer Stores number of
integers
i Integers Loop variable

Program 38: Factors of number


Algorithm:
Step Start
1: input n
Step 2: i=1
Step3: while i<n //repeat steps 4 and 5
Step 4: if n%i==0 then print i
Step 5: i++;
Stop

Program:
Import java.util.Scannner;
Class factors{
System.out.println(“Enter a number”);
Int I =1
While(i<=n){
If (n%i==0)
System.out.println(i)
I++;
}

Output:
Enter a number: 6
1
2
3
6

Variable List:
Data Name Data Type Description
I Integer Loop variable
N Integer It is the accepted value

Program 39: Triangular Number


Algorithm:
Start
Step 1: Enter a number and store it in n
Step 2: initialise s to 0
Step 3: for(int i =1;i<=n;i++)
Step 4: s+=i
Step 5: print s
Stop
Program:
import java.util.Scanner;
class triangle{
Scanner sc = new Scanner(System.in);
void main(){
System.out.println("Enter a number");
int n = sc.nextInt();
int s=2;
for(int i =1;i<=n;i++){
s+=i;
System.out.println(s);
}
}
}

Output:
Enter a number
5
=1
3
6
10
15

Variable List:
Data Name Data Type Description
N Integer Number upto which
the pattern is formed
S Integer These are triangular
numbers
I Integer Loop variable

Program 40: Automorphic


Algorithm:
Start
Step1: input n
Step 2: t=n
Step 3: c=0
Step 4: while n>0 // repeat Step 5 to Step 6
Step 5: n=n/10
Step 6: c=c+1
Step 7: v= t*t
Step 8: if(t==v%10to the power c) then it is automorphic else it is not
automorphic
Stop

Program:
import java.util.Scanner;
class automorphic{
Scanner sc = new Scanner(System.in);
void display(){
System.out.println("Enter a number");
int n = sc.nextInt();
int t =n;
int c=0;
int v=1;
while(n>0){
n= n/10;
c=c+1;
v=t*t;
}
if(t==v%(Math.pow(10,c))){
System.out.println("It is automorphic");
}
else
System.out.println("It is not automorphic");
}
}

Output:
Enter a number
345
It is not automorphic

Variable List:
Data Name Data Type Description
t Integer Copy variable
n Integer Number to be checked
c Integer Helps to verify the number
v Integer Helps to verify the number

THANKYOU…

You might also like