In this, part of programming tutorial i will try to explaining more than 50 java * pattern program, Which are asked in interview and online exams and university exam with different logic for the same type of pattern
Program 1- Arrow pattern Program
import java.util.Scanner; public class MainClass { public static void main(String[] args) { Scanner sc = new Scanner(System.in); //Taking rows value from the user System.out.println("How many rows you want in this pattern?"); int rows = sc.nextInt(); System.out.println("Here is your pattern....!!!"); //Printing upper half of the pattern for (int i = 1; i <= rows; i++) { for (int j = 1; j <= i; j++) { System.out.print(j+" "); } System.out.println(); } //Printing lower half of the pattern for (int i = rows-1; i >= 1; i--) { for (int j = 1; j <= i; j++) { System.out.print(j+" "); } System.out.println(); } //Closing the resources sc.close(); } }
Output
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4 5 6
1 2 3 4 5 6 7
1 2 3 4 5 6
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
Youtube Link-
Program 2- Right Angle Triangle
import java.util.Scanner;
public class MainClass
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
//Taking rows value from the user
System.out.println("How many rows you want in this pattern?");
int rows = sc.nextInt();
System.out.println("Here is your pattern....!!!");
for (int i = 1; i <= rows; i++)
{
for (int j = 1; j <= i; j++)
{
System.out.print(j+" ");
}
System.out.println();
}
//Close the resources
sc.close();
}
}
Output-
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4 5 6
1 2 3 4 5 6 7
Youtube Link-
Program 3- Number pattern program decreasing order
import java.util.Scanner;
public class MainClass
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
//Taking rows value from the user
System.out.println("How many rows you want in this pattern?");
int rows = sc.nextInt();
System.out.println("Here is your pattern....!!!");
for (int i = 1; i <= rows; i++)
{
for (int j = rows; j >= i; j--)
{
System.out.print(j+" ");
}
System.out.println();
}
//Closing the resources
sc.close();
}
}
Output-
7 6 5 4 3 2 1
6 5 4 3 2 1
5 4 3 2 1
4 3 2 1
3 2 1
2 1
1
Youtube Link-
Program 4- Number pattern Program with sequence according to raw and Column
import java.util.Scanner;
public class MainClass
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
//Taking rows value from the user
System.out.println("How many rows you want in this pattern?");
int rows = sc.nextInt();
System.out.println("Here is your pattern....!!!");
for (int i = rows; i >= 1; i--)
{
for (int j = 1; j <= i; j++)
{
System.out.print(j+" ");
}
System.out.println();
}
//Closing the resources
sc.close();
}
}
Output-
1 2 3 4 5 6 7
1 2 3 4 5 6
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
Youtube Link-
Program 5- Palindrome Number Program Java
A palindromic number is a number that is the same when written forwards or backwards always be same, i.e., of the form . The first few palindromic numbersare therefore are 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 22, 33, 44, 55, 66, 77, 88, 99, 101, 111, 121.
class Palindrome_Dizsweb{
public static void main(String args[])
{
{
int r,sum=0;
int temp;
int n=656;
temp=n;
while(n>0)
{
{
r=n%10;
sum=(sum*10)+r;
n=n/10;
}
if(temp==sum)
System.out.println("Palindrome Number YES");
else
System.out.println("Palindrome Number NO");
}
}
Program 6 - Fibonacci Series Program Java
A palindromic number is a number that is the same when written forwards or backwards always be same, i.e., of the form . The first few palindromic numbersare.
import java.util.Scanner;
}
Output-
Enter value of n:5
Fibonacci Series:0 1 1 2 3
Youtube Link-
Program 6- Number pattern Program with sequence according to raw and Column
import java.util.Scanner;
public class Rectangular_Pattern_Program
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
//Taking rows value from the user to print the star pattern
System.out.println("How many rows you want in this pattern?");
int rows = scan.nextInt();*
System.out.println("Here is your pattern....!!!");
for (int i = rows; i >= 1; i--)
{ for (int j = 1; j <= rows/2; j++)
{
print(" ");
}
for (int j = 1; j <= ((i*2)-1); j++)
{
// Printing of star
System.out.print("* ");
}
for (int j = 1; j <= i; j++) {
print(" ");
}
for (int j = i; j < i-; j--)
{
// Printing of star
System.out.print("* ");
}
//Linr termionation will be done when internal loop will complete once
System.out.println();
}
//Closing the resources
sc.close();
}
}
Output-
*
* * *
* * * * *
* * * * * * *
* * * * *
* * *
*
Program 7- Leap Year Program
Task to Complete
To check whether given gear is a year is leap year or not
Following program is divided into two main Logic. If any logic fails then the given year is not a leap year.
All these logic should be implemented in same sequence
Logic 1:
- We divide the year by 400
- If remainder is 0 then it is leap year.
- if not then we should go for second.
Logic 2:
- We first divide the year by 4 and check remainder.
- if remainder is 0 then it is a leap year.
- If it is not divisible by 4 then it is not a leap year
- year should also be not divisible by 100.
Then it is a leap year
import java.util.Scanner;public class Leap Year { public static void main(String[] args) { //scanner class declaration Scanner scaner_sawan=new Scanner(System.in); //input year from user System.out.println("Enter a value of Year"); int year = sc.nextInt(); //condition for checking year if((year % 4 == 0 && year % 100!=0) || year % 400 == 0) System.out.println(year + " is a leap year."); else System.out.println(year + " is not a leap year."); } }
Output:-
When input is :- 2000:- It is leap year
When input is :- 1900:- It is Not leap year
No comments:
Post a Comment