1. Design a class Demo1 as follows:
int fact(int k): Return the factorial of k
void main (): Enter a number. Find the factorial of the number by calling the above function. Then check whether the factorial of the number is Buzz number or not.
ANS
import java.util.*;
class demo_1
{
int fact (int k)
{
int i,f=1;
for(i=1;i<=k;i++)
{
f=f*i;
}
return f;
}
void main()
{
Scanner ob=new Scanner(System.in);
int a ,m;
System.out.println ("Enter a no");
a=ob.nextInt();
m=fact(a);
if(m%7==0 && m%10==7)
{
System.out.println("yes");
}
else
{
System.out.println("no");
}
}
}
2. Design a class Demo2 as follows:
int sum(int y): Return the sum of odd digits of y
void main (): Enter a number and find the sum of odd digits of the number by calling the above function.
ANS
import java.util.*;
class demo_2
{
int sum (int y)
{
int d,s=0;
while(y!=0)
{
d=y%10;
if(d==2 || d==3 || d==5 || d==7)
{
s=s+d;
}
y=y/10;
}
return s;
}
void main()
{
Scanner ob=new Scanner (System.in);
int n;
System.out.println("Enter a no");
n=ob.nextInt();
System.out.println("Sum of odd digits of"+n+"is"+sum(n));
}
}
3. Design a class Demo3 as follows:
int pro(int x): Return the product of even digits of x
void main (): Enter 2 numbers. Find the product of even digits of first number by calling the above function, if the product is divisible by 9 then find the square of the second number else print appropriate messgae.
ANS
import java.util.*;
class demo_3
{
int pro(int n)
{
int s=1,d;
while(n!=0)
{
d=n%10;
if(d==2 || d==4 || d==6 ||d==8)
{
s=s*d;
}
n=n/10;
}
return s;
}
void main()
{
Scanner ob=new Scanner(System.in);
int a,b,sq,m;
System.out.println("Enter the first no");
a=ob.nextInt();
System.out.println("Enter the second no");
b=ob.nextInt();
m=pro(a);
System.out.println("the product is"+m);
if(m%9==0)
{
sq=b*b;
System.out.println("The no is divisible");
System.out.println("The square of the second number is"+sq);
}
else
{
System.out.println("The first product is not divisible by 9");
}
}
}