Java tutorial: Practice Questions on Loops

preview_player
Показать описание
Java practice questions on loops, break and continue: This video will talk about loops practice questions in java. Here we will practice about different types of loops used to control the flow of a program in Java

Best Hindi Videos For Learning Programming:

►C Language Complete Course In Hindi -

►JavaScript Complete Course In Hindi -

►Django Complete Course In Hindi -

Follow Me On Social Media
Рекомендации по теме
Комментарии
Автор

Another way of solving the first problem -

//This for loop is for each line
for (int i = 0; i < 4; i++) {
//This for loop is to print some number of stars in one line
for (int j = i; j<4; j++) {
System.out.print("*");
}
System.out.print("\n");
}



E.g. When i =0, j = 0 as well. So, since 0 < 4 is true in 2nd loop, the "*" is printed 4 times (0, 1, 2, 3).
Then after the 2nd loop ends, newline is printed and the first iteration of the first loop ends.

Now, i = 1 so j = 1 as well. 1 < 4. So "*" is printed thrice (1, 2, 3);

Then i = 2 so j = 2 as well. 2 < 4. So "*" is printed twice (2, 3);

Finally, i = 3 so j = 3 and 3 < 4. So "*" is printed once (3).'

After this, when i = 4, the condition (i < 4 ) is false hence the loop ends.



The SImplest way to start writing code for this problem is by starting with how we print one star -

--> System.out.print("*");

Now, how to print a 4 stars in one line? We will use a loop for that.

---> for (int j = 0; j<4; j++) {
System.out.print("*");
}

Now, how can we do the same in 4 different lines. This means we need to run the above loop 4 times. So we need to put it inside another loop.

----> for (int i = 0; i < 4; i++) {
for (int j = 0; j<4; j++) {
System.out.print("*");
}
}

But when we run this, all the stars will be printed in one line. That's because after we print 4 starts in one line, we need to add a new
line

---> for (int i = 0; i < 4; i++) {
for (int j = 0; j<4; j++) {
System.out.print("*");
}
System.out.print("\n");
}

Now we see that there are 4 lines with each line having 4 stars. Each line has four stars because no matter how many times the loop 1 iterates, in every iteration, the "j" starts with an initial value of 0.

So the simple solution is to set "j = i" in the 2nd loop instead of "j = 0" because in that way, with every new iteration, the 2nd loop gets the current value of i & then sets the value of "j" accordingly instead of us hardcoding its value to 0;

PS: TRIED MY BEST TO MAKE IT SIMPLE TO UNDERSTAND xD

DroidHolicOfficial
Автор

Entire Java course is going nice.
Thanks for providing these helpful courses at free of cost.

arijeetkumar
Автор

These practice Programs are really useful for us

RohanDasRD
Автор

6:17 sum of the even numbers using while loop:


public class Main
{
public static void main (String[]args)
{
int sum = 0;
int n = 4;
int i = 0;

while (i < n)
{
sum = sum + (2 * i);
i++;
}
System.out.println
("the sum of the first 4 even numbers using while loop is below");
System.out.print (sum);

}

}

sagarbhadre
Автор

Ads - whitehat jr
But me -- THE LEGEND HARRY

Lila
Автор

This is honestly the most fun I've had while studying, I love this course and the questions are amazing :D <3

joybrar
Автор

I'm feeling lucky because I don't even thought of learning programming in such a fun way, and practice sets are cherry on the top.

motivationalquotes
Автор

Question 11 using while loop first n even number sum is :
int sum = 0;
int i = 0;
int n = 5;
while (i<n) {
sum = sum +(2* i);
i++;
}
System.out.println("sum of even number is :");
System.out.println(sum);

shivanklohiya
Автор

5:03 here we can also use,
public class Practise_Q17 {
public static void main(String[] args) {
String str= "*";
for( int i= 4;i<5 && i!=0;i--){

}
}
}

Piyush-menu
Автор

Solution of every Q is given below...👇

import java.util.Scanner;


public class CWH_Ch5_PS {
public static void main(String[] args) {
// Question 1

for (int a = 4; a>0; a--){
for (int b = 0; b<a; b++){
System.out.print("*");
}
System.out.print("\n");
}

// Question 2

int c = 0;
int sum = 0;
while(c<6){
System.out.println(2*c);
sum = sum+(2*c);
c++;
}
System.out.println("The sum of these numbers is: " + sum);

// Question 3

Scanner multi;
multi = new Scanner(System.in);
System.out.println("Enter the number");
int no = multi.nextInt();
int cons = 1;
System.out.printf("Table of %d\n", no);
while (cons<11){
System.out.printf("%d x %d = %d \n", no, cons, no*cons);
cons++;
}

// Question 4

System.out.println("Reverse Table of 10");
int reNo = 10;
int reCons = 10;
while (reCons>0){
System.out.printf("%d x %d = %d \n", reNo, reCons, reNo*reCons);
reCons--;
}

// Question 5

int num = 5;
int fact = 1;
for (int i = 1; i<=num; i++){
System.out.println(num*i);
fact *= i;
}
System.out.printf("Factorial = %d \n", fact);

// Question 6

int x = 5;
int y = 25;
while(y>x){
System.out.println(x);
y--;
}

// Question 7

for (int z = 0; z<20; z++){
System.out.println(1);
}

// Question 9
int newSum = 0;
int newNo = 8;
int newCons = 1;
// System.out.printf("Table of %d\n", newNo);
while (newCons<11){
// System.out.printf("%d x %d = %d \n", newNo, newCons, newNo*newCons);
newSum += newNo*newCons;
newCons++;
}
System.out.println("The Sum Of Numbers In Table Of 8 Is: " + newSum);

// Question 11

for (int p = 0; p<20; p++){
System.out.println(2);
}
}
}

prajwalitsinghraja
Автор

00:20:09 -

problem - 7 -
/* Q. 7 - Solve Q.1 Using while loop */
int n = 4;
int i = 0;

while (i<n)
{
int j = 0;
while (j < n - i)
{
System.out.print("*");
j++;
}
System.out.println("\n");
i++;
}

sursangeetujjain
Автор

another way to solve first problem:-
public static void main(String[] args){
int n=10;
for (int i=0;i<n;i++){


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


Jainil-yqfc
Автор

9:00 by using while loop

int i = 0;
int sum = 0;
int n = 3;

while (i<=n)
{
sum = sum + 2*i;
i++;
};
System.out.print("the sum is ");
System.out.println(sum);

crescent
Автор

Bohot maza aaya Harry bhai practice set me... Question no 1 Star problem ko solve krte waqt to mujhe to sach me taare dikhne lage the😂😂... Phir 1 din baad aaj aapki ye video dekh kr usko banana seekh gya... Thank you so much, brother! May Allah bless you!

mohammadtufailahmad
Автор

Kids: Carry
Men: Marry
Legends: Harry 👌

zaid
Автор

Hi Harry,
First of all Thank you so much for a systematic tutorials.
and Thanks Ayush, who recommend me to study from your tutorial.
You know I am a mechanical engineer(passed in 2014) and now I have decided to learn java after waste of my 6 years. and really it is shocking, I have understand all concept because of your tutorial.
You are really a great teacher.
I have some basic idea about that but now I have cleared my all doubts.

Thank you so much!!!

Rajgour
Автор

---- Answers ---

Question 7: 1 in while loop:
int i=4;
while(i>=0){
int j=i;
while(j>0){
System.out.print("* ");
j--;
}
System.out.println();
i--;
}

question 10: question 2 in while loop:

Scanner scan=new Scanner(System.in);
System.out.println("Enter the maximum limit of numbers: ");
int n=scan.nextInt();
int i=0;

while(i<=n){
sum+=(i*2);
i++;
}
System.out.println("Sum: "+sum);

anshdholakia
Автор

This playlist is useful even after 6-7 years, thanks for all your efforts, no words to describe your efforts, you are the hero for the new programmers 🙏🙏🙏🙏🙏😎😎😎

sauravpatil
Автор

Harry bhai thanks for the amazing practice set. It gave me a lot of understanding about practical usage of for loops .Maza aa gaya problems dekh kar👌✌

manmohansingh
Автор

Before u solve these problems, , , I solved it confidently, , , because, I learnt c and c++ from your Playlist and now java as well with u, ,,, your amazing thanks a ton ..🎉❤..

snehalhsambrekar