This program is about nested loops. IN C language The Problem Statement You and
ID: 3667410 • Letter: T
Question
This program is about nested loops. IN C language
The Problem Statement
You and your roommates have embarked on a project to paint your room in a manner similar to the stripes on the US flag. For example, if you wanted 5 rows with the first row with 8 stars, your design would look like the following:
* * * * * * * *
* * * * * * *
* * * * * * * *
* * * * * * *
* * * * * * * *
Program Setup
Your program should ask the user to enter the number of rows and the number of stars in the first row. The even numbered rows should have one fewer star than the odd numbered rows. Then, construct your program to print the star design.
Sample Run
Here are two examples of what your program should look like when its finished.
How many rows for your star design?
5
How many stars on the first row?
8
* * * * * * * *
* * * * * * *
* * * * * * * *
* * * * * * *
* * * * * * * *
---
How many rows for your star design?
2
How many stars on the first row?
17
* * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * *
Specification
Homework assignments are either correct or incorrect. You must match these prompts and output formatting including punctuation.
How many rows for your star design?
How many stars on the first row?
Odd rows should not have any spaces in front of them.
Even rows should have one space at the beginning of the line.
Separate each asterisk with one space.
Explanation / Answer
#include <stdio.h>
int main(){
int i,r,first,j;
printf("How many rows for your star design? ");
scanf("%d",&r);
printf("How many stars on the first row? ");
scanf("%d",&first);
for(i=1;i<=r;i++)
{
for(j=1;j<=first;j++)
{
if(i%2==0&&j==first)
break;
else
printf("* ");
}
printf(" ");
}
return 1;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.