Write a c++ program that display the ff. on screen using while loop. * ** *** **** *****
Here is a program : #include<stdio.h> #include<stdlib.h> int main() { int i=1, j=1,k=1; while(i<=5){ // i for lines (5) , in this case. while(j<=k){ //j and k for *. printf("*"); j += 1; } k += 1; j = 1; i += 1; printf("\n"); } system("pause"); }
what is printf("*"); ? sorry im new at c++. :)
You want to print : * ** *** **** ***** So I used printf("*") to print the * on the screen.
Sorry in C++, it should be like that : #include<stdio.h> #include<stdlib.h> #include <iostream> using namespace std; int main() { int i=1, j=1,k=1; while(i<=5){ while(j<=k){ cout << "*"; j += 1; } k += 1; j = 1; i += 1; cout << "\n"; } system("pause"); } use cout instead of printf.
yes. im using c++. let me try your code. thanks :)
Ok yes, Welcome.
Hello ktobah.. the code worked and its great thanks. but i cant understand how it works. can you explain and trace it for me?
Yes sure, here what I did : int main() { // Here declaration of variables; int i=1, j=1,k=1; // We want to print 5 lines so for that I used 5, so remember i is for lines. while(i<=5){ // Here I used k as the number of maximum "*", and I increment "j" until j>k, so when j<=k , I print "*" while(j<=k){ cout << "*"; j += 1; } //When I print the "*" of the first line, I increment k, example : // For the first line we have i=1 (first line), and k=1 (the number of "*") // for the first line, so we initialize j with 1, and we start the loop // i = 1, is (i=1) <= (5) --> yes, so enter the loop; // (j=1) <= (k=1) --> yes, so enter the loop; // print "*"; // j = 2 , we increment j // (j=2) <= (k=1) --> No, so exit the loop; // Now increment k to 2, means two "*" for the second line. k += 1; // Reset j to 1, so we start from the beginning of the second line j = 1; // And increment i to 2, means we will go to the 2nd line. i += 1; // This instruction means return to a new line; for example we print "*" // of the first line, so after (cout << "\n"), we will go to the 2nd line // and print "**". cout << "\n"; } And so on.... until you print all the 5 lines. Good luck.
Thank you very much! you're the best! :))
haha it's ok, you are welcome.
How about this figure in while loop and do while: * * * * * * * * * * * * * * *
Hello, well I will solve it with the while loop, and you make it with the do while loop: #include<stdio.h> #include<stdlib.h> main() { /* Declare our variables, row for the number of lines 'J' to print the ' ' speaces and the '*' 'temp' is used as the number of spaces for each line. */ int i, j, temp; //intialize temp with 1 temp = 5; //intialize i with 1, means the 1st line. i = 1; while (i <= 5){ //intialize j with 1, means the Number of ' ' and '*'. j = 1 ; // Temp is the number of spaces, at first it's 5 while (j < temp){ // Print the space printf(" "); // And increment j with 1 j++; } /*After finishing the 5 spaces for the 1st line, we should minus 1 from temp, that mean for the 2nd line we will have 4 spaces, and so on... */ temp--; // Now time to print the '*', so we reintialize j to 1 j = 1 ; /* We take j<2*i-1 because we are going to print the double number, example if i=2 then j <3 that means we will print 3 '*' in the 2nd line*/ while(j <= 2*i - 1){ printf("*"); j++; } /* After finishing with a line, we should return to a new line*/ printf("\n"); // And go to the next line. i++; } system("pause"); return 0; } Good luck, just delete the comments from the program and it will looks simple and little!
Hello @ktobah i reviewed your reply, i understand your explanation but when i do your code suddenly went wrong. i don't know how to trace the error. here is what i typed. // triangle (whilw loop).cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <iostream> using namespace std; int _tmain(int argc, _TCHAR* argv[]) { int i,j,temp; temp = 5; i = 1; while (i <= 5) { j = 1; { while (j < temp) { cout<<" "; j=1; } temp--; j = 1; while(j <= 2*i - 1) { cout<<" "; j++; } cout<<"\n"; i++; } system("pause"); return 0; }
Well you have a problem in the "{}", this is what I wrote : while (i <= 5){ j = 1 ; while (j < temp){ printf(" "); j++; } temp--; j = 1 ; while(j <= 2*i - 1){ printf("*"); j++; } printf("\n"); i++; } You can notice that both "while(j < temp)" and "while(j < = 2*i -1)" are inside the big loop "while (i <= 5). That's your error. Good luck.
Join our real-time social learning platform and learn together with your friends!