Comp Project
Comp Project
1
Write a java program to accept a number from the user and check whether it
is an Armstrong number or not.
ALGORITHM
Step 1: Start
Step 2: read number
Step 3: set sum=0 and duplicate=number
Step 4: reminder=number%10
Step 5: sum=sum+(reminder*reminder*reminder)
Step 6: number=number/10
Step 7: repeat steps 4 to 6 until number > 0
Step 8: if sum = duplicate
Step 9: display number is armstrong
Step 10: else
Step 11: display number is not armstrong
Step 12: Stop
PROGRAM
Importjava.util.*;
classarmstrong
{
public static void main(String args[])
{
int n, nu, num=0, rem;
Scanner sc = new Scanner(System.in);
System.out.print("Enter any Positive Number : ");
n = sc.nextInt();
nu = n;
while(nu != 0)
{
rem = nu%10;
num = num + rem*rem*rem;
nu = nu/10;
}
if(num == n)
{
System.out.println("Armstrong Number");
}
else
{
System.out.println("Not an Armstrong Number") } } }
1
OUTPUT:
Enter any Positive Number : 121
Armstrong Number
2
QUESTION NO. 2
Write a java program to check whether a given number is part of Fibonacci
series or not.
ALGORITHM
Step 1: Start
Step 2: Declare variables i, a,b , show
Step 3: Initialize the variables, a=0, b=1, and show =0
Step 4: Enter the number of terms of Fibonacci series to be printed
Step 5: Print First two terms of series
Step 6: Use loop for the following steps
-> show=a+b
-> a=b
-> b=show
-> increase value of i each time by 1
-> print the value of show
Step 7: End
PROGRAM
importjava.util.*;
class Fibonacci
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.print("Enter a number : ");
int n = sc.nextInt();
if(n<0)
System.out.println(" enter a positive number.");
else
{
int a=0, b=1 ,c=0;
while(c<n)
{
c = a + b;
a = b;
b = c;
}
if(c==n)
System.out.println("Output : The number belongs to Fibonacci Series.");
else
System.out.println("Output : The number does not belong to Fibonacci
Series.");
}
}
}
3
OUTPUT:
Enter a number : 45
4
QUESTION NO. 3
Write a java program to print the following pattern.
@
@#
@#@
@#@#
@#@#@
ALGORITHM
Step 1: Start
Step 2: Create class
Step 3: Write main function
Step 4: Perform loop
Step 5: Print
Step 6: Stop
PROGRAM
class pattern
{
public static void main(String args[])
{inti,j;
for(i=1;i<=5;i++)
{for(j=1;j<=i;j++)
{if(j%2==0)
System.out.print("#");
else
System.out.print("@");
}
System.out.println("");
}
}
}
5
OUTPUT:
@
@#
@#@
@#@#
@#@#@
6
QUESTION NO. 4
Write a program to input an array of integers and sort it using Bubble Sorting.
ALGORITHM
Step 1: Start
Step 2: Create class
Step 3: bubbleSort(array)
Step 4: for step <- 0 to size-1
Step 5: for i<- size-step-1
Step 6: if array[i] > array[i+1]
Step 7: swap array[i] and array[i+1]
Step 8: end if
Step 9: end for
Step 10: end for
Step 11: end bubbleSort
PROGRAM
importjava.util.*;
classBubbleSort
{
public static void main(String []args)
{intnum, i, j, temp;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of integers to sort:");
num = sc.nextInt();
int array[] = new int[num];
System.out.println("Enter " + num + " integers: ");
for (i = 0; i<num; i++)
array[i] = sc.nextInt();
for (i = 0; i<( num - 1 ); i++) {
for (j = 0; j <num - i - 1; j++) {
if (array[j] > array[j+1])
{temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
}
} }
System.out.println("Sorted list of integers:");
for (i = 0; i<num; i++)
System.out.println(array[i]);
} }
7
OUTPUT:
Enter the number of integers to sort:
5
Enter 5 integers:
5
8
9
3
1
Sorted list of integers:
1
3
5
8
9
8
QUESTION NO. 5
Write a java program to accept 10 numbers in an integer array and print the
largest number among them.
ALGORITHM
Step 1: START
Step 2: Create class
Step 3: Take an array A and define its values
Step 4: Declare largest as integer
Step 5: Set 'largest' to 0
Step 6: Loop for each value of A
Step 7: If A[n] > largest, Assign A[n] to largest
Step 8: After loop finishes, Display largest as largest element of array
Step 9: STOP
PROGRAM
importjava.util.*;
class max
{
public static void main(String args[])
{
int large,i;
intarr[] = new int[10];
Scanner sc = new Scanner(System.in);
System.out.println("Enter Array Elements : ");
for(i=0; i<10; i++)
{
arr[i] = sc.nextInt();
}
System.out.println("Searching for the Largest Number:");
large = arr[0];
for(i=0; i<10; i++)
{
if(large <arr[i])
{
large = arr[i];
}
}
System.out.print("Largest Number = " +large);
}
}
9
OUTPUT:
Enter Array Elements :
4
89
32
54
75
69
12
56
28
73
Searching for the Largest Number:
Largest Number = 89
10
QUESTION NO. 6
Write a java program to search an element in an integer array of 10 elements
by using linear search technique.
ALGORITHM
Step 1: Start
Step 2: Create class
Step 3: Linear Search ( Array A, Value x)
Step 4: Set i to 1
Step 5: if i> n then go to step 7
Step 6: if A[i] = x then go to step 6
Step 7:S eti to i + 1
Step 8: Go to Step 2
Step 9: Print Element x Found at index i and go to step 8
Step 10: Print element not found
Step 11: Exit
PROGRAM
importjava.util.*;
classLinearSearch
{
public static void main(String args[])
{
Scannersc= new Scanner(System.in);
intarr[]=new int[10];
int i, n, flag=0;
System.out.println("Enter " + 10 + " integers");
for (i= 0;i<10;i++)
arr[i]=sc.nextInt();
System.out.print("Enter value to find");
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
System.out.println("ELEMENT NOT FOUND");
}}
11
OUTPUT:
Enter 10 integers
45
85
36
98
15
48
75
36
25
79
Enter value to find :36
ELEMENT FOUND
12
QUESTION NO. 7
Write a java program to check whether a given string is palindrome or not.
ALGORITHM
Step 1: Start
Step 2: Create class
Step 3: Write main()
Step 4: Take input
Step 5: Check condition for palindrome
Step 6: Compare with original
Step 7: Print
Step 8: End
PROGRAM
Importjava.util.*;
class palindrome
{
public static void main(String args[])
{
intl,i;
charch;
String s,n="";
Scanner sc=new Scanner(System.in);
System.out.println("Enter a String:");
s=sc.nextLine();
l=s.length();
for(i=l-1;i>=0;i--)
{
n=n+s.charAt(i);
}
if(n.equals(s))
System.out.println("PALINDROME STRING");
else
System.out.println("NOT A PALINDROME STRING");
}
}
13
OUTPUT:
Enter a String:
MALAYALAM
PALINDROME STRING
14
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
Step 1: Start
Step 2: Create class
Step 3: Write main()
Step 4: Declare variables
Step 5: Take input
Step 6: Apply for loop
Step 7: Extract character
Step 8: Print
Step 9: Stop
PROGRAM
importjava.util.*;
class Initials
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
String s;
char x;
intl,i;
System.out.print("Enter any sentence: ");
s=sc.nextLine();
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)+".");
}
}
}
15
OUTPUT:
Enter any sentence: my name is shashu.
Output = M.N.I.S.
16
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
Step 1: Start
Step 2: Create class
Step 3: Write main()
Step 4: Find index of first vowel.
Step 5: Create pig latin by appending following three.
Step 6: Substring after starting with the first vowel till end.
Step 7: Substring before first vowel.
Step 8: Add "ay".
Step 9: Stop
PROGRAM
importjava.util.*;
classpiglatin
{
public static void main(String args[])
{
intl,i,pos=-1;
charch;
String s,a=””,b=””,pig=””;
Scanner sc= new Scanner(System.in);
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);
if(ch=='A' || ch=='E' || ch=='I' || ch=='O' || ch=='U')
{
pos=i;
break;
}
}
if(pos!=-1)
17
{
a=s.substring(pos);
b=s.substring(0,pos);
pig=a+b+"AY";
System.out.println("The Piglatin of the word = "+pig);
}
else
System.out.println("No vowel, hence piglatin not possible");
}
}
OUTPUT:
ENTER ANY WORD:london
The Piglatin of the word = ONDONLAY
18
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
Step 1: Start
Step 2: Create class
Step 3: Write main()
Step 4: Take input
Step 5: Check condition for removal of repeatef characters
Step 6: Print
Step 7: Stop
PROGRAM
importjava.util.*;
class repeat
{
public static void main(String args[])
{
String s,ans="";
char ch1,ch2;
intl,i;
Scanner sc=new Scanner(System.in);
System.out.print("Enter any word: ");
s = sc.nextLine();
s = s + " ";
l=s.length();
for(i=0; i<l-1; i++)
{
ch1=s.charAt(i);
ch2=s.charAt(i+1);
if(ch1!=ch2)
{
ans = ans + ch1;
}
}
System.out.println("Word after removing repeated characters = "+ans);
}
19
OUTPUT:
Enter any word: jaaaaaaavvvvvvvvvvvvvaaaaaaaaa
Word after removing repeated characters = java
Enter any word: sssssssshhhhhhhaaaaassssshhhhhhuuuuuuuuu
Word after removing repeated characters = shashu
20
Question No. 11
Write a Java Program to Sort Strings in an Alphabetical Order
ALGORITHM
Step 1: Start
Step 2: Create class
Step 3: Write main()
Step 4: Take input
Step 5: Perform bubble sort technique
Step 6: Compare the characters to sort
Step 7: Print
Step 8: Stop
PROGRAM
import java.util.Scanner;
public class JavaExample
{
public static void main(String[] args)
{
int count;
String temp;
Scanner scan = new Scanner(System.in);
System.out.print("Enter number of strings you would like to enter:");
count = scan.nextInt();
String str[] = new String[count];
Scanner scan2 = new Scanner(System.in);
System.out.println("Enter the Strings one by one:");
for(int i = 0; i< count; i++)
{
str[i] = scan2.nextLine();
}
scan.close();
scan2.close();
for (int i = 0; i< count; i++)
{
for (int j = i + 1; j < count; j++) {
if (str[i].compareTo(str[j])>0)
{
temp = str[i];
str[i] = str[j];
str[j] = temp;
}
}
}
System.out.print("Strings in Sorted Order:");
21
for (int i = 0; i<= count - 1; i++)
{
System.out.print(str[i] + ", ");
}
}
}
Output:
Enter number of strings you would like to enter: 5
Enter the strings one by one :
Rick
Steve
Robin
Lino
Tanya
Strings in Sorted Order: Lino, Rick, Robin, Steve, Tanya
22
Question No. 12
Write a Java Program to Count Vowels and Consonants in a String
ALGORITHM
Step 1: Start
Step 2: Create class
Step 3: Write main()
Step 4: Take input
Step 5: Extract character from the string
Step 6: Count each vowel and consonants
Step 7: Print
Step 8: Stop
PROGRAM
classJavaExample
{
publicstaticvoidmain(String[]args){
String str ="BeginnersBook";
intvcount=0,ccount=0;
str =str.toLowerCase();
for(inti=0;i<str.length();i++){charch=str.charAt(i);if(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u'){vco
unt++;}elseif((ch>='a'&&ch<='z'))
{
ccount++;
}
}
System.out.println("Number of Vowels: "+vcount);
System.out.println("Number of Consonants: "+ccount);
}
}
Output:
Number of Vowels: 5
Number of Consonants: 8
23
VARIABLE DESCRIPTION TABLE:
24
Question No. 13
Write a java program to find factorial of a given number using recursion.
ALGORITHM
Step 1: Start
Step 2: Fact(n)
Step 3: Begin
Step 4: if n == 0 or 1 then
Step 5: Return 1;
Step 6: else
Step 7: Return n*Call Fact(n-1);
Step 8: endif
Step 9: End
PROGRAM
importjava.util.Scanner;
classFactorialDemo
{
publicstaticvoidmain(Stringargs[])
{
Scannerscanner=newScanner(System.in);
System.out.println("Enter the number:");
intnum=scanner.nextInt();
int factorial = fact(num);
System.out.println("Factorial of entered number is: "+factorial);
}
staticintfact(int n)
{
int output;
if(n==1){
return1;
}
output = fact(n-1)* n;
return output;
}}
25
Output:
26
Question No. 14
Write a Java Program to Reverse a String using Recursion.
ALGORITHM
Step 1: Start
Step 2: Create class
Step 3: Write main()
Step 4: Initialise str with a string
Step 5: Reverse the string str
Step 6: Print
Step 7: End
PROGRAM
publicclassJavaExample
{
publicstaticvoidmain(String[]args){
String str ="Welcome to Beginnersbook";
String reversed =reverseString(str);
System.out.println("The reversed string is: "+ reversed);
}
publicstaticStringreverseString(String str)
{
if(str.isEmpty())
return str;
returnreverseString(str.substring(1))+str.charAt(0);
}
}
Output:
The reversed stringis:koobsrennigeBotemocleW
27
VARIABLE DESCRIPTION TABLE:
28
Question No. 15
Write a Java Program to Find square root of a Number without sqrt
ALGORITHM
Step 1: Start
Step 2: Create class
Step 3: Write main()
Step 4: doublesr= number /2;
Step 5: do
Step 6: temp =sr;
Step 7: sr=(temp +(number / temp))/2;
Step 8: while((temp -sr)!=0);
Step 9: returnsr;
Step 10: End
PROGRAM
importjava.util.Scanner;
classJavaExample
{
publicstaticdoublesquareRoot(int number)
{
double temp;
doublesr= number /2;
do
{
temp =sr;
sr=(temp +(number / temp))/2;
}while((temp -sr)!=0);
returnsr;
}
publicstaticvoidmain(String[]args)
{
System.out.print("Enter any number:");
Scannerscanner=newScanner(System.in);
intnum=scanner.nextInt();
scanner.close();
System.out.println("Square root of "+num+" is: "+squareRoot(num));
}
}
29
Output:
30
Question No. 16
Write a Java Program to find largest of three numbers using Ternary
Operator
ALGORITHM
Step 1: Start
Step 2: Create class
Step 3: Write main()
Step 4: Take three numbers input
Step 5: Compare with ternary operator
Step 6: Print largest number
Step 7: End
PROGRAM
importjava.util.*;
publicclassJavaExample
{
publicstaticvoidmain(String[]args)
{
int num1, num2, num3, result, temp;
Scannerscanner=newScanner(System.in);
System.out.println("Enter First Number:");
num1 =scanner.nextInt();
System.out.println("Enter Second Number:");
num2 =scanner.nextInt();
System.out.println("Enter Third Number:");
num3 =scanner.nextInt();
scanner.close();
temp = num1>num2 ? num1:num2;
result = num3>temp ? num3:temp;
System.out.println("Largest Number is:"+result);
}
}
31
Output:
EnterFirstNumber:
89
EnterSecondNumber:
109
EnterThirdNumber:
8
LargestNumberis:109
32
Question No. 17
Write a java program to Reverse a number using while Loop
ALGORITHM
Step 1: Start
Step 2: Create class
Step 3: Write main()
Step 4: Take input
Step 5: Use while loop
Step 6: Apply condition to reverse the number
Step 7: Print
Step 8: End
PROGRAM
importjava.util.*;
classReverseNumberWhile
{
publicstaticvoidmain(Stringargs[])
{
intnum=0;
intreversenum=0;
System.out.println("Input your number and press enter: ");
Scannerin=newScanner(System.in);
num=in.nextInt();
while( num!=0)
{
reversenum=reversenum*10;
reversenum=reversenum+ num%10;
num = num/10;
}
33
Output:
Input your number and press enter:
145689
Reverse of input number is:986541
34
Question No. 18
Write a Java program to sum the elements of an array
ALGORITHM
Step 1: Start
Step 2: Create class
Step 3: Write main()
Step 4: Take input
Step 5: Apply loop
Step 6: Calculate sum of elements
Step 7: Print the sum
Step 8: End
PROGRAM
importjava.util.Scanner;
classSumDemo
{
publicstaticvoidmain(Stringargs[])
{
Scannerscanner=newScanner(System.in);
intarray[]=newint[10];
int sum =0;
System.out.println("Enter the elements:");
for(inti=0;i<10;i++)
{
array[i]=scanner.nextInt();
}
for(int num : array){
sum =sum+num;
}
System.out.println("Sum of array elements is:"+sum);
}
}
35
Output:
Enter the elements:
1
2
3
4
5
6
7
8
9
10
Sum of array elements is:55
36
Question No. 19
Write a java program to check whether the input year is leap or not
ALGORITHM
Step 1: Start
Step 2: Create class
Step 3: Write main()
Step 4: Input yeat
Step 5: Apply condition to check for leap year
Step 6: Print
Step 7: End
PROGRAM
importjava.util.*;
publicclassDemo
{
publicstaticvoidmain(String[]args)
{
int year;
Scanner scan =newScanner(System.in);
System.out.println("Enter any Year:");
year =scan.nextInt();
scan.close();
booleanisLeap=false;
if(year %4==0)
{
if( year%100==0)
{
if( year%400==0)
isLeap=true;
else
isLeap=false;
}
else
isLeap=true;
}
else{
isLeap=false;
}
if(isLeap==true)
System.out.println(year +" is a Leap Year.");
else
System.out.println(year +" is not a Leap Year.");
}
}
37
Output:
Enter any Year:
2001
2001isnot a LeapYear.
38
Question No. 20
Write a java program to reverse the array
ALGORITHM
Step 1: Start
Step 2: Create class
Step 3: Write main()
Step 4: Take input
Step 5: Apply the condition to reverse the array
Step 6: Print the reversed array
Step 7: End
PROGRAM
importjava.util.*;
publicclassExample
{
publicstaticvoidmain(Stringargs[])
{
int counter,i=0, j=0, temp;
intnumber[]=newint[100];
Scannerscanner=newScanner(System.in);
System.out.print("How many elements you want to enter: ");
counter =scanner.nextInt();
for(i=0;i<counter;i++)
{
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--;
}
39
Output:
How many elements you want to enter:5
EnterArrayElement1:11
EnterArrayElement2:22
EnterArrayElement3:33
EnterArrayElement4:44
EnterArrayElement5:55
Reversed array:5544332211
40
Question No. 21
Write a java program to display Triangle as follow.
1
23
456
7 8 9 10 ... N */
ALGORITHM
Step 1: Start
Step 2: Create class
Step 3: Write main()
Step 4: Take input
Step 5: Apply loop
Step 6: Apply condition to print pattern
Step 7: Print
Step 8: End
PROGRAM
class Output1
{
public static void main(String args[]){
int c=0;
int n = Integer.parseInt(args[0]);
for(int i=1;i<=n;i++)
{
for(int j=1;j<=i;j++)
{
if(c!=n)
{
c++;
System.out.print(c+" ");
}
else
break loop1;
}
System.out.print("\n");
}
} }
41
Output:
1
23
456
7 8 9 10 ... N */
42
Question No. 22
Write a java program to find the sum of the digits of the number.
ALGORITHM
Step 1: Start
Step 2: Create class
Step 3: Write main()
Step 4: Take input
Step 5: Apply loop
Step 6: Extract digits
Step 7: Calculate sum of digits
Step 8: Print
Step 9: End
PROGRAM
import java.util.*;
class A
{
int sum(int no)
{
if(no==0)
return 0;
else
{
return(no%10+sum(no/10));
}
}
public static void main(String args[])
{
A obj =new A();
int ans=obj.sum(123);
System.out.println("sum="+ans);
}
}
Output:
sum=6
43
VARIABLE DESCRIPTION TABLE:
44
Question No. 23
Write a java program to input a number and check whether a number is
perfect or not.
ALGORITHM
Step 1: Start
Step 2: Create class
Step 3: Write main()
Step 4: Take input
Step 5: Condition to check perfect number
Step 6: Print
Step 7: End
PROGRAM
import java.util.*;
class perfect
{
public static void main()
{
Scanner sc=new Scanner(System.in);
int a,n,s;
s=0;
System.out.println("enter your number");
n=sc.nextInt();
for(a=1;a<n;a++)
{
if(n%a==0)
s=s+a;
}
if(s==n)
System.out.println("perfect number");
else
System.out.println("not a perfect number");
}
}
Output:
enter your number
6
perfect number
45
VARIABLE DESCRIPTION TABLE:
s int to compare
46
Question No. 24
Write a java program to calculate G.C.D of a number.
ALGORITHM
Step 1: Start
Step 2: Create class
Step 3: Write main()
Step 4: Initialise variables with a number
Step 5: Calculate G.C.D of two numbers
Step 6: Print
Step 7: End
PROGRAM
class Test
{
static int gcd(int a, int b)
{
if (a == 0)
return b;
if (b == 0)
return a;
if (a == b)
return a;
if (a > b)
return gcd(a-b, b);
return gcd(a, b-a);
}
47
Output:
GCD of 98 and 56 is 14
48
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
Step 1: Start
Step 2: Create class
Step 3: Write main()
Step 4: Apply loop
Step 5: Calculate the given series
Step 6: Print
Step 7: End
PROGRAM
class series1
{
public static void main(String args[])
{
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+",");
}
}
}
Output :
1,11,111,1111,11111,111111,1111111,11111111,1,11,111,1111,11111,111111,1111111,11111111,
49
VARIABLE DESCRIPTION TABLE:
50
Question No. 26
Write a program to enter 10 numbers and print the sum of all numbers.
ALGORITHM
Step 1:Start
Step 2:Create class
Step 3:Write main ()
Step 4:Input number
Step 5:Apply the loop
Step 6:Calculate sum
Step 7:Print the sum
Step 8:End
PROGRAM
import java.util.*;
class sum
{
public void main(String args[])
{
Scanner sc=new Scanner(System.in);
int a,n,s;
s=0;
for(a=1;a<10;a++)
{
System.out.println("enter a number");
n=sc.nextInt();
s=s+n;
}
System.out.println(“Sum=”+s);
}
}
Output:
enter a number
1
enter a number
2
enter a number
3
enter a number
4
enter a number
51
5
enter a number
6
enter a number
7
enter a number
8
enter a number
9
Sum=45
52
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
{
public static void main(String args[])
{
int a,s,p;s=0;p=1;
for(a=1;a<10;a++)
{
p=p*a;
s=s+p;
}
System.out.println("the sum of series="+s);
}
}
Output:
The sum of series=409113
53
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
Step 1:Start
Step 2:Create class
Step 3:Write main()
Step 4:Take input in a and n
Step 5:Apply formula of the series
Step 6:Print the series
Step 7:End
PROGRAM
import java.util.*;
class series3
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("enter the value of a and n");
int i,a,n;
double s=0;
a=sc.nextInt();
n=sc.nextInt();
for(i=1;i<=n;i++)
s=s+(double)a/(i+1);
System.out.println("sum of the series="+s);
}
}
Output:
enter the value of a and n
3
4
sum of the series=3.85
54
VARIABLE DESCRIPTION TABLE:
55
Question No. 29
Write a program to display the pattern.
1234567
12345
123
1
ALGORITHM
Start
Create class
Write main()
Apply loop
Apply condition for printing the pattern
Print
End
PROGRAM
class pattern1
{
public static void main(String args[])
{
int i,j;
for(i=7;i>=1;i=i-2)
{
for(j=1;j<=i;j++)
System.out.print(j+"");
System.out.println();
}
}
}
Output:
1234567
12345
123
1
56
VARIABLE DESCRIPTION TABLE:
57
Question No. 30
12345
22345
33345
44445
55555
ALGORITHM
Start
Create class
Write main()
Apply loop
Apply condition to print the pattern
Print
End
PROGRAM
class pattern2
{
public static void main(String args[])
{
int i,j,c,p=2;
for(i=1;i<=5;i++)
{
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
58
VARIABLE DESCRIPTION TABLE:
59
Question No. 31
Write a program to check whether a number is neon or not.
ALGORITHM
Start
Create class
Write main()
Enter input
Take square
Apply while loop
Apply the necessary conditions
Print that number is neon or not
End
PROGRAM
class neon
{
public static void main(String args[])
{
int n=9;
int p,s=0,d;
p=n*n;
do
{
d=p%10;
s=s+d;
p=p/10;
}
while(p!=0);
if(s==n)
System.out.println("It is a neon number");
else
System.out.println("It is not a neon number");
}
}
Output:
It is a neon number
60
VARIABLE DESCRIPTION TABLE:
61
Question No. 32
Write a java program to print sum of all elements in 2D Array.
ALGORITHM
Start
Create class
Write main()
Take input in 2d array
Apply for loop
Take sum
Print the sum
Print
End
PROGRAM
class DDA
{
public static void main(String args[])
{
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];
}
}
System.out.println("the sum of the elements in the matrix is:"+s);
}
}
Output:
the sum of the elements in the matrix is:102
62
VARIABLE DESCRIPTION TABLE:
63
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
Apply while loop
Display the token
End
PROGRAM
import java.util.*;
class display
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
String st;
System.out.println(" enter line ending with a space & terminated with(.):");
while(true)
{
st=sc.next();
if(st.equals("."))
break;
System.out.println(st);
}
}
}
Output:
enter line ending with a space & terminated with(.):
i love java.
i
love
java.
64
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()
Use a random “ ” function
Calculate the toss
Print the number of heads and tales
End
PROGRAM
import java.util.*;
class toss
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int i,c,h=0,t=0;
double d;
for(i=1;i<=20;i++)
{
d=Math.random()*2;
c=(int)d;
if(c==1)
h=h+1;
else
t=t+1;
}
System.out.println("number of times head obtained="+h);
System.out.println("number of times tail obtained="+t);
}
}
Output:
number of times head obtained=9
65
VARIABLE DESCRIPTION TABLE:
66
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
Apply for loop
Calculate the pattern
Print
End
PROGRAM
importjava.util.Scanner;
publicclassMainClass
{
publicstaticvoidmain(String[]args)
{
Scanner sc =newScanner(System.in);
System.out.println("How many rows you want in this pattern?");
int rows =sc.nextInt();
System.out.println("Here is your pattern....!!!");
for(inti=1;i<= rows;i++)
{
for(int j =1; j <=i;j++)
{
System.out.print(j+" ");
}
System.out.println();
}
for(inti= rows-1;i>=1;i--)
{
67
for(int j =1; j <=i;j++)
{
System.out.print(j+" ");
}
System.out.println();
}
sc.close();
}
}
Output:
1
12
123
1234
12345
1234567
123456
12345
1234
123
12
1
68
Question No. 36
ALGORITHM
Start
Create class
Write main()
Take input in two matrix
Apply loop
Multiply the given matrix with proper condition
Store the result in new array
Print
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]+" ");
System.out.println();//new line
}
}
}
Output:
6 6 6
12 12 12
18 18 18
69
VARIABLE DESCRIPTION TABLE:
70
Question No. 37
Write a java program to convert decimal number into binary number.
ALGORITHM
Start
Create class
Write main()
Take input of a number
Count number of one’s
Store it in a string
Print
End
PROGRAM
importjava.util.Scanner;
publicclass Convert
{
publicstaticvoidmain(String[]args)
{
int n, count =0, a;
String x ="";
Scanner s = newScanner(System.in);
System.out.print("Enter any decimal number:");
n =s.nextInt();
while(n >0)
{
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
71
VARIABLE DESCRIPTION TABLE:
72
Question No. 38
Write a java program to calculate the L.C.M of two numbers.
ALGORITHM
Start
Create class
Write main()
Take two numbers as input
Apply condition of LCM
Print the LCM of numbers
End
PROGRAM
class LCM
{
public static void main(String[] args)
{
int n1 = 72, n2 = 120, lcm;
lcm = (n1 > n2) ? n1 : n2;
while(true)
{
if( lcm % n1 == 0 && lcm % n2 == 0 )
{
System.out.printf("The LCM of %d and %d is %d.", n1, n2, lcm);
break;
}
++lcm;
}
}
}
Output:
The LCM of 72 and 120 is 360.
73
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
Extract each character and reverse a word
Reverse the string
Print the reversed string
End
PROGRAM
importjava.util.Scanner;
publicclassReverseEachWord
{
staticStringreverseWord(StringinputString)
{
Stringstrarray[] = inputString.split(" ");
StringBuilder sb = newStringBuilder();
for(String s:strarray)
{
if(!s.equals(""))
{
StringBuilder strB = new StringBuilder(s);
String rev = strB.reverse().toString();
sb.append(rev+" ");
}}
returnsb.toString();}
publicstatic void main(String[] args)
{
Scanner sc = newScanner(System.in);
System.out.println("Input String : ");
String str = sc.nextLine();
System.out.println("Input String : "+str);
System.out.println("String with RevereseWord : "+reverseWord(str));
}}
Output
Input String : Hello Welcome in India
String with RevereseWord :olleHemocleWniaidnI
74
VARIABLE DESCRIPTION TABLE:
75
76
Question No. 40
ALGORITHM
Start
Create class
Write main()
Input a string
Use index of function
Calculate the index of a given character
Print the index value
End
PROGRAM
import java.util.Scanner;
public class StringLastValue
{
public static void main(String[] arg)
{
String S;
Scanner SC=new Scanner(System.in);
System.out.print("Enter the string : ");
S=SC.nextLine();
int index = 0;
index = S.lastIndexOf('l');
System.out.println("Last index is : " +index);
}
}
Output:
Enter the string :IncludeHelp
Last index is : 9
77
VARIABLE DESCRIPTION TABLE:
78
BIBLIOGRAPHY
1. www.google.com
2. www.guideforschol.com
3. Book-“UNDERSTANDING I.S.C COMPUTER SCIENCE” by APC Publications
79