1. Write a program in Java to print the Floyd’s Triangle.
package com.w3resource;import java.util.Scanner;publicclassFloydTriangle{publicstaticvoidmain(String[] args){int i, j, n, p, q;
System.out.print("Input number of rows: ");
Scanner in =newScanner(System.in);
n = in.nextInt();for(i =1; i <= n; i++){if(i %2==0){
p =1;
q =0;}else{
p =0;
q =1;}for(j =1; j <= i; j++){if(j %2==0)
System.out.print(p);else
System.out.print(q);}
System.out.println();}}}
Output
Input number of rows:5101101010110101
2. Write a program in Java to display the n terms of odd natural number and their sum.
package com.w3resource;import java.util.Scanner;publicclassPrintOddAndSum{publicstaticvoidmain(String[] args){int i, n, sum =0;
System.out.print("Input number of terms is : ");
Scanner in =newScanner(System.in);
n = in.nextInt();
System.out.println("\nThe odd numbers are : ");for(i =1; i <= n; i++){
System.out.println(2* i -1);
sum +=2* i -1;}
System.out.println("The Sum of odd Natural Number upto "+ n +" terms is : "+ sum);}}
Output
Input number of terms is:5
The odd numbers are:13579
The Sum of odd Natural Number upto 5terms is :25
3. Write a program in Java to make such a pattern like right angle triangle with a number which will repeat a number in a row. The pattern is as follows
package com.w3resource;import java.util.Scanner;publicclassPrintRepeatNumber{publicstaticvoidmain(String[] args){int i, j, n;
System.out.print("Input number of n : ");
Scanner in =newScanner(System.in);
n = in.nextInt();for(i =1; i <= n; i++){for(j =1; j <= i; j++){
System.out.print(i);}
System.out.println();}}}
Output
Input number of n :41223334444
4. Write a program in Java to make such a pattern like right angle triangle with number increased by 1.
package com.w3resource;import java.util.Scanner;publicclassIncrementNumber{publicstaticvoidmain(String[] args){int i, j, n, k =1;
System.out.print("Input number of rows: ");
Scanner in =newScaner(System.in);
n = in.nextInt();for(i =1; i <= n; i++){for(j =1; j <= i; j++){
System.out.print(k++);}
System.out.println();}}}
Output
Input number of rows :412345678910
5. Write a program in Java to make such a pattern like a pyramid with a number which will repeat the number in the same row.
package com.w3resource;import java.util.Scanner;publicclassPrintRepeatElement{publicstaticvoidmain(String[] args){int i, j, n, s, x;
System.out.print("Input number of rows : ");
Scanner in =newScanner(System.in);
n = in.nextInt();
s = n +4-1;for(i =1; i <= n; i++){for(x = s; x !=0; x--){
System.out.print(" ");}for(j =1; j <= i; j++){
System.out.print(i +" ");}
System.out.println();
s--;}}}
Output
Input number of rows :41223334444
6. Write a Java program that accepts two floatingpoint numbers and checks whether they are the same up to two decimal places.
package com.w3resource;import java.util.Scanner;publicclassExercise{publicstaticvoidmain(String[] args){
Scanner input =newScanner(System.in);
System.out.print("Input first floatingpoint number: ");double num1 = input.nextDouble();
System.out.print("Input second floatingpoint number: ");double num2 = input.nextDouble();
input.close();if(Math.abs(num1 - num2)<=0.01){
System.out.println("These numbers are the same.");}else{
System.out.println("THese numbers are different.");}}}
Output
Input first floatingpoint number :20.569
Input second floatingpoint number :20.567
These numbers are the same.
7. Write a Java program to display the following character rhombus structure.
package com.w3resource;import java.util.Scanner;publicclassRhombusStructure{publicstaticvoidmain(String args[]){
Scanner sc =newScanner(System.in);
System.out.println("Input the number: ");int n = sc.nextInt();int count =1;int count2 =1;char ch ='A';for(int i =1; i <(n *2); i++){for(int space = n - count2; space >0; space--){
System.out.print(" ");}if(i < n){
count2++;}else{
count2--;}for(int j =0; j < count; j++){
System.out.print(ch);//print Characterif(j < count /2){
ch++;}else{
ch--;}}if(i < n){
count = count +2;}else{
count = count -2;}
ch ='A';
System.out.println();}}}
Output
Input the number:7
A
ABA
ABCBA
ABCDCBA
ABCDEDCBA
ABCDEFEDCBA
ABCDEFGFEDCBA
ABCDEFEDCBA
ABCDEDCBA
ABCDCBA
ABCBA
ABA
A
8. Write a Java program to display the number rhombus structure.
package com.w3resource;import java.util.Scanner;publicclassRhombusNumber{publicstaticvoidmain(String[] args){
Scanner in =newScanner(System.in);
System.out.print("Input the number : ");int n = in.nextInt();int no_of_spaces =1;int start =0;int count =1;for(int i =1; i <(n *2); i++){for(int space = n - no_of_spaces; space >0; space--){
System.out.print(" ");}if(i < n){
start = i;// for number
no_of_spaces++;// for spaces}else{
start = n *2- i;// for number
no_of_spaces--;// for spaces}for(int j =0; j < count; j++){
System.out.print(start);if(j < count /2){
start--;}else{
start++;}}if(i < n){
count = count +2;}else{
count = count -2;}
System.out.println();}}}
Output
Input the number :71212321234321234543212345654321234567654321234567654321234565432123454321234321232121
9. Write a program in Java to display the pattern like right angle triangle with a number.
package com.programiz;import java.util.Scanner;publicclassPrintPattern{publicstaticvoidmain(String[] args){int i, j, n;
System.out.print("Input number of rows : ");
Scanner in =newScanner(System.in);
n = in.nextInt();for(i =1; i <= n; i++){for(j =1; j <= i; j++){
System.out.print(j);}
System.out.println();}}}
10. Write a Java program to find the number of days in a month.
Conditional Statement1. Write a program in Java to print the Floyd’s Triangle.package com.w3resource;import java.util.Scanner;public class FloydTriangle { public static void main(String[] args) { int i, j, n, p, q;