+ 1
Write a program that reads four numbers (each between 1 and 20). Assume that the user enters only valid values. For each number
that is read, your program should print a line containing that number of adjacent asterisks. Sample input: 2 4 1 6 Sample output: 2 ** 4 **** 1 * 6 ****** Sample input: 2 4 1 6 Sample output: 2 ** 4 **** 1 * 6 ******
25 Réponses
+ 11
Shayma Khader We don't help in homework. You need to do self and show your attempts.
+ 4
Share your attempt. Just copy paste here that try.. It helps you find where is mistake..
+ 4
#include <iostream>
using namespace std;
int main()
{
int x ;
cout<<"enter the 4 numbers : "<<endl;
for(int j=1;j<=4;j++) //this is for repeating for 4 times reading
{
cin>>x;
cout<<x<<" "; //prints input number and space
for(int i=1;i<=x;i++) //this loop for printing * for x number of times
cout<<"*";
cout<< endl; //adding line break
}
return 0;
}
//Shayma Khader read comments for explanation, just did it to work 4 times inner loop, by a outer loop.
+ 3
Shayma Khader
This is your problem or assignment?
+ 3
int main(){
int x;
cout<<"enter the first number :"<<endl;
cin>>x;
cout<<x<<" " ;
for(int i=1;i<=x;i++)
cout<<"*"; //remove endl
return 0;
}
/*
input : 4
Output :
enter the first number:
4 ****
//for readng single input Shayma Khader
*/
+ 2
Shayma Khader do you need output like that?
Or it need like as you mentioned in question description.
The above code accept 4 inputs, try input by adding in pop-up as 2 3 4 5 and see output.
+ 2
Shayma Khader
input:
2 4 1 6
output you get as :
2 **
4 ****
1 *
6 ******
Check it in playground by copy paste and run..
+ 2
#include <iostream>
using namespace std;
int main() {
int a,b,c,d;
cout << " enter four numbers\n";
cin >> a >> b >> c >> d;
cout << a;
for(int i=0; i<a; i++)
cout <<"*";
cout << endl << b;
for(int i=0; i<b; i++)
cout <<"*";
cout << endl << c;
for(int i=0; i<cout << a;
for(int i=0; i<a; i++)
cout <<"*";; i++)
cout <<"*";
cout << endl << d;
for(int i=0; i<d; i++)
cout <<"*";
return 0;
}
+ 1
Jayakrishna🇮🇳 I Am Groot !
main(){
int x,y,z,w;
cout<<"enter the firt number :"<<endl;
cin>>x;
for(int i=1;i<=x;i++)
cout<<"*"<< endl;
}
*/ for first number
+ 1
Which code you are trying? Am getting exactly what I told and as what you mentioned in description..? How you entering input? & what input? Shayma Khader
+ 1
Shayma Khader which code?
+ 1
Jayakrishna🇮🇳
#include <iostream>
using namespace std;
int main()
{
int x ;
cout<<"enter the 4 numbers : "<<endl;
for(int j=1;j<=4;j++) //this is for repeating for 4 times reading
{
cin>>x;
cout<<x<<" "; //prints input number and space
for(int i=1;i<=x;i++) //this loop for printing * for x number of times
cout<<"*";
cout<< endl; //adding line break
}
return 0;
}
//Shayma Khader read comments for explanation, just did it to work 4 times inner loop, by a outer loop.
+ 1
#include <iostream>
using namespace std;
int main()
{
int x ;
cout<<"enter the 4 numbers : "<<endl;
for(int j=1;j<=4;j++) //this is for repeating for 4 times reading
{
cin>>x;
cout<<x<<" "; //prints input number and space
for(int i=1;i<=x;i++) //this loop for printing * for x number of times
cout<<"*";
cout<< endl; //adding line break
}
return 0;
}
/* Shayma Khader
input:
2 4 1 7
output you get as :
2 **
4 ****
1 *
7 *******
Screen shot :
https://www.sololearn.com/post/842628/?ref=app
*/
+ 1
Shayma Khader
outer loop works 4 times from j=1 to 4.
Inner loop print stars x times.
First if input 2 3 4 5 then
When j=1, it read 2 into x and prints * 2times by inner loop(from i=1 to x times repeated).
(2 **)
Next by j increments and j=2 so it reads 3 into x next time and prints x and x times stars..
(3 ***)
Next by j increments and j=3 so it reads 4 into x next time and prints x and x times stars..
(4 ****)
Next by j increments and j=4 so it reads 5 into x next time and prints x and x times stars..
(5 *****)
Next j=5 and condition j<=4 false so loop exits.
+ 1
J just variable. It's use of nested loops... Loop in loop.
Read about nested loops...
for(...) { //outer loop.
...
for(...) {
... //inner loop
}
...
...}
Shayma Khader you're welcome
+ 1
- i'd suggest putting numbers into array.
- display function to print "x"
- the rest in a for loop within a for loop ....this can be done probably for loop and do while
0
I Am Groot !
This is a homework on my class .. i write a first part and complete the second in description .. please i need a help
0
I Am Groot ! I swear i try many times in laptop i swear
I write for lop in first and thin read the first number then copy stars but its didn't look like the doctor how he need
0
Jayakrishna🇮🇳
The out put like this
enter the first number:
4
4*
*
*
*
put i need it like this
enter the first number:
4 ****
0
Jayakrishna🇮🇳
Put what about stars