COMPUTER PROJECT
TERM - 2
- SWARNJEET SINGH JADHAV
IX - A
1. Write a program to accept a number and print the sum of its digits and check whether it’s an
Armstrong number or not. Armstrong number is a number whose sum of the cubes of the number is
equal to the number itself. Ex: 153=13+53+33
Source code:
import java.util.Scanner;
public class p1
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
System.out.print("Enter a number: ");
int n = in.nextInt();
int temp=n;
int a = 0;int s=0;
while(temp>0)//loop for extraction of digits and addition
{
s = s+(temp%10);//find sum of digits
a = a+(int)Math.pow(temp%10,3);//finds sum of digits cubed
temp = temp/10;//changes initial number to move on to next digit
}
if (a==n)
System.out.println(n+" is an armstrong number and "+s+" is its sum of digits");
else
System.out.println(n+" is not an armstrong number and "+s+" is its sum of digits");
}
}
Output:
Variable description table:
Variable name Type Description
n int Inputs number
s int Stores sum of digits
a int Stores sum of cubes of digits
temp int Temporarily stores value of n
2. Write a menu driven program to accept a number and the choice from the user and check
whether it is:
i. Spy number (sum of the digits=product of the digits . Example: 1124)
ii. Perfect number(if the number=sum of it’s factors other than the number)
Source code:
import java.util.Scanner;
public class p2
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
System.out.print("Enter a number: ");
int n = in.nextInt();
System.out.print("Do you want to check if it is 'spy'number or 'perfect'number?: ");
String c = in.next();
int p = 1;int s=0;
switch (c)
{
case "spy"://case for finding whether its a spy number
int temp=n;
while(temp>0)
{
s=s+(temp%10);
p=p*(temp%10);
temp=temp/10;
}
if(s==p)
System.out.println(n+" is a spy number, "+s+" is its sum of digits and "+p+" is product
of its digits");
else
System.out.println(n+" is not a spy number, "+s+" is its sum of digits and "+p+" is
product of its digits");
break;
case "perfect"://case for finding whether its a perfect number
for(int i=1;i<n;i++)
{
if(n%i==0)
s=i+s;
}
if(s==p)
System.out.println(n+" is a perfect number and "+s+" is the sum of its factors");
else
System.out.println(n+" is not a perfect number and "+s+" is the sum of its factors");
Break;
default://default in case of wrong input(to prevent errors)
System.out.println("error in input; please check ensure that your input is same as the
words in single quotes");
}
}
}
Output:
Variable description table:
Variable name Type Description
n int Inputs number
s int Stores sum of digits
p int Stores product of digits
c string Stores value for switch-case
3. Write a Java program to accept a number and print the reverse of it. Also check whether it is a
palindrome.
Source code:
import java.util.Scanner;
public class p3
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
System.out.print("Enter a number: ");//input statement
int n = in.nextInt();
int temp=n,s=0;
while(temp>0)//loop for extracting and adding digits
{
s=s*10+(temp%10);
temp=temp/10;
}
System.out.println(s+" is the reverse of "+n);
if(n==s)//checks if palindrome
System.out.println(n+" is a Palindrome number");
else
System.out.println(n+" is not a Palindrome number");
}
}
Output:
Variable description:
Variable name Type Description
n int Inputs number
s int Stores sum of digits
temp int Stores value of n temporarily
4. Using a switch statement, write a menu driven program for the following:
a. To display the series: 1,11,111,1111,11111-------n terms.
b. To display the series: 2,5,10,17--------n terms.
Source code:
import java.util.Scanner;
public class p4
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
System.out.print("Enter a number(extent): ");
int n = in.nextInt();
System.out.print("Do you want to display series 'a' or series 'b'?: ");
char c = in.next().charAt(0);//inputs variable for switch-case
switch(c)
{
case 'a'://case for first series
long s = 1;
for(int i=1;i<=n;i++)
{
System.out.print(s+"|");
s = s+(int)Math.pow(10,i);
}
break;
case 'b'://case for second series
int sb = 2;
for(int i=3,j=1;j<=n;j++,i+=2)
{
System.out.print(sb+"|");
sb = sb+i;
}
break;
default:
System.out.println("error in input; please check ensure that your input is same as the
words in single quotes");
}
System.out.println("");
}
}
Output:
Variable description:
Variable name Type Description
n int Inputs number
s long Output variable for series a
sb int Output variable for series b
c string Stores value for switch-case
5. Using a switch statement, write a menu driven program for the following:
a. S=x1 -x 2+x3 -x 4+x5 -x 6+------+xn
b. S=1+1/3+1/5+1/7+---------1/29
Source code:
import java.util.Scanner;
public class p5
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
System.out.print("Do you want series 'a' or series 'b'?: ");
char c = in.next().charAt(0);
int S = 1;
switch(c)
{
case 'b'://case for series b
for(int i=1;i<=29;i+=2)
{
S = S+1/i;
}
break;
case 'a'://case for series a
System.out.print("Enter a number: ");
int x = in.nextInt();
System.out.print("Enter a number(extent for powers): ");
int n = in.nextInt();
for(int i=1;i<=n;i++)
{
if(i%2==0)
S=S-(int)Math.pow(x,i);
else
S=S+(int)Math.pow(x,i);
}
break;
default://default case for preventing error
System.out.println("error in input; please check ensure that your input is same as the
words in single quotes");
}
System.out.println(S+" is the result of series");
}
}
Output:
Variable description:
Variable name Type Description
S int Stores product of series
x int Inputs start number for case a
c char Stores value for switch-case
n int Inputs extent for case a
6. Write a Java program to check whether the given number is a Neon Number. (A number is said
to be ‘Neon’ if sum of the digits of the square of the number is equal to the number itself. eg 9 is a
Neon number)
Source code:
import java.util.Scanner;
public class p6
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
System.out.print("Enter a number: ");
int n = in.nextInt();//inputs number
int sq = n*n,s=0,temp=sq;
while(sq>0)
{
s=s+(sq%10);//stores sum of digits of square
sq=sq/10;
}
if(s==n)//checks if neon number
System.out.println(n+" is a neon number and "+s+" is sum of digits of its
square("+temp+")");
else
System.out.println(n+" is not a neon number and "+s+" is sum of digits of its
square("+temp+")");
}
}
Output:
Variable description:
Variable name Type Description
n int Inputs number
s int Stores sum of digits of square
sq int Stores square of n
7. Write a program to accept a number and display the factors of the number , number of factors
and check whether it is a prime or composite
Source code:
import java.util.Scanner;
public class p7
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
System.out.print("Enter a number: ");
int n = in.nextInt();
System.out.print("factors: ");
int c=0;
for(int i=1;i<=n;i++)
{
if(n%i==0)//checks if any factors are present)
{
System.out.print(i+"|");
c++;
}
}
System.out.println();
System.out.println("number of factors is "+c);
if(c>2)//checks if composite
System.out.println(n+" is a composite number");
else//checks if prime
System.out.println(n+" is a prime number");
}
}
Output:
Variable description:
Variable name Type Description
n int Inputs number
i int Control variable of for loop
c string Stores number of factors
8. Write a program to accept a number and check whether it is an Automorphic number or not
( Number which is contained in the last digit (or digits) in it’s square Ex: 25 ,square is 625, so 25
is automorphic)
Source code:
import java.util.Scanner;
public class p8
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
System.out.print("Enter a number: ");
int n = in.nextInt();
int c = 0,sq=n*n;//squares number
int temp=n;
while(temp>0)
{
temp=temp/10;//finds number of digits in number
c++;
}
if(sq%(int)Math.pow(10,c)==n)//checks if automorphic
System.out.println("automorphic");
else//if not automorphic
System.out.println("not automorphic");
}
}
Output:
Variable description:
Variable name Type Description
n int Inputs number
temp int Temporarily stores value of n
sq int Stores square of n
c string counter/flag
9. Write a program to input any 50 numbers (including positive and negative). Perform the
following tasks: (a) Count the positive numbers (b) Count the negative numbers (c) Sum of
positive numbers (d) Sum of negative numbers
Source code:
import java.util.Scanner;
public class p9
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
int sp=0,cp=0,sn=0,cn=0;
for(int i=1;i<=50;i++)//loop for 50 number inputs
{
System.out.print("Enter number "+i+": ");
int n = in.nextInt();
if(n>0)//if positive number
{
sp=sp+n;
cp++;
}
else//if negative number
{
sn=sn+n;
cn++;
}
}
System.out.println(sp+" is the sum of positive numbers which are "+cp+" in number");
System.out.println(sn+" is the sum of negative numbers which are "+cn+" in number");
}
}
Output:
Variable description:
Variable name Type Description
n int Inputs number
sp int Sum of positive numbers
cp int Count of positive numbers
sn int Sum of negative numbers
cn int Count of negative numbers
10. Write a program to accept a number and check whether it is a niven number or not ( Number
which is divisible by sum of the digits. Ex: 126, sum of digits=1+2+6=9, 126 divisible by 9)
Source code:
import java.util.Scanner;
public class p10
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
System.out.print("Enter a number: ");
int n = in.nextInt();
int temp=n;
int s=0;
while(temp>0)//finds sum of digits
{
s = s+(temp%10);
temp = temp/10;
}
if(n%s==0)//checks if niven
System.out.println("Niven number");
else//if not niven
System.out.println("not a Niven number");
}
}
Output:
Variable description:
Variable name Type Description
n int Inputs number
s int Stores sum of digits
temp int Stores value of n temporarily
11.Print all the prime numbers in the range of m to n (accept m,n from the user).
Source code:
import java.util.Scanner;
public class p11
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
System.out.println("Enter start of range and end of range respectively: ");
System.out.print("from: ");
int m = in.nextInt();
System.out.print("to: ");
int n = in.nextInt();
boolean p=false;
for(int i = m;i<=n;i++)//outer loop for range
{
for(int j = 2;j<i/2;j++)//inner loop for checking prime
{
if(i%j==0)
{
p = true;
break;
}
}
if(p==false)//checks if prime
System.out.print(i+"|");
p=false;
}
System.out.println();
}
}
Output:
Variable description:
Variable name Type Description
m int Start extent
m int End extent
p boolean True or false for prime or composite
j int Control variable of inner loop
i int Control variable of inner loop
12.Using a switch statement, write a menu driven program for the following:
a. 9 7 5 3 1
9753
975
97
9
b. 11 13 15 17 19
21 23 25 27 29
31 33 35 37 39
41 43 45 47 49
c. 1
10
101
1010
10101
d. 1 2 3 4 5 6 7
12345
123
1
Source code:
import java.util.Scanner;
public class p12
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Which pattern would you like to execute(a,b,c, or d): ");
char c = sc.next().charAt(0);
int i,j;
switch(c)
{
case 'a'://first pattern
for(i=1;i<=9;i+=2)
{
for(j=9;j>=i;j-=2)
{
System.out.print(j+" ");
}
System.out.println();
}
break;
case 'b'://second pattern
for(i=1;i<=4;i++)
{
for(j=1;j<=9;j+=2)
{
System.out.print(i+""+j+" ");
}
System.out.println();
}
break;
case 'c'://third pattern
for(i=1;i<=5;i++)
{
int k=1;
for(j=1;j<=i;j++,k++)
{
if(k==2)
k-=2;
System.out.print(k+" ");
}
System.out.println();
}
break;
case 'd'://fourth pattern
for(i=7;i>=1;i-=2)
{
for(j=1;j<=i;j++)
{
System.out.print(j+" ");
}
System.out.println();
}
break;
default:
System.out.println("Error in input. Please retry.");
}
}
}
Output:
Variable description:
Variable name Type Description
j int Control variable of inner loop
i int Control variable of outer loop
k int Constant value for loop
(either 1 or 0)
13. Write a program to input a number and display sum of it’s digits raised to the power of their
respective position. Input: 465 Output: 51+6 2 + 4 3=> 5+36+64=105
Source code:
import java.util.Scanner;
public class p13
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
System.out.print("Enter a number: ");//inputs number
int n = in.nextInt();
int s=0;int c=1;
while(n>0)
{
s = s+(int)Math.pow(n%10,c);//sum of digits raised to power of positions
n = n/10;
c++;//power of positions
}
System.out.println(s+" is the sum of digits raised to the power of their respective position.");
}
}
Output:
Variable description:
Variable name Type Description
n int Inputs number
s int Stores sum of digits raised to power
of position
c int Counter for position
14.Write a program to accept a number, check whether it is a prime or not, if it is prime check
whether it’s reverse is also prime. If the number and the reverse of the number are primes then
print it is a Twisted prime else it not a twisted prime
Source code:
import java.util.Scanner;
public class p14
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
System.out.print("Enter a number: ");
int n = in.nextInt();
int temp=n;
int r=0;
while(temp>0)//reversing number
{
r = r*10+(temp%10);
temp = temp/10;
}
boolean p = false;
for(int j = 2;j<n/2;j++)//if number is prime
{
if(n%j==0)
{
p = true;
break;
}
}
if(p==false)//if reverse is prime
{
for(int j = 2;j<r/2;j++)
{
if(r%j==0)
{
p = true;
break;
}} }
if(p==false)
System.out.println("twisted prime number");
else
System.out.println("not a twisted prime number");
}
}
Output:
Variable description:
Variable name Type Description
n int Inputs number
s int Stores sum of digits
temp int Stores value of n temporarily
r int Stores value of reverse of n