CS Project
CS Project
import java.util.*;
public class special
{
int n;
special()
{
n=0;
}
void read()
{
Scanner sc=new Scanner (System.in);
System.out.println("enter a number:");
n=sc.nextInt();
}
int factorial(int x)// to calculate the factorial of the number
{
int f=1;
for(int i=1;i<=x;i++)
{
f=f*i;
}
return f;
}
boolean isSpecial()// to check if the number is a special number
{
int c=n;
int sum=0;
while(c>0)
{
int digit=c%10;
c=c/10;
sum=sum+factorial(digit);
}
if(sum==n)
return true;
else
return false;
}
void display()// to display whether the number is a special number or not
{
boolean a=isSpecial();
if(a==true)
System.out.println("Special Number");
else
System.out.println("Not a Special Number");
}
public static void main()
{
special obj=new special();
obj.read();
obj.display();
}
}
Question 4
import java.util.*;
public class happy
{
int n;
happy()
{
n=0;
}
void getnum(int nn)
{
n=nn;
}
int sum_sq_digits(int x)//to calculate the sum of square of digits
{
int sq=0;
int sum=0;
while(x>0)
{
int digit=x%10;
x=x/10;
sq=digit*digit;
sum+=sq;
}
return sum;
}
void ishappy()// to check if the number is a happy number
{
int a=n;
while(a>9)
{
a=sum_sq_digits(a);
}
if(a==1)
System.out.println("It is a happy number");
else
System.out.println("It is not a happy number");
}
public static void main()
{
happy obj=new happy();
Scanner sc=new Scanner (System.in);
System.out.println("Enter any number:");//accepting a number to check if it
is a happy number
int m=sc.nextInt();
obj.getnum(m);
obj.ishappy();
}
}
Sr. No Datatype Variable Name Description
1. Int n To accept a
number and
check if it is a
happy number.
2. Int sq To calculate and
store the square
of digits.
3. Int sum To calculate and
store the sum of
square of digits.
4. Int digit To store the
extracted digit.
5. Int a To story a copy of
the number.
Question 5
import java.util.*;
public class disarium
{
int num,size;
disarium(int nn)
{
num=nn;
size=0;
}
void countDigit()// to count and store the number of digits
{
int c=num;
while(c>0)
{
int digit=c%10;
c=c/10;
size++;
}
}
int sumofDigits(int n, int p)//to store the sum of digits raised to their respective
powers
{
int sum=0;
while(n>0)
{
int d=n%10;
n=n/10;
sum+=(int)Math.pow(d,p);
p--;
}
return sum;
}
void check()// to check if the number is disarium
{
if(num==sumofDigits(num,size))
{
System.out.println("Disarium");
}
else
System.out.println("Not Disarium");
}
public static void main()
{
Scanner sc=new Scanner (System.in);
System.out.println("Enter any Number:");
int m=sc.nextInt();
disarium obj=new disarium(m);
obj.countDigit();
obj.check();
}
}
Sr. No Datatype Variable Name Description
1. Int num To check if the
number is a
disarium number.
2. Int size To count and
store the number
of digits.
3. Int c To story a copy of
the original
number.
4. Int digit To extract and
store the digits of
the number.
5. Int sum To store the sum
of square of
digits.
6. Int p To store the
power the digits
are raised to.
7. Int d To store the
extracted digit.
8. Int m To accept a
number from the
user to check if it
is a disarium
number.
Question 6
import java.util.*;
public class Palin
{
int num, revnum;
Palin()
{
num=0;
revnum=0;
}
void accept()// to accept a number from the user
{
Scanner sc=new Scanner (System.in);
System.out.println("Enter a number");
num=sc.nextInt();
}
int reverse(int y)// to reverse the original number
{
while(y>0)
{
int d=y%10;
y=y/10;
revnum=revnum*10+d;
}
return revnum;
}
void check()//to check if the number is a palindrome number
{
if(reverse(num)==num)
System.out.println("Palindrome Number");
else
System.out.println("Not a Palindrome Number");
}
public static void main()
{
Palin obj=new Palin();
obj.accept();
obj.check();
}
}
Sr. No Datatype Variable Name Description
1. Int num To accept a
number from the
user and check if
it is a palindrome
number.
2. Int revnum To reverse the
number and store
it.
3. Int d To store the digit
that is extracted
from the number.
Question 7
import java.util.*;
public class Security
{
int h;
String n;
double r,w;
Security()
{
h=0;
n="";
r=0.0;
w=0.0;
}
void accept()
{
Scanner sc=new Scanner (System.in);
System.out.println("Enter Name:");
n=sc.next();
System.out.println("Enter Hours:");
h=sc.nextInt();
}
void calwage()
{
if(h<30)
w= h*1;
else if(h>=30&&h<60)
w=30*1 + (h-30)*2;
else
w=30*1 + 30*2 + (h-60)*5;
}
void display()
{
System.out.println(" NAME HOURS WAGES");
System.out.println(n+" "+h+" "+w+" ");
}
public static void main()
{
Security obj=new Security();
obj.accept();
obj.calwage();
obj.display();
}
}
Variable Table
Question 8
import java.util.*;
public class SeriesSum
{
int x,n;
double sum;
SeriesSum(int xx,int nn)
{
n=nn;
x=xx;
sum=0.0;
}
double findfact(int m)
{
int factorial=1;
for(int i=1;i<=m;i++)
{
factorial=factorial*i;
}
return factorial;
}
double findpower(int x,int y)
{
double pow=Math.pow(x,y);
return pow;
}
void calculate()
{
int a=1;
for(int i =0;i<=n;i++)
{
sum+=findpower(x,(a+1))/findfact(a);
a=a+2;
}
}
void display()
{
System.out.println("Sum: "+sum);
}
public static void main()
{
Scanner sc=new Scanner (System.in);
System.out.println("Enter an Integer:");
int z=sc.nextInt();
System.out.println("Enter number of terms:");
int y=sc.nextInt();
SeriesSum obj=new SeriesSum(z,y);
obj.calculate();
obj.display();
}
}
Sr. No Datatype Variable Name Description
1. Int x To store an
integer.
2. Int n To store the
number of terms.
3. Int sum To calculate and
store the sum.
4. Int Factorial To store the
factorial of a
number
5. Int Power To store the
exponential
power
6. Int a To store the
power the digits
are raised to.
7. Int z To accept an
integer from the
user
8. Int y To accept the
number of terms
from the user.
import java.util.*;
public class Unique
{
String wrd;
int len;
Unique()
{
wrd="";
len=0;
}
void acceptWord()
{
Scanner sc=new Scanner (System.in);
System.out.println("Enter a word:");
wrd=sc.next();
wrd=wrd.toUpperCase();
len=wrd.length();
}
boolean checkUnique()
{
if(wrd.charAt(0)=='A'||wrd.charAt(0)=='E'||wrd.charAt(0)=='I'||
wrd.charAt(0)=='O'||wrd.charAt(0)=='U'&&wrd.charAt(0)=='A'||
wrd.charAt(0)=='E'||wrd.charAt(0)=='I'||wrd.charAt(0)=='O'||
wrd.charAt(0)=='U')
{
if(wrd.charAt(0)==wrd.charAt(len-1))
{
return true;
}
}
return false;
}
void display()
{
if(checkUnique())
System.out.println("It is a Unique word");
else
System.out.println("It is not a unique word");
}
public static void main()
{
Unique obj=new Unique();
obj.acceptWord();
obj.display();
}
}
Variable Table
}
if(ctr==2)
return 1;
else
return 0;
}
int isPalin(int i)
{
int rev=0;
int copy=i;
while(i>0)
{
int digit=i%10;
i=i/10;
rev=rev*10+digit;
}
if(rev==copy)
return 1;
else
return 0;
}
void generate()
{
for(int x=start;x<=end;x++)
{
if(isPrime(x)==1&&isPalin(x)==1)
{
System.out.println(x+"Is a Prime Palindrome Number");
}
}
}
public static void main()
{
Scanner sc=new Scanner (System.in);
System.out.println("Enter Start Range:");
int z=sc.nextInt();
System.out.println("Enter end Range:");
int y=sc.nextInt();
PrimePalinGen obj=new PrimePalinGen(z,y);
obj.generate();
}
}