0% found this document useful (0 votes)
194 views

Comp Project

The document contains 6 questions related to Java programming. Each question provides an algorithm to solve the problem and includes the Java code. The questions cover topics like checking if a number is Armstrong, checking if a number is part of the Fibonacci series, printing patterns, sorting an array using bubble sort, finding the largest number in an array, and using linear search to find an element in an array.

Uploaded by

Kunwar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
194 views

Comp Project

The document contains 6 questions related to Java programming. Each question provides an algorithm to solve the problem and includes the Java code. The questions cover topics like checking if a number is Armstrong, checking if a number is part of the Fibonacci series, printing patterns, sorting an array using bubble sort, finding the largest number in an array, and using linear search to find an element in an array.

Uploaded by

Kunwar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 79

QUESTION NO.

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

Not an Armstrong Number

Enter any Positive Number : 153

Armstrong Number

VARIABLE DESCRIPTION TABLE:

VARIABLE NAME VARIABLE DATA TYPE PURPOSE

n Integer To take input a number.

nu Integer To store duplicate value of n.

num Integer To store Armstrong number.

rem Integer To find remainder.

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

Output : The number does not belong to Fibonacci Series.

VARIABLE DESCRIPTION TABLE:

VARIABLE NAME VARIABLE DATA TYPE PURPOSE

n Integer To input a number.

a Integer 1. To initialise the value


with ‘0’ .
2. To swap the values.

b Integer 1. To initialise the value


with ‘1’.
2. To swap the values.

c Integer 1. To store the Fibonacci


number.
2. To swap the values.

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:
@

@#

@#@

@#@#

@#@#@

VARIABLE DESCRIPTION TABLE:

VARIABLE VARIABLE PURPOSE


NAME DATA TYPE
i Integer Used in for-loop
condition.
j Integer Used in for-loop
condition.

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

VARIABLE DESCRIPTION TABLE:

VARIABLE NAME VARIABLE DATATYPE PURPOSE

num Integer To store size of an


array.
i Integer Used in for-loop
condition.
j Integer Used in for-loop
condition.
temp Integer Used as a swapping
variable.
array[] Integer integer array of size
num.

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

VARIABLE DESCRIPTION TABLE:

VARIABLE NAME VARIABLE PURPOSE


DATATYPE
large Integer To store the largest value.

i Integer Used in for-loop


condition.
arr[] Integer Integer array of 10
elements.

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

VARIABLE DESCRIPTION TABLE:

VARIABLE NAME VARIABLE DATATYPE PURPOSE


i Integer Used in a for-loop
condition.
n Integer Input a number to be
searched.
flag Integer Used for searching the
number.
arr[] Integer Array of 10 elements.

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

VARIABLE DESCRIPTION TABLE:

VARIABLE NAME VARIABLE DATATYPE PURPOSE


l Integer To store length of the string.
i Integer Used in for-loop condition.
ch Character To store the extracted
character from the string.
s String To store a string input by
user.
n String To store a duplicate string a
same as string s to check for
palindrome.

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.

VARIABLE DESCRIPTION TABLE:

VARIABLE NAME VARIABLE DATATYPE PURPOSE


i Integer Used in for-loop condition.
l Integer To store length of String.
s String To store a String input by user
.
x Character To extract character from the
string.

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

VARIABLE DESCRIPTION TABLE:

VARIABLE NAME VARIABLE DATATYPE PURPOSE


l Integer To store the length of input
string.
i Integer Used in a for-loop condition.
pos Integer To find position of 1st vowel.
ch Character To extract character from the
string.
s String To take input a string.
a String To store 1st extracted string.
b String To store 2nd extracted string.
pig String To store the piglatin word.

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

VARIABLE DESCRIPTION TABLE:

VARIABLE NAME VARIABLE DATATYPE PURPOSE


s String To store the string input by
user.
ans String To store the output string.
ch1 Character To extract 1st character from
the string.
ch2 Character To extract next character from
the string.
i Integer Used in for-loop condition.
l Integer To store the length of the
string.

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

VARIABLE DESCRIPTION TABLE:

VARIABLE NAME VARIABLE DATATYPE PURPOSE

count Integer Number of Strings.


i Integer Used in for-loop
condition.
j Integer Used in for-loop
condition.
temp string Used as a swapping
variable.
str[] Integer integer array of size
count.

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:

VARIABLE NAME VARIABLE DATATYPE PURPOSE

str String To store string.

vcount Int Initialise as 0.

ccount Int Initialise as 0.

ch char To store character of String.

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:

Enter the number:5


Factorial of entered number is:120

VARIABLE DESCRIPTION TABLE:

VARIABLE NAME VARIABLE DATATYPE PURPOSE

num Int Input variable

factorial Int Find factorial

n Int Input variable

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:

VARIABLE NAME DATATYPE PURPOSE

str String To store normal string.

reversed String To store reversed string.

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:

Enter any number:16


Square root of 16 is: 4.0

VARIABLE DESCRIPTION TABLE:

VARIABLE NAME DATATYPE PURPOSE

number int to input number.

temp Double temperory variable.

sr Double to store quotient.

num int input variable

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

VARIABLE DESCRIPTION TABLE:

VARIABLE NAME DATATYPE PURPOSE


num1 int input variable

num2 int input variable

num3 int input variable

temp int to store temperory number

result int to store result

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;
}

System.out.println("Reverse of input number is: "+reversenum);


}
}

33
Output:
Input your number and press enter:
145689
Reverse of input number is:986541

VARIABLE DESCRIPTION TABLE:

VARIABLE NAME DATATYPE PURPOSE

num int input variable

reversenum int to store reversed number

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

VARIABLE DESCRIPTION TABLE:

VARIABLE NAME DATATYPE PURPOSE

i int looping variable

array[] int array of elements

sum int to store sum of array elements

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.

VARIABLE DESCRIPTION TABLE:

VARIABLE NAME DATATYPE PURPOSE

year int to store year

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--;
}

System.out.print("Reversed array: ");


for(i=0;i<counter;i++)
{
System.out.print(number[i]+" ");
}}}

39
Output:
How many elements you want to enter:5
EnterArrayElement1:11
EnterArrayElement2:22
EnterArrayElement3:33
EnterArrayElement4:44
EnterArrayElement5:55
Reversed array:5544332211

VARIABLE DESCRIPTION TABLE:

VARIABLE NAME DATATYPE PURPOSE


i int looping variable
j int looping variable
counter int to store length of array

number[] int array to store elements

temp int swaping variable

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 */

VARIABLE DESCRIPTION TABLE:

VARIABLE NAME DATATYPE PURPOSE

c int counter variable

n int input variable

i int looping variable

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:

VARIABLE NAME DATATYPE PURPOSE

no int input variable

ans int output variable

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:

VARIABLE NAME DATATYPE PURPOSE

a int looping variable

n int input variable

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);
}

public static void main(String[] args)


{
int a = 98, b = 56;
System.out.println("GCD of " + a +" and " + b + " is " + gcd(a, b));
}
}

47
Output:
GCD of 98 and 56 is 14

VARIABLE DESCRIPTION TABLE:

VARIABLE NAME DATATYPE PURPOSE

a int 1st input variable

b int 2nd input variable

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:

VARIABLE NAME DATATYPE PURPOSE


a int looping variable
p int calculating variable
s Double to convert value integer

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

VARIABLE DESCRIPTION TABLE:

VARIABLE NAME DATATYPE PURPOSE


a int looping variable
n int input variable
s int to find sum of all elements

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

VARIABLE DESCRIPTION TABLE:

VARIABLE NAME DATATYPE PURPOSE


a int looping variable
s int to store sum
p int to store factorial

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:

VARIABLE NAME DATATYPE PURPOSE


i int looping variable
a int input variable
n int input variable
s Double to store sum of series

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:

VARIABLE NAME DATATYPE PURPOSE

i int looping variable for 1st loop

j int looping variable for 2nd loop

57
Question No. 30

Write a program to display the following pattern

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:

VARIABLE NAME DATATYPE PURPOSE


i int looping variable
j int looping variable
c int looping and output variable
p int increment variable

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:

VARIABLE NAME DATATYPE PURPOSE


n int to initialise with any number
p int to find square of number
s int to store neon number
d int to store quotient of number

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:

VARIABLE NAME DATATYPE PURPOSE


i int looping variable
j int looping variable
s int to store sum of all elements
m[][] int 2D array to store elements

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.

VARIABLE DESCRIPTION TABLE:

VARIABLE NAME DATATYPE PURPOSE

st String to input a string.

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

number of times tail obtained=11

65
VARIABLE DESCRIPTION TABLE:

VARIABLE NAME DATATYPE PURPOSE


i int looping variable
c int to check condition
h int to store head
t int to store tail
d Double to store random number

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

VARIABLE DESCRIPTION TABLE:

VARIABLE NAME DATATYPE PURPOSE


i int looping variable

j int looping variable

rows int to store number of rows

68
Question No. 36

Write a program to multiply two matrices in a 2D Array.

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:

VARIABLE NAME DATATYPE PURPOSE


a[][] int array to store elements
b[][] int array to store elements
c[][] int array to store multiplied
elements
i int looping variable
j int looping variable
k int looping variable

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:

VARIABLE NAME DATATYPE PURPOSE


n int to input decimal number
count int to count no. of 1’s
a int to check condition
x String to store binary number as a
string

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.

VARIABLE DESCRIPTION TABLE:

VARIABLE NAME DATATYPE PURPOSE

n1 int to store 1st number

n2 int to store 2nd number

lcm int to store lcm of two numbers

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:

VARIABLE NAME DATATYPES PURPOSE

rev String to store reversed string

str String to input a string

75
76
Question No. 40

Write a Program to find last index of any character in string in Java.

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:

VARIABLE NAME DATATYPE PURPOSE

S String to input a string

index int to store last index a character

78
BIBLIOGRAPHY

1. www.google.com
2. www.guideforschol.com
3. Book-“UNDERSTANDING I.S.C COMPUTER SCIENCE” by APC Publications

79

You might also like