Академический Документы
Профессиональный Документы
Культура Документы
1,2,3,4,5,6,7,8, 9,10,
11,12,13,14,15,16,17,18,19,20,21,22,23,24,25………………….
Algorithm
Step 1: Start
Step 6: End
import java.io.*;
classLuckyNumber
int c=n;
for(int i=0;i<n;i++)
a[i]=i+1;
int del=1;
while(del<n){
for(int i=del;i<n;i+=del)
for(int j=i;j<n-1;j++){
a[j]=s[j+1];
n--;
}
del++;
for(int i=0;i<n;i++)
System.out.print(a[i]+“ ”);
System.out.println();
for(int i=0;i<n;i++)
System.out.print(a[i]+“ ”);
}}}
Q2. Write a program to display an entered number in words.
Algorithm
Step 1: Start
Step 7: End
import java.io.*;
class Num2Word
int z,g;
String x[]={“”,“Ten”,“Eleven”,“Twelve”,“Thirteen”,“Fourteen”,“Fifteen”,“
Sixteen”,“Seventeen”,“Eighteen”,“Nineteen”};
String
x1[]={“”,“One”,“Two”,“Three”,“four”,“Five”,“Six”,“Seven”,“Eight”,“Nine”};
String
x2[]={“”,“Twenty”,“Thirty”,“Forty”,“Fifty”,“Sixty”,“Seventy”,“Eighty”,“Ninety”
};
g=amt/10;
if(g!=1)
System.out.println(x2[g-1]+“ ”+x1[z]);
else
System.out.println(x[amt-9]);
}}
Q3. Write a program to create Pascal’s Triangle.
Algorithm
Step 1: Start
Step 4: Use first for loop with ‘i’ and second with ‘j<=i’ to print the pascal’s
triangle.
Step 6: End
import java.io.*;
class Pascal
System.out.println(“Enter a Number”);
int n=Integer.parseInt(br.readLine());
pas[0]=1;
for(int i=0;i<n;i++)
for(int j=0;j<=i;++j)
System.out.print(pas[j]+{“ ”);
System.out.println();
for(int j=i+1;j>0;j--)
pas[j]=pas[j]+pas[j-1];
}}
Q4. Write a program to create a string and replace all vowels with ‘*’.
Algorithm
Step 1: Start
Step 4: With the length of the string use the for loop with variable ‘z’ and replace
all the vowels with ‘*’.
Step 6: End
import java.io.*;
classvowel_star
System.out.println(“Enter a String”);
int z;
if(a.charAt(z)==‘a’||a.charAt(z)==‘e’||a.charAt(z)==‘i’||a.charAt(z)==‘o’||a.charAt(
z)==‘u’||a.charAt(z)==‘A’||a.charAt(z)==‘E’||a.charAt(z)==‘I’||a.charAt(z)==‘O’||a.
charAt(z)==‘U’)//checking and replacing the variables
a.setCharAt(z,‘*’);
}
Q5. Write a program to display the frequency of each character in an entered
string.
Algorithm
Step 1: Start
Step 7: End
import java.io.*;
class Frequency
int al,l,p,j,freq;
int i;
l=str.length();
System.out.print(str);
for(i=0;i<l;i++)
char b=str.charAt(i);
if(a==b)
freq=freq+l;
System.out.println(a+“ ”+“occurs”+freq+“times”);
freq=0;
}
public static void main(String args[])throws IOException//main function
System.out.println(“Enter String”);
String str=br.readLine();
x.count(str);
}
Q6. Write a program to print all prime number from 1 to 100.
Algorithm
Step 1: Start
Step 3: Check if the no. is divisible it ‘1’ and only from ‘itself’. If true then Step 4
else Step 5.
Step 5: End
public class pro
for(int i=1;i<=100;i=i+1)
for(int j=1;j<=i;j++)
if(i%j==0)
k++;
}}
if(k==2)
System.out.print(i+“ ”);
}
Q7. Write a program to print all palindrome number from 10 to 100.
Algorithm
Step 1: Start
Step 2: Take number 10 to 100 in first for loop with variable ‘i’.
Step 3: Take next for loop less than ‘i’ with variable ‘j’.
Step 5: Compare if the number remains same as it was then Step 6 else Step7.
Step 7: End
public class prog
int k=0;
for(int i=10;i<=100;i=i+1)
for(int j=i;j!=0;j=j/10)
r=j%10;
k=k*10+r;
if(i==k)
System.out,print(i+“ ”);
}}
Q8. Write a java program to print the Armstrong Numbers between the given
ranges.
Algorithm
Step 1: Start
Step 2: Enter the starting number in ‘start’ and last in ‘end’ and start for loop with
variable ‘i’.
Step 3: Take each number one by one store them in ‘temp’ variable for later
calculations and chop them into single digits.
Step 4: Count the length of the digits and the multiply each digit from itself to that
times the length is found and store them in ‘num’.
Step 5: After multiplying each digit add them and check if (num==temp) the
number is equal to given number. If true then Step 6 else Step 7.
Step 7: End
import java.util.*;
start = scanner.nextInt();
end = scanner.nextInt();
temp = i;
num = 0;
while(temp != 0)
rem = temp%10;
temp = temp/10;
if(i == num)
{
if(counter == 0)
counter++;
if(counter == 0)
}
Q9. Write a program to find perfect number or not.
A perfect number is a positive integer that is equal to the sum of its proper
positive divisors, that is, the sum of its positive divisors excluding the number
itself. Equivalently, a perfect number is a number that is half the sum of all of
its positive divisors. The first perfect number is 6, because 1, 2 and 3 are its
proper positive divisors, and 1 + 2 + 3 = 6. Equivalently, the number 6 is equal
to half the sum of all its positive divisors:
(1 + 2 + 3 + 6 ) / 2 = 6.
Algorithm
Step 1: Start
Step 2:
class IsPerfectNumber
int temp = 0;
for(int i=1;i<=number/2;i++)
if(number%i == 0)
{
temp += i;
if(temp == number)
return true;
else
return false;
}
Q10. Caesar Cipher is an encryption technique which is implemented as
ROT13(‘rotate by 13 places’). It is a simple letter substitution cipher that
replaces a letter with the letter 13 places after it in the alphabets, with the
other characters remaining unchanged.
Test your program with the sample data and some random data:
Uryyb? Ubjnerlbh?
void rot13(String w)
charch;
int a = 0;
{ch = w.charAt(i);
a = ch + 13;
if((Character.isUpperCase(ch) && a>90) || (Character.isLowerCase(ch) &&
a>122))
a = a - 26;
ch = (char)a;
String s = sc.nextLine();
int L = s.length();
if(L<4 || L>99)
System.out.println("INVALID LENGTH");
}else
ob.rot13(s);
}}}
Q11. Write a program to accept a sentence which may be terminated by
either’.’, ‘?’ or ’!’ only. The words may be separated by more than one blank
space and are in UPPER CASE.
importjava.util.*;
class Ques_4
int l = w.length();
return true;
}else
{
return false;
String s = sc.nextLine();
s = s.toUpperCase();
int l = s.length();
/* Checking whether the sentence ends with '.' or '?' or not */ if(last != '.' &&last !=
'?' &&last != '!') {System.out.println("Invalid Input. End a sentence with either '.',
'?' or '!'only");
}else
int x = str.countTokens();
int c = 0;
a = a + w + " "; // saving all words beginning and ending with a vowel in variable
'a'
else
System.out.println(a+b);
}}
Q12. Write a program to declare a square matrix A[][] of order (M x M)
where ‘M’ must be greater than 3 and less than 10. Allow the user to input
positive integers into this matrix. Perform the following tasks on the matrix:
importjava.util.*;
class Ques_5
int A[][],B[],m,n;
m=sc.nextInt();
if(m<4 || m>10){
System.out.println("Invalid Range");
System.exit(0);
else{
A = new int[m][m];
n = (m-2)*(m-2);
for(int j=0;j<m;j++)
A[i][j]=sc.nextInt();
void convert(int s)
int x=0;
for(inti=0;i<m;i++)
for(int j=0;j<m;j++)
{if(s==1)
B[x] = A[i][j];
else
A[i][j] = B[x];
x++;
}}}}
voidsortArray() //Function for sorting Non-Boundary elements stored in array B[]
{int c = 0;
{if(B[i]>B[j])
c = B[i];
B[i] = B[j];
B[j] = c;
}}}}
{for(inti=0;i<m;i++)
{for(int j=0;j<m;j++)
System.out.print(A[i][j]+"\t");
}System.out.println();
}}
voidprintDiagonal() //Function for printing the diagonal elements and their sum
{int sum = 0;
for(inti=0;i<m;i++)
{for(int j=0;j<m;j++)
{if(i==j || (i+j)==m-1)
{System.out.print(A[i][j]+"\t");
sum = sum + A[i][j];
}else
System.out.print("\t");
}System.out.println();
ob.input();
System.out.println("*********************");
System.out.println("*********************");
System.out.println("*********************");
System.out.println("*********************");
System.out.println("*********************");
System.out.println("*********************");
ob.printDiagonal(); //Printing the diagonal elements and their sum
}}
Q13. A Circular Prime is a prime number that remains prime under cyclic
shifts of its digits. When the leftmost digit is removed and replaced at the end
of the remaining string of digits, the generated number is still prime. The
process is repeated until the original number is reached again.
OUTPUT:
197
971
719
booleanisPrime(int n)
int c = 0;
c++;
if(c == 2)
return true;
else
return false;
}int circulate(int n) //Function for circulating the digits to form new number {
String s = Integer.toString(n);
String p = s.substring(1)+s.charAt(0);
int a = Integer.parseInt(p);
return a;
{int f = 0,a = n;
do
System.out.println(a);
if(isPrime(a)==false)
{f = 1;
break;
}a = circulate(a);
}while(a!=n);
if(f==1)
}public static void main(String args[]) //main fn. for calling the methods {
int n = sc.nextInt();
ob.isCircularPrime(n);
}}
Q14. Write a Program in Java to input a number and check whether it is a
Bouncy Number or not.
Increasing Number:-
Decreasing Number:-
Bouncy Number:-
importjava.util.*;
String s = Integer.toString(n);
charch;
int f = 0;
ch = s.charAt(i);
if(ch>s.charAt(i+1))// If any digit is more than next digit then we have to stop
checking
{f = 1;
break;
}if(f==1)
return false;
else
return true;
String s = Integer.toString(n);
charch;
int f = 0;
ch = s.charAt(i);
if(ch<s.charAt(i+1))// If any digit is less than next digit then we have to stop
checking
f = 1;
break;
}if(f==1)
return false;
else
return true;
}voidisBouncy(int n)
if(isIncreasing(n)==true)
else if(isDecreasing(n)==true)
else
int n = sc.nextInt();
ob.isBouncy(n);
}}
Q15. Write a Program in Java to input a number and check whether it is an
Evil Number or not.
Example:
Design a program to accept a positive whole number and find the binary
equivalent of the number and count the number of 1’s in it and display
whether it is an Evil number or not with an appropriate message.
importjava.util.*;
classEvilNumber
char dig[]={'0','1'}; //array storing the digits (as characters) in a binary number
system
while(n>0)
s=dig[r]+s; //adding the remainder to the result and reversing at the same time
n=n/2;
}return s;
int c = 0, l = s.length();
charch;
{ch=s.charAt(i);
if(ch=='1')
{c++;
}return c;
int n = sc.nextInt();
int x = ob.countOne(bin);
if(x%2==0)
else
}}
Q16. Write a Program in Java to input a number and check whether it is a
Pronic Number or Heteromecic Number or not.
Pronic Number:
0, 2, 6, 12, 20, 30, 42, 56, 72, 90, 110, 132, 156, 182, 210, 240, 272, 306,
int flag = 0;
flag = 1;
break;
}}
}}
Q17. Write a Program in Java to input a number and check whether it is a
NIVENNumber or Niven Number or not.
NIVEN Number:
The number 18 is a NIVEN number in base 10, because the sum of the
digits 1 and 8 is 9 (1 + 8 = 9), and 18 is divisible by 9 (since 18 % 9=0)
The number 1729 is a NIVEN number in base 10, because the sum of
the digits 1 ,7, 2 and 9 is 19 (1 + 7 + 2 + 9 = 19), and 1729 is divisible by
19 (1729 = 19 * 91)
The number 19 is not a NIVEN number in base 10, because the sum of
the digits 1 and 9 is 10 (1 + 9 = 10), and 19 is not divisible by 10 (since 19
% 10 = 9)
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 18, 20, 21, 24, 27, 30, 36, 40, 42, 45, 48, 50,54, 60,
63, 70, 72, 80, 81, 84, 90, 100, 102, 108, 110, 111, 112, 114, 117,120, 126, 132,
133, 135, 140, 144, 150, 152, 153, 156, 162, 171, 180, 190,192, 195, 198, 200 etc.
sum = sum + d;
c = c/10;
else
}}
Q18. Write a Program in Java to input a word and print its Anagrams Note:
Anagrams are words made up of all the characters present in the original
word by re-arranging the characters.
Example:
Anagrams of the word TOP are: TOP, TPO, OPT, OTP, PTO and POT
void input()throws Exception //function for inputting all values {Scanner sc = new
Scanner(System.in);
display("",s);
c++;
System.out.println(s1+s2);
}else
{
String x = s2.substring(i, i+1);
String z = s2.substring(i+1);
display(s1+x, y+z);
}}}
public static void main(String args[])throws Exception //for calling all methods.
ob.input();
}}
Q19. Write a Program in Java to input a number in Decimal number system
and convert it into its equivalent number in the Binary number system.
Note:-
int n=Integer.parseInt(br.readLine());
int r;
char dig[]={'0','1'}; //array storing the digits (as characters) in a binary number
system
while(n>0)
s=dig[r]+s; //adding the remainder to the result and reversing at the sametime
n=n/2;
}
System.out.println("Output = "+s);
}}
Q20. WAP to check whether a number is a valid ISBN or not.
The first nine digits represent the Group, Publisher and Title of the book and
the last digit is used to check whether ISBN is correct or not.
Sum = 1*10 + 2*9 + 5*8 + 9*7 + 0*6 + 6*5 + 0*4 + 9*3 + 7*2 + 7*1 = 209
Now divide it with 11 = 20%/11 = 0. If the resultant value will be Zero then it
is a valid ISBN.
intlen=s.length();
System.out.println("Invalid Input");
else{char ch;
{
ch=s.charAt(i);
dig=10;
else
dig=ch-48;
sum=sum+dig*k;
k--;}
System.out.println("Valid Code");
else
System.out.println("Invalid Code");