In: Computer Science
C Programming- How would the whole part 1 be coded with multiple loops?
Part 1:
You can do A, B, and C in one program with multiple loops (not nested) or each one in a small program, it doesn’t matter.
A. Create a loop that will output all the positive multiples of 9 that are less than 99.
9 18 27 36 45 ….
B. Create a loop that will output all the positive numbers less than 200 that are evenly divisible by both 2 and 7.
14 28 42 …
C. Create a loop that will calculate the sum of the positive multiples of 8 that are between 100 and 500. Output the sum.
Part 2:
You’re the teacher now! You need to count how many passing grades are entered. We don’t know how many grades there will be. Use a sentinel controlled while loop that will ask the user to enter student grades until a value of -1 is entered. Use a counter variable to count all the grades that are passing grades, where 70 is the minimum passing grade (I know, you’re a tough grader!) If there are any grades that are out of the valid range (0 through 100), present an error message to the user, and do not count that grade as passing (or valid). We also would like to see what percentage of the valid grades are passing.
Create 3 test cases. Use this as one of them:
Grades Entered: Expected Results
45
90
70
87
123 That is not a valid grade!
100
-1 You entered 4 passing grades.
80.0% of the valid grades are passing.
Create two more sets of test data of your own, and include screenshots of your program running them. Demonstrate all program functionality.
Solution
Part 1
code
#include <stdio.h>
int main()
{
//part A
//print multiples of 8 less than 99
printf("print multiples of 8 less than 99\n");
for(int i=1;i<99;i++)
if(i%9==0)
printf("%d ",i);
printf("\n");
//part B
//print all the positive numbers less than 200 that are evenly
divisible by both 2 and 7
printf("Output all the positive numbers less than 200 that are
evenly divisible by both 2 and 7\n");
for(int i=1;i<200;i++)
if(i%2==0 && i%7==0)
printf("%d ",i);
//part C
//Calculate the sum of the positive multiples of 8 that are between
100 and 500
printf("\nsum of the positive multiples of 8 that are between 100
and 500\n");
int sum=0 ;
for(int i=100;i<=500;i++)
if(i%8==0)
sum=sum+i;
printf("Sum=%d",sum);
return 0;
}
Screenshot
Output
---
Part 2
Code
#include <stdlib.h>
#include <stdio.h>
int main() {
int grade = 0;
double passPercent, countgrades = 0,
countpassinggrades = 0,countvalidgrades=0;
while (grade != -1) {
printf("Enter the test grades of
the class (-1 to quit): ");
scanf("%d", &grade);
countgrades =
countgrades + 1;
if (grade >= 0 && grade<=100) {
countvalidgrades
= countvalidgrades + 1;
}
if (grade >= 70 &&
!(grade < 0 || grade > 100)) {
countpassinggrades = countpassinggrades + 1;
}
if (grade < 0 &&
!(grade == -1) || grade > 100) {
printf("That is
not a valid grade!. \n");
}
}
passPercent = (countpassinggrades /
countvalidgrades) * 100.00;
printf("You entered %.0lf passing grades \n",
countpassinggrades);
printf("The percentage of passing grades is: %.1lf
\n", passPercent);
return 0;
}
Screenshot
Output
---
all the best