Programming in C Loops – (counting loops and sentinel loops) Instructions: 1. Cr
ID: 3863776 • Letter: P
Question
Programming in C
Loops – (counting loops and sentinel loops)
Instructions:
1. Create a program that asks the user to enter a number from 1 to 25.
2. Print the statement “I'd rather be doing something else” the number of times entered by the user using a for loop.
3. Print the statement “Programming is easy” the same number of times using a while loop.
4. Print the statement “I like to have fun” the same number of times using a do/while loop.
5. Next, prompt the user to enter a series of numbers until the user enters a -1 to stop.
6. As long as the number is not -1, add the input number to an ongoing sum and print the sum and the number on the screen.
7. When the user enters a -1 print the message “Have a Great Day :)” and end the program. –the -1 should not be added to the sum.
Explanation / Answer
#include <stdio.h>
int main(void)
{
int i,n;
int sum = 0;
printf("Enter a number from 1 to 25");
scanf("%d",&n); //input number
for(i=1;i<=n;i++)
printf(" I'd rather be doing something else"); //execute loop n times
i=1;
while(i<=n)
{
printf(" Programming is easy"); //execute loop n times
i++;
}
i=1;
do
{
printf(" I like to have fun"); //execute loop n times
i++;
}while(i<=n);
printf(" Enter a series of numbers enter -1 to stop.");
do
{
scanf("%d",&n);
if(n != -1)
{
sum = sum+n;
printf(" Num = %d Sum = %d",n,sum); //print number and sum
}
else
break;
}while(n!= -1);
printf(" Have a Great Day !");
return 0;
}
Output:
Enter a number from 1 to 25
3
I'd rather be doing something else
I'd rather be doing something else
I'd rather be doing something else
Programming is easy
Programming is easy
Programming is easy
I like to have fun
I like to have fun
I like to have fun
Enter a series of numbers enter -1 to stop
4 Num = 4 Sum = 4
67 Num = 67 Sum = 71
12 Num = 12 Sum = 83
23 Num = 83 Sum = 106
-1
Have a Great Day!
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.