1
Assignment
1. Write a menu driven program to perform the following:-
a. Check if a character is a digit or not
b. Print the reverse of a number
c. Check if a number is a lucky number or not
d. Input three numbers. Using library function find the largest.
System.out.println(“a Check if a character is a digit or not”);
System.out.println(“b Print the reverse of a number”);
System.out.println(“c Check if a number is a lucky number or not”);
System.out.println(“d Find the largest of three numbers”);
System.out.println(“Enter your choice”);
char ch=sc.next().charAt(0);
switch(ch)
{
case ‘a’ : System.out.println(“Enter a character”);
char c=sc.next().charAt(0);
if(c>= ‘0’&&c<=‘9’)
System.out.println(“Digit”);
else
System.out.println(“Not a digit”);
break;
case ‘b’ : System.out.println(“Enter a number”);
int n=sc.nextInt();
int i=n,d,rev=0;
while(i>0)
{
d=i%10;
rev = rev*10+d;
i/=10;
}
System.out.println(“Reverse = ”+rev);
break;
case ‘c’ : System.out.println( “Enter number”);
int n=sc.nextInt();
int i=n,d,s=0;
while(i>0)
{
d=i%10;
int f=1;
for(int j=1;j<=d;j++)
f=f*j;
s=s+f;
i=i/10;
}
if(s==n)
System.out.println(“Lucky number”);
else
System.out.println(“Not a Lucky number”);
break;
case ‘d’ : System.out.println( “Enter three numbers”);
int a=sc.nextInt();
int b=sc.nextInt();
int c=sc.nextInt();
int largest=Math.max(Math.max(a,b),c);
System.out.println(“Largest = ”+ largest);
break;
default: System.out.println(“Wrong choice”);
}
}
1
2
2. Print the sum and product of the series: [ 3 separate programs]
a. 1,4,9,16……….n terms
System.out.println(“Enter number of terms”);
int n=sc.nextInt();
int s=0,p=1;
for(int i=1;i<=n;i++)
{
s=s+(i*i);
p=p*(i*i);
}
System.out.println(“Sum=”+s);
System.out.println(“Product=”+p);
b. 0,7,26,63…… for 30 terms
int s=0,p=1;
for(int i=1;i<=30;i++)
{
s=s+(i*i*i-1);
p=p*(i*i*i-1);
}
System.out.println(“Sum=”+s);
System.out.println(“Product=”+p);
c. X7, X12, X22………………… for m terms
System.out.println(“Enter value for X and number of terms”);
int X=sc.nextInt()
int m=sc.nextInt();
int a=7,b=5;
double s=0,p=1;
for(int i=1;i<=m;i++)
{
s=s+Math.pow(X,a);
p=p*Math.pow(X,a);
a=a+b;
b=b*2;
}
3. Print the 30th term of the series : 1,4,10,22…………….…….
int a=1, b=3;
for(int i=1;i<30;i++)
{
a=a+b;
b=b*2;
}
System.out.println(a);
4. Which term is 4096 in the series 1,2,4,8,16……………………...?
int i,a=1;
for(i=1;a<4096;i++)
a*=2;
System.out.print(i);
2
3
5. Input a number. Check if it is binary. If binary, convert it to decimal.
System.out.println("Enter a number");
int n=sc.nextInt();
int i=n,d;
boolean flag=false;
while(i>0)
{
d=i%10;
if(d>1)
{
flag=true;
break;
}
i/=10;
}
if(flag==false)
{
System.out.println(“It is a Binary number”);
i=n,d,e=0;
double s=0.0;
while(i>0)
{
d=i%10;
s=s+d*Math.pow(2,e++);
i/=10;
}
System.out.println("Decimal value = "+(int)s);
}
else
System.out.println(“Not Binary number”);
6. Find the HCF and LCM of two numbers
System.out.println(“Enter the two numbers:”);
int a = sc.nextInt();
int b = sc.nextInt();
int small = a<b ? a : b;
int hcf=0;
for(int i=1;i<=small;i++)
{
if(a%i==0&&b%i==0)
hcf=i;
}
int lcm=a*b/hcf;
System.out.println(“HCF = ” +hcf);
System.out.println(“LCM = ” +lcm);
3
4
7. Input a number . Check if it is an even automorphic / not.
System.out.println(“Enter a number”);
int n=sc.nextInt();
int i=n,p=1;
while(i>0)
{
p=p*10;
i=i/10;
}
if(n==n*n%p&&n%2==0)
System.out.println(“Even Automorphic”);
else
System.out.println(“Not an Even Automorphic”);
8. Print perfect numbers from 10 to 1000. Also count the automorphic numbers in this range
int i,j,s,c=0;
System.out.println(“Perfect numbers”);
for(i=10;i<=100;i++)
{
s=0;
for(j=1;j<=i/2;j++)
{
if(i%j==0)
s+=j;
}
if(s==i)
System.out.print(i+”\t”);
int k=i,p=1;
while(k>0)
{
p=p*10;
k=k/10;
}
if(i==i*i%p)
c++;
}
System.out.println(“Count of Automorphic numbers = ”+c);
9. Check if a number is an Armstrong number or not
System.out.println(“Enter number:”);
int n = sc.nextInt();
int i=n,d,s=0;
while(i>0)
{
d=i%10;
s=s+d*d*d;
i/=10;
}
if(n==s)
System.out.println(“Armstrong”);
else
System.out.println(“Not an Armstrong”);