Sei sulla pagina 1di 96

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

Start

read number

set sum=0 and duplicate=number

reminder=number%10

sum=sum+(reminder*reminder*reminder)

number=number/10

repeat steps 4 to 6 until number > 0

if sum = duplicate

display number is armstrong

else

display number is not armstrong

Stop

PROGRAM

Import java.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 : ");

1
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") } } }

2
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.

3
QUESTION NO. 2
Write a java program to check whether a given number is part of Fibonacci series or not.

ALGORITHM

Start

Declare variables i, a,b , show

Initialize the variables, a=0, b=1, and show =0

Enter the number of terms of Fibonacci series to be printed

Print First two terms of series

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

End

PROGRAM

importjava.util.*;

class Fibonacci

public static void main(String args[])

Scanner sc=new Scanner(System.in);

System.out.print("Enter a number : ");

4
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.");

} }

5
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.

6
QUESTION NO. 3
Write a java program to print the following pattern.

@#

@#@

@#@#

@#@#@

ALGORITHM

Start

Create class

Write main function

Perform loop

Print

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("#");

7
else

System.out.print("@");

System.out.println("");

8
OUTPUT:
@

@#

@#@

@#@#

@#@#@

VARIABLE DESCRIPTION TABLE:

VARIABLE VARIABLE PURPOSE


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

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)

for step <- 0 to size-1

for i <- size-step-1

if array[i] > array[i+1]

swap array[i] and array[i+1]

end if

end for

end for

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: ");

10
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]);

} }

11
OUTPUT:
Enter the number of integers to sort:

Enter 5 integers:

Sorted list of integers:

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.

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

Take an array A and define its values

Declare largest as integer

Set 'largest' to 0

Loop for each value of A

If A[n] > largest, Assign A[n] to largest

After loop finishes, Display largest as largest element of array

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++)

13
{

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

}}

14
OUTPUT:
Enter Array Elements :

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.

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

Linear Search ( Array A, Value x)

Set i to 1

if i > n then go to step 7

if A[i] = x then go to step 6

Set i to i + 1

Go to Step 2

Print Element x Found at index i and go to step 8

Print element not found

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++)

16
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");

17
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.

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

Check condition for palindrome

Compare with original

Print

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

19
{

n=n+s.charAt(i);

if(n.equals(s))

System.out.println("PALINDROME STRING");

else

System.out.println("NOT A PALINDROME STRING");

20
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.

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

Apply for loop

Extract character

Print

Stop

PROGRAM

Import java.util.*;

class Initials

public static void main(String args[])

Scanner sc=new Scanner(System.in);

String s;

char x;

int l,i;

System.out.print("Enter any sentence: ");

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.

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.

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()

Find index of first vowel.

Create pig latin by appending following three.

Substring after starting with the first vowel till end.

Substring before first vowel.

Add "ay".

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

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

if(ch=='A' || ch=='E' || ch=='I' || ch=='O' || ch=='U')

pos=i;

break;

if(pos!=-1)

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

25
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.

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

Check condition for removal of repeatef characters

Print

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 + " ";

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

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.

28
Question No. 11
Write a Java Program to Sort Strings in an Alphabetical Order

ALGORITHM

Start

Create class

Write main()

Take input

Perform bubble sort technique

Compare the characters to sort

Print

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:");

29
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:");

for (int i = 0; i <= count - 1; i++)

System.out.print(str[i] + ", ");

30
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.

31
Question No. 12
Write a Java Program to Count Vowels and Consonants in a String

ALGORITHM

Start

Create class

Write main()

Take input

Extract character from the string

Count each vowel and consonants

Print

Stop

PROGRAM

class JavaExample

public static void main(String[] args) {

String str = "BeginnersBook";

int vcount = 0, ccount = 0;

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

System.out.println("Number of Vowels: " + vcount);

System.out.println("Number of Consonants: " + ccount);

32
}

Output:

Number of Vowels: 5

Number of Consonants: 8

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.

33
Question No. 13

Write a java program to find factorial of a given number using recursion.

ALGORITHM

Start

Fact(n)

Begin

if n == 0 or 1 then

Return 1;

else

Return n*Call Fact(n-1);

endif

End

PROGRAM

import java.util.Scanner;

class FactorialDemo

public static void main(String args[])

Scanner scanner = new Scanner(System.in);

System.out.println("Enter the number:");

int num = scanner.nextInt();

int factorial = fact(num);

34
System.out.println("Factorial of entered number is: "+factorial);

static int fact(int n)

int output;

if(n==1){

return 1;

output = fact(n-1)* n;

return output;

} }

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

35
Question No. 14

Write a Java Program to Reverse a String using Recursion.

ALGORITHM

Start

Create class

Write main()

Initialise str with a string

Reverse the string str

Print

End

PROGRAM

public class JavaExample

public static void main(String[] args) {

String str = "Welcome to Beginnersbook";

String reversed = reverseString(str);

System.out.println("The reversed string is: " + reversed);

public static String reverseString(String str)

if (str.isEmpty())

return str;

36
return reverseString(str.substring(1)) + str.charAt(0);

Output:

The reversed string is: koobsrennigeB ot emocleW

VARIABLE DESCRIPTION TABLE:

VARIABLE NAME DATATYPE PURPOSE

str String To store normal string.

reversed String To store reversed string.

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;

sr = (temp + (number / temp)) / 2;

while ((temp - sr) != 0);

return sr;

End

PROGRAM

import java.util.Scanner;

class JavaExample

public static double squareRoot(int number)

double temp;

double sr = number / 2;

do

temp = sr;

38
sr = (temp + (number / temp)) / 2;

} while ((temp - sr) != 0);

return sr;

public static void main(String[] args)

System.out.print("Enter any number:");

Scanner scanner = new Scanner(System.in);

int num = scanner.nextInt();

scanner.close();

System.out.println("Square root of "+ num+ " is: "+squareRoot(num));

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

39
.Question No. 16
Write a Java Program to find largest of three numbers using Ternary Operator

ALGORITHM

Start

Create class

Write main()

Take three numbers input

Compare with ternary operator

Print largest number

End

PROGRAM

import java.util.*;

public class JavaExample

public static void main(String[] args)

int num1, num2, num3, result, temp;

Scanner scanner = new Scanner(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:");

40
num3 = scanner.nextInt();

scanner.close();

temp = num1>num2 ? num1:num2;

result = num3>temp ? num3:temp;

System.out.println("Largest Number is:"+result);

41
Output:
Enter First Number:

89

Enter Second Number:

109

Enter Third Number:

Largest Number is: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

42
Question No. 17
Write a java program to Reverse a number using while Loop

ALGORITHM

Start

Create class

Write main()

Take input

Use while loop

Apply condition to reverse the number

Print

End

PROGRAM

import java.util.*;

class ReverseNumberWhile

public static void main(String args[])

int num=0;

int reversenum =0;

System.out.println("Input your number and press enter: ");

Scanner in = new Scanner(System.in);

num = in.nextInt();

while( num != 0 )

43
reversenum = reversenum * 10;

reversenum = reversenum + num%10;

num = num/10;

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

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

44
Question No. 18

Write a Java program to sum the elements of an array

ALGORITHM

Start

Create class

Write main()

Take input

Apply loop

Calculate sum of elements

Print the sum

End

PROGRAM

import java.util.Scanner;

class SumDemo

public static void main(String args[])

Scanner scanner = new Scanner(System.in);

int array[] = new int[10];

int sum = 0;

System.out.println("Enter the elements:");

for (int i=0; i<10; i++)

45
{

array[i] = scanner.nextInt();

for( int num : array) {

sum = sum+num;

System.out.println("Sum of array elements is:"+sum);

Output:
Enter the elements:

10

Sum of array elements is:55

46
VARIABLE DESCRIPTION TABLE:

VARIABLE NAME DATATYPE PURPOSE

i int looping variable

array[] int array of elements

sum int to store sum of array elements

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

Apply condition to check for leap year

Print

End

PROGRAM

import java.util.*;

public class Demo

public static void main(String[] args)

int year;

Scanner scan = new Scanner(System.in);

System.out.println("Enter any Year:");

year = scan.nextInt();

scan.close();

boolean isLeap = false;

if(year % 4 == 0)

48
{

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.");

49
Output:
Enter any Year:

2001

2001 is not a Leap Year.

VARIABLE DESCRIPTION TABLE:

VARIABLE NAME DATATYPE PURPOSE

year int to store year

50
Question No. 20

Write a java program to reverse the array

ALGORITHM

Start

Create class

Write main()

Take input

Apply the condition to reverse the array

Print the reversed array

End

PROGRAM

import java.util.*;

public class Example

public static void main(String args[])

int counter, i=0, j=0, temp;

int number[] = new int[100];

Scanner scanner = new Scanner(System.in);

System.out.print("How many elements you want to enter: ");

counter = scanner.nextInt();

for(i=0; i<counter; i++)

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

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

for(i=0; i<counter; i++)

System.out.print(number[i]+ " ");

52
Output:
How many elements you want to enter: 5

Enter Array Element1: 11

Enter Array Element2: 22

Enter Array Element3: 33

Enter Array Element4: 44

Enter Array Element5: 55

Reversed array: 55 44 33 22 11

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

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

Apply condition to print pattern

Print

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++)

54
{

if(c!=n)

c++;

System.out.print(c+" ");

else

break loop1;

System.out.print("\n");

} }

Output:

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

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

Calculate sum of digits

Print

End

PROGRAM

import java.util.*;

class A

int sum(int no)

if(no==0)

return 0;

else

56
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

VARIABLE DESCRIPTION TABLE:

VARIABLE NAME DATATYPE PURPOSE

no int input variable

ans int output variable

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

Condition to check perfect number

Print

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++)

58
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

perfect number

VARIABLE DESCRIPTION TABLE:

VARIABLE NAME DATATYPE PURPOSE

a int looping variable

n int input variable

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()

Initialise variables with a number

Calculate G.C.D of two numbers

Print

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)

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

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

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

Calculate the given series

Print

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+",");

62
}

Output :
1,11,111,1111,11111,111111,1111111,11111111,1,11,111,1111,11111,111111,1111111,11111111,

VARIABLE DESCRIPTION TABLE:

VARIABLE NAME DATATYPE PURPOSE


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

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

Apply the loop

Calculate sum

Print the sum

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

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:

VARIABLE NAME DATATYPE PURPOSE


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

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

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

67
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

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()

Take input in a and n

Apply formula of the series

Print the series

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++)

69
s=s+(double)a/(i+1);

System.out.println("sum of the series="+s);

Output:

enter the value of a and n

sum of the series=3.85

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

70
Question No. 29
Write a program to display the pattern.

1234567

12345

123

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++)

71
System.out.print(j+"");

System.out.println();

Output:
1234567

12345

123

VARIABLE DESCRIPTION TABLE:

VARIABLE NAME DATATYPE PURPOSE

i int looping variable for 1st loop

j int looping variable for 2nd loop

72
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++)

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

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

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

75
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

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

76
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];

77
}

System.out.println("the sum of the elements in the matrix is:"+s);

Output:
the sum of the elements in the matrix is:102

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

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

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("."))

79
break;

System.out.println(st);

Output:
enter line ending with a space & terminated with(.):

i love java.

love

java.

VARIABLE DESCRIPTION TABLE:

VARIABLE NAME DATATYPE PURPOSE

st String to input a string.

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()

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;

81
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

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

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

Apply for loop

Calculate the pattern

Print

End

PROGRAM

import java.util.Scanner;

public class MainClass

83
public static void main(String[] args)

Scanner sc = new Scanner(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 (int i = 1; i <= rows; i++)

for (int j = 1; j <= i; j++)

System.out.print(j+" ");

System.out.println();

for (int i = rows-1; i >= 1; i--)

for (int j = 1; j <= i; j++)

System.out.print(j+" ");

System.out.println();

sc.close();

84
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

85
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]+" ");

86
System.out.println();//new line
}
}
}

Output:

6 6 6
12 12 12
18 18 18

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

87
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

import java.util.Scanner;

public class Convert

public static void main(String[] args)

int n, count = 0, a;

String x = "";

Scanner s = new Scanner(System.in);

System.out.print("Enter any decimal number:");

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

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

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

90
}

++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

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

Extract each character and reverse a word

Reverse the string

Print the reversed string

End

PROGRAM

import java.util.Scanner;

public class ReverseEachWord

static String reverseWord(String inputString)

String strarray[] = inputString.split(" ");

StringBuilder sb = new StringBuilder();

for(String s:strarray)

if(!s.equals(""))

92
StringBuilder strB = new StringBuilder(s);

String rev = strB.reverse().toString();

sb.append(rev+" ");

} }

return sb.toString(); }

public static void main(String[] args)

Scanner sc = new Scanner(System.in);

System.out.println("Input String : ");

String str = sc.nextLine();

System.out.println("Input String : "+str);

System.out.println("String with Reverese Word : "+reverseWord(str));

} }

Output
Input String : Hello Welcome in India

String with Reverese Word : olleH emocleW ni aidnI

VARIABLE DESCRIPTION TABLE:

VARIABLE NAME DATATYPES PURPOSE

rev String to store reversed string

str String to input a string

93
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

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

94
Output:
Enter the string : IncludeHelp

Last index is : 9

VARIABLE DESCRIPTION TABLE:

VARIABLE NAME DATATYPE PURPOSE

S String to input a string

index int to store last index a character

95
BIBLIOGRAPHY

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

96

Potrebbero piacerti anche