Loops in Java (Exercise 14)

preview_player
Показать описание
Java Programming: Programming Question on Loops in Java Programming
Topics Discussed:
1. Writing a program that reads a positive integer N and displays a rectangle of N rows.

Music:
Axol x Alex Skrindo - You [NCS Release]

#JavaByNeso #JavaProgramming #Loops
Рекомендации по теме
Комментарии
Автор

i did this one. yay.
though i just solved a problem using loops i feel im a web developer lol,
but it really feels nice when we solve something we thought we couldnt solve.
thanks a lot neso academy and Ali Badran sir!!

riteshmadishetty
Автор

Finally! first time solving problems on my own! after struggling with 13 loop excercises!

whereismyyoung
Автор

2nd part of the program using 2 loops:

import java.util.Scanner;
public class MyClass {
public static void main(String args[]) {
Scanner sc=new Scanner (System.in);
int x=sc.nextInt();
int i, j;
for(i=1;i<=x;i++){
for(j=1;j<=x;j++){

System.out.print("*");
}

else{
System.out.print(" ");
}

}
System.out.println();
}
}
}

aryanrasaily
Автор

import java.util.Scanner;

class Check {
public static void main(String[] args) {

System.out.print("Enter a positive integer: ");
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();

for(int i = 1; i <= n; i++) {
for(int j = 1; j <= n; j++) {
System.out.print("*");
}

System.out.println();
}
}
}

//Time Complexity - O(2n) -> n = length of the string.

suswithcherry
Автор

* Updated Question - 1

import java.util.Scanner;

class Check {
public static void main(String[] args) {

System.out.print("Enter a positive integer: ");
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();

for(int i = 1; i <= n; i++) {
for(int j = 1; j <= n; j++) {
if(i == 1 || i == n)
System.out.print("*");
else {
if(j == 1 || j == n)
System.out.print("*");
else
System.out.print(" ");
}
}

System.out.println();
}
}
}

//Time Complexity - O(2n) -> n = length of the string.

suswithcherry
Автор

for(int i = 1; i <= n; i++) {
for(int j=1;j <= n; j++) {



");
}
System.out.println();
}

this code is much easier

TroddenAnimations
Автор

or can use this for the empty rectangle



Scanner in = new Scanner(System.in);
int n= in.nextInt();
for(int i=1;i<=n;i++) {
for(int j=1;j<=n;j++) //loop for spaces
if(i==1||i==n || j==1|| j==n)
System.out.print("*");
else
System.out.print(" ");



System.out.println();

}
}

onlyshorts
Автор

import java.util.*;

public class Main {
public static void main(String[] args) {

Scanner input = new Scanner(System.in);

int num = input.nextInt();

for (int i =1 ; i <= num ; i++) {
for(int j =1 ; j<= num ; j++) {
System.out.print("*");
}
System.out.println();
}
}
}

Absalatreal
Автор

import java.util.Scanner;
public class Main
{
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.print("Please enter number of rows: ");
int n = s.nextInt();

for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
if(j==1 || j==n || i==1 || i ==n){
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println();
}
}
}

And exercise solution:
import java.util.Scanner;
public class Main
{
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.print("Please enter number of rows: ");
int n = s.nextInt();

for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
if( (j!=1 && j!=n) || i==1 || i ==n){
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println();
}
}
}

zubaerahammed
Автор

public class Main {
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        System.out.print("Enter an Int: ");
        int n = s.nextInt();
        for(int i = 1; i <= n; i++) {
            System.out.print("*");
            for(int j = 1; j <= n -2; j++)
                System.out.print(i == n || i == 1 ? "*" : " ");
            System.out.print("*");
            System.out.println();
        }
    }
}

madasmsm
Автор

* Updated Question - 2

import java.util.Scanner;

class Check {
public static void main(String[] args) {

System.out.print("Enter a positive integer: ");
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();

for(int i = 1; i <= n; i++) {
for(int j = 1; j <= n; j++) {
if(i == 1 || i == n)
System.out.print("*");
else {
if(j == 1 || j == n)
System.out.print(" ");
else
System.out.print("*");
}
}

System.out.println();
}
}
}

//Time Complexity - O(2n) -> n = length of the string.

suswithcherry
join shbcf.ru