Cvcorp
Cvcorp
• 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.
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).
import java.util.Scanner;
public class FibonacciSeries {
}//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
• 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.
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 {
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
• 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.
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.
import java.util.Scanner;
public class SumOfFibonacciSeries {
}//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.
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 {
}//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.
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 {
}//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
• 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.
import java.util.Scanner;
public class SumOfFactorials {
}//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.
• 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.
import java.util.Scanner;
public class Reverse {
}//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
• 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".
import java.util.Scanner;
public class Palindrome {
}//end of Class.
Ø Difference between range of the given numbers and between the given number?
• Range--> Including the given limits.
• Between-->Excluding the given limits.
import java.util.Scanner;
}//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
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.
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;
}//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
Ø Difference between range of the given numbers and between the given number?
• Range--> Including the given limits.
• Between--> Excluding the given limits.
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;
}//end of for-loop.
}//end of Class.
Tracing of Code:-
Input:- 1 10
Output:- 1 3 5 7 9
Ø Difference between range of the given numbers and between the given number?
• Range--> Including the given limits.
• Between--> Excluding the given limits.
Note:-
• Here i value starts from min value.
• Here j value starts from 1.
import java.util.Scanner;
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).
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.
import java.util.Scanner;
Ø Difference between range of the given numbers and between the given number?
• Range--> Including the given limits.
• Between-->Excluding the given limits.
Note:-
• Here i value starts from min value.
• Here j value starts from 1.
import java.util.Scanner;
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.
}//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)?
import java.util.Scanner;
public class EvenOrOddWay1 {
int UnitsPlace=num%10; //Taking Units digit of Given Number and storing in UnitsPlace
varaible.
}//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
import java.util.Scanner;
public class EvenOrOddWay2 {
int temp=num/2; //Here given value divides by 2 and store into temp variable.
}//end of Class.
}//end of Class.
• 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.
import java.util.Scanner;
public class PowerWay1 {
• 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.
import java.util.Scanner;
public class PowerWay1 {
• 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.
import java.util.Scanner;
public class PowerWay3 {
public static void main(String[] args) {
Scanner obj=new Scanner(System.in);
• 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.
import java.util.Scanner;
public class PowerWay4 {
}//end of Class.
• 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.
import java.util.Scanner;
}//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.
import java.util.Scanner;
}//end of Class.
import java.util.Scanner;
}//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.
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
• 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".
import java.util.Scanner;
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.
import java.util.Scanner;
}//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.
import java.util.Scanner;
• 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"..
import java.util.Scanner;
}//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.
• 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.
import java.util.Scanner;
}//end of loop.
}//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.
• 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.
import java.util.Scanner;
}//end of Class.
• 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.
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;
}//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) ,...........
Note:-
• a-->First term in Arithmetic Progression.
• d-->Common Difference.
• n-->No of Terms.
• Here i value starts from 0.
import java.util.Scanner;
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)).
}//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).
Note:-
• a-->First term in Arithmetic Progression.
• d-->Common Difference.
• n-->No of Terms.
import java.util.Scanner;
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)).
}//end of Class.
• 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.
Note:-
• a-->First term in Arithmetic Progression.
• d-->Common Difference.
• n-->No of Terms.
• Here i value starts from 2.
import java.util.Scanner;
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)).
}//end of Class.
• 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))
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;
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)).
}//end of Class.
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:-7 Repeat the steps from 5 to 6 until i value reaches before value of Given Number(n).
import java.util.Scanner;
public class SumOfTermsInArithmeticProgressionWithOutUsingFormula {
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)).
}//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
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;
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 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)) ,...........
Note:-
• a-->First term in Harmonic Progression.
• d-->Common Difference.
• n-->Number of Terms.
• Here i value starts from 0.
import java.util.Scanner;
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 Class.
• 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.
import java.util.Scanner;
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)).
}//end of Class.
• 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;
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)).
}//end of Class.
• 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:-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.
import java.util.Scanner;
System.out.println("Enter b value");
int b=obj.nextInt(); //Taking input from user (Second value(b)).
}//end of Class.
• 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.
import java.util.Scanner;
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)).
}//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.
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;
}//end of Class.
• 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.
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;
}//end of Class.
• 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.
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;
}//end of loop.
System.out.println(s); //Print the s value (It is nothing but Hexa Decimal value).
}//end of Class.
• 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
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 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
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
• 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).
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
If Input is 1011000
000-->0
011-->3
1-->1
Final value is 130
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 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
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
• 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).
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;
String hd=""; //Declare and Initialize the String variable 'hd' as nothing/null("").
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
Tracing of Code:-
Input:- 11101001
Output:- E9
• 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).
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:-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).
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;
}//end of loop.
}//end of loop.
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
• 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:-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;
}//end of loop.
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
Tracing of Code:-
Input:- 233
Output:- 155
• 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.
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).
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 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
Tracing of Code:-
Input:- 500
Output:- 140
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
• 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.
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:-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:-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 {
}//end of loop.
}//end of loop.
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
Tracing of Code:-
Input:- ad8
Output:- 101011011000
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
• 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:-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:-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 {
}//end of loop.
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
Tracing of Code:-
Input:- ad8
Output:- 2776
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
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.
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
500-->764
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:-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) {
}//end of loop.
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
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
• 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:-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
}//end of loop.
}//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”.
Ø Difference between range of the given numbers and between the given number?
• Range--> Including the given limits.
• Between--> Excluding the given limits.
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;
}//end of for-loop.
}//end of Main Method.
}//end of Class.
Ø Difference between range of the given numbers and between the given number?
• Range--> Including the given limits.
• Between--> Excluding the given limits.
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).
}//end of Class.
Tracing of Code:-
Input:- 10 20
Output:- 10 12 14 16 18 20
Tracing of Code:-
Input:- 15 31
Output:- 16 18 20 22 24 26 28 30
Ø Difference between range of the given numbers and between the given number?
• Range--> Including the given limits.
• Between-->Excluding the given limits.
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 {
}//end of while-loop.
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.
Examples:-
If input is 5 then print
*****
*****
*****
*****
*****
Note:-
• Here i value is used for rows.
• Here j value is used for columns.
import java.util.Scanner;
public class Pattern1 {
System.out.println("Enter n value");
int n=obj.nextInt(); //Taking input from the user(n).
}//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
Examples:-
Note:-
• Here i value is used for rows.
• Here j value is used for columns.
import java.util.Scanner;
public class Pattern2 {
}//end of Class.
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:-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:-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 {
System.out.println("Enter no of rows");
int n=obj.nextInt(); //Taking input from the user(n) for no of rows.
}//end of Class.
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 {
System.out.println("Enter no of rows");
int n=obj.nextInt(); //Taking input from the user(n) for no of rows.
}//end of Class.