Hi everyone … I will share some java source code which will help beginner level programmer to solve basic programming problems..
All code has been solved by : Afjalur Rahman Rana
Note: firstly try to solve the problem by yourself.. then if you can’t see the solution & if you find difficulty to any code then comment below… I will try to give you proper guide 🙂
Problem #6:
Triangle – Left Justified
Draw right angled triangle of given height
Sample input:
Sample input:
4
Sample output
1
12
123
1234
Solution:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
import java.util.Scanner; public class Task6{ public static void main(String[] args){ int n; Scanner sc = new Scanner(System.in); System.out.print("Input row"); int row = sc.nextInt(); for(int r = 0 ;r<=row;r++){ for(int c=1;c<=r;c++){ System.out.print(c); } System.out.print("\n"); } } } |
Problem #7:
Triangle – Right Justified
Draw right angled triangle of given height
Sample input:
4
Sample output
*
**
***
****
Solution:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
import java.util.Scanner; public class Task7{ public static void main(String[] args){ int n; Scanner sc = new Scanner(System.in); System.out.print("Input row"); int row = sc.nextInt(); for(int r = 1 ;r<=row;r++){ //while row is increasing space is decreasing for(int space = 1;space<=row-r;space++){ System.out.print(" "); } for(int c=1;c<=r;c++){ System.out.print("*"); } System.out.print("\n"); } } } |
Problem #8:
Triangle – Right Justified
Draw right angled triangle of given height
Sample input:
4
Sample output
1
12
123
1234
Solution:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
import java.util.Scanner; public class Task8{ public static void main(String[] args){ int n; Scanner sc = new Scanner(System.in); System.out.print("Input row"); int row = sc.nextInt(); for(int r = 1 ;r<=row;r++){ //while row is increasing space is decreasing for(int space = 1;space<=row-r;space++){ System.out.print(" "); } for(int c=1;c<=r;c++){ System.out.print(c); } System.out.print("\n"); } } } |
Problem #9:
Rectangle Number
Sample input:
3
Sample output
Triangle – Isosceles
Draw triangle of given height
Solution:
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 |
import java.util.Scanner; public class Task9{ public static void main(String[] args){ int n; Scanner sc = new Scanner(System.in); System.out.print("Input row"); int row = sc.nextInt(); int j =1; for(int r = 1 ;r<=row;r++){ //while row is increasing space is decreasing for(int space = 1;space<=row-r;space++){ System.out.print(" "); } for(int c=1;c<=j;c++){ System.out.print("*"); } j+=2; System.out.print("\n"); } } } |
Problem #10:
Triangle – Isosceles
Draw triangle of given height
Sample input:
4
Sample output
1
123
12345
Solution:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
import java.util.Scanner; public class Task10{ public static void main(String[] args){ int n; Scanner sc = new Scanner(System.in); System.out.print("Input row"); int row = sc.nextInt(); int j =1; for(int r = 1 ;r<=row;r++){ //while row is increasing space is decreasing for(int space = 1;space<=row-r;space++){ System.out.print(" "); } for(int c=1;c<=j;c++){ System.out.print(c); } j+=2; System.out.print("\n"); } } } |