8 years ago (Jul 29, 2016) | 2,380 views |
Basic Java programs using loop(Part -3)
Category: Featured, Java Programming, Programming Tags: Hollow Rectangle java, java basic pogams, Rhombus program in java, Rhombus star print java by Rana
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:
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 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:
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 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
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:
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 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
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:
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 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:
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 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"); } } } |
To be continued……
3 responses to “Basic Java programs using loop(Part -3)”
Leave a Reply
You must be logged in to post a comment.
Categories
- Android (24)
- Android Applications (15)
- Android Develop (6)
- Android Custom Rom (2)
- Android Root (6)
- Xposed (1)
- Android Games (1)
- Android Tips (7)
- Education (3)
- Examination Results (2)
- Facebook Tips (14)
- Featured (6)
- Free Internet Trick (13)
- Airtel Free Internet (2)
- BL Free Internet (2)
- Gp Free Internet (4)
- Robi Free Internet (2)
- Freelancing (2)
- Hacking (2)
- Hot (5)
- Java (2)
- Java Development (1)
- Mobile Tips (4)
- Operator News (43)
- Airtel (8)
- Banglalink (3)
- Grameenphone (25)
- Offer & Promotion (1)
- Robi (9)
- Teletalk (1)
- Other (8)
- Programming (6)
- Java Programming (6)
- Sponsored (1)
- Technology Update (1)
- TrickBD Updates (5)
- Web Development (13)
- Alexa Tips (1)
- Domain & Hosting (1)
- Seo (5)
- Wapka (3)
- wwordpress (2)
- Windows (4)
- Windows Games (1)
- Windows Tips (3)
Java programmer, right?
Keep going bro I’m with you.
Rana vai TrickBD.com e kisu problem hoise.ami signup korte partasi na.plz thik koren.ami singup korbo.
make me author,