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 #1:
Number Line
Sample input:
6
Sample output
123456
Solution:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
import java.util.Scanner; public class Task1{ public static void main(String[] args){ int n; Scanner sc = new Scanner(System.in); System.out.println("Input a number"); n = sc.nextInt(); for(int c=1;c<=n;c++){ System.out.print(c); } } } |
Problem #2:
Star Line
Print as many stars as given in input
Sample input:
6
Sample output
******
Solution:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
import java.util.Scanner; public class Task2{ public static void main(String[] args){ int n; Scanner sc = new Scanner(System.in); System.out.print("Input a number"); n = sc.nextInt(); for(int c=1;c<=n;c++){ System.out.print("*"); } } } |
Problem #3:
Rectangle Star
Sample input:
4
6
Sample output
******
******
******
******
Solution:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
import java.util.Scanner; public class Task3{ public static void main(String[] args){ int n; Scanner sc = new Scanner(System.in); System.out.print("Input row"); int row = sc.nextInt(); System.out.print("Input a number"); n = sc.nextInt(); //r define as row here for(int r = 1;r<=row;r++){ //c define as column here for(int c=1;c<=n;c++){ System.out.print("*"); } System.out.print("\n"); } } } |
Problem #4:
Rectangle Number
Sample input:
4
6
Sample output
123456
123456
123456
123456
Solution:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
import java.util.Scanner; public class Task4{ public static void main(String[] args){ int n; Scanner sc = new Scanner(System.in); System.out.print("Input row"); int row = sc.nextInt(); System.out.print("Input a number"); n = sc.nextInt(); //r define as row here for(int r = 1;r<=row;r++){ //c define as row here for(int c=1;c<=n;c++){ System.out.print(c); } System.out.print("\n"); } } } |
Problem #5:
Triangle – Left 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 |
import java.util.Scanner; public class Task5{ public static void main(String[] args){ int n; Scanner sc = new Scanner(System.in); System.out.print("Input row"); int row = sc.nextInt(); //r define as row here for(int r = 1 ;r<=row;r++){ //c define as row here for(int c=1;c<=r;c++){ System.out.print("*"); } System.out.print("\n"); } } } |