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 #11:
Triangle – Right Justified
Draw right angled triangle of given height
Sample input:
Sample input:
4
Sample output
4
34
234
1234
Solution:
import java.util.Scanner;
public class Task11{
public static void main(String[] args){
int n;
int space=0;
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(space = 1;space<=row-r;space++){
System.out.print(" ");
}
for(int c=space;c<=row;c++){
System.out.print(c);
}
System.out.print("\n");
}
}
}
Problem #12:
Rhombus
Just draw the image of the above triangle once. And then, the opposite, once.
Sample input:
3
Sample output
*
***
*****
***
*
Solution:
import java.util.Scanner;
public class Task12{
public static void main(String[] args){
int n;
Scanner sc = new Scanner(System.in);
System.out.print("Input row");
int row = sc.nextInt();
row*=2;
int j =1;
int k=1;
for(int r = 1 ;r<row;r++){
if(r<=row/2){
//while row is increasing space is decreasing
for(int space = 1;space<=row/2-r;space++){
System.out.print(" ");
}
for(int c=1;c<=j;c++){
System.out.print("*");
}
j+=2;
System.out.print("\n");
//define k variable for saving the new value of j which is unachnagble
k=j-4;
}else{
//space is inceasing & column is decreasing here
for(int space=row/2;space<r;space++){
System.out.print(" ");
}
for(int c=1;c<=k;c++){
System.out.print("*");
}
k-=2;
System.out.print("\n");
}
}
}
}
Problem #13:
Rhombus
Just draw the image of the above triangle once. And then, the opposite, once.
Sample input:
Sample input:
3
Sample output
1
123
12345
123
1
Solution:
import java.util.Scanner;
public class Task13{
public static void main(String[] args){
int n;
Scanner sc = new Scanner(System.in);
System.out.print("Input row");
int row = sc.nextInt();
row*=2;
int j =1;
int k=1;
for(int r = 1 ;r<row;r++){
if(r<=row/2){
//while row is increasing space is decreasing
for(int space = 1;space<=row/2-r;space++){
System.out.print(" ");
}
for(int c=1;c<=j;c++){
System.out.print(c);
}
j+=2;
System.out.print("\n");
//define k variable for saving the new value of j which is unachnagble
k=j-4;
}else{
//space is inceasing & column is decreasing here
for(int space=row/2;space<r;space++){
System.out.print(" ");
}
for(int c=1;c<=k;c++){
System.out.print(c);
}
k-=2;
System.out.print("\n");
}
}
}
}
Problem #14:
Hollow Rectangle
Display a rectangle of given length and width.
Sample input:
4
5
Sample output
*****
* *
* *
*****
Solution:
import java.util.Scanner;
public class Task14{
public static void main(String[] args){
int n;
Scanner sc = new Scanner(System.in);
System.out.print("Input row/length");
int row = sc.nextInt();
System.out.print("Int column/width");
int col = sc.nextInt();
for(int r = 1 ;r<=row;r++){
for(int c=1;c<=col;c++){
if(r==1|r==row|c==1|c==col){
System.out.print("*");
}else{
System.out.print(" ");
}
}
System.out.print("\n");
}
}
}
Problem #15:
Hollow Rectangle
Display a rectangle of given length and width.
Sample input:
4
5
Sample output
12345
1 5
1 5
12345
Solution:
import java.util.Scanner;
public class Task15{
public static void main(String[] args){
int n;
Scanner sc = new Scanner(System.in);
System.out.print("Input row/length");
int row = sc.nextInt();
System.out.print("Int column/width");
int col = sc.nextInt();
for(int r = 1 ;r<=row;r++){
for(int c=1;c<=col;c++){
if(r==1|r==row|c==1|c==col){
System.out.print(c);
}else{
System.out.print(" ");
}
}
System.out.print("\n");
}
}
}
