0% found this document useful (0 votes)
6 views237 pages

Cvcorp

The document outlines various programming tasks related to Fibonacci series, including generating the series, calculating sums and averages, and working with alternative values. It provides step-by-step logic and Java code examples for each task, such as printing the Fibonacci series, calculating the sum of the first n terms, and finding the average within a specified range. Additionally, it includes tracing of the code execution for better understanding.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views237 pages

Cvcorp

The document outlines various programming tasks related to Fibonacci series, including generating the series, calculating sums and averages, and working with alternative values. It provides step-by-step logic and Java code examples for each task, such as printing the Fibonacci series, calculating the sum of the first n terms, and finding the average within a specified range. Additionally, it includes tracing of the code execution for better understanding.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 237

1) Fibonacci series.

2) Alternative Fibonacci series.


3) Sum of Fibonacci series of first n terms.
4) Average of the Fibonacci series in a Given Range.
5) Factorial of a number.
6) Sum of a factorial of a numbers like 1! +2! + 3! + 4! + 5! +....upto n!.
7) Reverse a number.
8) Palindrome or not.
9) Sum of all palindrome numbers between the Given range & between Given Numbers.
10) Average of all palindrome numbers between the Range & between Given Numbers.
Fibonacci Series:-
Q) Develop a Program to Print Fibonacci series?

• To Print the Fibonacci Series, follow the below logic and steps.
• Input:- one Integer value (n).
• Output:- Print the first n terms of Fibonacci Series.
• Logic:- Fibonacci Series is nothing but the sum of the two numbers before the new term, and
starting two numbers is 0 and 1.

0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987……


Assume that:- a b c
a b c
a b c
a b c and soon…………….
Like this a, b, c values changes from iteration to iteration.

Step:-1 Take the Input from user and store it in variable 'n' it indicates no of terms.
Step:-2 Assign the first two values in Fibonacci series to a and b (a=0, b=1).
Step:-3 Print first value (a).
Step:-4 Add and store the two values in c.
Step:-5 Assign the b value to a.
Step:-6 Assign the c value to b.
Step:-7 Increment the i value by 1.
Step:-8 Repeat the steps from 3 to 7 until i values reaches n(i<=n).

Note:- Here by default i value starts from 1.

import java.util.Scanner;
public class FibonacciSeries {

public static void main(String[] args) {

Scanner obj=new Scanner(System.in);

System.out.println("Enter the no of terms");


int n=obj.nextInt(); //Taking input from the user.
int a=0,b=1; //Initializing and declaring the a, b values as first two
values in Fibonacci Series.

for(int i=1;i<=n;i++) //Iterating a loop from 1 to the given number.


{
System.out.print(a+" "); //Printing a value.
int c=a+b; //According to our Logic adding before two numbers(a, b)
and store in c.
a=b; //reassign the b value to a.
b=c; //reassign the c value to b.
}//end of loop.
}//end of Main Method.

}//end of Class.

Tracing of Code:-
Input:- 15
Output:- 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377
n i output c a b
15 0 1
1 0 0+1=1 1 1
2 1 1+1=2 1 2
3 1 1+2=3 2 3
4 2 2+3=5 3 5
5 3 3+5=8 5 8
6 5 5+8=13 8 13
7 8 8+13=21 13 21
8 13 13+21=34 21 34
9 21 21+34=55 34 55
10 34 34+55=89 55 89
11 55 55+89=144 89 144
12 89 89+144=233 144 233
13 144 144+233=377 233 377
14 233 233+377=610 377 610
15 377 377+610=987 610 987
16àHere Loop breaks

Alternative Fibonacci Series:-


Q) Develop a Program to Print the Alternative Values of Fibonacci Series?

• To Print the Alternative Fibonacci Series, follow the below logic and steps.
• Input:- one Integer value (n).
• Output:- Print the first 'n' terms of Alternative Fibonacci Series.
• Logic:- Alternative Fibonacci Series is nothing but the sum of the two numbers before the new
term, and starting two numbers is 0 and 1 but Printing is alternative values.

à0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987……… (Actual Series)


Assume that:- a b c
a b c
a b c
a b c and soon…………….
Like this a, b, c values changes from iteration to iteration.

à0 1 3 8 21 55 144 377 987…… (Alternative Series)

Step:-1 Take the Input from user and store it in variable 'n' it indicates no of terms.
Step:-2 Assign the first two values in Fibonacci series to a and b (a=0, b=1).
Step:-3 Checking t variable is 'true' or not, if 'true' goto step-4 or else goto-6.
Step:-4 Print first value (a).
Step:-5 Assign 'false' to t variable to skip next Printing value (According to Logic) and goto-7.
Step:-6 Assign 'true' to t variable to don't skip next Printing value (According to Logic).
Step:-7 Add and store the two values in c.
Step:-8 Assign the b value to a.
Step:-9 Assign the c value to b.
Step:-10 Increment the i value by 1.
Step:-11 Repeat the steps from 3 to 10 until i values reaches n(i<=n).
Note:-
• Here by default i value starts from 1.
• Here using boolean variable to skip second value from Printing for every 2 elements.

import java.util.Scanner;
public class AltFibonacciSeries {

public static void main(String[] args) {

Scanner obj=new Scanner(System.in);

System.out.println("Enter the no of terms");


int n=obj.nextInt(); //Taking input from the user.

int a=0,b=1; //Initializing and declaring the a,b values as first two
values in Fibonacci Series.
boolean t=true; //Initializing and declaring a boolean variable as 'true'.
for(int i=1;i<=n;i++) //Iterating a loop from 1 to the given number.
{
if(t) //here Checking t value is 'true' or not.
{
System.out.print(a+" "); //Printing ‘a’ value.
t=false; //Here assigning 'false' to variable t, to skip the
printing in next Iteration.
}//end of If-condition.
else
t=true; //Here assigning 'true' to variable t, to don't skip the
printing in next Iteration.
int c=a+b; //According to our Logic adding before two numbers(a,b)
and storing in c.
a=b; //reassign the b value to a.
b=c; //reassign the c value to b.
}//end of loop.
}//end of Main Method.

}//end of Class.
Tracing of Code:-
Input:- 15
Output:- 0 1 3 8 21 55 144 377
n i output t c a b
15 true 0 1
1 0 false 0+1=1 1 1
2 true 1+1=2 1 2
3 1 false 1+2=3 2 3
4 true 2+3=5 3 5
5 3 false 3+5=8 5 8
6 true 5+8=13 8 13
7 8 false 8+13=21 13 21
8 true 13+21=34 21 34
9 21 false 21+34=55 34 55
10 true 34+55=89 55 89
11 55 false 55+89=144 89 144
12 true 89+144=233 144 233
13 144 false 144+233=377 233 377
14 true 233+377=610 377 610
15 377 false 377+610=987 610 987
16àHere Loop breaks

Sum of Fibonacci Series:-


Q) Develop a Program to Print the sum of Fibonacci Series of first n values?

• To Print the Sum of the Fibonacci Series, follow the below logic and steps.
• Input:- one Integer value(n).
• Output:- Print the Sum of the first 'n' terms of Fibonacci Series.
• Logic:- Sum of the Fibonacci Series is nothing but the sum of the two numbers before the new
term, and starting two numbers is 0 and 1, summation all the new terms.

à0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987……… (Actual Series)


Assume that:- a b c
a b c
a b c
a b c and soon…………….
Like this a, b, c values changes from iteration to iteration.

Step:-1 Take the Input from user and store it in variable 'n' it indicates no of terms.
Step:-2 Assign the first two values in Fibonacci series to a and b (a=0, b=1).
Step:-3 Adding the a value to sum variable.
Step:-4 Add and store the two values in c.
Step:-5 Assign the b value to a.
Step:-6 Assign the c value to b.
Step:-7 Increment the i value by 1.
Step:-8 Repeat the steps from 3 to 7 until i values reaches n(i<=n).
Step:-9 At last Finally Print the sum value.

Note:- Here by default i value starts from 1.

import java.util.Scanner;
public class SumOfFibonacciSeries {

public static void main(String[] args) {

Scanner obj=new Scanner(System.in);

System.out.println("Enter the no of terms");


int n=obj.nextInt(); //Taking input from the user.

int a=0,b=1; //Initializing and declaring the a, b values as first two


values in Fibonacci Series.
int sum=0; //Initializing and declaring the values as 0 to sum
variable (To add all the Fibonacci numbers).
for(int i=1;i<=n;i++) //Iterating a loop from 1 to the given number.
{
sum=sum+a; //Adding a value to sum variable.
int c=a+b; //According to our Logic adding before two numbers(a, b)
and storing in c.
a=b; //reassign the b value to a.
b=c; //reassign the c value to b.
}//end of loop.
System.out.println(sum); //Print the sum of fibonacci series.
}//end of Main Method.

}//end of Class.

Tracing of Code:-
Input:- 15
Output:- 986
n i sum c a b
15 0 0 1
1 0+0=0 0+1=1 1 1
2 0+1=1 1+1=2 1 2
3 1+1=2 1+2=3 2 3
4 2+2=4 2+3=5 3 5
5 4+3=7 3+5=8 5 8
6 7+5=12 5+8=13 8 13
7 12+8=20 8+13=21 13 21
8 20+13=33 13+21=34 21 34
9 33+21=54 21+34=55 34 55
10 54+34=88 34+55=89 55 89
11 88+55=143 55+89=144 89 144
12 143+89=232 89+144=233 144 233
13 232+144=376 144+233=377 233 377
14 376+233=609 233+377=610 377 610
15 609+377=986 377+610=987 610 987
16àHere Loop breaks
• Finally Print the sum value.
Average of Fibonacci Series in between given Range and between numbers:-
Q) Develop a Program to Print the Average of Fibonacci Series in between given range and between
numbers?

Ø Difference between range of the given numbers and between the given number?
• Range--> Including the given limits.
• Between--> Excluding the given limits.

This Program is for between the given Range.


• To Print the Average of the Fibonacci Series in between the given range, follow the below logic
and steps.
• Input:- Two Integer values (min and max).
• Output:- Print the Average of the Fibonacci Series in between the given range.
• logic:-
o Average of the Fibonacci Series is nothing but the sum of the two numbers before the new
term, and starting two numbers is 0 and 1, summation all the new terms.
o Calculate the average and Print.

à0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987…… (Actual Series)


Assume that:- a b c
a b c
a b c
a b c and soon…………….
Like this a, b, c values changes from iteration to iteration.

Step:-1 Take the Inputs from user min and max values.
Step:-2 Assign the first two values in Fibonacci series to a and b (a=0,b=1).
Step:-3 Check that a value is between the given range (min<=a && a<=max) then goto step-4 else goto
step-6.
Step:-4 Adding the ‘a’ value to sum variable.
Step:-5 And Increment the count variable 1 (This count is used to find Average).
Step:-6 Add and store the two values (a and b) in c.
Step:-7 Assign the b value to a.
Step:-8 Assign the c value to b.
Step:-9 Increment the i value by 1.
Step:-10 Repeat the steps from 3 to 9 until a values reaches max(a<=max).
Step:-11 Find the Average.
Step:-12 At last Finally Print the Average value.

Note:-
• Here by default i value starts from 1.
• Same Logic for Between the given range also but excluding user given numbers.
• Taking few variables as data type of float to get decimal as output/store.

import java.util.Scanner;
public class AvgOfFibonacciSeriesInBwOrRange {

public static void main(String[] args) {

Scanner obj=new Scanner(System.in);

System.out.println("Enter the Min And Max values:");


int min=obj.nextInt(); //Taking input from the user and Storing in min Variable.
int max=obj.nextInt(); //Taking input from the user and Storing in max Variable.

int a=0,b=1; //Initializing and declaring the a, b values as first two


values in Fibonacci Series.
float sum=0; //Initializing and declaring the values as 0 to sum
variable (To add all the Fibonacci numbers).
int count=0; //Initializing and declaring the values as 0 to count
variable (To count no of Fibonacci numbers between
the given range).
for(int i=1;a<=max;i++) //Iterate the loop upto the a value less than max value
(a<=max).
{
if(min<=a && a<=max) //Checking Fibonacci series value a is less than or equal
to max and greater than or equal to min.
{
sum=sum+a; //Adding a value to sum variable.
count++; //Increment the count value by 1 (it will use to find
average).
}//end of if-condition.
int c=a+b; //According to our Logic adding before two numbers(a, b)
and storing in c.
a=b; //reassign the b value to a.
b=c; //reassign the c value to b.
}//end of loop.
float average=sum/count; //Finding average value.
System.out.println(average); //Print the average of fibonacci series between the given
range.
}//end of Main Method.

}//end of Class.

Tracing of Code:-
Input:- 0 8
Output:- 2.857143
min max i sum count c a b
0 8 0 0 0 1
1 0+0=0 1 0+1=1 1 1
2 0+1=1 2 1+1=2 1 2
3 1+1=2 3 1+2=3 2 3
4 2+2=4 4 2+3=5 3 5
5 4+3=7 5 3+5=8 5 8
6 7+5=12 6 5+8=13 8 13
7 12+8=20 7 8+13=21 13 21
13àHere Loop breaks
• Calculate Average by using sum and count (Average=sum/count).
• Finally Print the Average value.
Factorial of a Given Number:-
Q) Develop a Program to Print Factorial of a Given Number?

• To Print the Factorial of a Given Number, follow the below logic and steps.
• Input:- one Integer value (n).
• Output:- Print the Factorial of a Given Number.
• Logic:- From 1 to the given number (n) should multiply all the numbers like, 1*2*3*4*5. ...... *n
to find the factorial of a Given Number.

Assume that:- if Input is n=5, then logic is


1*1*2*3*4*5 this operation in programming do it as 1*1=1
1*2=2
2*3=6
6*4=24
24*5=120
Print this value as output(5!=120).

Step:-1 Take the Input from user (n).


Step:-2 Assign the factorial variable as 1.
Step:-3 Multiply the i value with factorial variable and store the value in factorial variable.
Step:-4 Increment the i value by 1.
Step:-5 Repeat the steps from 3 to 4 until i value reaches Given Number(n) (i<=n).
Step:-6 At last Print the Factorial value.

Note:-
• Here by default i value starts from 1.
• Here factorial value is assigned as 1 due to if we multiply with any number value never change or
if assigned as 0 then it becomes 0(this point is for first Iteration only).

import java.util.Scanner;
public class Factorial {

public static void main(String[] args) {


// TODO Auto-generated method stub
Scanner obj=new Scanner(System.in);

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


int n=obj.nextInt(); //Taking input from user (n).

int factorial=1; //Initialize and declare the variable factorial as 0.

for(int i=1;i<=n;i++) //Iterating loop from 1 to the given number.


{
factorial=factorial*i; //Multiplying the factorial and i value.
}//end of loop.
System.out.println(factorial); //Printing the Factorial value of a given number.
}//end of Main Method.

}//end of Class.

Tracing of Code:-
Input:- 5
Output:- 120

n i factorial
5 1
1 1*1=1
2 1*2=2
3 2*3=6
4 6*4=24
5 24*5=120
6àHere Loop breaks

• Finally Print the factorial value.


Sum of the Factorials:-
Q) Develop a Program to Print the Sum of the Factorials upto the Given Number?

• To Print the Factorial of a Given Number, follow the below logic and steps.
• Input:- one Integer value (n).
• Output:- Print the Sum of Factorials upto Given Number.
• logic:- From 1 to the given number (n) have to find the factorials and sum of those individual
factorial values.

Step:-1 Take the Input from user (n).


Step:-2 Declare a Variable as sum and assign value as 0.
Step:-3 Assign the factorial variable as 1.
Step:-4 Multiply the i value with factorial variable and store the value in factorial variable.
Step:-5 Increment the i value by 1.
Step:-6 Repeat the steps from 4 to 5 until i value reaches j value (i<=j).
Step:-7 Add the factorial value to the variable sum.
Step:-8 Increment the j value by 1.
Step:-9 Repeat the steps from 4 to 8 until j value reaches given number (n) (j<=n).
Step:-10 At last Print the sum value.

Note:- Here by default i and j values starts from 1.

import java.util.Scanner;
public class SumOfFactorials {

public static void main(String[] args) {

Scanner obj=new Scanner(System.in);

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


int n=obj.nextInt(); //Taking input from user (n).
int sum=0; //Initialize and declare the variable sum as 0.
for(int j=1;j<=n;j++) //Iterating loop from 1 to the given number.
{
int factorial=1; //Initialize and declare the variable factorial as
0(resetting the factorial value).

for(int i=1;i<=j;i++) //Iterating loop from 1 to the j value.


{
factorial=factorial*i; //Multiplying the factorial and i value.
}//end of i-loop.
sum=sum+factorial; //Adding factorial result to sum.
}//end of j-loop.
System.out.println(sum); //Printing the sum value.
}//end of Main Method.

}//end of Class.

Tracing of Code:-
Input:- 4
Output:- 33
N j i factorial sum
4 1 1
1 1*1=1
2àHere i-Loop breaks
0+1=1
2 1
1 1*1=1
2 1*2=2
3àHere i-Loop breaks
1+2=3
3 1
1 1*1=1
2 1*2=2
3 3*2=6
4àHere i-Loop breaks
3+6=9
4 1
1 1*1=1
2 1*2=2
3 3*2=6
4 6*4=24
5àHere i-Loop breaks
9+24=33
5àHere j-Loop breaks
• Finally Print the sum value.

Reverse of a Given Number:-


Q) Develop a Program to Print Reverse of a Given Number?

• To Print the Reverse of a Given Number, follow the below logic and steps.
• Input:- one Integer value (n).
• Output:- Print the reverse of a Given Number.
• Logic:-
o Store unit digit from given number.
o Add the stored value to rev variable after multiplying by 10.
o Remove the units digit from the given number
o Repeat the above same steps until given number becomes 0.
o Print the rev value.

Step:-1 Take the Input from user (n).


Step:-2 Initialize and declare the variable rev as 0.
Step:-3 Store the units place in the Given Number(n).
Step:-4 Multiply the rev value by 10 and add the above stored value to the rev variable.
Step:-5 Remove the units place from the Given Number(n).
Step:-6 Repeat the steps from 3 to 5 until n value becomes 0 (n>0).
Step:-7 At last Print the rev value.

import java.util.Scanner;
public class Reverse {

public static void main(String[] args) {


Scanner obj=new Scanner(System.in);

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


int n=obj.nextInt(); //Taking input from user (n).

int rev=0; //Initialize and declare the variable rev as 0.


while(n!=0) //Iterating a loop upto t value becomes 0.
{
int r=n%10; //Taking the Last Digit(once place) value from t
variable and Storing in variable r.
rev=rev*10+r; //Multiplying rev variable by 10 and adding r.
n=n/10; //Removing the units places digit from variable t.
}//end of loop.
System.out.println("Reverse of a Given Number is "+rev);
//Printing the reverse number.
}//end of Main Method.

}//end of Class.

Tracing of Code:-
Input:- 543
Output:- 345

n r rev((rev*10)+r)
543 0
543%10=3 (0*10)+3=3
54 54%10=4 (3*10)+4=34
5 5%10=5 (34*10)+5=345
0àHere Loop breaks

• Finally Print the rev value.


Palindrome:-
Q) Develop a Program to Print Given Number is Palindrome or not?

• To Print the Given Number is Palindrome or Not, follow the below logic and steps.
• Input:- one Integer value (n).
• Output:- Print the given number is Palindrome or not.
• Logic:-
o Store unit digit from given number.
o Add the stored value to rev variable after multiplying by 10.
o Remove the units digit from the given number
o Repeat the above same steps until given number becomes 0.
o Check the rev value with given value if both are equal Print "Given Number is Palindrome" or
else Print "Given Number is Not a Palindrome".

Step:-1 Take the Input from user (n).


Step:-2 Initialize and declare the variable rev as 0.
Step:-3 Duplicate the Given value into variable t.
Step:-4 Store the units place in the Given Number(t).
Step:-5 Multiply the rev value by 10 and add the above stored value to the rev variable.
Step:-6 Remove the units place from the Given Number(t).
Step:-7 Repeat the steps from 3 to 5 until t value becomes 0 (t>0).
Step:-8 Now Compare the variable rev and Given Number(n) if equals goto step-9 else goto step-10.
Step:-9 Print "Given Number is Palindrome".
Step:-10 Print "Given Number is Not a Palindrome".

import java.util.Scanner;
public class Palindrome {

public static void main(String[] args) {


Scanner obj=new Scanner(System.in);

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


int n=obj.nextInt(); //Taking input from user (n).

int rev=0,t=n; //Initialize and declare the variable rev as 0,


Duplicating the Given value into variable t.
while(t!=0) //Iterating a loop upto t value becomes 0.
{
int r=t%10; //Taking the Last Digit(once place) value from t variable
and Storing in variable r.
rev=rev*10+r; //Multiplying rev variable by 10 and adding r.
t=t/10; //Removing the units places digit from variable t.
}//end of loop.
if(rev==n) //Comparing the rev value with Given Number to check
Palindrome or Not.
System.out.println("Given Number is Palindrome");
else
System.out.println("Given Number is Not a Palindrome");

}//end of Main Method.

}//end of Class.

Tracing of Code:- Tracing of Code:-


Input:- 543 Input:- 121
Output:- Given Number is Not a Palindrome Output:- Given Number is Palindrome
n t r rev((rev*10)+r) n t r rev((rev*10)+r)
543 543 0 121 121 0
543%10=3 (0*10)+3=3 121%10=1 (0*10)+1=1
54 54%10=4 (3*10)+4=34 12 12%10=2 (1*10)+2=12
5 5%10=5 (34*10)+5=345 1 1%10=1 (12*10)+1=121
0àHere Loop breaks 0àHere Loop breaks
• Finally compare the Given Number(n) and rev • Finally compare the Given Number(n) and rev
value is same or not. value is same or not.
• If same Print "Given Number is Palindrome" • If same Print "Given Number is Palindrome"
else Print "Given Number is Not a else Print "Given Number is Not a
Palindrome". Palindrome"
Sum of all Palindrome:-
Q) Develop a Program to Print the Sum of all palindromes between the given range and between the given
numbers?

Ø Difference between range of the given numbers and between the given number?
• Range--> Including the given limits.
• Between-->Excluding the given limits.

This Program is for between the given Range.


• To Print the sum of all Palindromes between the Given Range and Between the Given Numbers, follow
the below logic and steps.
• Input:- Two Integer values (min and max).
• Output:- Print the sum of all palindromes between the given range.
• logic:-
o Store unit digit from the number.
o Add the stored value to rev variable after multiplying by 10.
o Remove the units digit from the given number
o Repeat the above same steps until number becomes 0.
o Check the rev value with given value if both are equal Print add the number.
o Repeat the same process for the numbers between values of min to max.

Step:-1 Take the Inputs from user (min and max).


Step:-2 Initialize and declare the variable sum as 0.
Step:-3 Initialize and declare the variable rev as 0.
Step:-4 Duplicate the i value into variable t.
Step:-5 Store the units place of t.
Step:-6 Multiply the rev value by 10 and add the above stored value to the rev variable.
Step:-7 Remove the units place from the variable t.
Step:-8 Repeat the steps from 4 to 7 until t value becomes 0 (t>0).
Step:-9 Now Compare the variable rev and variable i.
Step:-10 If equals add the i value to the sum value.
Step:-11 Increment the i value by 1.
Step:-12 Repeat the steps from 3 to 11 until i value reaches max (i<=max).
Note:- i value starts from min value.

import java.util.Scanner;

public class SumOfPalindromeBwOrRange {

public static void main(String[] args) {

Scanner obj=new Scanner(System.in);

System.out.println("Enter the min and max values");


int min=obj.nextInt(); //Taking input from user (min).
int max=obj.nextInt(); //Taking input from user (max).

int sum=0; //Initialize and declare the variable sum as 0.


for(int i=min;i<=max;i++) //Iterating loop from min value to max value in the from
of 'i' variable.
{
int rev=0,t=i; //Initialize and declare the variable rev as 0,
Duplicating the value of i into t(It is nothing but
resetting the value).
while(t!=0) //Iterating a loop upto t value becomes 0.
{
int r=t%10; //Taking the Last Digit(once place) value from t variable
and Storing in variable r.
rev=rev*10+r; //Multiplying rev variable by 10 and adding r.
t=t/10; //Removing the units places digit from variable t.
}//end of while-loop.
if(rev==i) //Comparing the 'rev' value with 'i' value to check
Palindrome or Not.
sum=sum+i; //Adding the 'i'value to the variable 'sum'.
}//end of for-loop.
System.out.println(sum); //Printing sum value.
}//end of Main Method.

}//end of Class.
Tracing of Code:-
Input:- 100 102
Output:- 101
min max i t r rev((rev*10)+r) sum
100 102 0 0
100 100 100%10=0 (0*10)+0=0
10 10%10=0 (0*10)+0=0
1 1%10=1 (0*10)+1=1
0àHere Loop breaks
0
101 0
101 101%10=1 (0*10)+1=1
10 10%10=0 (1*10)+0=10
1 1%10=1 (10*10)+1=101
0àHere Loop breaks
101
102 102 102%10=2 (0*10)+2=2
10 10%10=0 (2*10)+0=20
1 1%10=1 (20*10)+1=201
0àHere Loop breaks
101

• Finally Print the sum value.

Average of Palindrome:-
Q) Develop a Program to Print the Average of all palindromes between the given range or between the
given numbers?

Ø Difference between range of the given numbers and between the given number?
• Range--> Including the given limits.
• Between-->Excluding the given limits.

This Program is for between the given Range.


• To Print the Average of all Palindromes between the Given Range and Between the Given Numbers,
follow the below logic and steps.
• Input:- Two Integer values (min and max).
• Output:- Print the Average of all palindromes between the given range.
• Logic:-
o Store unit digit from the number.
o Add the stored value to rev variable after multiplying by 10.
o Remove the units digit from the number
o Repeat the above same steps until number becomes 0.
o Check the rev value with given value if both are equal Print add the number and Increment the
count value.
o Repeat the same process for the numbers between values of min to max.
o Now Calculate the average and Print.

Step:-1 Take the Inputs from user (min and max).


Step:-2 Initialize and declare the variable sum as 0.
Step:-3 Initialize and declare the variable count as 0.
Step:-4 Initialize and declare the variable rev as 0.
Step:-5 Duplicate the i value into variable t.
Step:-6 Store the units place of t.
Step:-7 Multiply the rev value by 10 and add the above stored value to the rev variable.
Step:-8 Remove the units place from the variable t.
Step:-9 Repeat the steps from 5 to 8 until t value becomes 0 (t>0).
Step:-10 Now Compare the variable rev and variable i.
Step:-11 If equals add the i value to the sum value and Increment the count value by 1.
Step:-12 Increment the i value by 1.
Step:-13 Repeat the steps from 4 to 12 until i value reaches max (i<=max).

Note:-
• i value starts from min value.
• Count variable is used to calculate the average.
• Taking few variables as data type of float to get decimal as output/store.
import java.util.Scanner;

public class AvgOfPalindromeBwOrRange {

public static void main(String[] args) {

Scanner obj=new Scanner(System.in);

System.out.println("Enter the min and max values");


int min=obj.nextInt(); //Taking input from user (min).
int max=obj.nextInt(); //Taking input from user (max).

float sum=0; //Initialize and declare the variable sum as 0.


int count=0; //Initialize and declare the variable count as 0.
for(int i=min;i<=max;i++) //Iterating loop from min value to max value in the from
of 'i' variable.
{
int rev=0,t=i; //Initialize and declare the variable rev as 0,
Duplicating the value of i into t(It is nothing but
resetting the value).
while(t!=0) //Iterating a loop upto t value becomes 0.
{
int r=t%10; //Taking the Last Digit(once place) value from t variable
and Storing in variable r.
rev=rev*10+r; //Multiplying rev variable by 10 and adding r.
t=t/10; //Removing the units places digit from variable t.
}//end of while-loop.
if(rev==i) //Comparing the 'rev' value with 'i' value to check
Palindrome or Not.
{
sum=sum+i; //Adding the 'i' value to the variable 'sum'.
count++; //Incrementing the count value by 1 (It is used to find
average).
}//end of if-condition.
}//end of for-loop.
float average=sum/count; //Calculating the average.
System.out.println(average); //Printing average value.
}//end of Main Method.

}//end of Class.

Tracing of Code:-
Input:- 100 102
Output:- 101
min max i t r rev((rev*10)+r) sum count
100 102 0 0
100 100 100%10=0 (0*10)+0=0
10 10%10=0 (0*10)+0=0
1 1%10=1 (0*10)+1=1
0àHere Loop breaks
0
101 0
101 101%10=1 (0*10)+1=1
10 10%10=0 (1*10)+0=10
1 1%10=1 (10*10)+1=101
0àHere Loop breaks
101 1
102 102 102%10=2 (0*10)+2=2
10 10%10=0 (2*10)+0=20
1 1%10=1 (20*10)+1=201
0àHere Loop breaks
101

• Find the average (sum/count).


• Finally Print the Average value.
1) Alternative Palindrome Numbers
2) Armstrong Numbers Between the range
3) Alternative Armstrong
4) Sum of Armstrong
5) Even or Odd(Without dividing by 2)
6) Even or Odd(Without using %, /, +)
7) Even or Odd(Without using %, /, +, *)
8) Power of a Number(by using Pre-Defined Method)
9) Power of a Number(Without using Pre Defined Method)
10) Power of a Number(by using While Loop)
11) Power of a Number(by using do-While Loop)
12) Highest Digit In a Number.
Alternative Palindrome Numbers:-
Q) Develop a Program to Print the Alternative palindrome numbers between the Given range and between
the Given Numbers?

Ø Difference between range of the given numbers and between the given number?
• Range--> Including the given limits.
• Between--> Excluding the given limits.

This Program is for between the given Range.


• To Print the alternative Palindromes between the Given Range and Between the Given Numbers, follow
the below logic and steps.
• Input:- Two Integer values (min and max).
• Output:- Print the sum of all palindromes between the given range.
• Logic:-
o Store unit digit from the number.
o Add the stored value to rev variable after multiplying by 10.
o Remove the units digit from the given number
o Repeat the above same steps until given number becomes 0.
o Check the rev value with given value if both are equal Print add the number( but Print the
numbers alternatively).
o Repeat the same process for the numbers between values of min to max.

Step:-1 Take the Inputs from user (min and max).


Step:-2 Initialize and declare the boolean variable b as 'true'.
Step:-3 Initialize and declare the variable rev as 0.
Step:-4 Duplicate the i value into variable t.
Step:-5 Store the units place of t.
Step:-6 Multiply the rev value by 10 and add the above stored value to the rev variable.
Step:-7 Remove the units place from the variable t.
Step:-8 Repeat the steps from 4 to 7 until t value becomes 0 (t>0).
Step:-9 Now Compare the variable rev and variable i.
Step:-10 If equals goto step-11 else goto step-15.
Step:-11 Check the boolean variable is 'true' or not if 'true' goto step-12 else goto step-14.
Step:-12 Print the i value or rev value.
Step:-13 Reassign the value of b as 'false'
Step:-14 Reassign the value of b as 'true'
Step:-15 Increment the i value by 1.
Step:-16 Repeat the steps from 3 to 15 until i value reaches max (i<=max).

Note:-
• i value starts from min value.
• Here using boolean variable to skip second value from Printing for every 2 elements.

import java.util.Scanner;

public class AltPalindromesBwOrRange {

public static void main(String[] args) {

Scanner obj=new Scanner(System.in);

System.out.println("Enter the min and max values");


int min=obj.nextInt(); //Taking input from user (min).
int max=obj.nextInt(); //Taking input from user (max).

boolean b=true; //Initialize and declare the boolean variable b as 'true'.


for(int i=min;i<=max;i++) //Iterating loop from min value to max value in the from
of 'i' variable.
{
int rev=0,t=i; //Initialize and declare the variable rev as 0,
Duplicating the value of i into t(It is nothing but
resetting the value).
while(t!=0) //Iterating a loop upto t value becomes 0.
{
int r=t%10; //Taking the Last Digit(once place) value from t variable
and Storing in variable r.
rev=rev*10+r; //Multiplying rev variable by 10 and adding r.
t=t/10; //Removing the units places digit from variable t.
}//end of while-loop.
if(rev==i) //Comparing the 'rev' value with 'i' value to check
Palindrome or Not.
{
if(b) //Checking 'b' value is 'true' or not(to decide the value
has Print or skip the print).
{
System.out.println(i); //Printing the 'i' value.
b=false; //Reassigning the b value as 'false' to skip the
Printing in next Iteration.
}//end of if-condition(b).
else
b=true; //Reassigning the b value as 'true' to print in the
next Iteration.
}//end of if-condition(rev==i).

}//end of for-loop.

}//end of Main Method.

}//end of Class.

Tracing of Code:-
Input:- 1 10
Output:- 1 3 5 7 9

min max i t r rev((rev*10)+r) b output


1 10 0 true
1 1 1%10=1 (0*10)+1=1
0àHere While Loop breaks 1
false
0
2 2 2%10=2 (0*10)+2=2
0àHere While Loop breaks
true
0
3 3 3%10=3 (0*10)+3=3
0àHere While Loop breaks 3
false
0
4 4 4%10=4 (0*10)+4=4
0àHere While Loop breaks
True
0
5 5 5%10=5 (0*10)+5=5
0àHere While Loop breaks 5
false
0
6 6 6%10=6 (0*10)+6=6
0àHere While Loop breaks
True
0
7 7 7%10=7 (0*10)+7=7
0àHere While Loop breaks 7
false
0
8 8 8%10=8 (0*10)+8=8
0àHere While Loop breaks
True
0
9 9 9%10=9 (0*10)+9=9
0àHere While Loop breaks 9
false
0
10 10 10%10=0 (0*10)+0=0
1 1%10=1 (0*10)+1=1
0àHere While Loop breaks
0
11àHere for Loop breaks
Armstrong Numbers Between the range:-
Q) Develop a Program to Print the Armstrong Numbers Between the Given range and between the Given
Numbers?

Ø Difference between range of the given numbers and between the given number?
• Range--> Including the given limits.
• Between--> Excluding the given limits.

This Program is for between the given Range.


• To Print the all Armstrong Numbers between the given Range, have to follow the below logic and
steps
• Input:- Two Integer values.
• Output:- Print the all Armstrong Numbers.
• Logic:-
o Armstrong Number means nothing but Sum of the n-th power of a Digits in a number.
o n--> n is nothing but no of digits in a given number.
o Then check sum value and number is same or not if same print number.
o Repeat the Above process for all the numbers between the given two numbers.

Step:-1 Take the Inputs from user (min and max).


Step:-2 Duplicate the i value into variable temp.
Step:-3 Initialize and declare the variable noOfDigits as 0.
Step:-4 Remove the units place from the variable temp.
Step:-5 Increment the noOfDigits variable by 1.
Step:-6 Repeat the steps from 4 to 5 until temp value becomes 0.
Step:-7 Now temp becomes 0, so here again duplicating the i value into temp variable.
Step:-8 To store the summation value, declaring a variable(isArm) as 0.
Step:-9 Store the units place of temp.
Step:-10 Initialize and declare the variable digitPower as 1.
Step:-11 Multiply the above stored value with digitPower value and store the result in digitPower
variable.
Step:-12 Increment the j variable by 1.
Step:-13 Repeat the steps from 11 to 12 Until j value reaches noOfDigits(j<=noOfDigits).
Step:-14 And Add the digitPower value to the isArm value.
Step:-15 Remove the units place from the variable temp.
Step:-16 Repeat the steps from 9 to 15 Until temp value becomes 0.
Step:-17 Now check the i value and isArm values equal or not if equal (nothing but Armstrong Number),
Print i value.
Step:-18 Increment the i variable by 1.
Step:-19 Repeat the steps from 2 to 18 Until i value becomes max(i<=max).

Note:-
• Here i value starts from min value.
• Here j value starts from 1.

import java.util.Scanner;

public class ArmstorngBwOrRange {

public static void main(String[] args) {

Scanner obj=new Scanner(System.in);

System.out.println("Enter the Min and Max values");


int min=obj.nextInt(); //Taking inputs from the user(min).
int max=obj.nextInt(); //Taking inputs from the user(max).

for(int i=min;i<=max;i++) //Iterating the loop from min value to the max value in
the form of i variable.
{
int temp=i; //duplicating the user i value for further useage.
int noOfDigits=0; //to store no of digits in a ‘i’ value(It is also a
resetting the value).

while(temp!=0) //Iterating a loop to count no of digits in a ‘i’ value.


{
temp=temp/10; //Removing the Last Digit(once place) value from temp
variable and Storing in variable temp.
noOfDigits++; //counting of no of Digits.
}//end of while
temp=i; //After Execution of while loop temp value becomes 0 so
reassigning the ‘i’ value.
int isArm=0; //To store the sum value(It is also a resetting the value
here).

while(temp!=0) //by Iterating this loop find sum(isArm) value.


{
int digit=temp%10; //Taking the Last Digit(once place) value from temp
variable and Storing in variable digit.
int digitPower=1; //reset the value to 1.
for(int j=1;j<=noOfDigits;j++) //Iterating a loop to find out the power of a
digit.
digitPower=digitPower*digit;

isArm=isArm+digitPower; //here adding the power value to the sum value(isArm).


temp=temp/10; //Removing the units places digit from variable temp.
}//end of while
if(isArm==i) // here checking the i value with sum
value(isArm) to find same or not.
System.out.println(i); //Printing the i value.
}//end of i-Loop.

}//end of main method


}//end of class

Alternative Armstrong:-
Q) Develop a Program to Print the Alternative Armstrong numbers between the given Range and between the
Given Numbers?

Ø Difference between range of the given numbers and between the given number?
• Range--> Including the given limits.
• Between--> Excluding the given limits.

This Program is for between the given Range.


• To Print the Alternative Armstrong Numbers between the given Range, have to follow the below logic
and steps
• Input:- Two Integer values.
• Output:- Print the alternative Armstrong Numbers.
• Logic:-
o Armstrong Number means nothing but Sum of the n-th power of a Digits in a number.
o n--> n is nothing but no of digits in a given number.
o Then check sum value and number is same or not if same print number(Print Alternatively).
o Repeat the Above process for all the numbers between the given two numbers.

Step:-1 Take the Inputs from user (min and max).


Step:-2 Initialize and declare the boolean variable b as 'true'.
Step:-3 Duplicate the i value into variable temp.
Step:-4 Initialize and declare the variable noOfDigits as 0.
Step:-5 Remove the units place from the variable temp.
Step:-6 Increment the noOfDigits variable by 1.
Step:-7 Repeat the steps from 5 to 6 until temp value becomes 0.
Step:-8 Now temp becomes 0, so here again duplicating the i value into temp variable.
Step:-9 To store the summation value, declaring a variable(isArm) as 0.
Step:-10 Store the units place of temp.
Step:-11 Initialize and declare the variable digitPower as 1.
Step:-12 Multiply the above stored value with digitPower value and store the result in digitPower
variable.
Step:-13 Increment the j variable by 1.
Step:-14 Repeat the steps from 12 to 13 Until j value reaches noOfDigits(j<=noOfDigits).
Step:-15 And Add the digitPower value to the isArm value.
Step:-16 Remove the units place from the variable temp.
Step:-17 Repeat the steps from 10 to 16 Until temp value becomes 0.
Step:-18 Now check the i value and isArm values equal or not.
Step:-19 if equal (nothing but Armstrong Number) goto step-20 else goto step-24.
Step:-20 Check the boolean variable is 'true' or not if 'true' goto step-21 else goto step-23.
Step:-21 Print the i value or isArm value.
Step:-22 Reassign the value of b as 'false'
Step:-23 Reassign the value of b as 'true'
Step:-24 Increment the i value by 1.
Step:-25 Repeat the steps from 3 to 24 Until i value becomes max(i<=max).
Note:-
• Here i value starts from min value.
• Here j value starts from 1.
• Here using boolean variable to skip second value from Printing for every 2 elements.

import java.util.Scanner;

public class AltArmstorngBwOrRange {

public static void main(String[] args) {

Scanner obj=new Scanner(System.in);

System.out.println("Enter the Min and Max values");


int min=obj.nextInt(); //Taking inputs from the user(min).
int max=obj.nextInt(); //Taking inputs from the user(max).

boolean b=true; //Initialize and declare the boolean variable b as 'true'.


for(int i=min;i<=max;i++) //Iterating the loop from min value to the max value in
the form of i variable.
{
int temp=i; //Duplicating the user i value for further useage.
int noOfDigits=0; //To store no of digits in a ‘i’ value(It is also a
resetting the value).

while(temp!=0) //Iterating a loop to count no of digits in ‘i’ value.


{
temp=temp/10; //Removing the Last Digit(once place) value from temp
variable and Storing in variable temp.
noOfDigits++; //counting of no of Digits.
}//end of while

temp=i; //After Execution of while loop temp value becomes 0 so


Reassigning ‘i’ value to temp.
int isArm=0; //To store the sum value(It is also a resetting the value
here).

while(temp!=0) //Iterating this loop to find sum(isArm) value.


{
int digit=temp%10; //Taking the Last Digit(once place) value from temp
variable and Storing in variable digit.
int digitPower=1; //Reset the value to 1.
for(int j=1;j<=noOfDigits;j++) //Iterating this loop to find out the power
of a digit.
digitPower=digitPower*digit;

isArm=isArm+digitPower; //here adding the power value to the sum value(isArm).


temp=temp/10; //Removing the units places digit from variable temp.
}//end of while
if(isArm==i) // Here checking the i value with sum value(isArm) to
find same or not
{
if(b) //Checking 'b' value is 'true' or not(to decide the
value has Print or skip the print).
{
System.out.println(i); //Printing the i value.
b=false; //Reassigning the b value as 'false' to skip the
Printing in next Iteration.
}//end of if-condition(b).
else
b=true; //Reassigning the b value as 'true' to print in
the next Iteration.
}//end of if-condition(isArm==i).
}//end of i-Loop.

}//end of main method.


}//end of class.
Sum of Armstrong:-
Q) Develop a Program to Print the Sum of the Armstrong Numbers Between the range and between the Given
Numbers?

Ø Difference between range of the given numbers and between the given number?
• Range--> Including the given limits.
• Between-->Excluding the given limits.

This Program is for between the given Range.


• To Print the sum of the Armstrong Numbers between the given Range, have to follow the below logic
and steps
• Input:- Two Integer values.
• Output:- Print the sum of the Armstrong Numbers.
• Logic:-
o Armstrong Number means nothing but Sum of the n-th power of a Digits in a number.
o n--> n is nothing but no of digits in a given number.
o Then check sum value and number is same or not if same add the i value to sum variable.
o Repeat the Above process for all the numbers between the given two numbers.
o Finally Print sum value.

Step:-1 Take the Inputs from user (min and max).


Step:-2 Initialize and declare the variable sum as 0.
Step:-3 Duplicate the i value into variable temp.
Step:-4 Initialize and declare the variable noOfDigits as 0.
Step:-5 Remove the units place from the variable temp.
Step:-6 Increment the noOfDigits variable by 1.
Step:-7 Repeat the steps from 5 to 6 until temp value becomes 0.
Step:-8 Now temp becomes 0, so here again duplicating the i value into temp variable.
Step:-9 To store the summation value, declaring a variable(isArm) as 0.
Step:-10 Store the units place of temp.
Step:-11 Initialize and declare the variable digitPower as 1.
Step:-12 Multiply the above stored value with digitPower value and store the result in digitPower
variable.
Step:-13 Increment the j variable by 1.
Step:-14 Repeat the steps from 12 to 13 Until j value reaches noOfDigits(j<=noOfDigits).
Step:-15 And Add the digitPower value to the isArm value.
Step:-16 Remove the units place from the variable temp.
Step:-17 Repeat the steps from 10 to 16 Until temp value becomes 0.
Step:-18 Now check the i value and isArm values equal or not if equal (nothing but Armstrong Number)
goto step-19 else goto step:-20..
Step:-19 Add this i value to the variable sum.
Step:-20 Increment the i variable by 1.
Step:-21 Repeat the steps from 3 to 20 Until i value becomes max(i<=max).
Step:-22 Finally Print the sum value.

Note:-
• Here i value starts from min value.
• Here j value starts from 1.

import java.util.Scanner;

public class SumArmstorngBwOrRange {

public static void main(String[] args) {

Scanner obj=new Scanner(System.in);

System.out.println("Enter the Min and Max values");


int min=obj.nextInt(); //Taking inputs from the user(min).
int max=obj.nextInt(); //Taking inputs from the user(max).

int sum=0; //initialize and declare a variable sum as 0.

for(int i=min;i<=max;i++) //Iterating the loop from min value to the max value in
the form of i variable.
{
int temp=i; //duplicating the i value for further useage.
int noOfDigits=0; //To store no of digits in a ‘i’ value(It is also a
resetting the value).
while(temp!=0) //Iterating a loop to count no of digits in ‘i’ value.
{
temp=temp/10; //Removing the Last Digit(once place) value from temp
variable and Storing in variable temp.
noOfDigits++; //counting of no of Digits.
}//end of while.

temp=i; //After Execution of while loop temp value becomes 0 so


Reassigning the ‘i’ value.
int isArm=0; //To store the sum value(It is also a resetting the value
here).

while(temp!=0) //Iterating this loop to find sum(isArm) value.


{
int digit=temp%10; //Taking the Last Digit(once place) value from temp
variable and Storing in variable digit.
int digitPower=1; //reset the value to 1.
for(int j=1;j<=noOfDigits;j++) //Iterating loop to find out the power of a digit.
digitPower=digitPower*digit;

isArm=isArm+digitPower; //here adding the power value to the sum value(isArm).


temp=temp/10; //Removing the units places digit from variable temp.
}//end of while.
if(isArm==i) //Checking the ‘i’ value with sum value(isArm) to
find same or not.
sum=sum+i; //Adding i value to sum.
}//end of i-Loop.

System.out.println(sum); //Printing the sum value.

}//end of main method.

}//end of class.
Even or Odd:-(Without dividing by 2)
Q) Develop a program to check whether the given number is even or odd(Without dividing by 2)?

• To check Given number is even or odd without Dividing by 2.


• Input:- one Integer value.
• Output:- Print the Given Number is Even Number or Odd Number.
• Logic:-
o Here checking only units place value is Even or Odd.
o By checking units place we can say that given number is Even or Odd.
o Checking that Units Place is 0, 2, 4 ,6, 8 then it is an Even Number else Odd Number.

import java.util.Scanner;
public class EvenOrOddWay1 {

public static void main(String[] args) {


Scanner obj=new Scanner(System.in);

int num=obj.nextInt(); //Taking input from the user.

int UnitsPlace=num%10; //Taking Units digit of Given Number and storing in UnitsPlace
varaible.

if(UnitsPlace==0 || UnitsPlace==2 || UnitsPlace==4 || UnitsPlace==6 || UnitsPlace==8)


//Checking the Units digit(UnitsPlace) with all
single digit even numbers.
System.out.println("Given Number is even Number");
else
System.out.println("Given Number is Odd Number");

}//end of Main Method.

}//end of Class.
Tracing of Code:- Tracing of Code:-
Input:- 10 Input:- 7
Output:- Given Number is Even Number Output:- Given Number is Odd Number
num UnitsPlace output num UnitsPlace output
10 0 Given Number is Even Number 7 1 Given Number is Odd Number

Even or Odd:-(Without using %, /, +)


Q) Develop a program to check whether the given number is even or odd(Without using % , / , + )?

• To check Given number is even or odd without Using (% , / , + ).


• Input:- one Integer value.
• Output:- Print the Given Number is Even Number or Odd Number.
• Logic:-
o According to given conditions:
o Here the Logic is
o Given number is divides with 2.
o And again Multiply with 2.
o If the result and given value is same then it is even or else odd.

import java.util.Scanner;
public class EvenOrOddWay2 {

public static void main(String[] args) {


Scanner obj=new Scanner(System.in);

int num=obj.nextInt(); //Taking input from the user.

int temp=num/2; //Here given value divides by 2 and store into temp variable.

temp=temp*2; //Multiply the temp value by 2 and store in temp variable.

if(num==temp) //Checking the result(temp) and given number(num).


System.out.println("Given Number is even Number");
else
System.out.println("Given Number is Odd Number");

}//end of Main Method.

}//end of Class.

Tracing of Code:- Tracing of Code:-


Input:- 10 Input:- 7
Output:- Given Number is Even Number Output:- Given Number is Odd Number
num temp output num temp output
10 10/2=5 7 7/2=3
5*2=10 Given Number is Even Number 3*2=6 Given Number is Odd Number

Even or Odd:-(Without using %, /, +, *)


Q) Develop a program to check whether the given number is even or odd(Without using % , / , + ,* )?

• To check Given number is even or odd without Using(% , / , +, *).


• Input:- one Integer value.
• Output:- Print the Given Number is Even Number or Odd Number.
• Logic:-
o According to given conditions:
o Here the Logic is
o Subtracting by 2 from given number.
o Repeat the above step upto number becomes 1 or 0.
o if 1 then it is odd or else 0 then it is even.

Step:-1 Take the input from the user.


Step:-2 Decrement by 2 from given number.
Step:-3 Repeat the step-2 until value becomes 0 or 1.
Step:-4 If value is 1 then Print "Given Number is Odd Number".
Step:-5 If value is 0 then Print "Given Number is Even Number".
import java.util.Scanner;
public class EvenOrOddWay3 {

public static void main(String[] args) {


Scanner obj=new Scanner(System.in);

int num=obj.nextInt(); //Taking input from the user.

while(num!=1 && num!=0) //Iterating a Loop until num value becomes 0 or 1.


{
num=num-2; //Decrementing num value by 2.
}//end of Loop.

if(num==0) //Checking the num value is 0 or not.


System.out.println("Given Number is even Number");
else
System.out.println("Given Number is Odd Number");
}//end of Main Method.

}//end of Class.

Tracing of Code:- Tracing of Code:-


Input:- 10 Input:- 7
Output:- Given Number is Even Number Output:- Given Number is Odd Number
num num
10 7
10-2=8 7-2=5
8-2=6 5-2=3
6-2=4 3-2=1àLoop Breaks here
4-2=2
2-2=0àLoop Breaks here Given Number is Odd Number

Given Number is Even Number


Power of a Number:-(by using Pre-Defined Method)
Q) Develop a program to calculate power of a number by using Pre-Defined Method?

• To Calculate and Print the power of a number, follow the below logic and steps.
• Input:- Two Integer values (base value and power value).
• Output:- Print the Power of a number.
• Logic:-
o By using Pre-Defined Method(math.pow(b,p)).
o But this method returns Double value.
o so have to convert this value into integer by using type-casting.

Step:-1 Take the Input from user (base value).


Step:-2 Take the Input from user (power value).
Step:-3 Call a Pre-defined Method by passing two variables.
Step:-4 It returns double value so by type-casting to integer.
Step:-5 Finally Print the value.

import java.util.Scanner;
public class PowerWay1 {

public static void main(String[] args) {


Scanner obj=new Scanner(System.in);

System.out.println("Enter the Base value");


int b=obj.nextInt(); //Taking input from user (b).

System.out.println("Enter the Power value");


int p=obj.nextInt(); //Taking input from user (p).

int value=(int)Math.pow(b, p); //calling a Pre-defined Method and type-casting to integer.

System.out.println(value); //Printing the Value.


}//end of Main Method.
}//end of Class.
Power of a Number:-(Without using Pre Defined Method)
Q) Develop a program to calculate power of a number without using Pre-Defined Method?

• To Calculate and Print the power of a number, follow the below logic and steps.
• Input:- Two Integer values (base value and power value).
• Output:- Print the Power of a number.
• Logic:- multiplying the base for power times.

Step:-1 Take the Input from user (base value).


Step:-2 Take the Input from user (power value).
Step:-3 Declare and Initialize the variable value as 1.
Step:-4 Multiply the value with base variable and store in value variable.
Step:-5 Repeat the step-4 for power times.
Step:-6 Finally Print the value.

import java.util.Scanner;
public class PowerWay1 {

public static void main(String[] args) {


Scanner obj=new Scanner(System.in);

System.out.println("Enter the Base value");


int b=obj.nextInt(); //Taking input from user (b).

System.out.println("Enter the Power value");


int p=obj.nextInt(); //Taking input from user (p).

int value=1; //Declare and Initialize the variable value as 1.


for(int i=1;i<=p;i++) //Iterating loop for given power times.
{
value=value*b; //multiplying the base value with value variable and
storing in value variable.
}//end of Loop.
System.out.println(value); //Printing the Value.
}//end of Main Method.
}//end of Class.
Tracing of Code:- Tracing of Code:-
Input:- 3 6 Input:- 7 4
Output:- 729 Output:- 2401
b p i value b p i value
3 6 1 7 4 1
1 1*3=3 1 1*7=7
2 3*3=9 2 7*7=49
3 9*3=27 3 49*7=343
4 27*3=81 4 343*7=2401
5 81*3=243 5àLoop Breaks here
6 243*3=729
7àLoop Breaks here

Power of a Number:-(by using While Loop)


Q) Develop a program to calculate power of a number by using While Loop?

• To Calculate and Print the power of a number, follow the below logic and steps.
• Input:- Two Integer values (base value and power value).
• Output:- Print the Power of a number.
• Logic:- multiplying the base for power times.

Step:-1 Take the Input from user (base value).


Step:-2 Take the Input from user (power value).
Step:-3 Declare and Initialize the variable value as 1.
Step:-4 Declare and Initialize the variable i as 1.
Step:-5 Multiply the value with base variable and store in value variable.
Step:-6 Increment the i value by 1.
Step:-7 Repeat the step-5 to 6 Until i value reaches power value.
Step:-8 Finally Print the value.

import java.util.Scanner;
public class PowerWay3 {
public static void main(String[] args) {
Scanner obj=new Scanner(System.in);

System.out.println("Enter the Base value");


int b=obj.nextInt(); //Taking input from user (b).

System.out.println("Enter the Power value");


int p=obj.nextInt(); //Taking input from user (p).

int value=1; //Declare and Initialize the variable value as 1.


int i=1; //Declare and Initialize the variable i as 1.
while(i<=p) //Iterating loop for given power times.
{
value=value*b; //multiplying the base value with value variable and
storing in value variable.
i++; //Incrementing i value by 1.
}//end of Loop.
System.out.println(value); //Printing the Value.
}//end of Main Method.
}//end of Class.

Tracing of Code:- Tracing of Code:-


Input:- 3 6 Input:- 7 4
Output:- 729 Output:- 2401
b p i value b p i value
3 6 1 7 4 1
1 1*3=3 1 1*7=7
2 3*3=9 2 7*7=49
3 9*3=27 3 49*7=343
4 27*3=81 4 343*7=2401
5 81*3=243 5àLoop Breaks here
6 243*3=729
7àLoop Breaks here
Power of a Number:-(by using do-While Loop)
Q) Develop a program to calculate power of a number by using do-While Loop?

• To Calculate and Print the power of a number, follow the below logic and steps.
• Input:- Two Integer values (base value and power value).
• Output:- Print the Power of a number.
• Logic:- multiplying the base for power times.

Step:-1 Take the Input from user (base value).


Step:-2 Take the Input from user (power value).
Step:-3 Declare and Initialize the variable value as 1.
Step:-4 Declare and Initialize the variable i as 1.
Step:-5 Multiply the value with base variable and store in value variable.
Step:-6 Increment the i value by 1.
Step:-7 Repeat the step-5 to 6 Until i value reaches power value.
Step:-8 Finally Print the value.

import java.util.Scanner;
public class PowerWay4 {

public static void main(String[] args) {


Scanner obj=new Scanner(System.in);

System.out.println("Enter the Base value");


int b=obj.nextInt(); //Taking input from user (b).

System.out.println("Enter the Power value");


int p=obj.nextInt(); //Taking input from user (p).

int value=1; //Declare and Initialize the variable value as 1.


int i=1; //Declare and Initialize the variable i as 1.
do
{
value=value*b; //multiplying the base value with value variable and
storing in value variable.
i++; //Incrementing i value by 1.
}while(i<=p); //Iterating loop for given power times.
System.out.println(value); //Printing the Value.
}//end of Main Method.

}//end of Class.

Tracing of Code:- Tracing of Code:-


Input:- 3 6 Input:- 7 4
Output:- 729 Output:- 2401
b p i value b p i value
3 6 1 7 4 1
1 1*3=3 1 1*7=7
2 3*3=9 2 7*7=49
3 9*3=27 3 49*7=343
4 27*3=81 4 343*7=2401
5 81*3=243 5àLoop Breaks here
6 243*3=729
7àLoop Breaks here

Highest Digit In a Number:-


Q) Develop a Program to print the Highest digit in a Given Number?

• To Find and Print the Maximum digit in a given number, follow the below logic and steps.
• Input:- One Integer value (n).
• Output:- Print the Maximum digit.
• Logic:- Compare all the digits with each other and find the maximum value.

Step:-1 Take the Input from user (n).

Step:-2 Declare and Initialize the variable max as 0.


Step:-3 Store the units place of given number(n) in r variable.

Step:-4 Checking Max Value with r value (max<r).

Step:-5 Remove the units place from the Given Number(n).

Step:-6 Repeat the step-3 to 5 Until n value becomes 0.

Step:-7 Finally Print the max value.

import java.util.Scanner;

public class HIghestDigitInGivenNumber {

public static void main(String[] args) {


Scanner obj=new Scanner(System.in);

System.out.println("Enter the Number");


int n=obj.nextInt(); //Taking input from user (n).

int max=0; //Declare and initialize the variable max as 0.


while(n!=0) //Iterating a Loop until n value becomes 0.
{
int r=n%10; //Taking the Last Digit(once place) value from n variable and
Storing in variable r.
if(max<r) //Checking the max value is greater than or not with
remainder(r) value.
max=r; //Storing the remainder(r) value in max variable.
n=n/10; //Removing the units places digit from variable n.
}//end of loop.
System.out.println(max); //Print the max value.

}//end of Main Method.

}//end of Class.
Tracing of Code:- Tracing of Code:-
Input:- 729 Input:- 8642
Output:- 9 Output:- 8
n r max n r max
729 0 8642 0
729%10=9 9 8642%10=2 2
729/10=72 72%10=2 8642/10=864 864%10=4 4
72/10=7 7%10=7 7864/10=86 86%10=6 6
7/10=0àLoop Breaks here 86/10=8 8%10=8 8
8/10=0àLoop Breaks here
Finally print max value.
Finally print max value.
1) Smallest Digit in a Number.
2) Highest Span of a Number.
3) Count no of Digits.
4) Ugly Number or Not.
5) Cubes Format.(𝒏𝟑,. ......... 64,27,8,1).
6) Square Format.(𝒏𝟐,. ......... 16+9+4+1).
7) Perfect Square or Not.
8) Prime Number or Not(Without Using count)
9) First 50 Prime Numbers(Without Using count)
10) Nearest Prime Number.
Smallest Digit in a Number:-
Q) Develop a Program to print the smallest digit in a Given Number?

• To Find and Print the Minimum digit in a given number, follow the below logic and steps.
• Input:- One Integer value (n).
• Output:- Print the Minimum digit.
• Logic:- Compare all the digits with each other and find the minimum value.

Step:-1 Take the Input from user (n).


Step:-2 Declare and Initialize the variable min as 10.
Step:-3 Store the units place of given number(n) in r variable.
Step:-4 Checking min Value with r value (min<r).
Step:-5 Remove the units place from the Given Number(n).
Step:-6 Repeat the step-3 to 5 Until n value becomes 0.
Step:-7 Finally Print the min value.

import java.util.Scanner;

public class LowestDigitInGivenNumber {

public static void main(String[] args) {


Scanner obj=new Scanner(System.in);

System.out.println("Enter the Number");


int n=obj.nextInt(); //Taking input from user (n).

int min=10; //Declare and initialize the variable min as 0.


while(n!=0) //Iterating a Loop until n value becomes 0.
{
int r=n%10; //Taking the Last Digit(once place) value from n variable
and Storing in variable r.
if(min>r) //Checking the min value is lesser than or not with
remainder(r) value.
min=r; //Storing the remainder(r) value in min variable.
n=n/10; //Removing the units places digit from variable n.
}//end of loop.
System.out.println(min); //Print the min value.
}//end of Main Method.

}//end of Class.

Tracing of Code:- Tracing of Code:-


Input:- 729 Input:- 2468
Output:- 2 Output:- 2
n r min n r min
729 10 2468 10
729%10=9 9 2468%10=8 8
729/10=72 72%10=2 2 2468/10=246 246%10=6 6
72/10=7 7%10=7 246/10=24 24%10=4 4
7/10=0àLoop Breaks here 24/10=2 2%10=2 2
2/10=0àLoop Breaks here
Finally print min value.
Finally print min value.

Highest Span of a Number:-


Q) Develop a Program to print the highest span of a Given Number?
Ex: input: 25861 , output: 8-1 (Max digit- min digit)?
• To Find and Print the Highest Span of a given number, follow the below logic and steps.
• Input:- One Integer value (n).
• Output:- Print the Difference between Maximum and Minimum digits.
• Logic:-
o Compare all the digits with each other.
o Find the minimum value.
o Find the maximum value.
o Print the difference between the maximum value and minimum value.

Step:-1 Take the Input from user (n).


Step:-2 Declare and Initialize the variable max as 0.
Step:-3 Declare and Initialize the variable min as 10.
Step:-4 Store the units place of given number(n) in r variable.
Step:-5 Checking min Value with r value (min<r).
Step:-6 Checking max Value with r value (max>r)
Step:-7 Remove the units place from the Given Number(n).
Step:-8 Repeat the step-4 to 7 Until n value becomes 0.
Step:-9 Finally Print the max-min value.

import java.util.Scanner;

public class HighestSpanOfAGivenNumber {

public static void main(String[] args) {


Scanner obj=new Scanner(System.in);

System.out.println("Enter the Number");


int n=obj.nextInt(); //Taking input from user (n).

int max=0; //Declare and initialize the variable max as 0.


int min=10; //Declare and initialize the variable min as 0.
while(n!=0) //Iterating a Loop until n value becomes 0.
{
int r=n%10; //Taking the Last Digit(once place) value from n variable
and Storing in variable r.
if(min>r) //Checking the min value is lesser than or not with
remainder(r) value.
min=r; //Storing the remainder(r) value in min variable.
if(max<r) //Checking the max value is greater than or not with
remainder(r) value.
max=r; //Storing the remainder(r) value in max variable.
n=n/10; //Removing the units places digit from variable n.
}//end of loop.
System.out.println(max-min); //Print the Difference between max and min values.
}//end of Main Method.

}//end of Class.
Tracing of Code:- Tracing of Code:-
Input:- 729 Input:- 2468
Output:- 2 Output:- 2
n r min max n r min max
729 10 0 2468 10 0
729%10=9 9 9 2468%10=8 8 8
729/10=72 72%10=2 2 2468/10=246 246%10=6 6
72/10=7 7%10=7 246/10=24 24%10=4 4
7/10=0àLoop Breaks here 24/10=2 2%10=2 2
2/10=0àLoop Breaks here
Finally print max-min value.
Finally print max-min value.

Count no of Digits:-
Q) Develop a program to count no of digits in a Given Number?

• To Find and Print the count of a no of Digits in a given number, follow the below logic and steps.
• Input:- One Integer value (n).
• Output:- Print the no of digits count.
• Logic:-
o Remove one digit from user given number.
o While removing count the digits.
o Repeat the above steps until Given Number is becomes 0.
o Now Print count.

Step:-1 Take the Input from user (n).


Step:-2 Declare and Initialize the variable noOfDigits as 0.
Step:-3 Increment the noOfDigits value by 1.
Step:-4 Remove the units place from the Given Number(n).
Step:-5 Repeat the step-3 to 4 Until n value becomes 0.
Step:-6 Finally Print the noOfDigits value.
import java.util.Scanner;

public class CountOfNumberOfDigits {

public static void main(String[] args) {

Scanner obj=new Scanner(System.in);

System.out.println("Enter the Number");


int n=obj.nextInt(); //Taking input from user (n).

int noOfDigits=0; //Declare and initialize the variable noOfDigits as 0.


while(n!=0) //Iterating a Loop until n value becomes 0.
{
n=n/10; //Removing the units places digit from variable n.
noOfDigits++; //Increment the value of noOfDigits by 1 to
represent/count the no of digits.
}//end of loop.
System.out.println(noOfDigits); //Print the noOfDigits values.
}//end of Main Method.
}//end of Class.

Tracing of Code:-
Input:- 418163 Tracing of Code:-
Output:- 6 Input:- 85618
n noOfDigits Output:- 5
418163 0 n noODigits
418163/10=41816 1 85618 0
41816/10=4181 2 85618/10=8561 1
4181/10=418 3 8561/10=856 2
418/10=41 4 856/10=85 3
41/10=4 5 85/10=8 4
4/10=0 6 8/10=0 5
0àLoop Breaks here 0àLoop Breaks here

Finally print noOfDigits value. Finally print noOfDigits value.


Ugly Number or Not:-
Q) Develop a Program to Print Given Number is ugly Number or not?( we have to Divides with 2 , 3 ,5
only)

• To Find and Print the given number is Ugly number or not, follow the below logic and steps.
• Input:- One Integer value (n).
• Output:- Print the Ugly number or not.
• Logic:-
o Ugly number is nothing but 2x * 3y * 5z is equals to given number or not.
o Check the number is perfectly divides with 2 or not.
o If it divides repeat the above step.
o Check the number is perfectly divides with 3 or not.
o If it divides repeat the above step.
o Check the number is perfectly divides with 5 or not.
o If it divides repeat the above step.
o Now check the number value is 1 or not.
o If 1 Print "Given number is Ugly Number".
o else Print "Given number is Not a Ugly Number".

Step:-1 Take the Input from user (n).


Step:-2 Divides the number(n) with 2.
Step:-3 If remainder is 0 then goto step-3 else goto step-6.
Step:-4 Divides the number(n) with 2 and store the quotient value in variable n.
Step:-5 Repeat the steps from 2 to 4.
Step:-6 Divides the number(n) with 3.
Step:-7 If remainder is 0 then goto step-8 else goto step-10.
Step:-8 Divides the number(n) with 3 and store the quotient value in variable n.
Step:-9 Repeat the steps from 6 to 8.
Step:-10 Divides the number(n) with 5.
Step:-11 If remainder is 0 then goto step-12 else goto step-14.
Step:-12 Divides the number(n) with 5 and store the quotient value in variable n.
Step:-13 Repeat the steps from 10 to 12.
Step:-14 Now Check number(n) is 1 or not.
Step:-15 if 1 then Print "Given Number is Ugly Number".
Step:-16 else Print "Given Number is Not a Ugly Number".

import java.util.Scanner;

public class UglyNumber {

public static void main(String[] args) {


Scanner obj=new Scanner(System.in);

System.out.println("Enter the Number");


int n=obj.nextInt(); //Taking input from user (n).

while(n%2==0) //Iterating a Loop to check number is Divisible by 2 or not.


{
n=n/2; //Dividing the number(n) by 2 and Storing in variable n.
}//end of while loop(n%2==0).

while(n%3==0) //Iterating a Loop to check number is Divisible by 3 or not.


{
n=n/3; //Dividing the number(n) by 3 and Storing in variable n.
}//end of while loop(n%3==0).

while(n%5==0) //Iterating a Loop to check number is Divisible by 5 or not.


{
n=n/5; //Dividing the number(n) by 5 and Storing in variable n.
}//end of while loop(n%5==0).

if(n==1) //Checking given number is 1 or not.


System.out.println("Given Number is Ugly Number");
else
System.out.println("Given number is Not a ugly Number");
}//end of Main Method.
}//end of Class.

Tracing of Code:- Tracing of Code:-


Input:- 21 Input:- 60
Output:- Given number is Not a ugly Number Output:- Given Number is Ugly Number
n n
21 60
21%2!=0 so, 21àLoop Breaks here 60%2==0 so, 30
21%3==0 so, 7 30%2==0 so, 15
7%3!=0 so, 7àLoop Breaks here 15%2!=0 so, 15àLoop Breaks here
7%5!=0 so, 7àLoop Breaks here 15%3==0 so, 5
5%3!=0 so, 5àLoop Breaks here
Finally check n value is 1 or not. 5%5==0 so, 1
1%5!=0 so, 1àLoop Breaks here

Finally check n value is 1 or not.

Cubes Format:-
Q) Write a Program to Print the numbers in the following format.
𝒏𝟑,...........64,27,8,1.

• To Calculate and Print the cubes of a numbers from n to the 1, follow the below logic and steps.
• Input:- One Integer value (n).
• Output:- Print the cubes of n to 1.
• Logic:-
o Multiplying three times each number it self and print the value
o Repeat the above step from n to the 1.

Step:-1 Take the Input from user (n).


Step:-2 Declare and Initialize the boolean variable b as 'true'.
Step:-3 Mutliply the i value for three times it-self.
Step:-4 Check the b value is true or not, if 'true' goto step-5 else goto step:-7.
Step:-5 Print the value.
Step:-6 Reassign the boolean variable b as 'false'.
Step:-7 Print the ',' with value.
Step:-8 Decrement the i value by 1.
Step:-9 Repeat the steps from 3 to 8 until i becomes 0.

Note:- Here i value starts from user Given Number.

import java.util.Scanner;

public class CubesFormat {

public static void main(String[] args) {


Scanner obj=new Scanner(System.in);

System.out.println("Enter the Number");


int n=obj.nextInt(); //Taking input from user (n).
boolean b=true; //Declare and initialize the boolean variable b as 'true'.

for(int i=n;i>0;i--) //Iterating the loop from given value to 1.


{
if(b) //Checking b value is 'true' or not, To print the ','
between values.
{
System.out.print(i*i*i); //Printing the cube value of number(i).
b=false; //reassigning the b value as 'false' to enter into else
from next iteration.
}//end of if-condition.
else
System.out.print(","+i*i*i); //Printing the cube value of number(i) with ','.
}//end of loop.

}//end of Main Method.

}//end of Class.
Tracing of Code:- Tracing of Code:-
Input:- 10 Input:- 8
Output:- 1000,729,512,343,216,125,64,27,8,1 Output:- 512,343,216,125,64,27,8,1
n i b output n i b output
10 true 10 true
10 10*10*10 1000 8 8*8*8 512
false false
9 9*9*9 ,729 7 7*7*7 ,343
8 8*8*8 ,512 6 6*6*6 ,216
7 7*7*7 ,343 5 5*5*5 ,125
6 6*6*6 ,216 4 4*4*4 ,64
5 5*5*5 ,125 3 3*3*3 ,27
4 4*4*4 ,64 2 2*2*2 ,8
3 3*3*3 ,27 1 1*1*1 ,1
2 2*2*2 ,8 0àLoop Breaks here
1 1*1*1 ,1
0àLoop Breaks here

Square Format:-
Q) Write a Program to Print the numbers in the following format.
𝒏𝟐,...........16+9+4+1.

• To Calculate and Print the Squares of a numbers from n to the 1, follow the below logic and steps.
• Input:- One Integer value (n).
• Output:- Print the Squares of n to 1.
• Logic:-
o Multiplying two times each number it self and print the value.
o Repeat the above step from n to the 1.

Step:-1 Take the Input from user (n).


Step:-2 Declare and Initialize the boolean variable b as 'true'.
Step:-3 Mutliply the i value for two times it-self.
Step:-4 Check the b value is true or not, if 'true' goto step-5 else goto step:-7.
Step:-5 Print the value.
Step:-6 Reassign the boolean variable b as 'false'.
Step:-7 Print the ',' with value.
Step:-8 Decrement the i value by 1.
Step:-9 Repeat the steps from 3 to 8 until i becomes 0.

Note:- Here i value starts from user Given Number.

import java.util.Scanner;

public class SquaresFormat {

public static void main(String[] args) {


Scanner obj=new Scanner(System.in);

System.out.println("Enter the Number");


int n=obj.nextInt(); //Taking input from user (n).
boolean b=true; //Declare and initialize the boolean variable b as 'true'.

for(int i=n;i>0;i--) //Iterating the loop from given value to 1.


{
if(b) //Checking b value is 'true' or not,To print the ','
between values.
{
System.out.print(i*i); //Printing the cube value of number(i).
b=false; //reassigning the b value as 'false' to enter into else
from next iteration.
}//end of if-condition.
else
System.out.print(","+i*i); //Printing the cube value of number(i) with ','.
}//end of loop.

}//end of Main Method.


}//end of Class.
Tracing of Code:- Tracing of Code:-
Input:- 10 Input:- 8
Output:- 100,81,64,49,36,25,16,9,4,1 Output:- 64,49,36,25,16,9,4,1
n i b output n i b output
10 true 10 true
10 10*10 100 8 8*8 64
false false
9 9*9 ,81 7 7*7 ,49
8 8*8 ,64 6 6*6 ,36
7 7*7 ,49 5 5*5 ,25
6 6*6 ,36 4 4*4 ,16
5 5*5 ,25 3 3*3 ,9
4 4*4 ,16 2 2*2 ,4
3 3*3 ,9 1 1*1 ,1
2 2*2 ,4 0àLoop Breaks here
1 1*1 ,1
0àLoop Breaks here

Perfect Square or Not:-


Q) Develop a Program to check a Given Number is perfect square or not.
Ex: input: 36, output: Perfect square(6*6=36)

• To Find and Print the Given Number is Perfect Square or not, follow the below logic and steps.
• Input:- One Integer value (n).
• Output:- Print the Perfect Square or not.
• Logic:-
o Form 1 to the given value we have to calculate squares and compares with n value is greater
than or equal.
o If it is Greater than or equal then stop the process there.
o Now check the value is equal to Given Number or not.
Step:-1 Take the Input from user (n).
Step:-2 Check the i*i value is Greater than or equal to Given Number.
Step:-3 If i*i value is greater than or equal to Given Number(n) then take a 'break' and goto step-5.
Step:-4 Increment the i value by 1.
Step:-5 Repeat the Steps from 2 to 4.
Step:-6 Now check i*i value is equal to Given Number(n) or not, if yes goto Step-7 else Step-8.
Step:-7 Print "Given Number is Perfect Square".
Step:-8 Print "Given Number is Not a Perfect Square"..

Note:- Here i value starts from 1.

import java.util.Scanner;

public class PerfectSquare {

public static void main(String[] args) {


Scanner obj=new Scanner(System.in);

System.out.println("Enter the Number");


int i,n=obj.nextInt(); //Taking input from user (n).

for(i=1;i<n;i++) //Iterating the loop from 1 to the given value.


{
if(i*i>=n) //Checking i*i value is greater than or equal to n.
{
break; //If-condition satisfies then above loop will breaks he
forcibly.
}//end of if-condition.
}//end of loop.
if(i*i==n) //Checking i*i value is equal to given value or not.
System.out.println("Given Number is Perfect Square");
else
System.out.println("Given Number is Not a Perfect Square");
}//end of Main Method.

}//end of Class.
Tracing of Code:- Tracing of Code:-
Input:- 16 Input:- 10
Output:- Given Number is Perfect Square Output:- Given Number is Not a Perfect Square
n i n i
16 10
1 1*1=1 1 1*1=1
2 2*2=4 2 2*2=4
3 3*3=9 3 3*3=9
4 4*4=16àLoop Breaks here 4 4*4=16àLoop Breaks here
Now Checks the i*i==n or not. Now Checks the i*i==n or not.

Prime Number or Not(Without Using count):-


Q) Write a Program to Print the given number is Prime or not without using count?

• To Find and Print the Given Number is Prime Number or not without using count, follow the below
logic and steps.
• Input:- One Integer value (n).
• Output:- Print the Prime Number or not.
• Logic:-
o Logic to identify the Given Number is Prime Number or not.
o Here our Logic is Dividing given number with each and every number from 2 if divides
perfectly then Checking i and n values.
o If both are equal means its a Perfect Prime Number or else Not a Prime Number.

Step:-1 Take the Input from user (n).


Step:-2 Divides the Given Number(n) with i value.
Step:-3 If Remainder is 0 then take break goto step-6.
Step:-4 Increment the i value by 1.
Step:-5 Repeat the Steps from 2 to 4.
Step:-6 Now check i value is equal to Given Number(n) or not, if yes goto Step-7 else Step-8.
Step:-7 Print "Given Number is Prime Number".
Step:-8 Print "Given Number is Not a Prime Number".

Note:- Here i value starts from 1.


Ø For this logic we are ending/stopping/taking break at any of these 2 conditions.
• when for loop condition becomes false, at that time 'i' value becomes given number(i=num).
• At the time of execute "break" (It means a factor identified for a given number), at that time
'i' value becomes less than given number(i<num).

import java.util.Scanner;

public class PrimeNumberWithOutCount {

public static void main(String[] args) {


Scanner obj=new Scanner(System.in);

System.out.println("Enter the Number");


int i,n=obj.nextInt(); //Taking input from user (n).

for(i=2;i<=n;i++) //Iterating the loop from 2 to the given value.


{
if(n%i==0) //Checking Given Number(n) is perfectly divides with i or not.
{
break; //If-condition satisfies then above loop will breaks he
forcibly.
}//end of if-condition.

}//end of loop.

if(i==n) //Checking i value is equal to given value or not.


System.out.println("Given Number is Prime Number");
else
System.out.println("Given Number is Not a Prime Number");

}//end of Main Method.

}//end of Class.
Tracing of Code:- Tracing of Code:-
Input:- 25 Input:- 7
Output:- Given Number is Not a Prime Number Output:- Given Number is Prime Number
n i n i
25 7
2 25%2!=0 2 7%2!=0
3 25%3!=0 3 7%3!=0
4 25%4!=0 4 7%4!=0
5 25%5==0àLoop Breaks here 5 7%5!=0
Now Checks the i==n or not. 6 7%6!=0
7 7%7==0àLoop Breaks here
Now Checks the i==n or not.

First 50 Prime Numbers(Without Using count):-


Q) Develop a Program to print the first 50 prime Numbers without using count?

• To Find and Print the first n no of Prime Numbers without using count, follow the below logic and
steps.
• Input:- One Integer value (n).
• Output:- Print the first n Prime Number.
• Logic:- By taking first Prime number in a variable and starting from that printing all first n
Prime Numbers.

Step:-1 Take the Input from user (n).


Step:-2 Initialize and Declare the i value as first Prime Number(2).
Step:-3 Divides the i value by j value.
Step:-4 If remainder is 0 then goto step-7, else goto step-5.
Step:-5 Increment the j value by 1.
Step:-6 Repeat the steps from 3 to 5 until j value reaches i value.
Step:-7 Now Check the i value is equal to j value or not.
Step:-8 If equal then goto step:-9, else goto step-11.
Step:-9 Print the i value.
Step:-10 Decrement the n value by 1.
Step:-11 Increment the i value by 1.
Step:-12 Repeat the steps from 3 to 11.

Note:- Here j value starts from 2.

Ø For this logic we are ending/stopping/taking break at any of these 2 conditions.


• When for loop condition becomes false, at that time 'i' value becomes given number(i=num).
• At the time of execute "break" (It means a factor identified for a given number), at that time
'i' value becomes less than given number(i<num).

import java.util.Scanner;

public class PrimeNumberWithOutCountFirst50 {

public static void main(String[] args) {


Scanner obj=new Scanner(System.in);

System.out.println("Enter the Number");


int i=2,j,n=obj.nextInt(); //Taking input from user (n).

while(n!=0) //Iterating a Loop until Given Number becomes 0.


{
for(j=2;j<=i;j++) //Iterating the loop from 2 to the i value.
{
if(i%j==0) //Checking i value is perfectly divides with j or not.
{
break; //If-condition satisfies then above loop will breaks he
forcibly.
}//end of if-condition.
}//end of for loop.

if(j==i) //Checking i value is equal to j value or not.


{
System.out.println(i); //Printing i value.
n--; //Decrementing n value by 1 due to identify of Prime
Number.
}//end of if-condition.
i++; //Incrementing i value by 1.

}//end of while loop.

}//end of Main Method.

}//end of Class.

Nearest Prime Number:-


Q) Develop a Program to print the nearest Prime Number of a Given Number?

• To Find and Print the nearest Prime Number of a Given Number, follow the below logic and steps.
• Input:- One Integer value (n).
• Output:- Print the Nearest Prime Number.
• Logic:-
o Find the Immediate Prime number after the Given number.
o Find the Immediate Prime number before the Given number.
o Check the shortest difference and print respective Prime Number.
o If same Difference then Print both.

Step:-1 Take the Input from user (n).


Step:-2 Declare a variables value as afterN and before.
Step:-3 Assign a next value of given number to afterN variable.
Step:-4 Divides the afterN value by i value.
Step:-5 If remainder is 0 then goto step-8, else goto step-6.
Step:-6 Increment the i value by 1.
Step:-7 Repeat the steps from 4 to 6 until i value reaches afterN value.
Step:-8 Now Check the i value is equal to afterN value or not.
Step:-9 If equal then goto step:-12, else goto step-10.
Step:-10 Increment the afterN value by 1.
Step:-11 Repeat the steps from 4 to 10.
Step:-12 Take a 'break' forcefully.
Step:-13 Assign a before value of given number to beforeN variable.
Step:-14 Divides the beforeN value by i value.
Step:-15 If remainder is 0 then goto step-18, else goto step-16.
Step:-16 Increment the i value by 1.
Step:-17 Repeat the steps from 14 to 16 until i value reaches beforeN value.
Step:-18 Now Check the i value is equal to beforeN value or not.
Step:-19 If equal then goto step:-22, else goto step-20.
Step:-20 Increment the beforeN value by 1.
Step:-21 Repeat the steps from 14 to 20.
Step:-22 Take a 'break' forcefully.
Step:-23 Now check for which is minimum distance from n with beforeN or AfterN and Print that number.
Step:-24 If Both the Distance is same then print both the numbers.

Note:-
• Here j value starts from 2.
• For this logic we are ending/stopping/taking break at any of these 2 conditions.
o when for loop condition becomes false, at that time 'i' value becomes given number(i=num).
o At the time of execute "break" (It means a factor identified for a given number), at that
time 'i' value becomes less than given number(i<num).

import java.util.Scanner;

public class NearestPrimeNumber {

public static void main(String[] args) {


Scanner obj=new Scanner(System.in);

System.out.println("Enter the Number");


int i,beforeN,afterN,n=obj.nextInt(); //Taking input from user (n).

afterN=n+1; //Assign the next value of given value (n+1) to the


variable afterN.
while(true) //Iterate a Loop to infinity times we will break this
once we found Prime Number.
{
for(i=2;i<=afterN;i++) //Iterate a loop from 2 to the afterN Value.
{
if(afterN%i==0) //Check afterN value Divides with i value or not.
break; //Forcefully terminates the i-for loop.
}//end of i-for loop.
if(afterN==i) //Checking i value and afterN value is equal or not.
break; //Forcefully terminates the while loop.
else
afterN++; //Increment the afterN value by 1.
}//end of While loop.

beforeN=n-1; //Assign the before value of given value (n-1) to the


variable beforeN.
while(true) //Iterate a Loop to infinity times we will break this
once we found Prime Number.
{
for(i=2;i<=beforeN;i++) //Iterate a loop from 2 to the beforeN Value.
{
if(beforeN%i==0) //Check beforeN value Divides with i value or not.
break; //Forcefully terminates the i-for loop.
}//end of i-for loop.
if(beforeN==i) //Checking i value and beforeN value is equal or not.
break; //Forcefully terminates the while loop.
else
beforeN--; //Decrement the afterN value by 1.
}//end of while loop.

if(n-beforeN>afterN-n) //Checking the differences


System.out.println(afterN);
else if(n-beforeN<afterN-n)
System.out.println(beforeN);
else
System.out.println(afterN+" "+beforeN);

}//end of Main Method.

}//end of Class.
1) Arithmetic Progression.
2) nth Term in Arithmetic Progression(By Using Formula)
3) nth Term Arithmetic Progression(Without using formula)
4) Arithmetic Progression(By using formula)
5) Arithmetic Progression(Without using formula)
6) Geometric Progression
7) Harmonic Progression
8) LCM of two Numbers
9) LCM of three Numbers
10) GCD of two Numbers
11) GCD of three Numbers
12) Decimal to Binary Number
Arithmetic Progression:-
Q) Write a Program to Print first n Numbers by taking input of 1st term(a), common difference(d) and No
of terms(n) in the Arithmetic progression series?

• To Find and Print the Arithmetic progression series of the following Given inputs, follow the
below logic and steps.
• Input:-
o First Line Consists of One Integer value (a).
o Second Line Consists of One Integer value (d).
o Third Line Consists of One Integer value (n).
• Output:- Print the Arithmetic progression series.
• Logic:- The Series of Arithmetic Progression is like
è a , a+d , a+2d , a+3d , a+4d , a+5d ,...........
=a , a+(1*d) , a+(2*d) , a+(3*d) , a+(4*d) , a+(5*d) ,...........
=a+(0*d) , a+(1*d) , a+(2*d) , a+(3*d) , a+(4*d) , a+(5*d) ,...........

Step:-1 Take the Input from user (a).


Step:-2 Take the Input from user (d).
Step:-3 Take the Input from user (n).
Step:-4 Multiply the i value with common difference(d) and add the First term in the series(a) and
then Print.
Step:-5 Increment the i value by 1.
Step:-6 Repeat the steps from 4 to 5 until i value reaches before value of Given Number(n).

Note:-
• a-->First term in Arithmetic Progression.
• d-->Common Difference.
• n-->No of Terms.
• Here i value starts from 0.

import java.util.Scanner;

public class ArithmeticProgression {


public static void main(String[] args) {
Scanner obj=new Scanner(System.in);

System.out.println("Enter a Value");
int a=obj.nextInt(); //Taking input from user (Starting value(a)).

System.out.println("Enter d value");
int d=obj.nextInt(); //Taking input from user (Difference value(d)).

System.out.println("Enter n value");
int n=obj.nextInt(); //Taking input from user (No Of Terms(n)).

for(int i=0;i<n;i++) //Iterating loop from 0 to before number of n


value(nothing but n Iterations).
{
System.out.println(a+(i*d)); //Printing the terms.
}//end of loop.

}//end of Main Method.

}//end of Class.
Tracing of Code:- Tracing of Code:-
Input:- 7 2 9 Input:- 2 4 6
Output:- 7 9 11 13 15 17 19 21 23 Output:- 2 6 10 14 18 22
a d n i a+(i*d) output a d n i a+(i*d) output
7 2 9 2 4 6
0 7+(0*2)=7 7 0 2+(0*4)=2 2
1 7+(1*2)=9 9 1 2+(1*4)=6 6
2 7+(2*2)=11 11 2 2+(2*4)=10 10
3 7+(3*2)=13 13 3 2+(3*4)=14 14
4 7+(4*2)=15 15 4 2+(4*4)=18 18
5 7+(5*2)=17 17 5 2+(5*4)=22 22
6 7+(6*2)=19 19 6àLoop Breaks here
7 7+(7*2)=21 21
8 7+(8*2)=23 23
9àLoop Breaks here
nth Term in Arithmetic Progression:-(By Using Formula)
Q) Find the nth term value in the Arithmetic progression series by taking input of 1st term(a), common
difference(d) and nth term that has to be found? (By using formula)

• To Find and Print the Arithmetic progression series of the following Given inputs, follow the
below logic and steps.
• Input:-
o First Line Consists of One Integer value (a).
o Second Line Consists of One Integer value (d).
o Third Line Consists of One Integer value (n).
• Output:- Print the nth term of Arithmetic progression series.
• Logic:- The nth term of Arithmetic Progression by using formula
formula:- a+((n-1)*d).

Step:-1 Take the Input from user (a).


Step:-2 Take the Input from user (d).
Step:-3 Take the Input from user (n).
Step:-4 Find and Print the value of a+((n-1)*d).

Note:-
• a-->First term in Arithmetic Progression.
• d-->Common Difference.
• n-->No of Terms.

import java.util.Scanner;

public class nthTermOfArithmeticProgression {

public static void main(String[] args) {


Scanner obj=new Scanner(System.in);
System.out.println("Enter a Value");
int a=obj.nextInt(); //Taking input from user (Starting value(a)).

System.out.println("Enter d value");
int d=obj.nextInt(); //Taking input from user (Difference value(d)).

System.out.println("Enter n value");
int n=obj.nextInt(); //Taking input from user (No Of Terms(n)).

System.out.println(a+((n-1)*d)); //Printing nth term by using formula.

}//end of Main Method.

}//end of Class.

Tracing of Code:- Tracing of Code:-


Input:- 7 2 9 Input:- 2 4 6
Output:- 23 Output:- 22
a d n a+((n-1)*d) output a d n a+((n-1)*d) output
7 2 9 2 4 6
7+(9-1)*2=23 23 2+(6-1)*4=22 22

nth Term Arithmetic Progression:-(Without using formula)


Q) Find the nth term value in the Arithmetic progression series by taking input of 1st term(a), common
difference(d) and nth term that has to be found? (Without using formula)

• To Find and Print the Arithmetic progression series of the following Given inputs, follow the
below logic and steps.
• Input:-
o First Line Consists of One Integer value (a).
o Second Line Consists of One Integer value (d).
o Third Line Consists of One Integer value (n).
• Output:- Print the nth term of Arithmetic progression series.
• Logic:-
o The nth term of Arithmetic Progression Without Using Formula.
o Check n value is greater than or equal to 1 or not if yes then add a value to sum variable.
o Now add common difference value by n-1 times to sum variable.

Step:-1 Take the Input from user (a).


Step:-2 Take the Input from user (d).
Step:-3 Take the Input from user (n).
Step:-4 Declare and Initialize the sum variable as 0.
Step:-5 Check n value is Greater than or equal to 1, if yes goto step-6 else goto step-10.
Step:-6 Add the first term value(a) with sum value.
Step:-7 Add the common difference value(d) with sum value.
Step:-8 Increment the i value by 1;
Step:-9 Repeat the steps from 7 to 8 Until i value reaches Given number(n).
Step:-10 Finally Print sum value.

Note:-
• a-->First term in Arithmetic Progression.
• d-->Common Difference.
• n-->No of Terms.
• Here i value starts from 2.

import java.util.Scanner;

public class nthTermOfArithmeticProgressionWithoutFormula {

public static void main(String[] args) {

Scanner obj=new Scanner(System.in);

System.out.println("Enter a Value");
int a=obj.nextInt(); //Taking input from user (Starting value(a)).

System.out.println("Enter d value");
int d=obj.nextInt(); //Taking input from user (Difference value(d)).
System.out.println("Enter n value");
int n=obj.nextInt(); //Taking input from user (No Of Terms(n)).

int sum=0; //Declare and Initialize the variable sum as 0.


if(n>=1) //If n value is greater than or equal to 1 then add a
value to sum value.
sum=sum+a; //Adding a value to sum value and storing in sum variable.

for(int i=2;i<=n;i++) //Iterating a loop from 2 to the user given number(n).


sum=sum+d; //Adding d value to sum value and storing in sum variable.

System.out.println(sum); //Print the sum value.

}//end of Main Method.

}//end of Class.

Tracing of Code:- Tracing of Code:-


Input:- 7 2 9 Input:- 2 4 6
Output:- 23 Output:- 22
a d n i sum output a d n i sum output
7 2 9 0 2 4 6 0
0+7=7 0+2=2
2 7+2=9 9 2 2+4=6 6
3 9+2=11 11 3 6+4=10 10
4 11+2=13 13 4 10+4=14 14
5 13+2=15 15 5 14+4=18 18
6 15+2=17 17 6 18+4=22 22
7 17+2=19 19 7àLoop Breaks here
8 19+2=21 21
9 21+2=23 23 Finally Print the sum value.
10àLoop Breaks here

Finally Print the sum value.


Arithmetic Progression:- (By using formula)
Q) Write a Program to Print sum of the first n terms by taking input of 1st term(a), common
difference(d) and No of terms(n) in the Arithmetic progression series? (By using formula)

• To Find and Print the sum of terms in Arithmetic progression series of the following Given inputs
by using formula, follow the below logic and steps.
• Input:-
o First Line Consists of One Integer value (a).
o Second Line Consists of One Integer value (d).
o Third Line Consists of One Integer value (n).
• Output:- Print the Sum of terms in Arithmetic progression series.
• Logic:- The Sum of Arithmetic Progression term by using formula

sum=(n/2)*(a+(a+((n-1)*d)))

=(n/2)*(2a+((n-1)*d))

Step:-1 Take the Input from user (a).


Step:-2 Take the Input from user (d).
Step:-3 Take the Input from user (n).
Step:-4 Declare and Initialize the sum variable as 0.
Step:-5 Find the sum value by executing formula.
Step:-6 Print the sum value.

Note:-
• a-->First term in Arithmetic Progression.
• d-->Common Difference.
• n-->No of Terms.
• Taking few variables as data type of float to get decimal as output/store.

import java.util.Scanner;

public class SumOfTermsInArithmeticProgression {


public static void main(String[] args) {

Scanner obj=new Scanner(System.in);

System.out.println("Enter a Value");
int a=obj.nextInt(); //Taking input from user (Starting value(a)).

System.out.println("Enter d value");
int d=obj.nextInt(); //Taking input from user (Difference value(d)).

System.out.println("Enter n value");
float n=obj.nextInt(); //Taking input from user (No Of Terms(n)).

float sum=0; //Declare and Initialize the sum variable as 0.

sum=(n/2)*(a+(a+((n-1)*d))); //Calculating sum value(Finding the sum value by


using formula).

System.out.println(sum); //Printing sum value.

}//end of Main Method.

}//end of Class.

Tracing of Code:- Tracing of Code:-


Input:- 7 2 9 Input:- 2 4 6
Output:- 135 Output:- 72

a d n sum output a d n sum output


7 2 9 0 2 4 6 0
(9/2)*(7+(7+(9-1)*2)) 135 (6/2)*(2+(2+(6-1)*4)) 72
Arithmetic Progression:- (Without using formula)
Q) Write a Program to Print sum of the first n terms by taking input of 1st term(a), common
difference(d) and No of terms(n) in the Arithmetic progression series? (Without using formula)

To Find and Print the sum of the terms in Arithmetic progression series of the following Given inputs
without using formula, follow the below logic and steps.
• Input:-
o First Line Consists of One Integer value (a).
o Second Line Consists of One Integer value (d).
o Third Line Consists of One Integer value (n).
• Output:- Print the sum value of terms in Arithmetic progression series.
• Logic:- The Series of Arithmetic Progression is like
a , a+d , a+2d , a+3d , a+4d , a+5d ,...........
a , a+(1*d) , a+(2*d) , a+(3*d) , a+(4*d) , a+(5*d) ,...........
a+(0*d) , a+(1*d) , a+(2*d) , a+(3*d) , a+(4*d) , a+(5*d) ,...........
sum=(a+(0*d))+(a+(1*d))+(a+(2*d))+(a+(3*d))+(a+(4*d))+(a+(5*d))+...........

Step:-1 Take the Input from user (a).

Step:-2 Take the Input from user (d).

Step:-3 Take the Input from user (n).

Step:-4 Declare and Initialize the sum value as 0.

Step:-5 Add the Next term value to variable sum(like, sum=sum+(a+(i*d))).

Step:-6 Increment the i value by 1.

Step:-7 Repeat the steps from 5 to 6 until i value reaches before value of Given Number(n).

Step:-8 Finally Print the sum value.


Note:-
• a-->First term in Arithmetic Progression.
• d-->Common Difference.
• n-->No of Terms.
• Here i value starts from 0.

import java.util.Scanner;
public class SumOfTermsInArithmeticProgressionWithOutUsingFormula {

public static void main(String[] args) {


Scanner obj=new Scanner(System.in);

System.out.println("Enter a Value");
int a=obj.nextInt(); //Taking input from user (Starting value(a)).

System.out.println("Enter d value");
int d=obj.nextInt(); //Taking input from user (Difference value(d)).

System.out.println("Enter n value");
int n=obj.nextInt(); //Taking input from user (No Of Terms(n)).

int sum=0; //Declare and Initialize the sum value as 0.


for(int i=0;i<n;i++) //Iterating loop from 0 to before number of n
value(nothing but n Iterations).
{
sum=sum+(a+(i*d)); //Adding the next term value to variable sum.
}//end of for loop.

System.out.println(sum); //Printing the sum value.

}//end of Main Method.

}//end of Class.
Tracing of Code:- Tracing of Code:-
Input:- 7 2 9 Input:- 2 4 6
Output:- 135 Output:- 72
a d n i sum a d n i sum
7 2 9 0 2 4 6 0
0 0+(7+(0*2))=7 0 0+(2+(0*4))=2
1 7+(7+(1*2))=16 1 2+(2+(1*4))=8
2 16+(7+(2*2))=27 2 8+(2+(2*4))=18
3 27+(7+(3*2))=40 3 18+(2+(3*4))=32
4 40+(7+(4*2))=55 4 32+(2+(4*4))=50
5 55+(7+(5*2))=72 5 50+(2+(5*4))=72
6 72+(7+(6*2))=91 6àLoop Breaks here
7 91+(7+(7*2))=112
8 122+(7+(8*2))=135 Finally Print the sum value.
9àLoop Breaks here

Finally Print the sum value.

Geometric Progression:-
Q) Write a Program to Print first n Numbers by taking input of 1st term(a), common difference(d) and No
of terms(n) in the geometric progression series?

• To Find and Print the Geometric progression series of the following Given inputs, follow the below
logic and steps.
• Input:-
o First Line Consists of One Integer value (a).
o Second Line Consists of One Integer value (d).
o Third Line Consists of One Integer value (n).
• Output:- Print the Geometric progression series.
• Logic:- The Series of Geometric Progression is like
a , a*d , a*d^2 , a*d^3 , a*d^4 , a*d^5 ,...........
a , a*(d) , a*(d*d) , a*(d*d*d) , a*(d*d*d*d) , a*(d*d*d*d*d) ,...........
Step:-1 Take the Input from user (a).
Step:-2 Take the Input from user (d).
Step:-3 Take the Input from user (n).
Step:-4 Declare and Initialize the sum value as 0.
Step:-5 Add the first term(a) value to sum variable.
Step:-6 Check i value is greater than j value then got step-7 else got step-10.
Step:-7 Multiply the common difference value(d) with sum value and store in sum variable.
Step:-8 Increment the j value by 1.
Step:-9 Repeat the steps from 6 to 8 until j value reaches i value.
Step:-10 Print the sum value.
Step:-11 Increment the i value by 1.
Step:-12 Repeat the steps from 4 to 11 until i value reaches no of terms(n).

Note:-
• a-->First term in Geometric Progression.
• d-->Common Difference.
• n-->No of Terms.
• Here i value starts from 0.
• Here j value starts from 0.

import java.util.Scanner;

public class GeometricProgression {

public static void main(String[] args) {


Scanner obj=new Scanner(System.in);

System.out.println("Enter a Value");
int a=obj.nextInt(); //Taking input from user (Starting value(a)).

System.out.println("Enter d value");
int d=obj.nextInt(); //Taking input from user (Difference value(d)).

System.out.println("Enter n value");
int n=obj.nextInt(); //Taking input from user (No Of Terms(n)).
for(int i=0;i<n;i++) //Iterating loop from 0 to n(n times).
{
int sum=0; //Declare and Initialize the sum variable as 0.
sum=sum+a; //Adding the first term value(a) to sum variable.
for(int j=0;j<i;j++) //Iterating the loop from 0 to i(i times).
{
sum=sum*d; //Multiplying the common difference value with sum
value and storing in sum variable.
}//end of j-for loop.
System.out.println(sum); //Printing the sum value.
}//end of i-for loop.

}//end of Main Method.

}//end of Class.

Tracing of Code:-
Input:- 7 2 9
Output:- 7 14 28 56 112 224 448 896 1792
a d n i j sum output
7 2 9
0 0
0+7=7
0àLoop Breaks here 7
1 0
0+7=7
0 7*2=14
1àLoop Breaks here 14
2 0
0+7=7
0 7*2=14
1 14*2=28
2àLoop Breaks here 28
3 0
0+7=7
0 7*2=14
1 14*2=28
2 28*2=56
3àLoop Breaks here 56
4 0
0+7=7
0 7*2=14
1 14*2=28
2 28*2=56
3 56*2=112
4àLoop Breaks here 112
5 0
0+7=7
0 7*2=14
1 14*2=28
2 28*2=56
3 56*2=112
4 112*2=224
5àLoop Breaks here 224
6 0
0+7=7
0 7*2=14
1 14*2=28
2 28*2=56
3 56*2=112
4 112*2=224
5 224*2=448
6àLoop Breaks here 448
7 0
0+7=7
0 7*2=14
1 14*2=28
2 28*2=56
3 56*2=112
4 112*2=224
5 224*2=448
6 448*2=896
7àLoop Breaks here 896
8 0
0+7=7
0 7*2=14
1 14*2=28
2 28*2=56
3 56*2=112
4 112*2=224
5 224*2=448
6 448*2=896
7 896*2=1792
8àLoop Breaks here 896
9àLoop Breaks here

Tracing of Code:-
Input:- 2 4 6
Output:- 2 8 32 128 512 2048
a d n i j sum output
2 4 6
0 0
0+2=2
0àLoop Breaks here 2
1 0
0+2=2
0 2*4=8
1àLoop Breaks here 8
2 0
0+2=2
0 2*4=8
1 8*4=32
2àLoop Breaks here 32
3 0
0+2=2
0 2*4=8
1 8*4=32
2 32*4=128
3àLoop Breaks here 128
4 0
0+2=2
0 2*4=8
1 8*4=32
2 32*4=128
3 128*4=512
4àLoop Breaks here 512
5 0
0+2=2
0 2*4=8
1 8*4=32
2 32*4=128
3 128*4=512
4 512*4=2048
5àLoop Breaks here 2048
6àLoop Breaks here

Harmonic Progression:-
Q) Write a Program to Print first n Numbers by taking input of 1st term(a), common difference(d) and No
of terms(n) in the harmonic progression series?

• To Find and Print the Harmonic progression series of the following Given inputs, follow the below
logic and steps.
• Input:-
o First Line Consists of One Integer value (a).
o Second Line Consists of One Integer value (d).
o Third Line Consists of One Integer value (n).
• Output:- Print the Harmonic progression series.
• Logic:- The Series of Harmonic Progression is like
Arithmetic Progression àa , a+d , a+2d , a+3d , a+4d , a+5d ,...........
Harmonic Progression à1/a , 1/(a+d) , 1/(a+2d) , 1/(a+3d) , 1/(a+4d) , 1/(a+5d) ,...........
1/a , 1/(a+(1*d)) , 1/(a+(2*d)) , 1/(a+(3*d)) , 1/(a+(4*d)) , 1/(a+(5*d)) ,……
1/(a+(0*d)) , 1/(a+(1*d)) , 1/(a+(2*d)) , 1/(a+(3*d)) , 1/(a+(4*d)) ,
1/(a+(5*d)) ,...........

ü Harmonic Progression is nothing but reciprocal of Arithmetic Progression.

Step:-1 Take the Input from user (a).


Step:-2 Take the Input from user (d).
Step:-3 Take the Input from user (n).
Step:-4 Multiply the i value with common difference(d) and add the First term in the series(a) and
Store the value in 'ap' variable.
Step:-5 Print the Reciprocal of 'ap' value.
Step:-6 Increment the i value by 1.
Step:-7 Repeat the steps from 4 to 7 until i value reaches before value of Given Number(n).

Note:-
• a-->First term in Harmonic Progression.
• d-->Common Difference.
• n-->Number of Terms.
• Here i value starts from 0.

import java.util.Scanner;

public class HarmonicProgression {

public static void main(String[] args) {


Scanner obj=new Scanner(System.in);

System.out.println("Enter a Value");
int a=obj.nextInt(); //Taking input from user (Starting value(a)).

System.out.println("Enter d value");
int d=obj.nextInt(); //Taking input from user (Difference value(d)).

System.out.println("Enter n value");
int n=obj.nextInt(); //Taking input from user (No Of Terms(n)).
for(int i=0;i<n;i++) //Iterating loop from 0 to before number of n
value(nothing but n Iterations).
{
float ap=a+(i*d); //Finding Next Arithmetic Progression term.
System.out.println(1/ap); //Find Harmonic value and Printing.
}//end of loop.

}//end of Main Method.

}//end of Class.

Tracing of Code:- Tracing of Code:-


Input:- 7 2 9 Input:- 2 4 6
Output:- Output:- 0.5 0.17 0.10 0.07 0.06 0.05
0.14 0.11 0.09 0.08 0.07 0.06 0.053 0.048 0.044 a d n i ap=a+(i*d) 1/ap output
a d n i ap=a+(i*d) 1/ap output 2 4 6
7 2 9 0 2+(0*4)=2 1/2 0.5
0 7+(0*2)=7 1/7 0.14 1 2+(1*4)=6 1/6 0.17
1 7+(1*2)=9 1/9 0.11 2 2+(2*4)=10 1/10 0.10
2 7+(2*2)=11 1/11 0.09 3 2+(3*4)=14 1/14 0.07
3 7+(3*2)=13 1/13 0.08 4 2+(4*4)=18 1/18 0.06
4 7+(4*2)=15 1/15 0.07 5 2+(5*4)=22 1/22 0.05
5 7+(5*2)=17 1/17 0.06 6àLoop Breaks here
6 7+(6*2)=19 1/19 0.053
7 7+(7*2)=21 1/21 0.048
8 7+(8*2)=23 1/23 0.044
9àLoop Breaks here

LCM of two Numbers:-


Q) Write a Program to Print the Lcm of a given two numbers?

• To Find and Print the LCM of Two Numbers, follow the below logic and steps.
• Input:-
o First Line Consists of One Integer value (a).
o Second Line Consists of One Integer value (b).
• Output:- Print the LCM of two Numbers.
• Logic:- The LCM of two Numbers logic is find big number from given numbers and check it is
multiple or not with a and b if not add same value to itself and again same process until we get
common Multiple.

Step:-1 Take the Input from user (a).


Step:-2 Take the Input from user (b).
Step:-3 Declare and Initialize a 'Max' Variable as biggest value in a and b values.
Step:-4 Declare and Initialize a 't' variable as a Duplicate of max variable.
Step:-5 Now Check max value is exactly divisible by a and b or not, if yes goto step-8 else goto
step-6.
Step:-6 Add the t value to the max variable(max=max+t).
Step:-7 Goto step-5.
Step:-8 Now Print the max value(nothing but LCM).

import java.util.Scanner;

public class LcmofTwoNumbers {

public static void main(String[] args) {


Scanner obj=new Scanner(System.in);

System.out.println("Enter a Value");
int a=obj.nextInt(); //Taking input from user (First value(a)).

System.out.println("Enter b value");
int b=obj.nextInt(); //Taking input from user (Second value(b)).

int max=a>b?a:b; //Finding biggest number by using ternary operators.


int t=max; //Duplicating the max value into t variable.
while(max%a!=0 || max%b!=0) //Checking max value is Perfectly divisible by a and
b or not.
{
max=max+t; //Adding t value to max value.
}//end of loop.

System.out.println(max); //Printing the max value(It is nothing but LCM).

}//end of Main Method.

}//end of Class.

Tracing of Code:- Tracing of Code:-


Input:- 7 2 Input:- 16 24
Output:- 14 Output:- 48
a b max t max%a!=0||max%b!=0 a b max t max%a!=0||max%b!=0
7 2 7 16 24 24
7 24
7+7=14 ßtrue 24+24=48 ßtrue
falseàLoop Breaks here falseàLoop Breaks here
• Finally Print the max value it is nothing but • Finally Print the max value it is nothing but
LCM. LCM.

LCM of three Numbers:-


Q) Write a Program to Print the Lcm of a given three numbers?

• To Find and Print the LCM of Three Numbers, follow the below logic and steps.
• Input:-
o First Line Consists of One Integer value (a).
o Second Line Consists of One Integer value (b).
o Third Line Consists of One Integer value (c).
• Output:- Print the LCM of three Numbers.
• Logic:- The LCM of three Numbers logic is find big number from given numbers and check it is
multiple or not with a , b and c if not add same value to itself and again same process until we
get common Multiple.
Step:-1 Take the Input from user (a).
Step:-2 Take the Input from user (b).
Step:-3 Take the Input from user (c).
Step:-4 Declare and Initialize a 'max' Variable as Finding biggest value from a and b values.
Step:-5 Finding biggest value from max and c values and stores in max variable.
Step:-6 Declare and Initialize a 't' variable as a Duplicate of max variable.
Step:-7 Now Check max value is exactly divisible by a ,b and c values or not, if yes goto step-10
else goto step-8.
Step:-8 Add the t value to the max variable(max=max+t).
Step:-9 Goto step-7.
Step:-10 Now Print the max value(nothing but LCM).

import java.util.Scanner;

public class LcmofThreeNumbers {

public static void main(String[] args) {

Scanner obj=new Scanner(System.in);

System.out.println("Enter a Value");
int a=obj.nextInt(); //Taking input from user (First value(a)).

System.out.println("Enter b value");
int b=obj.nextInt(); //Taking input from user (Second value(b)).

System.out.println("Enter c value");
int c=obj.nextInt(); //Taking input from user (Third value(c)).

int max=a>b?a:b; //Finding biggest number by using ternary operators


in between a and b values.
max=max>c?max:c; //Finding biggest number by using ternary operators
in between max and c values.
int t=max; //Duplicating the max value into t variable.
while(max%a!=0 || max%b!=0 ||max%c!=0) //Checking max value is Perfectly divisible by a , b
and c values or not.
{
max=max+t; //Adding t value to max value.
}//end of loop.

System.out.println(max); //Printing the max value(It is nothing but LCM).

}//end of Main Method.

}//end of Class.

Tracing of Code:- Tracing of Code:-


Input:- 7 2 9 Input:- 2 4 6
Output:- 126 Output:- 12
a b c max t max%a!=0||max%b!=0||max%c!=0 a b c max t max%a!=0||max%b!=0||max%c!=0
7 2 9 2 4 6
9 6
9 6
9+9=18 ßtrue 6+6=12 ßtrue
18+9=27 ßtrue falseàLoop Breaks here
27+9=36 ßtrue
36+9=45 ßtrue • Finally Print the max value it is nothing but
45+9=54 ßtrue LCM.
54+9=63 ßtrue
63+9=72 ßtrue
72+9=81 ßtrue
81+9=90 ßtrue
90+9=99 ßtrue
99+9=108 ßtrue
108+9=117 ßtrue
117+9=126 ßtrue
falseàLoop Breaks here

• Finally Print the max value it is nothing but


LCM.
GCD of two Numbers:-
Q) Write a Program to Print the Gcd of a given two numbers?

• To Find and Print the GCD of Two Numbers, follow the below logic and steps.
• Input:-
o First Line Consists of One Integer value (a).
o Second Line Consists of One Integer value (b).
• Output:- Print the GCD of two Numbers.
• Logic:- The GCD of two Numbers logic is find small number from given numbers and check it is
factor or not for a and b if not decrement min value by 1 and again same process until we get
common Factor.

Step:-1 Take the Input from user (a).

Step:-2 Take the Input from user (b).

Step:-3 Declare and Initialize a 'min' Variable as smallest value in a and b values.

Step:-4 Now Check min value is factor of a and b or not, if yes goto step-7 else goto step-5.

Step:-5 Decrement the min value by 1.

Step:-6 Goto step-4.

Step:-7 Now Print the min value(nothing but GCD).

import java.util.Scanner;

public class GCDofTwoNumbers {

public static void main(String[] args) {


Scanner obj=new Scanner(System.in);
System.out.println("Enter a Value");
int a=obj.nextInt(); //Taking input from user (First value(a)).

System.out.println("Enter b value");
int b=obj.nextInt(); //Taking input from user (Second value(b)).

int min=a<b?a:b; //Finding smallest number by using ternary operators.


while(a%min!=0 || b%min!=0) //Checking min value is factor of a and b or not.
{
min--; //Decrementing minimum value by 1.
}//end of loop.

System.out.println(min); //Printing the min value(It is nothing but GCD).

}//end of Main Method.

}//end of Class.

Tracing of Code:- Tracing of Code:-


Input:- 7 9 Input:- 16 24
Output:- 1 Output:- 48
a b min a%min!=0||b%min!=0 a b min a%max!=0||b%max!=0
7 9 7 16 24 16
6 ßtrue 15 ßtrue
5 ßtrue 14 ßtrue
4 ßtrue 13 ßtrue
3 ßtrue 12 ßtrue
2 ßtrue 11 ßtrue
1 falseàLoop Breaks here 10 ßtrue
9 ßtrue
• Finally Print the min value it is nothing but 8 falseàLoop Breaks here
GCD.
• Finally Print the min value it is nothing but
GCD.
GCD of three Numbers:-
Q) Write a Program to Print the Gcd of a given three numbers?

• To Find and Print the GCD of Three Numbers, follow the below logic and steps.
• Input:-
o First Line Consists of One Integer value (a).
o Second Line Consists of One Integer value (b).
o Third Line Consists of One Integer value (c).
• Output:- Print the GCD of three Numbers.
• Logic:- The GCD of three Numbers logic is find min number from given numbers and check it is
factor or not for a , b and c if not Decrement the min value by 1 and again same process until we
get common Factor.

Step:-1 Take the Input from user (a).


Step:-2 Take the Input from user (b).
Step:-3 Take the Input from user (c).
Step:-4 Declare and Initialize a 'min' Variable as Finding smallest value from a and b values.
Step:-5 Finding smallest value from min and c values and stores in min variable.
Step:-6 Now Check min value is exactly divisible by a ,b and c values or not, if yes goto step-9 else
goto step-7.
Step:-7 Decrement min value by 1(min--).
Step:-8 Goto step-6.
Step:-9 Now Print the min value(nothing but GCD).

import java.util.Scanner;

public class GCDofThreeNumbers {

public static void main(String[] args) {


Scanner obj=new Scanner(System.in);

System.out.println("Enter a Value");
int a=obj.nextInt(); //Taking input from user (First value(a)).
System.out.println("Enter b value");
int b=obj.nextInt(); //Taking input from user (Second value(b)).

System.out.println("Enter c value");
int c=obj.nextInt(); //Taking input from user (Third value(c)).

int min=a<b?a:b; //Finding smallest number by using ternary operators


in between a and b values.
min=min<c?min:c; //Finding smallest number by using ternary operators
in between min and c values.
while(a%min!=0 || b%min!=0 || c%min!=0) //Checking min value is a factor of a , b and c
values or not.
{
min--; //Decrementing min value by 1.
}//end of loop.

System.out.println(min); //Printing the min value(It is nothing but GCD).

}//end of Main Method.

}//end of Class.
Tracing of Code:- Tracing of Code:-
Input:- 7 11 9 Input:- 8 12 20
Output:- 1 Output:- 4
a b c min max%a!=0||max%b!=0||max%c!=0 a b c max max%a!=0||max%b!=0||max%c!=0
7 11 9 8 12 20
7 8
6 ßtrue 7 ßtrue
5 ßtrue 6 ßtrue
4 ßtrue 5 ßtrue
3 ßtrue 4 falseàLoop Breaks here
2 ßtrue
1 falseàLoop Breaks here • Finally Print the min value it is nothing but
GCD.
• Finally Print the min value it is nothing but
GCD.
Decimal to Binary Conversion:-
Q) Write a Program to Convert the Decimal Number to Binary Number?

• To Find and Print Decimal to Binary conversion, follow the below logic and steps.
• Input:- One Integer value (n).
• Output:- Print the Binary Numbers.
• Logic:- To convert Decimal value to binary value we have to divide the decimal value with 2 and
have to store the remainder, we have to do this given value becomes 0 then print the remainders in
reverse order.

Step:-1 Take the Input from user (n).


Step:-2 Declare and Initialize a String Variable 's' as nothing/null("").
Step:-3 Divide the given number with 2.
Step:-4 Store the remainder in String variable(concat) before String value(s=rem+s).
Step:-5 Store the quotient in n.
Step:-6 Repeat the steps from 3 to 5 Until given number (n) becomes 0(n>0).
Step:-7 Now Print the String s value(It is nothing but Binary value).

Note:-
• Findout the Difference between s=s+r; and s=r+s if s is String variable and r is int variable.

import java.util.Scanner;

public class DecimalToBinaryConversion {

public static void main(String[] args) {


Scanner obj=new Scanner(System.in);

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


int n=obj.nextInt(); //Taking input from user.

String s=""; //Declare and Initialize the String variable as nothing/null("").


while(n!=0) //Iterate a loop until n value becomes 0.
{
int rem=n%2; //Divide n value by 2 and store the remainder in 'rem'.
s=rem+s; //Concat the remainder with String 's' and store in 's' variable.
n=n/2; //Divides the Number by 2.
}//end of loop.
System.out.println(s); //Print the s value(It is nothing but binary value).
}//end of Main Method.

}//end of Class.

Tracing of Code:- Tracing of Code:-


Input:- 88 Input:- 15
Output:- 1011000 Output:- 1111
n rem s n rem s
88 15
44 0 0 7 1 1
22 0 00 3 1 11
11 0 000 1 1 111
5 1 1000 0 1 1111
2 1 11000 àLoop Breaks here
1 0 011000
0 1 1011000 • Finally Print the s value it is nothing but
àLoop Breaks here Binary Number.

• Finally Print the s value it is nothing but


Binary Number.
1) Decimal To Octal Conversion.
2) Decimal To Hexa Decimal Conversion.
3) Binary To Decimal Conversion.
4) Binary To Octal Conversion.
5) Binary To Hexa Decimal Conversion.
6) Octal To Binary Conversion.
7) Octal To Decimal Conversion.
8) Octal To Hexa Decimal Conversion.
9) Hexa Decimal To Binary Conversion.
10) Hexa Decimal To Decimal Conversion.
Decimal To Octal Conversion:-
Q) Develop a Program to Convert the Decimal Number to Octal Number?

• To Find and Print Decimal to Octal conversion, follow the below logic and steps.
• Input:- One Integer value (n).
• Output:- Print the Octal Numbers.
• Logic:- To convert Decimal value to Octal value we have to divide the decimal value with 8 and
have to store the remainder, we have to do this Until given value becomes 0 then print the
remainders in reverse order.

Step:-1 Take the Input from user (n).


Step:-2 Declare and Initialize a String Variable 's' as nothing("").
Step:-3 Divide the given number with 8.
Step:-4 Store the remainder in String variable(concat) before String value(s=rem+s).
Step:-5 Store the quotient in n.
Step:-6 Repeat the steps from 3 to 5 Until given number (n) becomes 0(n>0).
Step:-7 Now Print the String s value(It is nothing but Binary value).

Note:- Find out the Difference between s=s+r and s=r+s if s is String variable and r is int variable.

import java.util.Scanner;

public class DecimalToOctalConversion {

public static void main(String[] args) {

Scanner obj=new Scanner(System.in);

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


int n=obj.nextInt(); //Taking input from user.

String s=""; //Declare and Initialize the String variable as


nothing/null("").
while(n!=0) //Iterate a loop until n value becomes 0.
{
int rem=n%8; //Divide n value by 8 and store the remainder in 'rem'.
s=rem+s; //Concat the remainder with String 's' and store in 's'
variable.
n=n/8; //Divides the Number by 8.
}//end of loop.

System.out.println(s); //Print the s value(It is nothing but Octal value).

}//end of Main Method.

}//end of Class.

Tracing of Code:- Tracing of Code:-


Input:- 88 Input:- 15
Output:- 130 Output:- 17
n rem s n rem s
88 15
11 0 0 1 7 7
1 3 30 0 1 17
0 1 130 àLoop Breaks here
àLoop Breaks here
• Finally Print the s value it is nothing but Octal
• Finally Print the s value it is nothing but Octal Number.
Number.

Decimal To Hexa Decimal Conversion:-


Q) Develop a Program to Convert the Decimal Number to Hexa Decimal Number?

• To Find and Print Decimal to Hexa Decimal conversion, follow the below logic and steps.
• Input:- One Integer value (n).
• Output:- Print the Hexa Decimal Numbers.
• Logic:-
• To convert Decimal value to Hexa Decimal value we have to divide the decimal value with 16.
• Store the remainder.
• If Remainder is 10, 11, 12, 13, 14, 15 then it represent/store it as A, B, C, D, E, F rest of
the remainders save store Directly.
• we have to do this Until given value becomes 0.
• Finally print the remainders in reverse order.

Step:-1 Take the Input from user (n).


Step:-2 Declare and Initialize a String Variable 's' as nothing("").
Step:-3 Divide the given number with 16.
Step:-4 If Remainder is in between 0 to 9 then store the remainder in String variable(concat) before
String value(s=rem+s).
Step:-5 If Remainder is in between 10 to 15 then store the remainders Like A (10), B (11), C(12),
D(13),E (14), F(15) in String variable(concat) before String value(s=rem+s).
Step:-6 Store the quotient in n.
Step:-7 Repeat the steps from 3 to 6 Until given number (n) becomes 0(n>0).
Step:-8 Now Print the String s value(It is nothing but Hexa Decimal value).

Note:- Find out the Difference between s=s+r; and s=r+s if s is String variable and r is int
variable.

import java.util.Scanner;

public class DecimalToHexaDecimalConversion {

public static void main(String[] args) {


Scanner obj=new Scanner(System.in);

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


int n=obj.nextInt(); //Taking input from user.

String s=""; //Declare and Initialize the String variable as nothing/null("").

while(n!=0) //Iterate a loop until n value becomes 0.


{
int rem=n%16; //Divide n value by 16 and store the remainder in 'rem'.
if(rem==10) //Checking remainder is 10 or not.
s='A'+s; //If Remainder is 10 then concat 'A' in String variable s.
else if(rem==11) //Checking remainder is 11 or not.
s='B'+s; //If Remainder is 11 then concat 'B' in String variable s.
else if(rem==12) //Checking remainder is 12 or not.
s='C'+s; //If Remainder is 12 then concat 'C' in String variable s.
else if(rem==13) //Checking remainder is 13 or not.
s='D'+s; //If Remainder is 13 then concat 'D' in String variable s.
else if(rem==14) //Checking remainder is 14 or not.
s='E'+s; //If Remainder is 14 then concat 'E' in String variable s.
else if(rem==15) //Checking remainder is 15 or not.
s='F'+s; //If Remainder is 15 then concat 'F' in String variable s.
else //If Remainder is in between the 0 and 9.
s=rem+s; //Concat the remainder with String 's' and store in 's' variable.
n=n/16; //Divides the Number by 16.

}//end of loop.

System.out.println(s); //Print the s value (It is nothing but Hexa Decimal value).

}//end of Main Method.

}//end of Class.

Tracing of Code:- Tracing of Code:-


Input:- 165 Input:- 233
Output:- A5 Output:- E9
n rem s n rem s
165 233
10 5 à5 5 14 9 à9 9
0 10 àA A+5=A5 0 14 àE E+9=E9
àLoop Breaks here àLoop Breaks here

• Finally Print the s value it is nothing but Hexa • Finally Print the s value it is nothing but Hexa
Decimal Number. Decimal Number.
Binary To Decimal Conversion:-
Q) Develop a Program to Convert the Binary Number to Decimal Number?

• To Find and Print Binary to Decimal conversion, follow the below logic and steps.
• Input:- One String value (s).
• Output:- Print the Decimal Numbers.
• Logic:- To convert Binary value to Decimal value we have to do this below process
If Input is 1011000
We have to take a digit from Right to Left.
0*(2^0)+0*(2^1)+0*(2^2)+1*(2^3)+1*(2^4)+0*(2^5)+1*(2^6)
=0*(1)+0*(2)+0*(4)+1*(8)+1*(16)+0*(32)+1*(64)
=0+0+0+8+16+0+64
=88

Step:-1 Take the Input(String) from user (s).


Step:-2 Declare and Initialize a Variable 'n' as 0.
Step:-3 Declare and Initialize a Variable 'power' as 0(It will helps us to find power value of base
value).
Step:-4 Taking a character from the Given String(Right to Left) in char variable 'c'.
Step:-5 Checking char variable 'c' is 1 or not, if 1 goto step-6 else goto step-14.
Step:-6 Call a User Defined Method(findPower(int b,int power)) to find the Digit respect power value
by passing a base value(2) and power value(Variable power).
Step:-7 In User Defined Method(findPower) Declare and Initialize a variable 'value' as 1.
Step:-8 In User Defined Method(findPower) next step is multiply the base value with variable 'value'
and store the result in 'value' variable.
Step:-9 In User Defined Method(findPower) Increment the j value by 1.
Step:-10 Repeat the steps from 8 to 9 Until the j value reaches power value(j<=power).
Step:-11 In User Defined Method(findPower) Return the Integer variable 'value' to the Main Method from
User Defined method.
Step:-12 Now Multiply with Digit value(nothing but 1).
Step:-13 Add the result to the n variable.
Step:-14 Decrement i value by 1.
Step:-15 Increment power value by 1.
Step:-16 Repeat the steps from 4 to 15 Until i value reaches -1(i>=0).
Step:-17 Now Print the n value(It is nothing but Decimal value).

Note:-
• Here i starts from length of the Given String minus 1(i=s.length()-1).
• Here j starts from 1.

import java.util.Scanner;

public class BinaryToDecimalConversion {

public static void main(String[] args) {


Scanner obj=new Scanner(System.in);

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


String s=obj.next(); //Taking input from user.

int n=0; //Declare and Initialize the 'n' variable as 0.


int power=0; //Declare and Initialize the 'power' variable as 0(It is
helpful to calculate the power value for each and every
character).
for(int i=s.length()-1;i>=0;i--,power++) //Iterating a loop to take one by one character
from the given String from last character
to first character.
{
char c=s.charAt(i); //Taking a Single character into a char variable c.
if(c=='1') //Checking that char variable value is 1 or not.
{
n=n+(1*findPower(2,power)); //Adding the value to variable n, after power
value(This value is finding by calling a
user Defined Method) is multiplying with
digit value.
}//end of if-condition.
}//end of loop.
System.out.println(n); //Printing the n value(It is nothing but Decimal value).

}//end of Main Method.

public static int findPower(int base,int p) //This Method have to return the power
value and return type is int.
{
int value=1; //Declare and Initialize the 'value' variable as 1(This
variable is used to store the power value).
for(int j=1;j<=p;j++) //Iterating the loop for p(power) times.
{
value=value*base; //Multiplying the base with value and storing the result in
'value' variable.
}//end of Loop.
return value; //Here returns the int value to main method from User defined
Method.
}//end of user defined method (findPower).

}//end of Class.

Tracing of Code:-
Input:- 1011000
Output:- 88
s n power i c value j base p
1011000 0 0
6 0
1 5 0
2 4 0
3 3 1 àfindPower(2,power) 2 3
1
1*2=2 ß1
2*2=4 ß2
4*2=8 ß3
4à Loop Breaks here then return ‘value’.
0+(1*8)=8 4 2 1 àfindPower(2,power) 2 4
1
1*2=2 ß1
2*2=4 ß2
4*2=8 ß3
8*2=16 ß4
5à Loop Breaks here then return ‘value’.
8+(1*16)=24 5 1 0
6 0 1 àfindPower(2,power) 2 6
1
1*2=2 ß1
2*2=4 ß2
4*2=8 ß3
8*2=16 ß4
16*2=32 ß5
32*2=64 ß6
7à Loop Breaks here then return ‘value’.
24+(1*64)=88 7 -1àLoop Breaks here

• Finally Print the n value it is nothing but Decimal Number.

Tracing of Code:-
Input:- 1111
Output:- 15
s n power i c value j base p
1111 0 0
3 1àfindPower(2,power) 2 0
1
1à Loop Breaks here then return ‘value’.
0+1=1 1 2 1àfindPower(2,power) 2 1
1
1*2=2 ß1
2à Loop Breaks here then return ‘value’.
1+2=3 2 1 1àfindPower(2,power) 2 2
1
1*2=2 ß1
2*2=4 ß2
3à Loop Breaks here then return ‘value’.
3+4=7 3 0 1àfindPower(2,power) 2 3
1
1*2=2 ß1
2*2=4 ß2
4*2=8 ß3
4à Loop Breaks here then return ‘value’.
7+8=15 4 -1àLoop Breaks here

• Finally Print the n value it is nothing but Decimal Number.

Binary To Octal Conversion:-


Q) Develop a Program to Convert the Binary Number to Octal Number?

• To Find and Print Binary to Octal conversion, follow the below logic and steps.
• Input:- One String value (binary).
• Output:- Print the Octal Numbers.
• Logic:- To convert Binary value to Octal value,
• First we have to convert the Binary value to Decimal value, then Decimal value to Octal
value.
• We have one more way also it is nothing but Taking every three values(from right to left) and
finding the decimal for those every three binary values then we get the final value(But it is
little bit difficult to understand the logic).

Way:-1 Binary value to Decimal value, Decimal value to Octal value

If Input is 1011000
part1: Binary value to Decimal value
We have to take a digit from Right to Left.
0*(2^0)+0*(2^1)+0*(2^2)+1*(2^3)+1*(2^4)+0*(2^5)+1*(2^6)
=0*(1)+0*(2)+0*(4)+1*(8)+1*(16)+0*(32)+1*(64)
=0+0+0+8+16+0+64
=88
part2: Decimal value to Octal Value
88-->130

Way:-2 Binary value to Octal value

If Input is 1011000
000-->0
011-->3
1-->1
Final value is 130

Step:-1 Take the Input(String) from user (binary).


Step:-2 Declare and Initialize a Variable 'dec' as 0.
Step:-3 Declare and Initialize a Variable 'power' as 0(It will helps us to find power value of base
value).
Step:-4 Taking a character from the Given String(Right to Left) in char variable 'c'.
Step:-5 Checking char variable 'c' is 1 or not, if 1 goto step-6 else goto step-14.
Step:-6 Call a User Defined Method(findPower(int b,int power)) to find the Digit respect power value
by passing a base value(2) and power value(Variable power).
Step:-7 In User Defined Method(findPower) Declare and Initialize a variable 'value' as 1.
Step:-8 In User Defined Method(findPower) next step is multiply the base value with variable 'value'
and store the result in 'value' variable.
Step:-9 In User Defined Method(findPower) Increment the j value by 1.
Step:-10 Repeat the steps from 8 to 9 Until the j value reaches power value(j<=power).
Step:-11 In User Defined Method(findPower) Return the Integer variable 'value' to the Main Method from
user Defined method.
Step:-12 Now Multiply with Digit value(nothing but 1).
Step:-13 Add the result to the dec variable.
Step:-14 Decrement i value by 1.
Step:-15 Increment power value by 1.
Step:-16 Repeat the steps from 4 to 15 Until i value reaches -1(i>=0).
Step:-17 Declare and Initialize a String Variable 'octal' as nothing/null("").
Step:-18 Divide the variable dec with 8.
Step:-19 Store the remainder in String variable(concat) before String value(octal=rem+octal).
Step:-20 Store the quotient in dec.
Step:-21 Repeat the steps from 18 to 20 Until variable dec becomes 0(dec>0).
Step:-22 Now Print the String octal value(It is nothing but Octal value).

Note:-
• Here i starts from length of the Given String minus 1(i=s.length()-1).
• Here j starts from 1.

import java.util.Scanner;

public class BinaryToOctalConversion {

public static void main(String[] args) {


Scanner obj=new Scanner(System.in);

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


String binary=obj.next(); //Taking input from user.

//Converting from Binary to Decimal.

int dec=0; //Declare and Initialize the 'dec' variable as 0.


int power=0; //Declare and Initialize the 'power' variable as 0(It is
helpful to calculate the power value for each and
every character).
for(int i=binary.length()-1;i>=0;i--,power++) //Iterating a loop to take one by one
character from the given String from last
character to first character.
{
char c=binary.charAt(i); //Taking a Single character into a char variable c.
if(c=='1') //Checking that char variable value is 1 or not.
{
dec=dec+(1*findPower(2,power)); //Adding the value to variable dec, after power
value(This value is finding by calling a
User Defined Method) is multiplying with
digit value.
}//end of if-condition.
}//end of loop.

//Converting from Decimal to Octal.

String octal=""; //Declare and Initialize the String variable 'octal' as


nothing/null("").

while(dec!=0) //Iterate a loop until dec value becomes 0.


{
int rem=dec%8; //Divide dec value by 8 and store the remainder in 'rem'.
octal=rem+octal; //Concat the remainder with String 'octal' and store in 'octal'
variable.
dec=dec/8; //Divides the dec variable by 8.
}//end of loop.

System.out.println(octal); //Print the octal value(It is nothing but Octal value).

}//end of Main Method.

public static int findPower(int base,int p) //This Method have to return the power value and
return type is int.
{
int value=1; //Declare and Initialize the 'value' variable as 1(This
variable is used to store the power value).
for(int j=1;j<=p;j++) //Iterating the loop for p(power) times.
{
value=value*base; //Multiplying the base with value and storing the result in
'value' variable.
}//end of Loop.

return value; //Here returns the int value to main method from User defined
Method.
}//end of user defined method (findPower).

}//end of Class.
Tracing of Code:-
Input:- 1011000
Output:- 130
binary dec power i c value j base p
1011000 0 0
6 0
1 5 0
2 4 0
3 3 1 àfindPower(2,power) 2 3
1
1*2=2 ß1
2*2=4 ß2
4*2=8 ß3
4à Loop Breaks here then return ‘value’.
0+(1*8)=8 4 2 1 àfindPower(2,power) 2 4
1
1*2=2 ß1
2*2=4 ß2
4*2=8 ß3
8*2=16 ß4
5à Loop Breaks here then return ‘value’.
8+(1*16)=24 5 1 0
6 0 1 àfindPower(2,power) 2 6
1
1*2=2 ß1
2*2=4 ß2
4*2=8 ß3
8*2=16 ß4
16*2=32 ß5
32*2=64 ß6
7à Loop Breaks here then return ‘value’.
24+(1*64)=88 7 -1àLoop Breaks here
dec octal rem
88 “”
11 0 ß88%8=0
1 3+0=30 ß11%8=3
0 1+30=130 ß1%8=1
àLoop Breaks here

• Finally Print the Octal variable it is nothing but Octal Number.

Tracing of Code:-
Input:- 1111
Output:- 17
s n power i c value j base p
1111 0 0
3 1àfindPower(2,power) 2 0
1
1à Loop Breaks here then return ‘value’.
0+1=1 1 2 1àfindPower(2,power) 2 1
1
1*2=2 ß1
2à Loop Breaks here then return ‘value’.
1+2=3 2 1 1àfindPower(2,power) 2 2
1
1*2=2 ß1
2*2=4 ß2
3à Loop Breaks here then return ‘value’.
3+4=7 3 0 1àfindPower(2,power) 2 3
1
1*2=2 ß1
2*2=4 ß2
4*2=8 ß3
4à Loop Breaks here then return ‘value’.
7+8=15 4 -1àLoop Breaks here
dec octal rem
15 “”
1 7 ß15%8=7
0 1+7=17 ß1%8=1
àLoop Breaks here

• Finally Print the Octal variable it is nothing but Octal Number.

Binary To Hexa Decimal Conversion:-


Q) Develop a Program to Convert the Binary Number to Hexa Decimal Number?

• To Find and Print Binary to Hexa Decimal conversion, follow the below logic and steps.
• Input:- One String value (binary).
• Output:- Print the Hexa Decimal Number.
• Logic:-
• To convert Binary value to Hexa Decimal value,
• First we have to convert the Binary value to Decimal value, then Decimal value to Hexa
Decimal value.
• We have one more way also it is nothing but Taking every four values(from right to left) and
finding the decimal for those every four binary values then we get the final value(But it is
little bit difficult to understand the logic).

Way:-1 Binary value to Decimal value, Decimal value to HexaDecimal value.


If Input is 1011000
part1: Binary value to Decimal value
We have to take a digit from Right to Left.
0*(2^0)+0*(2^1)+0*(2^2)+1*(2^3)+1*(2^4)+0*(2^5)+1*(2^6)
=0*(1)+0*(2)+0*(4)+1*(8)+1*(16)+0*(32)+1*(64)
=0+0+0+8+16+0+64
=88
part2: Decimal value to Hexa Decimal Value
88-->58

Way:-2 Binary value to Hexa Decimal value


If Input is 1011000
1000-->8
101-->5
Final value is 58

Step:-1 Take the Input(String) from user (binary).


Step:-2 Declare and Initialize a Variable 'dec' as 0.
Step:-3 Declare and Initialize a Variable 'power' as 0(It will helps us to find power value of base
value).
Step:-4 Taking a character from the Given String(Right to Left) in char variable 'c'.
Step:-5 Checking char variable 'c' is 1 or not, if 1 goto step-6 else goto step-14.
Step:-6 Call a User Defined Method(findPower(int b,int power)) to find the Digit respect power value
by passing a base value(2) and power value(Variable power).
Step:-7 In User Defined Method(findPower) Declare and Initialize a variable 'value' as 1.
Step:-8 In User Defined Method(findPower) next step is multiply the base value with variable
'value'and store the result in 'value' variable.
Step:-9 In User Defined Method(findPower) Increment the j value by 1.
Step:-10 Repeat the steps from 8 to 9 Until the j value reaches power value(j<=power).
Step:-11 In User Defined Method(findPower) Return the Integer variable 'value' to the Main Method from
User Defined method.
Step:-12 Now Multiply with Digit value(nothing but 1).
Step:-13 Add the result to the dec variable.
Step:-14 Decrement i value by 1.
Step:-15 Increment power value by 1.
Step:-16 Repeat the steps from 4 to 15 Until i value reaches -1(i>=0).
Step:-17 Declare and Initialize a String Variable 'octal' as nothing/null("").
Step:-18 Divide the variable dec with 16.
Step:-19 Declare and Initialize a String Variable 'hd' as nothing("").
Step:-20 Divide the variable dec with 16.
Step:-21 If Remainder is in between 0 to 9 then store the remainder in String variable(concat) before
String value(hd=rem+hd).
Step:-22 If Remainder is in between 10 to 15 then store the remainders Like A (10), B (11), C(12),
D(13),E (14), F(15) in String variable(concat) before String value(hd=rem+hd).
Step:-23 Store the quotient in dec.
Step:-24 Repeat the steps from 20 to 23 Until dec becomes 0(dec>0).
Step:-25 Now Print the String hd value(It is nothing but Hexa Decimal value).

Note:-
• Here i starts from length of the Given String minus 1(i=s.length()-1).
• Here j starts from 1.

import java.util.Scanner;

public class BinaryToHexaDecimalConversion {

public static void main(String[] args) {


Scanner obj=new Scanner(System.in);

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


String binary=obj.next(); //Taking input from user.

//Converting from Binary to Decimal.

int dec=0; //Declare and Initialize the 'dec' variable as 0.


int power=0; //Declare and Initialize the 'power' variable as 0(It is
helpful to calculate the power value for each and
every character).
for(int i=binary.length()-1;i>=0;i--,power++) //Iterating a loop to take one by one
character from the given String from
last character to first character.
{
char c=binary.charAt(i); //Taking a Single character into a char variable c.
if(c=='1') //Checking that char variable value is 1 or not.
{
dec=dec+(1*findPower(2,power)); //Adding the value to variable dec, after
power value(This value is finding by
calling a User Defined Method) is
multiplying with digit value.
}//end of if-condition.
}//end of loop.

//Converting from Decimal to Hexa Decimal.

String hd=""; //Declare and Initialize the String variable 'hd' as nothing/null("").

while(dec!=0) //Iterate a loop until dec value becomes 0.


{
int rem=dec%16; //Divide ‘dec’ value by 16 and store the remainder in 'rem'.
if(rem==10) //Checking remainder is 10 or not.
hd='A'+hd; //If Remainder is 10 then concat 'A' in String variable hd.
else if(rem==11) //Checking remainder is 11 or not.
hd='B'+hd; //If Remainder is 11 then concat 'B' in String variable hd.
else if(rem==12) //Checking remainder is 12 or not.
hd='C'+hd; //If Remainder is 10 then concat 'C' in String variable hd.
else if(rem==13) //Checking remainder is 13 or not.
hd='D'+hd; //If Remainder is 10 then concat 'D' in String variable hd.
else if(rem==14) //Checking remainder is 14 or not.
hd='E'+hd; //If Remainder is 10 then concat 'E' in String variable hd.
else if(rem==15) //Checking remainder is 15 or not.
hd='F'+hd; //If Remainder is 10 then concat 'F' in String variable hd.
else //If Remainder is in between the 0 and 9.
hd=rem+hd; //Concat the remainder with String 'hd' and store in 'hd'
variable.
dec=dec/16; //Divides the dec variable by 16.
}//end of loop.
System.out.println(hd); //Print the hd variable(It is nothing but hexaDecimal value).

}//end of Main Method.

public static int findPower(int base,int p) //This Method have to return the power value and
return type is int.
{
int value=1; //Declare and Initialize the 'value' variable as 1(This
variable is used to store the power value).
for(int j=1;j<=p;j++) //Iterating the loop for p(power) times.
{
value=value*base; //Multiplying the base with value and storing the result in
'value' variable.
}//end of Loop.
return value; //Here returns the int value to main method from User defined
Method.
}//end of user defined method (findPower).

}//end of Class.

Tracing of Code:-
Input:- 10100101
Output:- A5
binary dec power i c value j base p
10100101 0 0
7 1àfindPower(2,power) 2 0
1
1à Loop Breaks here then return ‘value’.
0+1=1 1 6 0
2 5 1àfindPower(2,power) 2 2
1
1*2=2 ß1
2*2=4 ß2
3à Loop Breaks here then return ‘value’.
4+1=5 3 4 0
4 3 0
5 2 1àfindPower(2,power) 2 5
1
1*2=2 ß1
2*2=4 ß2
4*2=8 ß3
8*2=16 ß4
16*2=32 ß5
6à Loop Breaks here then return ‘value’.
5+32=37 6 1 0
7 0 1àfindPower(2,power) 2 5
1
1*2=2 ß1
2*2=4 ß2
4*2=8 ß3
8*2=16 ß4
16*2=32 ß5
32*2=64 ß6
64*2=128 ß7
8à Loop Breaks here then return ‘value’.
37+128=165 -1 à Loop Breaks here
dec hd rem
165 “”
10 5 5 ß165%16=5
0 A+5=A5 A ß10%16=10
àLoop Breaks here

• Finally Print the hd variable it is nothing but Hexa Decimal Number.

Tracing of Code:-
Input:- 11101001
Output:- E9

binary dec power i c value j base p


11101001 0 0
7 1àfindPower(2,power) 2 0
1
1à Loop Breaks here then return ‘value’.
0+1=1 1 6 0
2 5 0
3 4 1àfindPower(2,power) 2 3
1
1*2=2 ß1
2*2=4 ß2
4*2=8 ß3
4à Loop Breaks here then return ‘value’.
1+8=9 4 3 0
5 2 1àfindPower(2,power) 2 5
1
1*2=2 ß1
2*2=4 ß2
4*2=8 ß3
8*2=16 ß4
16*2=32 ß5
6à Loop Breaks here then return ‘value’.
32+9=41 6 1 1àfindPower(2,power) 2 6
1
1*2=2 ß1
2*2=4 ß2
4*2=8 ß3
8*2=16 ß4
16*2=32 ß5
32*2=64 ß6
7à Loop Breaks here then return ‘value’.
41+64=105 7 0 1àfindPower(2,power) 2 7
1
1*2=2 ß1
2*2=4 ß2
4*2=8 ß3
8*2=16 ß4
16*2=32 ß5
32*2=64 ß6
64*2=128 ß7
8à Loop Breaks here then return ‘value’.
105+128=233 -1à Loop Breaks here
dec hd rem
233 “”
14 9 9 ß233%16=9
0 E+9=E9 E ß14%16=14
àLoop Breaks here

• Finally Print the hd variable it is nothing but Hexa Decimal Number.

Octal To Binary Conversion:-


Q) Develop a Program to Convert the Octal Number to Binary Number?

• To Find and Print Octal to Binary conversion, follow the below logic and steps.
• Input:- One String value (octal).
• Output:- Print the Binary Numbers.
• Logic:- To convert Octal value to binary value,
• First we have to convert the Octal value to Decimal value, then Decimal value to Binary value.
• We have one more way also it is nothing but Taking each and every character(from right to left)
and finding the binary value for those each and every character of octal values then we get the
final value(But it is little bit difficult to understand the logic).

Way:-1 Octal value to Decimal value, Decimal value to Binary value

If Input is 130
part1: Octal value to Decimal value
We have to take a digit from Right to Left.
0*(8^0)+3*(8^1)+1*(8^2)
=0*(1)+3*(8)+1*(64)
=0+24+64
=88
part2: Decimal value to Binary Value
88-->1011000
Way:-2 Octal value to Binary value

If Input is 130
0-->000
3-->011
1-->001
Final value is 001011000-->1011000.

Step:-1 Take the Input from user (octal).


Step:-2 Declare and Initialize a Integer Variable 'dec' as 0.
Step:-3 Declare and Initialize a Variable 'power' as 0(It will helps us to find power value of base
value).
Step:-4 Taking a character from the Given String(Right to Left) in char variable 'c'.
Step:-5 Convert the character value to integer Value by Type Casting.
Step:-6 In the time of Type Casting the value is converts into ASCII key value so subtract 48 from
that value.
Step:-7 Adding the value to variable dec, after power value(This value is finding by calling a User
Defined Method) is multiplying with digit value.

Step:-8 Call a User Defined Method(findPower(int b,int power)) to find the Digit respect power value
by passing a base value(8) and power value(Variable power).
Step:-9 In User Defined Method(findPower) Declare and Initialize a variable 'value' as 1.
Step:-10 In User Defined Method(findPower) next step is multiply the base value with variable 'value'
and store the result in 'value' variable.
Step:-11 In User Defined Method(findPower) Increment the j value by 1.
Step:-12 Repeat the steps from 8 to 9 Until the j value reaches power value(j<=power).
Step:-13 In User Defined Method(findPower) Return the Integer variable 'value' to the Main Method from
User Defined method.

Step:-14 Repeat the steps from 4 to 13 Until i value reaches -1(i>=0) it is nothing but the until
covers the all character in a given String(Octal value).

Step:-15 Declare and Initialize a String Variable 'binary' as nothing/null("").


Step:-16 Divide the given number with 2.
Step:-17 Store the remainder in String variable(concat) before String value(binary=rem+binary).
Step:-18 Store the quotient in dec.
Step:-19 Repeat the steps from 16 to 18 Until number (dec) becomes 0(dec>0).
Step:-20 Now Print the String binary variable(It is nothing but Binary value).

Note:-
• Here i starts from length of the Given String minus 1(i=s.length()-1).
• Here j starts from 1.
• Find out the Difference between s=s+r; and s=r+s if s is String variable and r is int variable.

import java.util.Scanner;

public class OctalToBinaryConversion {

public static void main(String[] args) {


Scanner obj=new Scanner(System.in);

System.out.println("Enter the Octal value");


String octal=obj.next(); //Taking input from user(Octal value).

//Octal to Decimal Conversion.

int dec=0; //Declare and Initialize the 'dec' variable as 0.


int power=0; //Declare and Initialize the 'power' variable as 0(It is
helpful to calculate the power value for each and every
character).
for(int i=octal.length()-1;i>=0;i--,power++) //Iterating a loop to take one by one character
from the given String from last character
to first character.
{
char c=octal.charAt(i); //Taking a Single character into a char variable c.

int t=(int)c; //Converting the value of character data type to


integer data type by type-casting.
t=t-48; //At the time of conversion the value converts to
ASCII value add substract 48 from that value to
make int value.
dec=dec+(t*findPower(8,power)); //Adding the value to variable dec, after power
value(This value is finding by calling a User
Defined Method) is multiplying with digit value.

}//end of loop.

//Decimal to Binary Conversion.

String binary=""; //Declare and Initialize the String variable 'binary' as


nothing/null("").

while(dec!=0) //Iterate a loop until dec value becomes 0.


{
int rem=dec%2; //Divide dec value by 2 and store the remainder in 'rem'.
binary=rem+binary; //Concat the remainder with String 'binary' and store in
'binary' variable.
dec=dec/2; //Divides the Number (dec) by 2.

}//end of loop.

System.out.println(binary); //Print the binary value(It is nothing but binary value).

}//end of Main Method.

public static int findPower(int base,int p) //This Method have to return the power value and
return type is int.
{
int value=1; //Declare and Initialize the 'value' variable as 1(This
variable is used to store the power value).
for(int j=1;j<=p;j++) //Iterating the loop for p(power) times.
{
value=value*base; //Multiplying the base with value and storing the result in
'value' variable.
}//end of Loop.

return value; //Here returns the int value to main method from User defined
Method.
}//end of user defined method (findPower).

}//end of Class.

Tracing of Code:-
Input:- 130
Output:- 1011000

octal dec power i c value j base p


130 0 0
2 0
1 1 3àfindPower(8,power) 8 1
1
1*8=8 ß1
2à Loop Breaks here then return ‘value’.
0+(3*8)=24
2 0 1àfindPower(8,power) 8 2
1
1*8=8 ß1
8*8=64 ß2
3à Loop Breaks here then return ‘value’.
24+(1*64)=88 3 -1à Loop Breaks here

dec binary rem


88 “”
88/2=44 0 ß88%2=0
44/2=22 0+0=0 ß44%2=0
22/2=11 0+00=000 ß22%2=0
11/2=5 1+000=1000 ß11%2=1
5/2=2 1+1000=11000 ß5%2=1
2/2=1 0+11000=011000 ß2%2=0
1/2=0 1+011000=1011000 ß1%2=1
àLoop Breaks here

• Finally Print the binary variable it is nothing but Binary Number.


Tracing of Code:-
Input:- 233
Output:- 10011011

octal dec power i c value j base p


233 0 0
2 3àfindPower(8,power) 8 0
1
1à Loop Breaks here then return ‘value’.
0+(3*1)=3
1 1 3àfindPower(8,power) 8 1
1
1*8=8 ß1
2à Loop Breaks here then return ‘value’.
3+(3*8)=27
2 0 2àfindPower(8,power) 8 2
1
1*8=8 ß1
8*8=64 ß2
3à Loop Breaks here then return ‘value’.
27+(2*64)=155 3 -1à Loop Breaks here

dec binary rem


155 “”
155/2=77 1 ß155%2=1
77/2=38 1+1=11 ß77%2=1
38/2=19 0+11=011 ß38%2=0
19/2=9 1+011=1011 ß19%2=1
9/2=4 1+1011=11011 ß9%2=1
4/2=2 0+11011=011011 ß4%2=0
2/2=1 0+011011=0011011 ß2%2=0
1/2=0 1+0011011=10011011 ß1%2=1
àLoop Breaks here
• Finally Print the binary variable it is nothing but Binary Number.
Octal To Decimal Conversion:-
Q) Develop a Program to Convert the Octal Number to Decimal Number?

• To Find and Print Octal to Decimal conversion, follow the below logic and steps.
• Input:- One String value (octal).
• Output:- Print the Decimal Numbers.
• Logic:- To convert Octal value to Decimal value,

If Input is 130
We have to take a digit from Right to Left.
0*(8^0)+3*(8^1)+1*(8^2)
=0*(1)+3*(8)+1*(64)
=0+24+64
=88

Step:-1 Take the Input from user (octal).


Step:-2 Declare and Initialize a Integer Variable 'dec' as 0.
Step:-3 Declare and Initialize a Variable 'power' as 0(It will helps us to find power value of base
value).
Step:-4 Taking a character from the Given String(Right to Left) in char variable 'c'.
Step:-5 Convert the character value to integer Value by Type Casting.
Step:-6 In the time of Type Casting the value is converts into ASCII key value so substract 48 from
that value.
Step:-7 Adding the value to variable dec, after power value(This value is finding by calling a User
Defined Method) is multiplying with digit value.

Step:-8 Call a User Defined Method(findPower(int b,int power)) to find the Digit respect power value
by passing a base value(8) and power value(Variable power).
Step:-9 In User Defined Method(findPower) Declare and Initialize a variable 'value' as 1.
Step:-10 In User Defined Method(findPower) next step is multiply the base value with variable 'value'
and store the result in 'value' variable.
Step:-11 In User Defined Method(findPower) Increment the j value by 1.
Step:-12 Repeat the steps from 8 to 9 Until the j value reaches power value(j<=power).
Step:-13 In User Defined Method(findPower) Return the Integer variable 'value' to the Main Method from
user Defined method.
Step:-14 Repeat the steps from 4 to 13 Until i value reaches -1(i>=0) it is nothing but the until
covers the all character in a given String(Octal value).

Step:-15 Now Print the dec variable(It is nothing but Decimal value).

Note:-
• Here i starts from length of the Given String minus 1(i=s.length()-1).
• Here j starts from 1.

import java.util.Scanner;

public class OctalToDecimalConversion {

public static void main(String[] args) {


Scanner obj=new Scanner(System.in);

System.out.println("Enter the Octal value");


String octal=obj.next(); //Taking input from user(Octal value).

int dec=0; //Declare and Initialize the 'dec' variable as 0.


int power=0; //Declare and Initialize the 'power' variable as 0(It is
helpful to calculate the power value for each and every
character).
for(int i=octal.length()-1;i>=0;i--,power++) //Iterating a loop to take one by one character
from the given String from last character
to first character.
{
char c=octal.charAt(i); //Taking a Single character into a char variable c.

int t=(int)c; //Converting the value of character data type to


integer data type by type-casting.
t=t-48; //At the time of conversion the value converts to
ASCII value add substract 48 from that value to
make int value.
dec=dec+(t*findPower(8,power)); //Adding the value to variable dec, after power
value(This value is finding by calling a User
Defined Method) is multiplying with digit value.

}//end of loop.

System.out.println(dec); //Print the dec value(It is nothing but Decimal value).

}//end of Main Method.

public static int findPower(int base,int p) //This Method have to return the power value and
return type is int.
{
int value=1; //Declare and Initialize the 'value' variable as 1 (This
variable is used to store the power value).
for(int j=1;j<=p;j++) //Iterating the loop for p(power) times.
{
value=value*base; //Multiplying the base with value and storing the result in
'value' variable.
}//end of Loop.
return value; //Here returns the int value to main method from User defined
Method.
}//end of user defined method (findPower).

}//end of Class.

Tracing of Code:-
Input:- 130
Output:- 88

octal dec power i c value j base p


130 0 0
2 0
1 1 3àfindPower(8,power) 8 1
1
1*8=8 ß1
2à Loop Breaks here then return ‘value’.
0+(3*8)=24
2 0 1àfindPower(8,power) 8 2
1
1*8=8 ß1
8*8=64 ß2
3à Loop Breaks here then return ‘value’.
24+(1*64)=88 3 -1à Loop Breaks here

• Finally Print the dec variable it is nothing but Decimal Number.

Tracing of Code:-
Input:- 233
Output:- 155

octal dec power i c value j base p


233 0 0
2 3àfindPower(8,power) 8 0
1
1à Loop Breaks here then return ‘value’.
0+(3*1)=3
1 1 3àfindPower(8,power) 8 1
1
1*8=8 ß1
2à Loop Breaks here then return ‘value’.
3+(3*8)=27
2 0 2àfindPower(8,power) 8 2
1
1*8=8 ß1
8*8=64 ß2
3à Loop Breaks here then return ‘value’.
27+(2*64)=155 3 -1à Loop Breaks here

• Finally Print the binary variable it is nothing but Binary Number.


Octal To Hexa Decimal Conversion:-
Q) Develop a Program to Convert the Octal Number to Hexa Decimal Number?

• To Find and Print Octal to HexaDecimal conversion, follow the below logic and steps.
• Input:- One String value (octal).
• Output:- Print the HexaDecimal Numbers.
• Logic:- To convert Octal value to HexaDecimal value,
We have to convert the first Octal value to Decimal value and then Decimal Value to Hexa
Decimal value.

Octal To Decimal Value.


If Input is 555
We have to take a digit from Right to Left.
5*(8^0)+5*(8^1)+5*(8^2)
=5*(1)+5*(8)+5*(64)
=5+40+320
=365

Decimal to HexaDecimal Value.


365-->16D

Step:-1 Take the Input from user (octal).


Step:-2 Declare and Initialize a Integer Variable 'dec' as 0.
Step:-3 Declare and Initialize a Variable 'power' as 0(It will helps us to find power value of base
value).
Step:-4 Taking a character from the Given String(Right to Left) in char variable 'c'.
Step:-5 Convert the character value to integer Value by Type Casting.
Step:-6 In the time of Type Casting the value is converts into ASCII key value so substract 48 from
that value.
Step:-7 Adding the value to variable dec, after power value(This value is finding by calling a User
Defined Method) is multiplying with digit value.

Step:-8 Call a User Defined Method(findPower(int b,int power)) to find the Digit respect power value
by passing a base value(8) and power value(Variable power).
Step:-9 In User Defined Method(findPower) Declare and Initialize a variable 'value' as 1.
Step:-10 In User Defined Method(findPower) next step is multiply the base value with variable 'value'
and store the result in 'value' variable.
Step:-11 In User Defined Method(findPower) Increment the j value by 1.
Step:-12 Repeat the steps from 8 to 9 Until the j value reaches power value(j<=power).
Step:-13 In User Defined Method(findPower) Return the Integer variable 'value' to the Main Method from
user Defined method.

Step:-14 Repeat the steps from 4 to 13 Until i value reaches -1(i>=0) it is nothing but the until
covers the all character in a given String(Octal value).

Step:-15 Declare and Initialize a String Variable 'hd' as nothing/null("").


Step:-16 Divide the number (dec) with 16.
Step:-17 If Remainder is in between 0 to 9 then store the remainder in String variable(concat) before
String value(hd=rem+hd).
Step:-18 If Remainder is in between 10 to 15 then store the remainders Like A (10), B (11), C(12),
D(13),E (14), F(15) in String variable(concat) before String value(hd=rem+hd).
Step:-19 Store the quotient in dec.
Step:-20 Repeat the steps from 16 to 19 Until number (dec) becomes 0(dec>0).
Step:-21 Now Print the String hd value(It is nothing but Hexa Decimal value).

Note:-
• Here i starts from length of the Given String minus 1(i=s.length()-1).
• Here j starts from 1.
• Find out the Difference between s=s+r; and s=r+s if s is String variable and r is int variable.

import java.util.Scanner;

public class OctalToHexaDecimalConversion {

public static void main(String[] args) {


Scanner obj=new Scanner(System.in);

System.out.println("Enter the Octal value");


String octal=obj.next(); //Taking input from user(Octal value).
int dec=0; //Declare and Initialize the 'dec' variable as 0.
int power=0; //Declare and Initialize the 'power' variable as 0(It is
helpful to calculate the power value for each and every
character).
for(int i=octal.length()-1;i>=0;i--,power++) //Iterating a loop to take one by one character
from the given String from last character
to first character.
{
char c=octal.charAt(i); //Taking a Single character into a char variable c.

int t=(int)c; //Converting the value of character data type to


integer data type by type-casting.
t=t-48; //At the time of conversion the value converts to
ASCII value add substract 48 from that value to
make int value.
dec=dec+(t*findPower(8,power)); //Adding the value to variable dec, after power
value(This value is finding by calling a User
Defined Method) is multiplying with digit value.
}//end of loop.

String hd=""; //Declare and Initialize the String variable hd as


nothing/null("").

while(dec!=0) //Iterate a loop until dec value becomes 0.


{
int rem=dec%16; //Divide dec value by 16 and store the remainder in 'rem'.

if(rem==10) //Checking remainder is 10 or not.


hd='A'+hd; //If Remainder is 10 then concat 'A' in String variable hd.
else if(rem==11) //Checking remainder is 11 or not.
hd='B'+hd; //If Remainder is 11 then concat 'B' in String variable hd.
else if(rem==12) //Checking remainder is 12 or not.
hd='C'+hd; //If Remainder is 12 then concat 'C' in String variable hd.
else if(rem==13) //Checking remainder is 13 or not.
hd='D'+hd; //If Remainder is 13 then concat 'D' in String variable hd.
else if(rem==14) //Checking remainder is 14 or not.
hd='E'+hd; //If Remainder is 14 then concat 'E' in String variable hd.
else if(rem==15) //Checking remainder is 15 or not.
hd='F'+hd; //If Remainder is 15 then concat 'F' in String variable hd.
else //If Remainder is in between the 0 and 9.
hd=rem+hd; //Concat the remainder with String 'hd' and store in 'hd'
variable.
dec=dec/16; //Divides the Number(dec) by 16.
}//end of loop.

System.out.println(hd); //Print the hd value(It is nothing but Hexa Decimal value).

}//end of Main Method.

public static int findPower(int base,int p) //This Method have to return the power value and
return type is int.
{
int value=1; //Declare and Initialize the 'value' variable as 1(This
variable is used to store the power value).
for(int j=1;j<=p;j++) //Iterating the loop for p(power) times.
{
value=value*base; //Multiplying the base with value and storing the result in
'value' variable.
}//end of Loop.
return value; //Here returns the int value to main method from User defined
method.
}//end of user defined method (findPower).

}//end of Class.

Tracing of Code:-
Input:- 555
Output:- 16D

octal dec power i c value j base p


555 0 0
2 5àfindPower(8,power) 8 0
1 1à Loop Breaks here then return ‘value’.
0+(5*1)=5
1 1 5àfindPower(8,power) 8 1
1
1*8=8 ß1
2à Loop Breaks here then return ‘value’.
5+(5*8)= 45
2 0 5àfindPower(8,power) 8 2
1
1*8=8 ß1
8*8=64 ß2
3à Loop Breaks here then return ‘value’.
45+(5*64)=365 3 -1à Loop Breaks here
dec hd rem
365 “”
365/16=22 D D ß365%16=13
22/16=1 6+D=6D 6 ß22%16=6
1/16=0 1+6D=16D 1 ß1%16=1
à Loop Breaks here

• Finally Print the hd variable it is nothing but HexaDecimal Number.

Tracing of Code:-
Input:- 500
Output:- 140

octal dec power i c value j base p


500 0 0
2 0
1 1 0
2 0 5àfindPower(8,power) 8 2
1
1*8=8 ß1
8*8=64 ß2
3à Loop Breaks here then return ‘value’.
0+(5*64)=320 3 -1à Loop Breaks here

dec hd rem
320 “”
320/16=20 0 0 ß320%16=0
20/16=1 4+0=40 4 ß24%16=4
1/16=0 1+40=140 1 ß1%16=1
à Loop Breaks here

• Finally Print the hd variable it is nothing but Hexa Decimal Number.

Hexa Decimal To Binary Conversion:-


Q) Develop a Program to Convert the Hexa Decimal Number to Binary Number?

• To Find and Print Hexa Decimal to Binary conversion, follow the below logic and steps.
• Input:- One String value (hd).
• Output:- Print the Binary Numbers.
• Logic:-
§ To convert Hexa Decimal value to Binary value we have to do that convert the given
HexaDecimal value into Decimal value and then Decimal Value into Binary Value.
§ we have one more way also, it is by taking one character have to find the binary value
of that character, like this we have to do for all characters and Print the final value.

Way:-1 HexaDecimal to Decimal value, Decimal to Binary value.

If Input is 1f4
Part:-1 HexaDecimal to Decimal value
We have to take a digit from Right to Left.
4*(16^0)+15*(16^1)+1*(16^2)
=4*(1)+15*(16)+1*(256)
=4+240+256
=500
Part:-2 Decimal to Binary value
500-->111110100
Way:-2 HexaDecimal to Binary Value

if Input is 1f4
1-->0001
f-->1111
4-->0100
Final value is 000111110100

Step:-1 Take the Input from user (hd).

Step:-2 Declare and Initialize a Variable 'dec' as 0.


Step:-3 Declare and Initialize a Variable 'power' as 0(It will helps us to find power value of base
value).
Step:-4 Taking a character from the Given String(Right to Left) in char variable 'c'.
Step:-5 Convert the char variable 'c' into Integer value but Type-Casting in this process it returns
ASCII key value and store this result in int variable 't'.
Step:-6 Check t value in between the range of 48 to 57 or not. If yes goto step-7 else goto step-8.
Step:-7 Subtract the 48 from t value.
Step:-8 Check c value is 'a' or 'A', if yes assign the t value as 10.
Step:-9 Check c value is 'b' or 'B', if yes assign the t value as 11.
Step:-10 Check c value is 'c' or 'C', if yes assign the t value as 12.
Step:-11 Check c value is 'd' or 'D', if yes assign the t value as 13.
Step:-12 Check c value is 'e' or 'E', if yes assign the t value as 14.
Step:-13 Check c value is 'f' or 'F', if yes assign the t value as 15.

Step:-14 Call a User Defined Method(findPower(int b,int power)) to find the Digit respect power value
by passing a base value(2) and power value(Variable power).
Step:-15 In User Defined Method(findPower) Declare and Initialize a variable 'value' as 1.
Step:-16 In User Defined Method(findPower) next step is multiply the base value with variable 'value'
and store the result in 'value' variable.
Step:-17 In User Defined Method(findPower) Increment the j value by 1.
Step:-18 Repeat the steps from 16 to 17 Until the j value reaches power value(j<=power).
Step:-19 In User Defined Method(findPower) Return the Integer variable 'value' to the Main Method from
user Defined method.
Step:-20 Now Multiply with Digit value(nothing but 1).
Step:-21 Add the result to the dec variable.
Step:-22 Decrement i value by 1.
Step:-23 Increment power value by 1.
Step:-24 Repeat the steps from 4 to 23 Until i value reaches -1(i>=0).

Step:-25 Declare and Initialize a String Variable 'binary' as nothing("").


Step:-26 Divide the number (dec) with 2.
Step:-27 Store the remainder in String variable(concat) before String value(binary=rem+binary).
Step:-28 Store the quotient in dec.
Step:-29 Repeat the steps from 26 to 28 Until number (dec) becomes 0(dec>0).

Step:-30 Now Print the binary variable(It is nothing but binary value).

Note:-
• Find out the Difference between s=s+r; and s=r+s if s is String variable and r is int variable.
• Should know the values of ASCII key.
• Here i starts from length of the Given String minus 1(i=s.length()-1).
• Here j starts from 1.

import java.util.Scanner;
public class HexaDecimalToBinaryConversion {

public static void main(String[] args) {

Scanner obj=new Scanner(System.in);


System.out.println("Enter the HexaDecimal number");
String hd=obj.next(); //Taking input from user (hd).

Hexa Decimal To Decimal conversion

int dec=0; //Declare and Initialize the 'dec' variable as 0.


int power=0; //Declare and Initialize the 'power' variable as 0(It is
helpful to calculate the power value for each and every
character).
for(int i=hd.length()-1;i>=0;i--,power++) //Iterating a loop to take one by one character
from the given String from last character
to first character.
{
char c=hd.charAt(i); //Taking a Single character into a char variable c.
int t=(int)c; //Converting the value of character data type to integer
data type by type-casting In this point of time
integer value returns (ASCII key value).
if(t>=48 && t<=57) //Checking t value is in between Range of 48 and 57 or not.
t=t-48; //Subtract the 48 from t variable to make a Perfect value.
if(c=='A' || c=='a') //Checking variable c value is 'a' or 'A'.
t=10; //If c='a' or c='A', Then assign the t value as 10.
if(c=='B' || c=='b') //Checking variable c value is 'b' or 'B'.
t=11; //If c='b' or c='B', Then assign the t value as 11.
if(c=='C' || c=='c') //Checking variable c value is 'c' or 'C'.
t=12; //If c='c' or c='C', Then assign the t value as 12.
if(c=='D' || c=='d') //Checking variable c value is 'd' or 'D'.
t=13; //If c='d' or c='D', Then assign the t value as 13.
if(c=='e' || c=='e') //Checking variable c value is 'e' or 'E'.
t=14; //If c='e' or c='E', Then assign the t value as 14.
if(c=='F' || c=='f') //Checking variable c value is 'f' or 'F'.
t=15; //If c='f' or c='F', Then assign the t value as 15.

dec=dec+(t*findPower(16,power)); //Adding the value to variable dec, after power


value(This value is finding by calling a
User Defined Method) is multiplying with
digit value.

}//end of loop.

Decimal value To Binary Value

String binary=""; //Declare and Initialize the String variable 'binary' as


nothing/null("").

while(dec!=0) //Iterate a loop until dec value becomes 0.


{
int rem=dec%2; //Divide dec value by 2 and store the remainder in 'rem'.
binary=rem+binary; //Concat the remainder with String 'binary' and store in
'binary' variable.
dec=dec/2; //Divides the Number (dec) by 2.

}//end of loop.

System.out.println(binary); //Print the binary variable(It is nothing but binary value).

}//end of Main Method.

public static int findPower(int base,int p) //This Method have to return the power value and
return type is int.
{
int value=1; //Declare and Initialize the 'value' variable as 1(This
variable is used to store the power value).
for(int j=1;j<=p;j++) //Iterating the loop for p(power) times.
{
value=value*base; //Multiplying the base with value and storing the result in
'value' variable.
}//end of Loop.
return value; //Here returns the int value to main method from User defined
method.
}//end of user defined method (findPower).

}//end of Class.
**********

Tracing of Code:-
Input:- 1f4
Output:- 111110100

hd dec power i c value j base p


1f4 0 0
2 4àfindPower(16,power) 16 0
1 1à Loop Breaks here then return ‘value’.
0+(4*1)=4
1 1 fàfindPower(16,power) 16 1
1
1*16=16 ß1
2à Loop Breaks here then return ‘value’.
4+(15*16)=244
2 0 1àfindPower(16,power) 16 2
1
1*16=16 ß1
16*16=256 ß2
3à Loop Breaks here then return ‘value’.
244+(1*256)=500 3 -1à Loop Breaks here

dec binary rem


500 “”
500/2=250 0 ß500%2=0
250/2=125 0+0=00 ß250%2=0
125/2=62 1+00=100 ß125%2=1
62/2=31 0+100=0100 ß62%2=0
31/2=15 1+0100=10100 ß31%2=1
15/2=7 1+10100=110100 ß15%2=1
7/2=3 1+110100=1110100 ß7%2=1
3/2=1 1+1110100=11110100 ß3%2=1
1/2=0 1+11110100=111110100 ß1%2=1
à Loop Breaks here

• Finally Print the binary variable it is nothing but binary Number.

Tracing of Code:-
Input:- ad8
Output:- 101011011000

hd dec power i c value j base p


ad8 0 0
0+(8*1)=8
2 ‘value’.
8àf
indPower(
16,power) 16 0
1
à

L
o
o
p

B
r
e
a
k
s

h
e
r
e

t
h
e
n

r
e
t
u
r
n
1 1 dàfindPower(16,power) 16 0
1
1*16=16 ß1
2à Loop Breaks here then return ‘value’.
8+(13*16)=216
2 0 aàfindPower(16,power) 16 2
1
1*16=16 ß1
16*16=256 ß2
3à Loop Breaks here then return ‘value’.
216+(10*256)=2776 3 -1à Loop Breaks here

dec binary rem


2776 “”
2776/2=1388 0 ß2776%2=0
1388/2=694 0+0=00 ß1388%2=0
694/2=347 0+00=000 ß694%2=0
347/2=173 1+000=1000 ß347%2=1
173/2=86 1+1000=11000 ß173%2=1
86/2=43 0+11000=011000 ß86%2=0
43/2=21 1+011000=1011000 ß43%2=1
21/2=10 1+1011000=11011000 ß21%2=1
10/2=5 0+11011000=011011000 ß10%2=0
5/2=2 1+011011000=1011011000 ß5%2=1
2/2=1 0+1011011000=01011011000 ß2%2=0
1/2=0 1+101011011000=101011011000 ß1%2=1
à Loop Breaks here

• Finally Print the binary variable it is nothing but Binary Number.

Hexa Decimal To Decimal Conversion:-


Q) Develop a Program to Convert the Hexa Decimal Number to Decimal Number?

• To Find and Print Hexa Decimal to Decimal conversion, follow the below logic and steps.
• Input:- One String value (hd).
• Output:- Print the Decimal Numbers.
• Logic:- To convert Hexa Decimal value to Decimal value we have to do.
If Input is 1f4
We have to take a digit from Right to Left.
4*(16^0)+15*(16^1)+1*(16^2)
=4*(1)+15*(16)+1*(256)
=4+240+256
=500

Step:-1 Take the Input from user (hd).

Step:-2 Declare and Initialize a Variable 'dec' as 0.


Step:-3 Declare and Initialize a Variable 'power' as 0(It will helps us to find power value of base
value).
Step:-4 Taking a character from the Given String(Right to Left) in char variable 'c'.
Step:-5 Convert the char variable 'c' into Integer value but Type-Casting in this process it returns
ASCII key value and store this result in int variable 't'.
Step:-6 Check t value in between the range of 48 to 57 or not. If yes goto step-7 else goto step-8.
Step:-7 Subtract the 48 from t value.
Step:-8 Check c value is 'a' or 'A', if yes assign the t value as 10.
Step:-9 Check c value is 'b' or 'B', if yes assign the t value as 11.
Step:-10 Check c value is 'c' or 'C', if yes assign the t value as 12.
Step:-11 Check c value is 'd' or 'D', if yes assign the t value as 13.
Step:-12 Check c value is 'e' or 'E', if yes assign the t value as 14.
Step:-13 Check c value is 'f' or 'F', if yes assign the t value as 15.

Step:-14 Call a User Defined Method(findPower(int b,int power)) to find the Digit respect power value
by passing a base value(2) and power value(Variable power).
Step:-15 In User Defined Method(findPower) Declare and Initialize a variable 'value' as 1.
Step:-16 In User Defined Method(findPower) next step is multiply the base value with variable 'value'
and store the result in 'value' variable.
Step:-17 In User Defined Method(findPower) Increment the j value by 1.
Step:-18 Repeat the steps from 16 to 17 Until the j value reaches power value(j<=power).
Step:-19 In User Defined Method(findPower) Return the Integer variable 'value' to the Main Method from
user Defined method.

Step:-20 Now Multiply with Digit value(nothing but 1).


Step:-21 Add the result to the dec variable.
Step:-22 Decrement i value by 1.
Step:-23 Increment power value by 1.
Step:-24 Repeat the steps from 4 to 23 Until i value reaches -1(i>=0).

Step:-25 Now Print the dec value(It is nothing but Decimal value).

Note:-
• Find out the Difference between s=s+r; and s=r+s if s is String variable and r is int variable.
• Should know the values of ASCII key.
• Here i starts from length of the Given String minus 1(i=s.length()-1).
• Here j starts from 1.

import java.util.Scanner;
public class HexaDecimalToDecimalConversion {

public static void main(String[] args) {

Scanner obj=new Scanner(System.in);


System.out.println("Enter the HexaDecimal number");
String hd=obj.next(); //Taking input from user (hd).

int dec=0; //Declare and Initialize the 'dec' variable as 0.


int power=0; //Declare and Initialize the 'power' variable as 0(It is
helpful to calculate the power value for each and every
character).
for(int i=hd.length()-1;i>=0;i--,power++) //Iterating a loop to take one by one character
from the given String from last character
to first character.
{
char c=hd.charAt(i); //Taking a Single character into a char variable c.
int t=(int)c; //Converting the value of character data type to integer
data type by type-casting In this point of time
integer value returns (ASCII key value).
if(t>=48 && t<=57) //Checking t value is in between Range of 48 and 57 or not.
t=t-48; //Subtract the 48 from t variable to make a Perfect value.
if(c=='A' || c=='a') //Checking variable c value is 'a' or 'A'.
t=10; //If c='a' or c='A', Then assign the t value as 10.
if(c=='B' || c=='b') //Checking variable c value is 'b' or 'B'.
t=11; //If c='b' or c='B', Then assign the t value as 11.
if(c=='C' || c=='c') //Checking variable c value is 'c' or 'C'.
t=12; //If c='c' or c='C', Then assign the t value as 12.
if(c=='D' || c=='d') //Checking variable c value is 'd' or 'D'.
t=13; //If c='d' or c='D', Then assign the t value as 13.
if(c=='e' || c=='e') //Checking variable c value is 'e' or 'E'.
t=14; //If c='e' or c='E', Then assign the t value as 14.
if(c=='F' || c=='f') //Checking variable c value is 'f' or 'F'.
t=15; //If c='f' or c='F', Then assign the t value as 15.

dec=dec+(t*findPower(16,power)); //Adding the value to variable dec, after power


value(This value is finding by calling a
user Defined Method) is multiplying with
digit value.

}//end of loop.

System.out.println(dec); //Printing the dec value(It is nothing but Decimal value).

}//end of Main Method.

public static int findPower(int base,int p) //This Method have to return the power value and
return type is int.
{
int value=1; //Declare and Initialize the 'value' variable as 1(This
variable is used to store the power value).
for(int j=1;j<=p;j++) //Iterating the loop for p(power) times.
{
value=value*base; //Multiplying the base with value and storing the result in
'value' variable.
}//end of Loop.
return value; //Here returns the int value to main method from User defined
Method.
}//end of user defined method (findPower).
}//end of Class.

Tracing of Code:-
Input:- 1f4
Output:- 500

hd dec power i c value j base p


1f4 0 0
2 4àfindPower(16,power) 16 0
1 1à Loop Breaks here then return ‘value’.
0+(4*1)=4
1 1 fàfindPower(16,power) 16 1
1
1*16=16 ß1
2à Loop Breaks here then return ‘value’.
4+(15*16)=244
2 0 1àfindPower(16,power) 16 2
1
1*16=16 ß1
16*16=256 ß2
3à Loop Breaks here then return ‘value’.
244+(1*256)=500 3 -1à Loop Breaks here

• Finally Print the dec variable it is nothing but Decimal Number.

Tracing of Code:-
Input:- ad8
Output:- 2776

hd dec power i c value j base p


ad8 0 0
0+(8*1)=8
2 ‘value’.
8àf
indPower(
16,power) 16 0
1
à

L
o
o
p

B
r
e
a
k
s

h
e
r
e

t
h
e
n

r
e
t
u
r
n
1 1 dàfindPower(16,power) 16 0
1
1*16=16 ß1
2à Loop Breaks here then return ‘value’.
8+(13*16)=216
2 0 aàfindPower(16,power) 16 2
1
1*16=16 ß1
16*16=256 ß2
3à Loop Breaks here then return ‘value’.
216+(10*256)=2776 3 -1à Loop Breaks here

• Finally Print the dec variable it is nothing but Decimal Number.


1) Hexa Decimal To Octal Conversion.
2) Prime Number (without using count).
3) Alternative Palindromes(boolean value).
4) Even numbers between the given range (using BREAK and CONTINUE).
5) Prime numbers between the given range (using BREAK and CONTINUE).

6) *****
*****
*****
*****
*****

7) 1 2 3 4 5
6 7 8 9 10
11 12 13 14 15

8) * * * * * *
* # # # # *
* # # # # *
* # # # # *
* # # # # *
* * * * * *

9) 1*2*3*4*5
6*7*8*9*10
11*12*13*14*15
16*17*18*19*20
21*22*23*24*25
Hexa Decimal To Octal Conversion:-
Q) Write a Program to Convert the Hexa Decimal Number to Octal Number?

• To Find and Print Hexa Decimal to Octal conversion, follow the below logic and steps.
• Input:- One String value (hd).
• Output:- Print the Octal Numbers.
• Logic:- To convert Hexa Decimal value to Octal value we have to do that convert the given
HexaDecimal value into Decimal value and then Decimal Value into Octal Value.

HexaDecimal to Decimal value

If Input is 1f4
We have to take a digit from Right to Left.
4*(16^0)+15*(16^1)+1*(16^2)
=4*(1)+15*(16)+1*(256)
=4+240+256
=500

Decimal to Octal value

500-->764

Step:-1 Take the Input from user (hd).

Step:-2 Declare and Initialize a Variable 'dec' as 0.


Step:-3 Declare and Initialize a Variable 'power' as 0(It will helps us to find power value of base
value).
Step:-4 Taking a character from the Given String(Right to Left) in char variable 'c'.
Step:-5 Convert the char variable 'c' into Integer value but Type-Casting in this process it returns
ASCII key value and store this result in int variable 't'.
Step:-6 Check t value in between the range of 48 to 57 or not. If yes goto step-7 else goto step-8.
Step:-7 Subtract the 48 from t value.
Step:-8 Check c value is 'a' or 'A', if yes assign the t value as 10.
Step:-9 Check c value is 'b' or 'B', if yes assign the t value as 11.
Step:-10 Check c value is 'c' or 'C', if yes assign the t value as 12.
Step:-11 Check c value is 'd' or 'D', if yes assign the t value as 13.
Step:-12 Check c value is 'e' or 'E', if yes assign the t value as 14.
Step:-13 Check c value is 'f' or 'F', if yes assign the t value as 15.

Step:-14 Call a User Defined Method(findPower(int b,int power)) to find the Digit respect power value
by passing a base value(2) and power value(Variable power).
Step:-15 In User Defined Method(findPower) Declare and Initialize a variable 'value' as 1.
Step:-16 In User Defined Method(findPower) next step is multiply the base value with variable 'value'
and store the result in 'value' variable.
Step:-17 In User Defined Method(findPower) Increment the j value by 1.
Step:-18 Repeat the steps from 16 to 17 Until the j value reaches power value(j<=power).
Step:-19 In User Defined Method(findPower) Return the Integer variable 'value' to the Main Method from
user Defined method.

Step:-20 Now Multiply with Digit value(nothing but 1).


Step:-21 Add the result to the dec variable.
Step:-22 Decrement i value by 1.
Step:-23 Increment power value by 1.
Step:-24 Repeat the steps from 4 to 23 Until i value reaches -1(i>=0).

Step:-25 Declare and Initialize a String Variable 'octal' as nothing("").


Step:-26 Divide the number (dec) with 8.
Step:-27 Store the remainder in String variable(concat) before String value(octal=rem+octal).
Step:-28 Store the quotient in dec.
Step:-29 Repeat the steps from 26 to 28 Until number (dec) becomes 0(dec>0).

Step:-30 Now Print the octal variable(It is nothing but octal value).

Note:-
• Find out the Difference between s=s+r; and s=r+s if s is String variable and r is int variable.
• Should know the values of ASCII key.
• Here i starts from length of the Given String minus 1(i=s.length()-1).
• Here j starts from 1.

import java.util.Scanner;
public class HexaDecimalToOctalConversion {
public static void main(String[] args) {

Scanner obj=new Scanner(System.in);


System.out.println("Enter the HexaDecimal number");
String hd=obj.next(); //Taking input from user (hd).

HexaDecimal TO Decimal Conversion

int dec=0; //Declare and Initialize the 'dec' variable as 0.


int power=0; //Declare and Initialize the 'power' variable as 0(It is
helpful to calculate the power value for each and every
character).
for(int i=hd.length()-1;i>=0;i--,power++) //Iterating a loop to take one by one character
from the given String from last character
to first character.
{
char c=hd.charAt(i); //Taking a Single character into a char variable c.
int t=(int)c; //Converting the value of character data type to integer
data type by type-casting In this point of time
integer value returns (ASCII key value).
if(t>=48 && t<=57) //Checking t value is in between Range of 48 and 57 or not.
t=t-48; //Subtract the 48 from t variable to make a Perfect value.
if(c=='A' || c=='a') //Checking variable c value is 'a' or 'A'.
t=10; //If c='a' or c='A', Then assign the t value as 10.
if(c=='B' || c=='b') //Checking variable c value is 'b' or 'B'.
t=11; //If c='b' or c='B', Then assign the t value as 11.
if(c=='C' || c=='c') //Checking variable c value is 'c' or 'C'.
t=12; //If c='c' or c='C', Then assign the t value as 12.
if(c=='D' || c=='d') //Checking variable c value is 'd' or 'D'.
t=13; //If c='d' or c='D', Then assign the t value as 13.
if(c=='e' || c=='e') //Checking variable c value is 'e' or 'E'.
t=14; //If c='e' or c='E', Then assign the t value as 14.
if(c=='F' || c=='f') //Checking variable c value is 'f' or 'F'.
t=15; //If c='f' or c='F', Then assign the t value as 15.
dec=dec+(t*findPower(16,power)); //Adding the value to variable dec, after power
value(This value is finding by calling a User
Defined Method) is multiplying with digit value.

}//end of loop.

Decimal To Octal Conversion

String octal=""; //Declare and Initialize the String variable 'octal' as


nothing/null("").

while(dec!=0) //Iterate a loop until dec value becomes 0.


{
int rem=dec%8; //Divide dec value by 8 and store the remainder in 'rem'.
octal=rem+octal; //Concat the remainder with String 'octal' and store in 'octal'
variable.
dec=dec/8; //Divides the Number (dec) by 8.
}//end of loop.

System.out.println(octal); //Print the octal variable(It is nothing but octal value).

}//end of Main Method.

public static int findPower(int base,int p) //This Method have to return the power value and
return type is int.
{
int value=1; //Declare and Initialize the 'value' variable as 1(This
variable is used to store the power value).
for(int j=1;j<=p;j++) //Iterating the loop for p(power) times.
{
value=value*base; //Multiplying the base with value and storing the result in
'value' variable.
}//end of Loop.

return value; //Here returns the int value to main method from User defined
method.
}//end of user defined method (findPower).
}//end of Class.

Tracing of Code:-
Input:- 1f4
Output:- 764

hd dec power i c value j base p


1f4 0 0
2 4àfindPower(16,power) 16 0
1 1à Loop Breaks here then return ‘value’.
0+(4*1)=4
1 1 fàfindPower(16,power) 16 1
1
1*16=16 ß1
2à Loop Breaks here then return ‘value’.
4+(15*16)=244
2 0 1àfindPower(16,power) 16 2
1
1*16=16 ß1
16*16=256 ß2
3à Loop Breaks here then return ‘value’.
244+(1*256)=500 3 -1à Loop Breaks here

dec octal rem


500 “”
500/8=62 4 ß500%8=4
62/8=7 6+4=64 ß62%8=6
7/8=0 7+64=764 ß7%8=7
à Loop Breaks here

• Finally Print the octal variable it is nothing but octal Number.

Tracing of Code:-
Input:- ad8
Output:- 5330
hd dec power i c value j base p
ad8 0 0
2 8àfindPower(16,power) 16 0
1à Loop Breaks here then return ‘value’.
0+(8*1)=8
1 1 dàfindPower(16,power) 16 0
1
1*16=16 ß1
2à Loop Breaks here then return ‘value’.
8+(13*16)=216
2 0 aàfindPower(16,power) 16 2
1
1*16=16 ß1
16*16=256 ß2
3à Loop Breaks here then return ‘value’.
216+(10*256)=2776 3 -1à Loop Breaks here

dec binary rem


2776 “”
2776/8=347 0 ß2776%8=0
347/8=43 3+0=30 ß347%8=3
43/8=5 3+30=330 ß43%8=3
5/8=0 5+330=5330 ß5%8=5
à Loop Breaks here

• Finally Print the octal variable it is nothing but octal Number.

Prime Number(without using count):-


Q) Write a program to check given number is prime number or not without using count?

• To Find and Print the Given Number is Prime Number or not without using count, follow the below
logic and steps.
• Input:- One Integer value (n).
• Output:- Print the Prime Number or not.
• Logic:-
§ Logic to identify the Given Number is Prime Number or not.
§ Here our Logic is Dividing given number with each and every number from 2 if divides perfectly
then checking i and n values.
§ If both are equal means its a Perfect Prime Number or else Not a Prime Number.

Step:-1 Take the Input from user (n).


Step:-2 Declare and Initialize the i variable as static and global.
Step:-3 Call a User Defined Method isPrime(int n) by passing a given number n as variable.

Step:-4 In User Defined Method Divides the Passing value(n) with i value.
Step:-5 If Remainder is 0 then take break goto step-8.
Step:-6 Increment the i value by 1.
Step:-7 Repeat the Steps from 4 to 6.

Step:-8 Now check i value is equal to Given Number(n) or not, if yes goto Step-9 else Step-10.
Step:-9 Print "Given Number is Prime Number".
Step:-10 Print "Given Number is Not a Prime Number"..

Note:-
• Here i value starts from 2.
• For this logic we are ending/stopping/taking break at any of these 2 conditions.
1. when for loop condition becomes false, at that time 'i' value becomes given number(i=num).
2. At the time of execute "break" (It means a factor identified for a given number), at that time
'i' value becomes less than given number(i<num).

import java.util.Scanner;
public class A {
static int i;
public static void main(String[] args) {
// TODO Auto-generated method stub

Scanner obj=new Scanner(System.in);

System.out.println("Enter the Number");


int n=obj.nextInt(); //Taking input from user (n).
isPrime(n); //Calling a Method by passing one Integer and Return type is
void.
if(i==n && n!=1) //Checking i value is equal to given value or not.
System.out.println("Given Number is Prime Number");
else
System.out.println("Given Number is Not a Prime Number");

}//end of Main Method.

public static void isPrime(int n)


{
for(i=2;i<=n;i++) //Iterating the loop from 2 to the given value (n).
{
if(n%i==0) //Checking Given Number(n) is perfectly divides with i or not.
{
break; //If-condition satisfies then above loop will breaks forcibly.
}//end of if-condition.

}//end of loop.

}//end of user defined method (isPrime).

}//end of Class.

Tracing of Code:-
Input:- 7
Output:- Given Number is Prime Number

n i n%i
7
isPrime(7)
2 7%2!=0
3 7%3!=0
4 7%4!=0
5 7%5!=0
6 7%6!=0
7à Loop Breaks here
Ending of IsPrime User defined Method.

• Finally Checks the i value is equal to n or not if n Print “Given Number is Prime Number” else
Print “Given Number is Not a Prime Number”.

Tracing of Code:-
Input:- 9
Output:- Given Number is Not a Prime Number

n i n%i
9
isPrime(9)
2 9%2!=0
3 9%3==0à Loop Breaks here
Ending of IsPrime User defined Method.

• Finally Checks the i value is equal to n or not if n Print “Given Number is Prime Number” else
Print “Given Number is Not a Prime Number”.

Alternative Palindromes(boolean value):-


Q) Write a Program to print the alternative Palindromes in the given range by using Boolean value?

Ø Difference between range of the given numbers and between the given number?
• Range--> Including the given limits.
• Between--> Excluding the given limits.

This Program is for between the given Range.


• To Print the sum of all Palindromes between the Given Range and Between the Given Numbers, follow
the below logic and steps.
• Input:- Two Integer values (min and max).
• Output:- Print the sum of all palindromes between the given range.
• logic:-
o Store unit digit from the number.
o Add the stored value to rev variable after multiplying by 10.
o Remove the units digit from the given number
o Repeat the above same steps until given number becomes 0.
o Check the rev value with given value if both are equal Print add the number(Print the numbers
alternatively).
o Repeat the same process for the numbers between values of min to max.

Step:-1 Take the Inputs from user (min and max).


Step:-2 Initialize and declare the boolean variable b as 'true'.
Step:-3 Initialize and declare the variable rev as 0.
Step:-4 Duplicate the i value into variable t.
Step:-5 Store the units place of t.
Step:-6 Multiply the rev value by 10 and add the above stored value to the rev variable.
Step:-7 Remove the units place from the variable t.
Step:-8 Repeat the steps from 4 to 7 until t value becomes 0 (t>0).
Step:-9 Now Compare the variable rev and variable i.
Step:-10 If equals goto step-11 else goto step-15.
Step:-11 Check the boolean variable is 'true' or not if 'true' goto step-12 else goto step-14.
Step:-12 Print the i value or rev value.
Step:-13 Reassign the value of b as 'false'
Step:-14 Reassign the value of b as 'true'
Step:-15 Increment the i value by 1.
Step:-16 Repeat the steps from 3 to 15 until i value reaches max (i<=max).

Note:-
• i value starts from min value.
• Here using boolean variable to skip second value from Printing for every 2 elements.

import java.util.Scanner;

public class AltPalindromesBwOrRange {

public static void main(String[] args) {


Scanner obj=new Scanner(System.in);
System.out.println("Enter the min and max values");
int min=obj.nextInt(); //Taking input from user (min).
int max=obj.nextInt(); //Taking input from user (max).

boolean b=true; //Initialize and declare the boolean variable b as 'true'.


for(int i=min;i<=max;i++) //Iterating loop from min value to max value in the form
of 'i' variable.
{
int rev=0,t=i; //Initialize and declare the variable rev as 0,
duplicating the value of i into t(It is nothing but
resetting the value).
while(t!=0) //Iterating a loop upto t value becomes 0.
{
int r=t%10; //Taking the Last Digit(once place) value from t variable
and Storing in variable r.
rev=rev*10+r; //Multiplying rev variable by 10 and adding r.
t=t/10; //Removing the units places digit from variable t.
}//end of while-loop.
if(rev==i) //Comparing the 'rev' value with 'i' value to check
Palindrome or Not.
{
if(b) //Checking 'b' value is 'true' or not(to decide the
value has Print or skip the print).
{
System.out.println(i); //Printing the 'i' value.
b=false; //Reassigning the b value as 'false' to skip the
Printing in next Iteration.
}//end of if-condition(b).
else
b=true; //Reassigning the b value as 'true' to print in the
next Iteration.
}//end of if-condition(rev==i).

}//end of for-loop.
}//end of Main Method.
}//end of Class.

Even numbers between the given range(using BREAK and CONTINUE):-


Q) Write a Program to print the even numbers between the given range by using BREAK and CONTINUE
statements?

Ø Difference between range of the given numbers and between the given number?
• Range--> Including the given limits.
• Between--> Excluding the given limits.

• This Program is for between the given Range.


• To Print the All Even Numbers between the given Range, have to follow the below logic and steps
• Input:- Two Integer values.
• Output:- Print the All even Numbers.
• Logic:-
• While number Divides with 2 if remainder is zero then Print the number.
• This logic have to do for each and every number between/range of Given two numbers.

Step:-1 Take the Inputs from user (min and max).


Step:-2 Check for Remainder when min value is divides with 2.

Step:-3 If remainder 2 Print the min value.

Step:-4 Increment the min variable by 1.

Step:-5 Check min value is less than or equal to max value or not.

Step:-6 If min value is less than or equal to max value then execute the 'continue' statement(To
continue the process).
Step:-7 If min value is not less than or equal to max value then execute the 'break' statement(To
break the process).

Step:-8 Repeat the steps from 2 to 7.


public class EvenNumbersBwOrRangeByUsingBreakAndContinue {

public static void main(String[] args) {


Scanner obj=new Scanner(System.in);

System.out.println("Enter the Min and Max values");


int min=obj.nextInt(); //Taking inputs from the user(min).
int max=obj.nextInt(); //Taking inputs from the user(max).

while(true) //Iterating a Loop for Infinity time.


{
if(min%2==0) //Checking min value even or not.
System.out.println(min); //If even Printing the min value here.
min++; //Increment the min value by 1.
if(min<=max) //Checking min value is less than or equal to max or
not.
continue; //If less than here executes the ‘continue’ statement
to continue the process.
else
break; //If not less than executes the ‘break’ statement to
break the Iteration by forcibly.

}//end of while loop.

}//end of main Method.

}//end of Class.

Tracing of Code:-
Input:- 10 20
Output:- 10 12 14 16 18 20

max min%2 output min min<=max


20 10
10%2==0 10 11 continue
11%2==1 12 continue
12%2==0 12 13 continue
13%2==1 14 continue
14%2==0 14 15 continue
15%2==1 16 continue
16%2==0 16 17 continue
17%2==1 18 continue
18%2==0 18 19 continue
19%2==1 20 continue
20%2==0 20 21 break à Loop Breaks here

Tracing of Code:-
Input:- 15 31
Output:- 16 18 20 22 24 26 28 30

max min%2 output min min<=max


31 15
15%2==1 16 continue
16%2==0 16 17 continue
17%2==1 18 continue
18%2==0 18 19 continue
19%2==1 20 continue
20%2==0 20 21 continue
21%2==1 22 continue
22%2==0 22 23 continue
23%2==1 24 continue
24%2==0 24 25 continue
25%2==1 26 continue
26%2==0 26 27 continue
27%2==1 28 continue
28%2==0 28 29 continue
29%2==1 30 continue
30%2==0 30 31 continue
31%2==1 32 break à Loop Breaks here
Prime numbers between the given range(using BREAK and CONTINUE):-
Q) Write a Program to print the Prime numbers between the given range by using BREAK and CONTINUE
statements?

Ø Difference between range of the given numbers and between the given number?
• Range--> Including the given limits.
• Between-->Excluding the given limits.

This Program is for between the given Range.


• To Print the Prime numbers between the given range, have to follow the below logic and steps.
• Input:- two Integer values.
• OutPut:- Print the Prime Numbers between the given range values.
• Logic:- From the starting number to ending number we have to check each and every number is that
number is Prime or not, if it is Prime then Print that number or else no need to Print that number.

Step:-1 Take the two input numbers from the user, and store the values in two variables for example,
small and big are the variables.
Step:-2 Declare a j variable.
Step:-3 Now Divide the small with j.
Step:-4 If remainder is '0' take a break(forcibly stop the loop) and goto step-8.
Step:-5 If remainder is not '0' then continue the process.
Step:-6 Increment the j value by 1.
Step:-7 Repeat the steps from 3 to 6 until j less than of small(j<small).
Step:-8 check small value and j value are equal or not if equal Print the Number 'small'.
Step:-9 Now Increment the small value by 1.
Step:-10 Repeat the steps from 3 to 9 until i less than of big(i<big).

Note:-
• Here j value starts from 2.
• Same Logic for Between the given range also but excluding user given numbers.

import java.util.Scanner;
public class PrimeBwOrRangeUsingBreakcontinue {

public static void main(String[] args) {


Scanner obj=new Scanner(System.in);

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


int small=obj.nextInt(); //Taking input from the user.

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


int big=obj.nextInt(); //Taking input from the user.

int j; //Declare j variable.


while(true) //Iterating loop for infinity times.
{
for(j=2;j<small;j++) //Iterating j value from 2 to the before value of small.
{
if(small%j==0) //Here dividing number small with j and checking is
remainder 0 or not.
break; //If 0 execute the break statement (it means one factor is
identify neither then 1 and itself).
else
continue; //If not 0 execute the continue statement.
}//end of j-loop.
if(j==small && small!=1) //For Prime number, factors are 0(Excluding 1 and Itself),
so here checking small is equal to j or not.
System.out.println(small); //If equal then Printing that respective number-small
value.

small++; //Increment small variable by 1.


if(small<=big) //Checking small is less than or equal to big or not.
continue; //If yes then 'continue' statement to continue the process.
else
break; //If no then 'break' statement to break the Iteration
forcibly.

}//end of while-loop.

}//end of main method.


}//end of class.
Q) Develop a Program to print the following pattern for Dynamic input.
*****
*****
*****
*****
*****

To print the Given Pattern, follow the below logic and steps.
• Input:- First Line Consists of One Integer value (n).
• Output:- Print the Given pattern.
• Logic:-
• Logic for the given pattern is
• We have stars in given number(n) of lines.
• Each and every line we have Stars for given number(n) of times.

Step:-1 Take the Input from user (a).


Step:-2 Declare and Initialize the i variable as 0.
Step:-3 Declare and Initialize the j variable as 0.
Step:-4 Print the Star(*).
Step:-5 Increment the j value by 1.
Step:-6 Repeat the steps from 4 to 5 until j value reaches before value of Given Number(n).
Step:-7 Sent the cursor to Next Line(System.out.println();).
Step:-8 Increment the i value by 1.
Step:-9 Repeat the steps from 3 to 8 until i value reaches before value of Given Number(n).

Examples:-
If input is 5 then print
*****
*****
*****
*****
*****

If input is 4 then print


****
****
****
****

Note:-
• Here i value is used for rows.
• Here j value is used for columns.

import java.util.Scanner;
public class Pattern1 {

public static void main(String[] args) {

Scanner obj=new Scanner(System.in);

System.out.println("Enter n value");
int n=obj.nextInt(); //Taking input from the user(n).

for(int i=0;i<n;i++) //Iterating a loop form 0 to n(n times) for rows.


{
for(int j=0;j<n;j++) //Iterating a loop form 0 to n(n times) for columns.
{
System.out.print("*"); //Printing the star(*).

}//end of j-loop(columns loop).

System.out.println(); //sending Cursor to next Line.

}//end of i- loop(rows loop).

}//end of main Method.

}//end of Class.
Q) Develop a Program to print the following pattern for Dynamic input.
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15

To print the Given Pattern, follow the below logic and steps.
• Input:-
o First Line Consists of One Integer value (rows).
o Second Line Consists of One Integer value (columns).
• Output:- Print the Given pattern.
• Logic:- Logic for the given pattern is

Step:-1 Take the Input from user (rows).


Step:-2 Take the Input from user (columns).
Step:-3 Declare and Initialize the printValue variable as 1.
Step:-4 Declare and Initialize the i variable as 0.
Step:-5 Declare and Initialize the j variable as 0.
Step:-6 Print the variable printValue.
Step:-7 Increment the printValue variable value by 1.
Step:-8 Increment the j value by 1.
Step:-9 Repeat the steps from 6 to 8 until j value reaches before value of Given No of
columns(columns).
Step:-10 Sent the cursor to Next Line(System.out.println();).
Step:-11 Increment the i value by 1.
Step:-12 Repeat the steps from 5 to 11 until i value reaches before value of Given No of rows(rows).

Examples:-

If inputs is 5 and 3 then print


1 2 3 4 5
6 7 8 9 10
11 12 13 14 15

If input is 4 and 5 then print


1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
17 18 19 20

Note:-
• Here i value is used for rows.
• Here j value is used for columns.

import java.util.Scanner;
public class Pattern2 {

public static void main(String[] args) {

Scanner obj=new Scanner(System.in);

System.out.println("Enter no of rows and columns");


int rows=obj.nextInt(); //Taking input from the user(rows) for no of rows.
int columns=obj.nextInt(); //Taking input from the user(columns) for no of columns.

int printValue=1; //Declare and Initialize the printValue Variable as 1.

for(int i=0;i<rows;i++) //Iterating a loop form 0 to rows(rows times) for rows.


{
for(int j=0;j<columns;j++) //Iterating a loop form 0 to columns(columns times)
for columns.
{
System.out.print(printValue+" "); //Printing the printValue Variable.
printValue++; //Incrementing the printValue by 1.

}//end of j-loop(columns loop).

System.out.println(); //sending cursor to next Line.

}//end of i- loop(rows loop).


}//end of main Method.

}//end of Class.

Q) Develop a Program to print the following pattern for Dynamic input.


* * * * * *
* # # # # *
* # # # # *
* # # # # *
* # # # # *
* * * * * *

To print the Given Pattern, follow the below logic and steps.
• Input:- First Line Consists of One Integer value (n).
• Output:- Print the Given pattern.
• Logic:- Logic for the given pattern is

Step:-1 Take the Input from user (n).

Step:-2 Declare and Initialize the i variable as 0.

Step:-3 Declare and Initialize the j variable as 0.

Step:-4 Check the i value or j value is n-1 or 0 (Particular location is belongs to circumference or
not), if yes goto step-5 else goto step-6.

Step:-5 if yes then print * followed by one space.

Step:-6 if No then print # followed by one space.

Step:-7 Increment the j value by 1.

Step:-8 Repeat the steps from 4 to 7 until j value reaches before value of Given Number(n).
Step:-9 Sent the cursor to Next Line(System.out.println();).
Step:-10 Increment the i value by 1.

Step:-11 Repeat the steps from 3 to 10 until i value reaches before value of Given Number(n).

Examples:-
If Input is 6 then
* * * * * *
* # # # # *
* # # # # *
* # # # # *
* # # # # *
* * * * * *

If Input is 8 then
* * * * * * * *
* # # # # # # *
* # # # # # # *
* # # # # # # *
* # # # # # # *
* # # # # # # *
* # # # # # # *
* * * * * * * *

Note:-
• Here i value is used for rows.
• Here j value is used for columns.

import java.util.Scanner;
public class Pattern3 {

public static void main(String[] args) {


Scanner obj=new Scanner(System.in);

System.out.println("Enter no of rows");
int n=obj.nextInt(); //Taking input from the user(n) for no of rows.

for(int i=0;i<n;i++) //Iterating a loop form 0 to n(n times) for rows.


{
for(int j=0;j<n;j++) //Iterating a loop form 0 to n(n times) for columns.
{
if(j==0 || j==n-1 || i==0 || i==n-1) //Checking that i or j values is 0 or
n-1 for identifying that this
location is belongs to circumference.
System.out.print("* "); //Printing the star(*).
else
System.out.print("# "); //Printing the #.

}//end of j-loop(columns loop).

System.out.println(); //sending cursor to next Line.

}//end of i- loop(rows loop).

}//end of main Method.

}//end of Class.

Q) Develop a Program to print the following pattern for Dynamic input.


1*2*3*4*5
6*7*8*9*10
11*12*13*14*15
16*17*18*19*20
21*22*23*24*25
To print the Given Pattern, follow the below logic and steps.
• Input:- First Line Consists of One Integer value (n).
• Output:- Print the Given pattern.
• Logic:- Logic for the given pattern is

Step:-1 Take the Input from user (n).


Step:-2 Declare and Initialize the printValue variable as 1.
Step:-3 Declare and Initialize the i variable as 0.
Step:-4 Declare and Initialize the j variable as 0.
Step:-5 Check the j value is n-1 (To know that Print * or not), if yes goto step-6 else goto step-7.
Step:-6 if yes then print the printValue.
Step:-7 if yes then print the printValue followed by one *.
Step:-8 Increment the printValue variable by 1.
Step:-9 Increment the j value by 1.
Step:-10 Repeat the steps from 5 to 9 until j value reaches before value of Given Number(n).
Step:-11 Sent the cursor to Next Line(System.out.println();).
Step:-12 Increment the i value by 1.
Step:-13 Repeat the steps from 4 to 12 until i value reaches before value of Given Number(n).

Examples:-

If input is 5 then
1*2*3*4*5
6*7*8*9*10
11*12*13*14*15
16*17*18*19*20
21*22*23*24*25

If input is 7 then
1*2*3*4*5*6*7
8*9*10*11*12*13*14
15*16*17*18*19*20*21
22*23*24*25*26*27*28
29*30*31*32*33*34*35
36*37*38*39*40*41*42
43*44*45*46*47*48*49
Note:-
• Here i value is used for rows.
• Here j value is used for columns.

import java.util.Scanner;
public class Pattern4 {

public static void main(String[] args) {

Scanner obj=new Scanner(System.in);

System.out.println("Enter no of rows");
int n=obj.nextInt(); //Taking input from the user(n) for no of rows.

int printValue=1; //Declare and Initialize the printValue as 1.


for(int i=0;i<n;i++) //Iterating a loop form 0 to n(n times) for rows.
{
for(int j=0;j<n;j++) //Iterating a loop form 0 to n(n times) for columns.
{
if(j==n-1) //Checking that j values is n-1 for identifying that this
location is end of the line or not.
System.out.print(printValue); //Printing the printValue variable.
else
System.out.print(printValue+"*"); //Printing the printValue variable
followed by *.

printValue++; //Increment the printValue by 1.


}//end of j-loop(columns loop).

System.out.println(); //sending cursor to next Line.

}//end of i-loop(rows loop).

}//end of main Method.

}//end of Class.

You might also like