computer project A ROLL NO 34
computer project A ROLL NO 34
thomas
School
import java.util.*;
class Smith
{
static int sum(int n)
{
int s,i,d;
s=0;
for(i=n;i!=0;i=i/10)
{ d=i
%10;
s=s+d;
}
return s;
}
public static void main()
{
Scanner sn=new Scanner(System.in);
int num,s,s1,p,n,cp,i;
System.out.println("enter a number");
num=sn.nextInt();
s=sum(num);
cp=num;
s1=0;
for(i=2;cp!=1;i++)
{
while(cp%i==0)
{
s1=s1+sum(i);
cp=cp/i;
}
}
if(s==s1)
System.out.println(num+" is smith number");
else
System.out.println(num+" is not smith number");
}
}
VARIBALE DESCRIPTION :
INPUT : 22
INPUT : 27
class Special
{
public static void main(int n)
{
int copy=n;int s=0; while(n>0)
{
int r=n%10;int p=1;
for(int j=r;j>=1;j--)
{
p=p*j;
}
s=s+p;
n/=10;
}
if(copy==s)
System.out.println(copy+" is a special number"); else
System.out.println(copy+" is not a special number");
}
}
VARIABLE DESCRIPTION:
INPUT: 145
class Fascinating
System.out.println("Enter a Number.");
int n=sc.nextInt();
int a=n*1;
int b=n*2;
int c=n*3;
String s1=Integer.toString(a);
String
s2=Integer.toString(b);
String s3=Integer.toString(c);
String s=s1+s2+s3;
int l=s.length();
int d=0;
char h=s.charAt(i);
for (int j=i+1;j<=(l-1);j++)
char m=s.charAt(j);
if(h==m)
d++;
if(d==2)
break;
if(d==2)
else
}}
VARIABLE DESCRIPTION :
class disarium
n=x;
l=(""+n).length();
int f1(int n)
if(n<10)
return (int)Math.pow(n%10,l--)+f1(n/10);
VARIABLE DESCRIPTION :
Input: 135.
import java.util.*;
class consecutive_sum
{
static void Cons(int n)
{
int start = 1;
int end = (n + 1) / 2;
while (start < end)
{
int sum = 0;
for (int i = start; i <= end; i++)
{
sum = sum + i;
if (sum == n)
{
for (int j = start; j <= i; j++)
System.out.print(j + " ");
System.out.println();
break;
}
if (sum > n)
break;
}
sum = 0;
start++;
}
}
INPUT : 27
OUTPUT : 2 + 3 + 4 + 5 + 6 + 7=27
8 + 9 + 10=27
PROGRAM 7 : Write a program to input a number and check
whether it is a composite magic number or not. Composite number:
A composite number is a number that has more than two factors. For
example: 10 Factors are: 1, 2, 5, 10. Magic number: A magic
number is a number in which the eventual sum of the digits is equal
to 1.
class composite_magic
{
public static void main(int n)
{
int c=0;int copy=n;
for(int i=1; i<=n;i++)
{
if (n%i==0) c++;
}
if(c>2)
{ int s;
while(n>1)
{ s=0;
while(n>0)
{int r=n%10; s=s+r; n/=10;} n=s;
}
if(n==1)
System.out.println(copy+" is a composite_magic");
else
System.out.println(copy+" is not a composite_magic number");
}
else
System.out.println( copy+" is not a composite_magic");
}
}
VARIABLE DESCRIPTION:
INPUT : 28
class Cbv
int x=10;
int y=20;
System.out.println(obj.x+" "+obj.y);
obj.swap(obj.x,obj.y);
System.out.println(obj.x+" "+obj.y);
int temp=0;
temp=a;
a=b;
b=temp;
System.out.println(a+" "+b);
}
VARIABLE DESCRIPTION :
class Cbr
{
int x=10;
int y=20;
public static void main (String args[])
{
Cbr obj=new Cbr();
System.out.println(obj.x+" "+obj.y);
obj.swap(obj);
System.out.println(obj.x+" "+obj.y);
}
public void swap(Cbr obj1)
{
int temp=0;
temp=obj1.x;
obj1.x=obj1.y;
obj1.y=temp;
System.out.println(obj1.x+" "+obj1.y);
}
}
VARIABLE DESCRIPTION :
VARIABLE DATA DESCRIPTION
NAME TYPE
t int To store first initial
a int To store elements in array
i int To execute the loop
PROGRAM 10 : A Perfect number is a positive integer, sum of its
proper divisor is equal to its own value. Input a number and check
whether it is Perfect or not through recursion.
import java.util.*;
class Perfect
{ int num;
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("enter your number"); int n=sc.nextInt();
Perfect obj=new Perfect(n); obj.check();
}
Perfect(int n)
{num=n;}
int SumOfFactors(int i)
{
if(i<num&&num%i==0)
return i+SumOfFactors(i+1);
else if(i<num&&num%i!=0)
return 0+SumOfFactors(i+1);
else
return 0;
}
void check()
{
if(num==SumOfFactors(1))
System.out.println(num+" is Perfect number");
else
System.out.println(num+" is not perfect number");
}
}
VARIABLE DESCRIPTION:
Input: 15
Output: 15 is not Perfect number
PROGRAM 11 : A happy number is one in which eventual sum of
square of all the digits is 1. Input a number and check whether it is
a happy number or not through recursion.
import java.util.*;
class Happy
{
int num;
public static void main(String args[])
{ Scanner sc=new Scanner(System.in); System.out.println("enter your
numer"); int n=sc.nextInt();
while(n!=1&&n!=4)
{
n=SumOfDigits(n);
}
if(n==1)
System.out.println("It is happy number");
else
System.out.println("It is not a happy number");
}
static int SumOfDigits(int n)
{
int sum=0;
while(n>0)
{
sum+=(int)Math.pow(n%10,2);
n=n/10;
}
return sum;
}
}
VARIABLE DESCRIPTION:
Input: 19
Output: It is a
HappyNumber
Explanation:
1^2 + 9^2 =82
6^2+8^2 =100
import java.util.*;
public class Palindrome
{
public static boolean checkPalindrome(String str)
{
if(str.length() == 0 || str.length() == 1)
return true;
if(str.charAt(0) == str.charAt(str.length() - 1))
return checkPalindrome(str.substring(1, str.length() - 1));
return false;
}
public static void main(String[]args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Please enter a string : ");
String strInput = sc.nextLine();
if(checkPalindrome(strInput))
{
System.out.println(strInput + " is palindrome");
}
else
{
System.out.println(strInput + " is not a palindrome");
}
sc.close();
}
}
VARIABLE DESCRIPTION :
INPUT : “ARORA”
OUTPUT : ARORA is a palindrome
PROGRAM 13 : A class contain a two dimensional integer
array of order M×N where maximum value of both M & N
are 5. Design a class to shuffle the matrix. The first row
becomes the last and the 2nd row becomes the 1st and so
on.
import java.io.*;
class Shift{
private int mat[][];
private int m;
private int n;
public Shift(int mm, int nn){
m = mm;
n = nn;
if(m > 5)
m = 5;
if(n > 5)
n = 5;
mat = new int[m][n];
}
public void input()throws IOException{
InputStreamReader in = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(in);
System.out.println("Enter matrix elements:");
for(int i = 0; i < m; i++)
for(int j = 0; j < n; j++)
mat[i][j] = Integer.parseInt(br.readLine());
}
public void cyclic(Shift P){
int s = m - 1;
int t = 0;
for(int i = 0; i < n; i++)
this.mat[s][t++] = P.mat[0][i];
s = 0;
for(int i = 1; i < m; i++){
t = 0;
for(int j = 0; j < n; j++)
this.mat[s][t++] = P.mat[i][j];
s++;
}
}
public void display()
{ for(int i = 0; i < m; i+
+){
for(int j = 0; j < n; j++)
System.out.print(mat[i][j] + "\t");
System.out.println();
}
}
public static void main(String args[])
throws IOException{
InputStreamReader in = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(in);
System.out.print("Number of rows: ");
int rows = Integer.parseInt(br.readLine());
System.out.print("Number of columns: ");
int cols = Integer.parseInt(br.readLine());
Shift obj1 = new Shift(rows, cols);
Shift obj2 = new Shift(rows, cols);
obj1.input();
System.out.println("ORIGINAL
MATRIX:"); obj1.display();
obj2.cyclic(obj1);
System.out.println("SHUFFLED
MATRIX:"); obj2.display();
}
}
VARIABLE DESCRIPTION :
INPUT :
Number of rows: 2
Number of columns: 1
Enter matrix elements:
12
34
OUTPUT :
ORIGINAL MATRIX:
12
34
SHUFFLED MATRIX:
34
12
PROGRAM 14: Write a program to print an inputted 8
digit number in date format.
import java.util.*;
class Date_8
{
public static void main( String args[] )
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter your date in eight digit format");
String s= sc.nextLine();
int dd=Integer.parseInt(s.substring(0,2));
int mm=Integer.parseInt(s.substring(2,4));
int yy=Integer.parseInt(s.substring(4,8));
int d[]={31,28,31,30,31,30,31,31,30,31,30,31};
int d1[]={31,29,31,30,31,30,31,31,30,31,30,31};
String
mname[]={"January","February","March","April","May","June","Jul
y","August"
,"September","October","November","December"};
if(yy%4==0||yy%100==0||yy%400==0)
{
if(d1[mm-1]>=dd)
{
System.out.println(dd+"th "+mname[mm-1]+","+yy);
System.out.println("Valid Date");
}
else
System.out.println("Invalid Date");
}
else
{
if(d[mm-1]>=dd)
{
System.out.println(dd+"th "+mname[mm-1]+","+yy);
System.out.println("Valid Date");
}
else
System.out.println("Invalid Date");
}
}
}
VARIABLE DESCRIPTION :
class evil
{
public static void main(int n)
{
String w="";
int count=1;
while(n!=1)
{
int r=n%2; w=w+r; n/=2;
}
for(int i=0;i<w.length();i++)
{
char ch=w.charAt(i);
if(ch=='1')
count++;
}
if(count%2==0)
System.out.println("evil number");
else
System.out.println("Odious Number(not an evil
number)");
}
}
VARIABLE DESCRIPTION :
Input : 3
Output : Evil Number
Explanation: Binary
expansion of 3 is 11, the
number of 1s in this is 2
i.e. even.
Input : 16
Output : Odious Number(not
an evil number) Explanation:
Binary expansion of 16 =
10000, having number of 1s
=1 i.e. odd.
PROGRAM 16: Write a program to accept a string and
print the letters of given string in reverse order
separated by ‘-'.
class reverse
{
public static void main(String s)
{ String w="";
int p=0;
for(int i=0;i<s.length();i++)
{
char ch=s.charAt(i);
if(ch==' ')
{
w=s.substring(p,i);
System.out.print(w.charAt(w.length()-
1)+w.substring(1,w.length()-1)+w.charAt(0)+" ");
p=i;
}
}
}
}
VARIABLE DESCRIPTION :
import java.util.*;
class exchange_first_to_last
{
public static void main(String Args[])
{
int m, l, i, q=0;
char a, z, x;
String t;
Scanner sc=new Scanner(System.in);
System.out.println("Enter the Sentence");
String s=sc.nextLine();
s=s+" ";
l=s.length();
for(i=0;i<l;i++)
{
String k;
a=s.charAt(i);
if(a==' ')
{
t=s.substring(q,i);
m=t.length();
if(m>1)
{
z=t.charAt(0);
x=t.charAt(m-1);
t=t.substring(1,m-1);
k=x+t+z;
}
else
k=t;
q=i+1;
System.out.print(" "+k);
}
}
}
}
VARIABLE DESCRIPTION :
INPUT : COMPUTER
OUTPUT: ROMPUTEC
PROGRAM 18 : Write a program to input number of
weekdays(1 to 7) and translate it to its equivalent name of
the day of the week.
class weeks
{
public static void main(int w)
{
String weekname[] =
{"Monday","Tuesday","Wednesday","Thursday","Friday",
"Saturday", "Sunday"};
System.out.println(weekname[w-1]);
}
}
VARIABLE DESCRIPTION :
INPUT : 05
OUTPUT : FRIDAY
PROGRAM 19 : A super class Worker has been defined
to share the details of a worker.
Define a subclass Wages to compute the monthly
wages for the worker. The details of both the classes
are given below:
Class name: Worker Data members:
Name: To store the name of the worker
Basic: to store the basic pay in decimal Member
functions:
Worker (): parameterized constructor to assign values
to the instance variable.
void display(): to display the
worker details. Class name :
Wages
Data members:
class Worker
{
String name; double basic;
Worker(String s,double b)
{
name=s; basic=b;
}
void display()
{ System.out.println("Name of the worker : "+name);
System.out.println("Basic pay of the worker : "+basic);
}
}
class Wages extends Worker
{ int hrs;
double rate; double wage;
Wages( String s,double b,int h,double r)
{
super(s,b); rate=r; hrs=h; wage=0.0;
}
double overtime()
{
return (hrs*rate);
}
void display()
{
super.display();
wage= overtime()+basic; System.out.println("Total Wage given =
"+wage);
}
}
VARIABLE DESCRIPTION :
Methods/Member functions:
class WordPile
{
char ch[];
int capacity, top;
WordPile(int cap)
{
ch=new char[cap]; capacity=cap; top=-1;
}
void pushChar(char v)
{
if(top>=capacity)
System.out.println("WordPile is full");
else
ch[++top]=v;
}
char popChar()
{
if(top<0)
return
'\\'; else
return ch[top--];
}
}
VARIABLE DESCRIPTION :