Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
1
Write a java program to accept a number from the user and check whether it is an Armstrong
number or not.
ALGORITHM
Start
read number
reminder=number%10
sum=sum+(reminder*reminder*reminder)
number=number/10
if sum = duplicate
else
Stop
PROGRAM
Import java.util.*;
classarmstrong
1
n = sc.nextInt();
nu = n;
while(nu != 0)
rem = nu%10;
nu = nu/10;
if(num == n)
System.out.println("Armstrong Number");
else
2
OUTPUT:
Enter any Positive Number : 121
Armstrong Number
3
QUESTION NO. 2
Write a java program to check whether a given number is part of Fibonacci series or not.
ALGORITHM
Start
-> show=a+b
-> a=b
-> b=show
-> increase value of i each time by 1
-> print the value of show
End
PROGRAM
importjava.util.*;
class Fibonacci
4
int n = sc.nextInt();
if(n<0)
else
while(c<n)
c = a + b;
a = b;
b = c;
if(c==n)
else
} }
5
OUTPUT:
Enter a number : 45
6
QUESTION NO. 3
Write a java program to print the following pattern.
@#
@#@
@#@#
@#@#@
ALGORITHM
Start
Create class
Perform loop
Stop
PROGRAM
class pattern
{inti,j;
for(i=1;i<=5;i++)
{for(j=1;j<=i;j++)
{if(j%2==0)
System.out.print("#");
7
else
System.out.print("@");
System.out.println("");
8
OUTPUT:
@
@#
@#@
@#@#
@#@#@
9
QUESTION NO. 4
Write a program to input an array of integers and sort it using Bubble Sorting.
ALGORITHM
Start
Create class
bubbleSort(array)
end if
end for
end for
end bubbleSort
PROGRAM
importjava.util.*;
classBubbleSort
{intnum, i, j, temp;
num = sc.nextInt();
10
for (i = 0; i <num; i++)
array[i] = sc.nextInt();
{temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
} }
System.out.println(array[i]);
} }
11
OUTPUT:
Enter the number of integers to sort:
Enter 5 integers:
12
QUESTION NO. 5
Write a java program to accept 10 numbers in an integer array and print the largest number
among them.
ALGORITHM
START
Create class
Set 'largest' to 0
STOP
PROGRAM
importjava.util.*;
class max
int large, i;
13
{
arr[i] = sc.nextInt();
large = arr[0];
if(large <arr[i])
large = arr[i];
}}
14
OUTPUT:
Enter Array Elements :
89
32
54
75
69
12
56
28
73
Largest Number = 89
15
QUESTION NO. 6
Write a java program to search an element in an integer array of 10 elements by using linear
search technique.
ALGORITHM
Start
Create class
Set i to 1
Set i to i + 1
Go to Step 2
Exit
PROGRAM
importjava.util.*;
classLinearSearch
intarr[]=new int[10];
int i, n, flag=0;
16
arr[i]=sc.nextInt();
n= sc.nextInt();
for (i=0;i<10;i++)
{ if (arr[i]==n)
{ flag=1;
break;
if (flag==1)
System.out.println("ELEMENT FOUND");
else
17
OUTPUT:
Enter 10 integers
45
85
36
98
15
48
75
36
25
79
ELEMENT FOUND
18
QUESTION NO. 7
Write a java program to check whether a given string is palindrome or not.
ALGORITHM
Start
Create class
Write main()
Take input
End
PROGRAM
importjava.util.*;
class palindrome
intl,i;
charch;
String s,n="";
System.out.println("Enter a String:");
s=sc.nextLine();
l=s.length();
for(i=l-1;i>=0;i--)
19
{
n=n+s.charAt(i);
if(n.equals(s))
System.out.println("PALINDROME STRING");
else
20
OUTPUT:
Enter a String:
MALAYALAM
PALINDROME STRING
21
QUESTION NO. 8
Write a program to accept a sentence and print only the first letter of each word of the sentence
in capital letters separated by a full stop.
ALGORITHM
Start
Create class
Write main()
Declare variables
Take input
Extract character
Stop
PROGRAM
Import java.util.*;
class Initials
String s;
char x;
int l,i;
s=sc.nextLine();
22
s=" "+s;
s=s.toUpperCase();
l=s.length();
System.out.print("Output = ");
For( i=0;i<l;i++)
x=s.charAt(i);
if(x==' ')
System.out.print(s.charAt(i+1)+".");
OUTPUT:
Enter any sentence: my name is shashu.
Output = M.N.I.S.
23
QUESTION NO. 9
Write a program that encodes a word into Piglatin. To translate a word into a Piglatin word,
convert the word into uppercase and then place the first vowel of the original word as the start of
the new word along with the remaining alphabets. The alphabets present before the vowel being
shifted towards the end followed by “AY”.
ALGORITHM
Start
Create class
Write main()
Add "ay".
Stop
PROGRAM
importjava.util.*;
classpiglatin
intl,i,pos=-1;
charch;
String s,a=””,b=””,pig=””;
24
System.out.print("ENTER ANY WORD:");
s= sc.nextLine();
s=s.toUpperCase();
l=s.length();
for(i=0;i<l;i++)
ch=s.charAt(i);
pos=i;
break;
if(pos!=-1)
a=s.substring(pos);
b=s.substring(0,pos);
pig=a+b+"AY";
else
25
OUTPUT:
ENTER ANY WORD:london
26
QUESTION NO. 10
Write a program to input a word from the user and remove the consecutive repeated characters by
replacing the sequence of repeated characters by its single occurrence.
ALGORITHM
Start
Create class
Write main()
Take input
Stop
PROGRAM
importjava.util.*;
class repeat
String s,ans="";
char ch1,ch2;
intl,i;
s = sc.nextLine();
s = s + " ";
27
l=s.length();
ch1=s.charAt(i);
ch2=s.charAt(i+1);
if(ch1!=ch2)
OUTPUT:
Enter any word: jaaaaaaavvvvvvvvvvvvvaaaaaaaaa
28
Question No. 11
Write a Java Program to Sort Strings in an Alphabetical Order
ALGORITHM
Start
Create class
Write main()
Take input
Stop
PROGRAM
import java.util.Scanner;
int count;
String temp;
count = scan.nextInt();
29
for(int i = 0; i < count; i++)
str[i] = scan2.nextLine();
scan.close();
scan2.close();
if (str[i].compareTo(str[j])>0)
temp = str[i];
str[i] = str[j];
str[j] = temp;
30
Output:
Enter number of strings you would like to enter: 5
Rick
Steve
Robin
Lino
Tanya
31
Question No. 12
Write a Java Program to Count Vowels and Consonants in a String
ALGORITHM
Start
Create class
Write main()
Take input
Stop
PROGRAM
class JavaExample
str = str.toLowerCase();
for(int i = 0; i < str.length(); i++) { char ch = str.charAt(i); if(ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o'
|| ch == 'u') { vcount++; } else if((ch >= 'a'&& ch <= 'z')) {
ccount++;
32
}
Output:
Number of Vowels: 5
Number of Consonants: 8
33
Question No. 13
ALGORITHM
Start
Fact(n)
Begin
if n == 0 or 1 then
Return 1;
else
endif
End
PROGRAM
import java.util.Scanner;
class FactorialDemo
34
System.out.println("Factorial of entered number is: "+factorial);
int output;
if(n==1){
return 1;
output = fact(n-1)* n;
return output;
} }
Output:
35
Question No. 14
ALGORITHM
Start
Create class
Write main()
End
PROGRAM
if (str.isEmpty())
return str;
36
return reverseString(str.substring(1)) + str.charAt(0);
Output:
37
Question No. 15
Write a Java Program to Find square root of a Number without sqrt
ALGORITHM
Start
Create class
Write main()
double sr = number / 2;
do
temp = sr;
return sr;
End
PROGRAM
import java.util.Scanner;
class JavaExample
double temp;
double sr = number / 2;
do
temp = sr;
38
sr = (temp + (number / temp)) / 2;
return sr;
scanner.close();
Output:
39
.Question No. 16
Write a Java Program to find largest of three numbers using Ternary Operator
ALGORITHM
Start
Create class
Write main()
End
PROGRAM
import java.util.*;
num1 = scanner.nextInt();
num2 = scanner.nextInt();
40
num3 = scanner.nextInt();
scanner.close();
41
Output:
Enter First Number:
89
109
42
Question No. 17
Write a java program to Reverse a number using while Loop
ALGORITHM
Start
Create class
Write main()
Take input
End
PROGRAM
import java.util.*;
class ReverseNumberWhile
int num=0;
num = in.nextInt();
while( num != 0 )
43
reversenum = reversenum * 10;
num = num/10;
Output:
Input your number and press enter:
145689
44
Question No. 18
ALGORITHM
Start
Create class
Write main()
Take input
Apply loop
End
PROGRAM
import java.util.Scanner;
class SumDemo
int sum = 0;
45
{
array[i] = scanner.nextInt();
sum = sum+num;
Output:
Enter the elements:
10
46
VARIABLE DESCRIPTION TABLE:
47
Question No. 19
Write a java program to check whether the input year is leap or not
ALGORITHM
Start
Create class
Write main()
Input yeat
End
PROGRAM
import java.util.*;
int year;
year = scan.nextInt();
scan.close();
if(year % 4 == 0)
48
{
if ( year % 400 == 0)
isLeap = true;
else
isLeap = false;
else
isLeap = true;
else {
isLeap = false;
if(isLeap==true)
else
49
Output:
Enter any Year:
2001
50
Question No. 20
ALGORITHM
Start
Create class
Write main()
Take input
End
PROGRAM
import java.util.*;
counter = scanner.nextInt();
51
System.out.print("Enter Array Element"+(i+1)+": ");
number[i] = scanner.nextInt();
j = i - 1;
i = 0;
scanner.close();
while(i<j)
temp = number[i];
number[i] = number[j];
number[j] = temp;
i++;
j--;
52
Output:
How many elements you want to enter: 5
Reversed array: 55 44 33 22 11
53
Question No. 21
Write a java program to display Triangle as follow.
23
456
7 8 9 10 ... N */
ALGORITHM
Start
Create class
Write main()
Take input
Apply loop
End
PROGRAM
class Output1
int c=0;
int n = Integer.parseInt(args[0]);
for(int i=1;i<=n;i++)
for(int j=1;j<=i;j++)
54
{
if(c!=n)
c++;
System.out.print(c+" ");
else
break loop1;
System.out.print("\n");
} }
Output:
23
456
7 8 9 10 ... N */
55
Question No. 22
Write a java program to find the sum of the digits of the number.
ALGORITHM
Start
Create class
Write main()
Take input
Apply loop
Extract digits
End
PROGRAM
import java.util.*;
class A
if(no==0)
return 0;
else
56
return(no%10+sum(no/10));
int ans=obj.sum(123);
System.out.println("sum="+ans);
Output:
sum=6
57
Question No. 23
Write a java program to input a number and check whether a number is perfect or not.
ALGORITHM
Start
Create class
Write main()
Take input
End
PROGRAM
import java.util.*;
class perfect
int a,n,s;
s=0;
n=sc.nextInt();
for(a=1;a<n;a++)
58
if(n%a==0)
s=s+a;
if(s==n)
System.out.println("perfect number");
else
Output:
perfect number
s int to compare
59
Question No. 24
Write a java program to calculate G.C.D of a number.
ALGORITHM
Start
Create class
Write main()
End
PROGRAM
class Test
if (a == 0)
return b;
if (b == 0)
return a;
if (a == b)
return a;
if (a > b)
60
return gcd(a-b, b);
Output:
GCD of 98 and 56 is 14
61
Question No. 25
Write a program in java to display first eight numbers of the series
1,11,111,1111,11111,111111,1111111,11111111,1,11,111,1111,11111,111111,1111111,11111111,
ALGORITHM
Start
Create class
Write main()
Apply loop
End
PROGRAM
class series1
int a,p;
double s;s=0;
for(a=0;a<=7;a++)
s=s+Math.pow(10,a);
p=(int)s;
System.out.print(p+",");
62
}
Output :
1,11,111,1111,11111,111111,1111111,11111111,1,11,111,1111,11111,111111,1111111,11111111,
63
Question No. 26
Write a program to enter 10 numbers and print the sum of all numbers.
ALGORITHM
Start
Create class
Write main()
Input number
Calculate sum
End
PROGRAM
import java.util.*;
class sum
int a,n,s;
s=0;
for(a=1;a<10;a++)
System.out.println("enter a number");
n=sc.nextInt();
64
s=s+n;
System.out.println(“Sum=”+s);
Output:
enter a number
enter a number
enter a number
enter a number
enter a number
enter a number
enter a number
enter a number
enter a number
Sum=45
65
VARIABLE DESCRIPTION TABLE:
66
Question No. 27
Write a program in java find out the sum of the given series:
S= 1+(1*2)+(1*2*3)+........................................ 10 terms.
PROGRAM
class series2
int a,s,p;s=0;p=1;
for(a=1;a<10;a++)
p=p*a;
s=s+p;
67
Output:
the sum of series=409113
68
Question No. 28
Write a program to find the sum of the series taking the value of ‘a’ and ‘n’ from the user.
S= a/2+a/3+a/4+.............................+a/n.
ALGORITHM
Start
Create class
Write main()
End
PROGRAM
import java.util.*;
class series3
int i,a,n;
double s=0;
a=sc.nextInt();
n=sc.nextInt();
for(i=1;i<=n;i++)
69
s=s+(double)a/(i+1);
Output:
70
Question No. 29
Write a program to display the pattern.
1234567
12345
123
ALGORITHM
Start
Create class
Write main()
Apply loop
End
PROGRAM
class pattern1
int i,j;
for(i=7;i>=1;i=i-2)
for(j=1;j<=i;j++)
71
System.out.print(j+"");
System.out.println();
Output:
1234567
12345
123
72
Question No. 30
12345
22345
33345
44445
55555
ALGORITHM
Start
Create class
Write main()
Apply loop
End
PROGRAM
class pattern2
int i,j,c,p=2;
for(i=1;i<=5;i++)
73
for(j=1;j<=i;j++)
System.out.print(i);
for(c=p;c<=5;c++)
System.out.print(c);
System.out.println();
p=p+1;
Output:
12345
22345
33345
44445
55555
74
Question No. 31
ALGORITHM
Start
Create class
Write main()
Enter input
Take square
End
PROGRAM
class neon
int n=9;
int p,s=0,d;
p=n*n;
do
d=p%10;
75
s=s+d;
p=p/10;
while(p!=0);
if(s==n)
else
Output:
It is a neon number
76
Question No. 32
ALGORITHM
Start
Create class
Write main()
Take sum
End
PROGRAM
class DDA
int i,j,s;s=0;
int m[][]={{3,4,5,6},{7,8,9,10},{11,12,13,14}};
for(i=0;i<3;i++)
for(j=0;j<4;j++)
s=s+m[i][j];
77
}
Output:
the sum of the elements in the matrix is:102
78
Question No. 33
Write a java program to display all the tokens present in the string by using scanner class.
ALGORITHM
Start
Create class
Write main()
Take input
End
PROGRAM
import java.util.*;
class display
String st;
while(true)
st=sc.next();
if(st.equals("."))
79
break;
System.out.println(st);
Output:
enter line ending with a space & terminated with(.):
i love java.
love
java.
80
Question No. 34
In a toss game, you want to know the number of times of getting ‘HEAD’ and ‘TAIL’. You keep the
record as ‘1’ for getting ‘HEAD’ or ‘0’ for getting ‘TAIL’. Write a program to perform the above task.
ALGORITHM
Start
Create class
Write main()
End
PROGRAM
import java.util.*;
class toss
int i,c,h=0,t=0;
double d;
for(i=1;i<=20;i++)
d=Math.random()*2;
81
c=(int)d;
if(c==1)
h=h+1;
else
t=t+1;
Output:
number of times head obtained=9
82
Question No. 35
Write a program to display the pattern
1
12
123
1234
12345
1234567
123456
12345
1234
123
12
1
ALGORITHM
Start
Create class
Write main()
Take input
End
PROGRAM
import java.util.Scanner;
83
public static void main(String[] args)
System.out.print(j+" ");
System.out.println();
System.out.print(j+" ");
System.out.println();
sc.close();
84
Output:
1
12
123
1234
12345
1234567
123456
12345
1234
123
12
1
85
Question No. 36
ALGORITHM
Start
Create class
Write main()
Apply loop
End
PROGRAM
class MatrixMultiplication
{
public static void main(String args[])
{
int a[][]={{1,1,1},{2,2,2},{3,3,3}};
int b[][]={{1,1,1},{2,2,2},{3,3,3}};
int c[][]=new int[3][3];
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
c[i][j]=0;
for(int k=0;k<3;k++)
{
c[i][j]+=a[i][k]*b[k][j];
}
System.out.print(c[i][j]+" ");
86
System.out.println();//new line
}
}
}
Output:
6 6 6
12 12 12
18 18 18
87
Question No. 37
ALGORITHM
Start
Create class
Write main()
Store it in a string
End
PROGRAM
import java.util.Scanner;
int n, count = 0, a;
String x = "";
n = s.nextInt();
while(n > 0)
88
{
a = n % 2;
if(a == 1)
count++;
x = x + "" + a;
n = n / 2;
System.out.println("Binary number:"+x);
System.out.println("No. of 1s:"+count);
Output:
Enter any decimal number:25
Binary number:10011
No. of 1s:3
89
Question No. 38
ALGORITHM
Start
Create class
Write main()
End
PROGRAM
class LCM
while(true)
break;
90
}
++lcm;
Output:
91
Question No. 39
Write a Java program to input a string from user and reverse each word of given string.
ALGORITHM
Start
Create class
Write main()
Input a string
End
PROGRAM
import java.util.Scanner;
for(String s:strarray)
if(!s.equals(""))
92
StringBuilder strB = new StringBuilder(s);
sb.append(rev+" ");
} }
return sb.toString(); }
} }
Output
Input String : Hello Welcome in India
93
Question No. 40
ALGORITHM
Start
Create class
Write main()
Input a string
End
import java.util.Scanner;
String S;
S=SC.nextLine();
int index = 0;
index = S.lastIndexOf('l');
94
Output:
Enter the string : IncludeHelp
Last index is : 9
95
BIBLIOGRAPHY
1. www.google.com
2. www.guideforschol.com
3. Book-“UNDERSTANDING I.S.C COMPUTER SCIENCE” by APC Publications
96