0% found this document useful (0 votes)
21 views31 pages

Comp Proj

COmputer Project

Uploaded by

yashchourasia715
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views31 pages

Comp Proj

COmputer Project

Uploaded by

yashchourasia715
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 31

1.

Armstrong Number
Code:
import java.util.*;
class Armstrong {
Scanner sc = new Scanner(System.in);
int n,s,t;
Armstrong() {
n=0;
s=0;
t=0;
}
void input() {
System.out.println("Enter a number ");
n = sc.nextInt();
t=n;
}
void sumcal() {
while(t>0) {
int d = t%10;
s = s*10 +(d*d*d);
t = t/10;
}
}
void check() {
if(s == n) {
System.out.println("Armstrong no ");
}
else {
System.out.println("Not an armstrong no ");
}
}
}
Algorithm:
Step 1:Input n;
Step 2:t=n;
Step 3:while(t>0) {Repeat 4,5,6}
Step 4:d = t%10;
Step 5:s = s*10+(d*d*d);
Step 6:t= t/10;
Step 7:if(s == n) then print “Armstrong no “;
Step 8:else print “Not an Armstrong no “;

2. Automorphic Number
Code:
import java.util.Scanner;
class Automorphic_Number {
Scanner sc = new Scanner(System.in);
void main() {
System.out.println("Enter a number ");
int in = sc.nextInt();
int t = in;
int c = 0;
while(in>0) {
in = in/10;
c++;
}
int v = t*t;
if(t == v%Math.pow(10,c) ) {
System.out.println("Automorphic number ");
}
else {
System.out.println("Not an automorphic number ");
}
}
}
Algorithm:
Step 1: Input in
Step 2: t = in,c=0;
Step 3: while(in>0) {Repeat Step 4 &5}
Step 4: in = in/10;
Step 5: c++;
Step 6: v = t*t;
Step 7: if(t == v% Math.pw(10,c) then print Automorphic no.
Step 8: else print Not Automorphic no.

3. DeciOct
Code:
import java.util.*;
class DeciOct {
int n,oct;
DeciOct() {
n =0;
oct =0;
}
void getnum() {
Scanner sc = new Scanner(System.in);
System.out.println("Enter a no ");
n = sc.nextInt();
}
void deci_oct() {
int t =n;
while(t>0) {
int d = t%8;
oct = oct*10+d;
t = t/10;
}
}
void show() {
System.out.println("Octal value " + oct);
}
}
Algorithm:
Step 1: n =0,oct=0;
Step 2: Input n
Step 3: t =n;
Step 4: while(t>0) {Repeat 5,6,7}
Step 5: d = t%8;
Step 6: oct = oct*10+d;
Step 7: t = t/10;
Step 8: Print oct;

4. Disarium Number
Code:
import java.util.*;
class Disarium_Number {
Scanner sc = new Scanner(System.in);
void main() {
System.out.println("Enter a number ");
int in = sc.nextInt();
int t =in,c=0,s=0;
while(t>0) {
t =t/10;
c++;
}
t = in;
while(t>0) {
int d = t%10;
s = s+ (int)Math.pow(d,c);
t=t/10;
c--;
}
if(s==in) {
System.out.println("Disarium Number ");
}
else {
System.out.println("Not a disarium number ");

}
}
}
Algorithm:
Step 1: Input in;
Step 2: t = in,c=0,s=0;
Step 3: while(t>0) {Repeat
Step 4: t =t/10;
Step 5: c++;
Step 6: while(t>0) {Repeat 4,5,6}
Step 7: d = t%10;
Step 8: s = s+Math.pow(d,c);
Step 9: t = t/10;
Step 10: c--;
Step 11: if(s == in) then print Disarium no;
Step 12: else print Not Disarium no;

5. Dudeney Number
Code:
import java.util.*;
class Dudeney {
Scanner sc = new Scanner(System.in);
void main() {
System.out.println("Enter a number ");
int in = sc.nextInt();
int t =in,s=0;
while(t>0) {
int d = t%10;
s = s+d;
t = t/10;
}
if(Math.cbrt(in) == s) {
System.out.println("Dudeney no ");
}
else {
System.out.println("Not a dudeney no ");
}
}
}
Algorithm:
Step 1: Input in;
Step 2: t = in,s=0;
Step 3: while(t>0) {Repeat 4,5,6}
Step 4: d = t%10;
Step 5: s = s+d;
Step 6: t = t/10;
Step 7: if(Math.cbrt(in) == s) then print Dudeney no;
Step 8: else print Not Dudeney no;

6. Emirp Number
Code:
import java.util.*;
class emirp {
int n,rev,f;
emirp() {
n = 0;
rev =0;
f =0;
}
void input() {
Scanner sc = new Scanner(System.in);
System.out.println("Enter a number ");
n = sc.nextInt();
}
void isprime() {
for(int i =1;i<n;i++) {
if(n%i == 0) {
f++;
}
}

}
void isEmirp() {
int t =n;
int count=0;
while(t>0) {
f = t%10;
rev = rev*10+f;
t = t/10;
}

for(int i =1;i<rev;i++) {
if(rev%i ==0 ) {
count++;
}
}
if(count ==2 && f==2) {
System.out.println("Emirp no ");
}
else {
System.out.println("Not emirp no ");
}
}
}
Algorithm:
Step 1: Input n;
Step 2: for(int i =1;i<n;i++) {Repeat 3}
Step 3: if(n%i ==0) then f=f+1;
Step 4: while(n>0) {Repeat 5,6,7}
Step 5: d = t%10;
Step 6: rev = rev*10+d;
Step 7: t = t/10;
Step 8: for(int i =1;i<rev;i++) {Repeat 9}
Step 9: if(rev%i ==0) then count++;
Step 10:if(count ==2 && f ==2) then print "Emirp no"
Step 11:else print "Not emirp no ";
7. Exchange
Code:
import java.util.*;
class Exchange {
Scanner sc = new Scanner(System.in);
void main() {
System.out.println("Enter a sentence ");
String s = sc.nextLine();
StringTokenizer st = new StringTokenizer(s);

String s1 = "",s2="";
while(st.hasMoreTokens()) {
s1= st.nextToken();
if(s1.length()>2) {
s2= s2+s1.charAt(s1.length()-
1)+s1.substring(1,s1.length()-1)+s1.charAt(0)+" ";
//System.out.println(s1.substring(1,s1.length()-1));
}
else if(s1.length()==2){
s2 = s2+s1.charAt(1)+s1.charAt(0)+" ";
}
else{
s2+=s1+" ";
}

}
System.out.println(s2);
}
}
Algorithm:
Step 1: Input s;
Step 2: Declare StringTokenizer st;
Step 3: s1 = "",s2="";
Step 4: while(st.hasMoreTokens()) {Repeat 5,6,7,8}
Step 5: s1 = st.nextToken();
Step 6: if(s1.length() >2) then s2 = s2+s1.charAt(s1.length()-
1)+s1.substring(1,s1.length()-1)+s1.charAt(0)+" ";
Step 7: else if(s1.length() == 2) then s2 = s2+s1.charAt(1)+s1.charAt(0)+"
";
Step 8: else s2+=s1+" ";
Step 9: Print s2;

8. Factors
Code:
import java.util.*;
class Factors {
Scanner sc = new Scanner(System.in);
void main() {
System.out.println("Enter a number ");
int n = sc.nextInt();
for(int i =1;i<n;i++) {
if(n%i == 0) {
System.out.println("Factors: " + n);
}
}
}
}
Algorithm:
Step 1: Input n
Step 2: for(i = 1;i<n;i++) {Repeat 3}
Step 3: if(n%i ==0) then print n;

9. Merger
Code:
import java.util.*;
class Merger {
Scanner sc = new Scanner(System.in);
int n1,n2;
long mergNum;
Merger() {
n1 =0;
n2 =0;
mergNum =0;
}
void readNum() {
System.out.println("Enter two numbers ");
n1 = sc.nextInt();
n2 = sc.nextInt();
}
void joinNum() {
mergNum = Long.valueOf(String.valueOf(n1) + String.valueOf(n2));
}
void show() {
System.out.println("First num : " + n1);
System.out.println("Second num : " + n2);
System.out.println("Concatenation : " + mergNum);
}
void main() {
Merger ob = new Merger();
ob.readNum();
ob.joinNum();
ob.show();
}
}
Algorithm:
Step 1:Input n1,n2;
Step 2:mergNum = Long.valueOf(String.valueOF(n1) + String
Step 3:Print mergNum;
uare.t

10. Num PRG 1


Code:
import java.util.*;
class num {
Scanner sc = new Scanner(System.in);
int n,f;
num() {
n =0;
f =1;
}
void input() {
System.out.println("Enter a number ");
n = sc.nextInt();
}
void fact() {
for(int i = n;i>0;i--) {
f = f*i;
}
}
void display() {
fact();
System.out.println("The factorial is : " + f);
}
}
Algorithm:
Step 1:Input n;
Step 2:for(int i =n;i>0;i--) {Repeat 3}
Step 3:f = f*i;
Step 4:Print f;

11. Num PRG 2


Code:
import java.util.*;
class num1 {
Scanner sc = new Scanner(System.in);
int n,s;
num1() {
n = 0;
s =0;
}
void input() {
System.out.println("Enter a number ");
n = sc.nextInt();
}
void sumfact() {
for(int i =1;i<n;i++) {
if(n%i ==0) {
s = s+i;
}
}
}
void display() {
sumfact();
System.out.println("Sum of factors: " + s);
}
}
Algorithm:
Step 1:Input n;
Step 2:for(int i=1;i<n;i++) {Repeat 3,4}
Step 3:if(n%i==0) then s = s+i;
Step 4:Print s;

12. Perfect Number


Code:
import java.util.*;
class Perfect {
Scanner sc = new Scanner(System.in);
void main() {
System.out.println("Enter a number ");
int in = sc.nextInt();
int sum =0,i=1;
while(i<in) {
if(in%i ==0) {
sum = sum+i;
}

}
if(sum == in) {
System.out.println("Perfect number");
}
else {
System.out.println("Not a perfect number ");
}
}
}
Algorithm:
Step 1:Input in;
Step 2:sum=0,i=1;
Step 3:while(i<in) {Repeat 4,5}
Step 4:if(in%i ==0) then sum = sum+i;
Step 5:if(sum == in) then print "Perfect number ";
Step 6:else print "Not perfect number “

13. Prime Matrix


Code:
import java.util.*;
class Prime_Matrix {
Scanner sc = new Scanner(System.in);
boolean Prime_Check(int x) {
int count =0;
boolean check = true;
for(int i =1;i<=x;i++) {
if(x%i ==0) {
count++;
}
}
if(count ==2) {
check = true;
}
else {
check = false;
}
return check;
}
void main() {
System.out.println("Enter the dimensions of the array ");
int d = sc.nextInt();
int m[][] = new int[d][d];
for(int i = 0;i<d;i++) {
for(int j = 0;j<d;j++) {
System.out.println("Enter a number ");
m[i][j] = sc.nextInt();
}
}
for(int i =0;i<d;i++) {
for(int j=0;j<d;j++) {
if(Prime_Check(m[i][j]) == true) {
System.out.println(m[i][j] + " is a prime no ");
}
}
}
}
}
Algorithm:
Prime Matrix
Step 1:boolean PrimeCheck(int x) {Step 2 to 6}
Step 2:count =0;
Step 3:boolean check = true;
Step 4:for(int i =1;i<x;i++) {Repeat 5}
Step 5:if(x%i ==0) then check = true,else check = false;
Step 6:return check;
Step 7:void main() {Step 7 to 11}
Step 8:Input d;
Step 9:for(int i =0;i<d;i++) {Repeat 10,11}
Step 10:for(int j =0;j<d;ij++) {Repeat 11}
Step 11:if(PrimeCheck(m[i][j] == true) then Print m[i][j] + 'is a prime
no

14. Saddle Point


Code:
import java.util.*;
class Saddle_Point {
Scanner sc = new Scanner(System.in);
int m[][] = new int[4][4];
void main() {
for(int i =0;i<4;i++) {
for(int j =0;j<4;j++) {
System.out.println("Enter a number ");
m[i][j] = sc.nextInt();
}
}
int min_r = 0,flag=0,pos_r=0,max_c=0;
for(int i =0;i<4;i++) {
min_r = m[i][0];
for(int j =0;j<4;j++) {
if(m[i][j] < min_r) {
min_r = m[i][j];
pos_r = j;
}
}
max_c = m[i][pos_r];
for(int y =0;y<4;y++) {
if(m[y][pos_r]> max_c) {
max_c = m[y][pos_r];
}
}
if(max_c==min_r) {
System.out.println("The saddle point is " + min_r);
flag++;
break;
}
}
if(flag>0) {
System.out.println("No saddle point ");
}
}
}
Algorithm:
Step 1:for(int I = 0;I<4;I++) {Repeat 2,3}
Step 2:for(int j =0;j<0;j<4;j++) {Repeat 3,4}
Step 3:Input values in m[I][j];
Step 4:for(int I =0;i<4;I++) {Repeat 5,6,7,8,9,10,11,12}
Step 5:min_r = m[I][0];
Step 6: for(int j=0;j<4j++) {Repeat 6,7,8,9,10,11}
Step 7:if(m[I][j]<min_r) then min_r = m[I][j];pos_r =j;
Step 8:max_c == m[I][pos_r}
Step 9:for(int y =0;y<y;y++) {Repeat 10}
Step 10:if(m[y][pos_r] >max_c) then max_c == m[y][pos_c]
Step 11:if(max_c == min_r) then print “Saddle Point is “ + min_r
Step 12:else print no saddle point ;

15. Sentence
Code:
import java.util.*;
class Sentence {
Scanner sc = new Scanner(System.in);
String line;
Sentence() {
line = "";
}
void input() {
System.out.println("Enter a sentence ");
line = sc.nextLine();
}
void count() {
StringTokenizer st = new StringTokenizer(line);
int count =0;
while(st.hasMoreTokens()) {
System.out.println(st.nextToken());
for(int i =0;i<line.length() ;i++) {
char ch = line.charAt(i);
ch = Character.toUpperCase(ch);
if(ch == 'A'||ch == 'E' || ch == 'I' || ch == 'O' || ch ==
'U') {
count++;
}
}
System.out.println("Vowels " + count);
}

}
}
Algorithm:
Step 1:Input line;
Step 2:StringTOkenizer st = new StringTokenizer(line);
Step 3:int count =0;
Step 4:while(st.hasMoreTokens) {Repeat 5,6}
Step 5:Print st.nextToken;
Step 6:for(int I =0i<line.length();I++) {Repeat 7,8}
Step 7:ch = line.charAt(I);
Step 8:ch = Character.toUpperCase(ch);
Step 9: if((ch == 'A'||ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U')
then count++;
Step 10: Print count

16. Smith Number


Code:
import java.util.*;
class Smith_No {
boolean prime(int n ) {

for(int i =2;i<n/2;i++) {
if(n%i ==0) {
return false;
}
}
return true;
}
int sum(int n ) {
int s =0;
while(n>0) {
s = s+n%10;
n = n/10;
}
return s;
}
void main() {
Scanner sc = new Scanner(System.in);
System.out.println("Enter a number ");
int in = sc.nextInt();
int s1 = sum(in);
int s2=0;
for(int i =2;i<=in;) {
if(in%i == 0 && prime(i) ==true) {
s2 = s2+sum(i);
}
else {
i++;
}
}
if(s1 == s2 ) {
System.out.println("Smith no");
}
else {
System.out.println("Not a smith no ");
}
}
}
Algorithm:
Step 1:boolean prime(int x) {Step 2 to Step 5}
Step 2:for(int I =2;I<n/2;I++) {Repeat 3,4}
Step 3:if(n%I==0) then return false;
Step 4:break;
Step 5:return true
Step 6:int sum(int n) {Step 7 to 10}
Step 7:while(n>0) {Repeat 8,9,10}
Step 8:d = n%10;
Step 9:s = s*10+d;
Step 10:n = n/10;
Step 11:void main() {Step 12 to Step 15}
Step 12:Input in;
Step 13:s1 = sum(in);
Step 14:for (int i =2;i<=in;) {Repeat 15,16}
Step 15: if(in%i == 0 && prime(i) ==true) then s2 = s2+sum(I);
Step 16:else I++;
Step 17:if(s1 == s2) then print “Smith no “;
Step 18:else print “Not a Smith no “;

17. String PRG 1


Code:
import java.util.*;
class str {
Scanner sc = new Scanner(System.in);
String s;
str() {
s = "";
}
void input() {
System.out.println("Enter a number ");
s = sc.nextLine();
}
boolean check(char ch) {
ch = Character.toLowerCase(ch);
if(ch == 'a'||ch == 'e'||ch == 'i'||ch == 'o'|| ch =='u') {
return true;
}
else {
return false;
}
}
void count() {
int count =0;
for(int i =0;i<s.length();i++) {
char c = s.charAt(i);
if(check(c) == true) {
count++;
}
}
System.out.println("Vowels in the given sentence " + count);
}
}
Algorithm:
Step 1:void input {Input s;}
Step 2:boolean check(char ch) {Step 3 to Step 5}
Step 3:ch = Character.toLowerCase(ch);
Step 4:if(ch ==’a’||ch == ‘e’||ch==’i’|| ch ==’o’|| ch == ‘u’) then return
true;
Step 5:else return false;
Step 6:void count() {Step 7 to 9}
Step 7:for(int i =0;i<s.length();i++) {Repeat 8,9}
Step 8:char c = s.charAt(I);
Step 9:if(check(I) == true) then count++;
Step 10;Print count

18. String PRG 2


Code:
import java.util.*;
class str2 {
Scanner sc = new Scanner(System.in);
String s;
str2() {
s = "";
}
void input() {
System.out.println("Enter a sentence ");
s = sc.nextLine();
}
String reverse(String x) {
String st = "";
for(int i =0;i<x.length();i++) {
char ch = x.charAt(i);
st = ch+st;
}
return st;
}
void check() {
if(s.equalsIgnoreCase(reverse(s))) {
System.out.println("Palindrome ");
}
else {
System.out.println("Not palindrome ");
}
}
}
Algorithm:
Step 1:void Input() {Input s}
Step 2:String reverse(String x) {Step 3 to 6}
Step 3: for(int i =0;i<x.length();i++) {Repeat 4,5}
Step 4: char ch = x.charAt(i);
Step 5:st = ch+st;
Step 6:return st;
Step 7:void check() {Step 8 to 10}
Step 8:if(s.equalsIgnoreCase(reverse(s))) then print “Palindrome”
Step 9:else print “Not palindrome “
19. Sum Matrix
Code:
import java.util.*;
class Sum_of_Matrix {
Scanner sc = new Scanner(System.in);
int m1[][] = new int[3][3];
int m2[][] = new int[3][3];
int m3[][] = new int[3][3];
void main() {
for(int i =0;i<3;i++) {
for(int j =0;j<3;j++) {
System.out.println("Enter a number for first matrix ");
m1[i][j] = sc.nextInt();
}
}
for(int i =0;i<3;i++) {
for(int j =0;j<3;j++) {
System.out.println("Enter a number for second matrix ");
m2[i][j] = sc.nextInt();
}
}
for(int i =0;i<3;i++) {
for(int j =0;j<3;j++) {
m3[i][j] = m1[i][j] + m2[i][j];
}
}
for(int i =0;i<3;i++) {
for(int j =0;j<3;j++) {
System.out.println(m3[i][j]);

}
}
}
}
Algorithm:
Step 1: for(int i =0;i<3;i++) {Repeat 2,3}
Step 2: for(int j =0;j<3;j++) {Repeat 3}
Step 3:input values in m1[I][j]
Step 4: for(int i =0;i<3;i++) {Repeat 5,6}
Step 5:for(int j=0;j<3;j++) {Repeat 6}
Step 6:Input values in m2[I][j];
Step 7: for(int i =0;i<3;i++) {Repeat 8,9}
Step 8: for(int j=0;j<3;j++) {Repeat 9}
Step 9:m3 = m1[I][j] + m2[I][j];

20. Sum of Series


Code:
import java.util.*;
class SumSeries {
int x,n;
double sum;
SumSeries(int xx,int nn) {
x =0;
n=0;
sum = 0.0;
}
double findFact(int m) {
double fact =1;
for(int i=1;i<m;i++) {
fact = fact*(m-i);
}
return fact;
}
double findPower(int x,int y) {
double pow = Math.pow(x,y);
return pow;
}
void calculate() {
sum = findPower(x,n)/findFact(n);
}
void display() {
System.out.println(sum);
}

}
Algorithm:
Step 1:double findFact(int m) {Step 2 to 4}
Step 2: for(int i=1;i<m;i++) {Repeat 3}
Step 3:fact = fact*(m-I);
Step 4:return fact;
Step 5: double findPower(int x,int y) {Step 6 to 7}
Step 6:pow = Math,pow(x,y);
Step 7:return pow
Step 8:void calculate() {sum = findPower(x,n)/findFact(n);
Step 9:void display() {Print sum}

21. Triangular Number


Code:
import java.util.*;
class Triangular_Number {
Scanner sc = new Scanner(System.in);
void main() {
System.out.println("Enter the number of triangular nos for display
");
int in = sc.nextInt();
int sum = 0;
for(int i =1;i<in;i++) {
sum = sum+i;
System.out.println("Triangular no: " + sum);
}
}
}
Algorithm:
Step 1:Input in;
Step 2:sum=0;
Step 3: for(int i =1;i<in;i++) {REpeat 4}
Step 4: sum = sum+i;
Step 5: Print sum;

22. Twin Prime


Code:
import java.util.*;
class Twin_Prime {
Scanner sc = new Scanner(System.in);
void main() {
System.out.println("Enter 2 numbers ");
int in = sc.nextInt();
int in1 = sc.nextInt();
int c = 0,co =0,i=1,j=1;
while(i<=in) {
if(in%i ==0) {
c++;
}
i++;
}
while(j<=in1) {
if(in1%j==0) {
co++;
}
j++;
}
if(c == 2) {
System.out.println(in + " is prime ");
}
else {
System.out.println(in + " not is prime ");
}
if(co == 2) {
System.out.println(in1 + " is prime ");
}
else {
System.out.println(in1 + " not is prime ");
}
if( c==2 && co ==2 && in1-in ==2) {
System.out.println("Twin prime ");
}
}
}
Algorithm:
Step 1:Input in and in1;
Step 2: c = 0,co =0,i=1,j=1;
Step 3: while(i<=in) {Repeat 4,5}
Step 4:if(in%n == 0) then c++;
Step 5:I++;
Step 6:while(j<=in1_ {Repeat 7,8}
Step 7:if(in1%j ==0) then co++;
Step 8:j++;
Step 9:if(co == 2 && c ==2) then print “Twin prime no”
Step 10:else print :Not twin prime;

23. Prime Number


Code:
import java.util.*;
class Prime {
Scanner sc = new Scanner(System.in);
void main() {
System.out.println("Enter a number ");
int in = sc.nextInt();;
int i,sum=0;
for(i=1;i<=in;i++)
{
if(in%i==0)
sum++;
}
if(sum == 2) {
System.out.println("Prime number");
}
else {
System.out.println("Not a prime number ");
}
}
}
Algorithm:
Step 1:Input in;
Step 2: for(i=1;i<=in;i++) {Repeat 3}
Step 3:if(in%I ==0) then count++;
Step 4:if(count ==2) then print “Prime no “;
Step 5:else Print “Not prime np”

24. Factors Constructor


Code:
import java.util.*;
class fact
{
Scanner sc=new Scanner(System.in);
int n;
fact()
{
n=0;
}
void input()
{
System.out.println("Enter a number");
n=sc.nextInt();
}
int sum_fact(int v)
{
int i,sum=0;
for(i=1;i<n;i++)
if(n%i==0)
sum+=i;
return sum;
}
void show()
{
System.out.println("The original number= "+n);
System.out.println("Sum of factors of the number= "+sum_fact(n));
}
}//end of class
Algorithm:
Step 1:Input n;
Step 2:for (i=1;i<n;i++) {Repeat 3,4}
Step 3:if(n%I ==0) then sum = sum+1;
Step 4:Print sum;

25. Unique Number


Code:
import java.util.*;
class UniqueNumber
{
public void main(int num)
{
int copy,i,count=0,d;
for(i=0;i<=9;i++)
{
copy=num;
while(copy>0)
{
d=copy%10;
if(d==i)
count++;
copy/=10;
}
if(count>1)
break;
else
count=0;
}
if(count<1)
System.out.println("Unique number");
else
System.out.println("Not unique number");
}
}//end of class
Algorithm:
Step 1:Input num;
Step 2:int copy =0,count=0,d=0;
Step 3: for(i=0;i<=9;i++) {Repeat 3 to 10}
Step 4:copy = num;
Step 5:while(copy>0) {Repeat 6 to 8}
Step 6:d = copy%10;
Step 7:if(d==i) then count++;
Step 8:copy = copy/10;
Step 9:if(count >1) then break;
Step 10:else count == 0
Step 11:ifcount == 0) then print “Unique no “;

26. Num PRG 3


Code:
import java.util.*;
class num2
{
int n,r;
num2()
{
n=0;
r=0;
}
void input()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a number");
n=sc.nextInt();
}
void rev()
{
int d,copy=n;
while(copy>0)
{
d=copy%10;
r=r*10+d;
copy/=10;
}
}
void dis()
{
System.out.println("The number: "+n);
System.out.println("Reverse of the number: "+r);
}
}//end of class
Algorithm:
Step 1: Input n;
Step 2: r=0;
Step 3: void rev() (Step 3 to 8)
Step 4: int d,copy=n;
Step 5: while(copy>0)
Step 6: d=copy%10;
Step 7: r=r*10+d;
Step 8: copy/=10;
Step 9: void dis() (Step 10 to 11)
Step 10: Print n;
Step 11: Print r;

27. Reverse
Code:
import java.util.*;
class rev
{
Scanner sc=new Scanner(System.in);
int n;
rev()
{
n=0;
}
void input()
{
System.out.println("Enter a number");
n=sc.nextInt();
}
int reverse(int v)
{
int rev=0,d;
while(v>0)
{
d=v%10;
rev=rev*10+d;
v/=10;
}
return rev;
}
void show()
{
System.out.println("The original number= "+n);
System.out.println("The reversed number= "+reverse(n));
}
}//end of class
Algorithm:
Step 1: Input n;
Step 2: int reverse(int v) (Step 3 to 8)
Step 3: int rev=0,d;
Step 4: while(v>0) (Repest 5,6,7)
Step 5: d=v%10;
Step 6: rev=rev*10+d;
Step 7: v/=10;
Step 8: return rev;
Step 9: void show() (Step 10 to 11)
Step 10: Print n;
Step 11: Print reverse(n);

28. Sum of Product


Code:
import java.util.*;
class sumproduct
{
Scanner sc=new Scanner(System.in);
int n;
sumproduct()
{
n=0;
}
void readnum()
{
System.out.println("Enter a number");
n=sc.nextInt();
}
int sum(int v)
{
int sum=0,d;
while(v>0)
{
d=v%10;
sum=sum+d;
v/=10;
}
return sum;
}
int product(int v)
{
int prod=1,d;
while(v>0)
{
d=v%10;
prod=prod*d;
v/=10;
}
return prod;
}
void check()
{
int copy=n;
if((sum(n)*product(n))==copy)
System.out.println("Sum product number");
else
System.out.println("Not sum product number");
}
public static void main(String args[])
{
sumproduct ob=new sumproduct();
ob.readnum();
ob.check();
}
}//end of class
Algorithm:
Step 1: Input n;
Step 2: int sum(int v) (Step 3 to 8)
Step 3: int sum=0,d;
Step 4: while(v>0) (Repeat 5,6,7)
Step 5: d=v%10;
Step 6: sum=sum+d;
Step 7: v/=10;
Step 8: return sum;
Step 9: int product(int v) (Step 10 to 15)
Step 10: int prod=1,d;
Step 11: while(v>0) (Repeat 12,13,14)
Step 12: d=v%10;
Step 13: prod=prod*d;
Step 14: v/=10;
Step 15: return prod;
Step 16: void check() (Step 17 to 19)
Step 17: int copy=n;
Step 18: if((sum(n)*product(n))==copy) then print sum product number;
Step 19: else print not sum product number;
Step 20: void main() (Step 21 to 23)
Step 21: sumproduct ob=new sumproduct();
Step 22: ob.readnum();
Step 23: ob.check();

29. Token
Code:
import java.util.*;
class Token
{
String str;
Token()
{
str="";
}
void input()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a string");
str=sc.nextLine();
str+=" ";
}
void display_word()
{
StringTokenizer y=new StringTokenizer(str);
int l=y.countTokens();
for(int i=0;i<l;i++)
System.out.println(y.nextToken());
}
public static void main(String args[])
{
Token ob=new Token();
ob.input();
ob.display_word();
}
}//end of class
Algorithm:
Step 1: Input str;
Step 2: str+=” “;
Step 3: void display_word() (Step 4 to 7)
Step 4: StringTokenizer y=new StringTokenizer(str);
Step 5: int l=y.countTokens();
Step 6: for(int i=0;i<l;i++) (Repeat 7)
Step 7: Print y.nextToken();
Step 8: void main() (Step 9 to 11)
Step 9: Token ob=new Token();
Step 10: ob.input();
Step 11: ob.display_word();

30. Name Initials


Code:
import java.util.*;
class NameInitials
{
public void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a string");
String str=sc.nextLine();
int n=str.length(),n1=str.lastIndexOf(' '),i,pos=0;
String title=str.substring(n1+1),word="";
str=str.substring(0,n1)+" ";
for(i=0;i<=n1;i++)
{
char ch=str.charAt(i);
if(ch==' ')
{
word=str.substring(pos,i);
System.out.print(word.charAt(0)+".");
pos=i+1;
}
}
System.out.print(title);
}
}//end of class
Algorithm:
Step 1: Input str;
Step 2: int n=str.length();
Step 3: int n1=str.lastIndexOf(' ');i
Step 4: int pos =0,i;
Step 5: String title=str.substring(n1+1);
Step 6: word="";
Step 7: str=str.substring(0,n1)+" ";
Step 8: for(i=0;i<=n1;i++) (Repeat 9,10)
Step 9: char ch=str.charAt(i);
Step 10: if(ch==' ') then word=str.substring(pos,i), print
word.charAt(0)+".”; and pos=i+1;
Step 11: Print title;

31. Spy Number


Code:
import java.util.*;
class SpyNumber
{
public void main()
{
System.out.println("Enter a number");
int num=new Scanner(System.in).nextInt();
if(sumOfDigits(num)==prodOfDigits(num))
System.out.println("Spy number");
else
System.out.println("Not Spy number");
}
int sumOfDigits(int n)
{
int sum=0;
while(n>0)
{
sum=sum+(n%10);
n/=10;
}
return sum;
}
int prodOfDigits(int n)
{
int prod=1;
while(n>0)
{
prod=prod*(n%10);
n/=10;
}
return prod;
}
}//end of class
Algorithm:
Step 1: Input num;
Step 2: void main() (Step 3 to 4)
Step 3: if(sumOfDigits(num)==prodOfDigits(num)) then print spy number;
Step 4: else print not spy number;
Step 5: int sumOfDigits(int n) (Step 6 to 10)
Step 6: int sum=0;
Step 7: while(n>0) (Repeat 8,9)
Step 8: sum=sum+(n%10);
Step 9: n/=10;
Step 10: return sum;
Step 11: int prodOfDigits(int n) (Step 12 to 16)
Step 12: int prod=1;
Step 13: while(n>0) (Repeat 14,15)
Step 14: prod=prod*(n%10);
Step 15: n/=10;
Step 16: return prod;

32. Palindrome String


Code:
import java.util.*;
class Palindromestr
{
String str;
Palindromestr()
{
str="";
}
void input()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a string");
str=sc.nextLine();
}
String reverse(String s)
{
String rev="";
for(int i=0;i<s.length();i++)
{
char ch=s.charAt(i);
rev=ch+rev;
}
return rev;
}
void check()
{
if(reverse(str)==str)
System.out.println("Palindrome string");
else
System.out.println("Not Palindrome string");
}
public static void main(String args[])
{
Palindromestr ob=new Palindromestr();
ob.input();
ob.check();
}
}//end of class
Algorithm:
Step 1: Input str;
Step 2: String reverse(String s) (Step 3 to 7)
Step 3: String rev="";
Step 4: for(int i=0;i<s.length();i++) (Repeat 5,6)
Step 5: char ch=s.charAt(i);
Step 6: rev=ch+rev;
Step 7: return rev;
Step 8: void check() (Step 9 to 11)
Step 9: if(reverse(str)==str) then print palindrome string
Step 10: else print not palindrome string
Step 11: void main() (Step 12 to 14)
Step 12: Palindromestr ob=new Palindromestr();
Step 13: ob.input();
Step 14: ob.check();
33. String NUM 4
Code:
import java.util.*;
class str4
{
Scanner sc=new Scanner(System.in);
String st;
str4()
{
st="";
}
void input()
{
System.out.println("Enter a string");
st=sc.nextLine();
}
boolean check(char ch)
{
String vowel="AEIOUaeiou";
if(vowel.indexOf(ch)!=-1)
return true;
else
return false;
}
void count()
{
int count=0;
StringTokenizer str=new StringTokenizer(st);
while(str.hasMoreTokens())
{
String w=str.nextToken();
char c=w.charAt(0);
if(check(c))
count++;
}
}
public static void main(String args[])
{
str4 ob=new str4();
ob.input();
ob.count();
}
}//end of class
Algorithm:
Step 1: Input st;
Step 2: boolean check(char ch) (Step 3 to 4)
Step 3: String vowel="AEIOUaeiou";
Step 4: if(vowel.indexOf(ch)!=-1) then return ture;
Step 5: else return false;
Step 6: void count() (Step 7 to 12)
Step 7: int count=0;
Step 8: StringTokenizer str=new StringTokenizer(st);
Step 9: while(str.hasMoreTokens()) (Repeat 10,11,12)
Step 10: String w=str.nextToken();
Step 11: char c=w.charAt(0);
Step 12: if(check(c)) then count++;
Step 13: void main() (Step 14 to 16)
Step 14: str4 ob=new str4();
Step 15: ob.input();
Step 16: ob.count();

34. String NUM 5


Code:
import java.util.*;
class str5
{
Scanner sc=new Scanner(System.in);
String st;
str5()
{
st="";
}
void input()
{
System.out.println("Enter a string");
st=sc.nextLine();
}
boolean check(char ch)
{
if(Character.isUpperCase(ch))
return true;
else
return false;
}
void display()
{
StringTokenizer str=new StringTokenizer(st);
System.out.println("Words that start with upper case are: ");
while(str.hasMoreTokens())
{
String w=str.nextToken();
char c=w.charAt(0);
if(check(c))
System.out.println(w);
}
}
public static void main(String args[])
{
str5 ob=new str5();
ob.input();
ob.display();
}
}//end of class
Algorithm:
Step 1: Input st;
Step 2: boolean check(char ch) (Step 3 to 4)
Step 3: if(Character.isUpperCase(ch)) then return true;
Step 4: else then return false;
Step 5: void display() (Step 6 to 10)
Step 6: StringTokenizer str=new StringTokenizer(st);
Step 7: while(str.hasMoreTokens()) (Repeat 8,9,10)
Step 8: String w=str.nextToken();
Step 9: char c=w.charAt(0);
Step 10: if(check(c)) then print w;
Step 11: void main() {Step 12 to 14)
Step 12: str5 ob=new str5();
Step 13: ob.input();
Step 14: ob.display();

35. Boundary Sum


Code:
import java.util.*;
class BoundarySum
{
int row,col,arr[][];
void accept()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter rows and columns");
row=sc.nextInt();
col=sc.nextInt();
arr=new int[row][col];
System.out.println("Enter elements");
int i,j;
for(i=0;i<row;i++)
for(j=0;j<col;j++)
arr[i][j]=sc.nextInt();
}
void boundarySum()
{
int i,j,bsum=0,nbsum=0;
for(i=0;i<row;i++)
for(j=0;j<col;j++)
if(i==0||i==row-1||j==0||j==col-1)
bsum+=arr[i][j];
System.out.println("Boundary sum= "+bsum);
}
public static void main(String args[])
{
BoundarySum ob=new BoundarySum();
ob.accept();
ob.boundarySum();
}
}//end of class
Algorithm:
Step 1: Input row, col, Array arr[][];
Step 2: void boundarysum (Step 3 to 6)
Step 3: for(i=0;i<row;i++) (Repeat 4,5)
Step 4: for(j=0;j<col;j++) (Repeat 5)
Step 5: if(i==0||i==row-1||j==0||j==col-1) then bsum+=arr[i][j];
Step 6: Print bsum;
Step 7: void main() (Step 8 to 10)
Step 8: BoundarySum ob=new BoundarySum();
Step 9: ob.accept();
Step 10: ob.boundarySum();

36. Matrix
Code:
import java.util.*;
class Matrix
{
int arr[][]=new int[4][4];
void input()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter 16 elements in 4x4 matrix");
for(int r=0;r<4;r++)
for(int c=0;c<4;c++)
arr[r][c]=sc.nextInt();
}
void findMax()
{
for(int r=0;r<4;r++)
{
int max=arr[r][0];
for(int c=0;c<4;c++)
if(arr[r][c]>max)
max=arr[r][c];
}
System.out.println(max);w
}
void findMin()
{
for(int r=0;r<4;r++)
{
int min=arr[0][r];
for(int c=0;c<4;c++)
if(arr[c][r]<min)
min=arr[c][r];
}
System.out.println(min);
}
public static void main(String args[])
{
Matrix ob=new Matrix();
ob.input();
ob.findMax();
ob.findMin();
}
}//end of class
Algorithm:
Step 1: Input 2D Array arr[][]
Step 2: void findmax() (Step 3 to 7)
Step 3: for(int r=0;r<4;r++) (Repeat 4,5,6)
Step 4: int max=arr[r][0];
Step 5: for(int c=0;c<4;c++) (Repeat 6)
Step 6: if(arr[r][c]>max) then max=arr[r][c];
Step 7: Print max;
Step 8: void findmin() (Step 9 to 13)
Step 9: for(int r=0;r<4;r++) (Repeat 9,10,11)
Step 10: int min=arr[0][r];
Step 11: for(int c=0;c<4;c++) (Repeat 11)
Step 12: if(arr[c][r]<min) then min=arr[c][r];
Step 13: Print min;
Step 14: void main() (Step 15 to 18)
Step 15: Matrix ob=new Matrix();
Step 16: ob.input();
Step 17: ob.findMax();
Step 18: ob.findMin();

You might also like