Loops in Java (Exercise 12)

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 pyramid of stars of N rows.

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

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

Thank you for the lessons and kind words. I am studying it for some time, but it is still difficult for me to solve most of the codes from this playlist. Your lessons are good anyway;

Anakanueva
Автор

Thank you .... You are good teacher ... Im from algeria

sofchou
Автор

I was confused the same way at begin, but just back to the first, we don't set any limitation on the * side( j = i ), only the number of" ". Therefore, in the second exercise, just print out how many of the " " and puls (2*i -1) number of " * ", that's should do. Don't think about how to middle "*" .

boteagao
Автор

Doing this 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++){
if(j<=x-i){
System.out.print(" ");
}
else{
System.out.print("*");
}
}
System.out.println();
}
}
}

aryanrasaily
Автор

I love your way for explaining the idea ❤️

omarahmed
Автор

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++) {
for(int j = n; j > i; j--)
System.out.print(" ");
for(int x = 1; x <= i; x++)
System.out.print("*");
System.out.println();
}
}
}

madasmsm
Автор

Updated Question

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 = n; j > i ; j--) {
System.out.print(" ");
}

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

System.out.println();
}
}
}

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

suswithcherry
Автор

You are amazing.

Take this solution guys

public class Mainclass {

public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int x = s.nextInt();
int i, j;
String Star = " ";
String Star2 = "";
for (i = 1 ; i <=x ; i++){
Star2 = Star2 + "*";
for (j=x;j>i;j--) {
Star = Star + " ";

}

Star = "";
}


}
}

mohammedhnaif
Автор

i've managed to do it but it ended up being different though the result is the same.

i've also cheesed the second part by just adding a third loop instead of doing i*2 which with my method is not possible

public static void main(String[] args) {


Scanner scan = new Scanner(System.in);

System.out.print("enter number:");
short input = scan.nextShort();
short valueI = input;

for ( ;valueI != 0; valueI--) {
(int j = 1; j <= valueI-1; j++)
");

(int k = input; k != valueI-1; k--)


(int k = input; k != valueI; k--)





}

theripper
Автор

How the stars are between the spaces and not in the end ? 6:35

devanshsolani
Автор

hey, i just dont get it
why space from five to one but ++ increment not -- decrement

klid
Автор

plzz make video of two loops in a one programs explain what toatl count of loops

junedahamed
Автор

Yes... it's simple and easier....

bhuvaneeshwarank
Автор

public static void main(String[] args) {
Scanner scan = new Scanner(System.in);

System.out.print("Enter A Number : ");
int N = scan.nextInt();

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

adanskiesabaria
Автор

didnt get the answer at first attempt. ill give another try and see your code

riteshmadishetty
Автор

I have done this with 1 for loop

for(int i=1;i<=n;i++){
String space = " ", star = "*";

}

SivaGarapati
Автор

// We can do it by using only 2 loops
import java.util.Scanner;
public class M_55
{
public static void main(String []args)
{
Scanner input = new Scanner(System.in);
int i, j;
int n_rows = input.nextInt() ;
for(i=1;i<=n_rows;i++)
{
for(j=1;j<=n_rows;j++)
{
if(j>=(5-i)+1)
System.out.print("*");
else
System.out.print(" ");
}
System.out.println("");
}

}
}

adityarajthsec-a
Автор

In 5:47 sec I thought I could do that but it wasn't working at that moment . How could anyone comeup with dealing what the pattern would be.

NakibAkash
Автор

yay i got the second exercise of this video without seeing the solution!!

riteshmadishetty
Автор

I just created another loop that complements the missing stars, check it:
import java.util.Scanner;

public class Main {
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
System.out.print("Triangleeee of numbers: ");
int userInput = scanner.nextInt();

for (int i = 1; i <= userInput; i++){

for (int j = 1; j <= userInput - i; j++)
System.out.print(" ");

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

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


System.out.println();
}
}

}

kaueberto