In: Computer Science
Write a C-program and run it to meet the requirements listed below.
Use the nano editor on Windows or Mac or nano/Geany on R-pi or Geany on Mac.
After running the program, it print outs messages on the Terminal and waits for you to accept a
positive number greater than 9. When you enter a positive number greater than 9, it print outs
what you entered to confirm your input. Then, it calculates and prints out the total sum of 1 + 2 +
... + N, where N is what you entered. It repeats three (3) times to test the program.
The lines of your program need to be properly intended. You must include the scanf function.
[Hints: Use printf, scanf, for-loop, and/or while-loop in your program.]
The results on the Terminal should be similar to the following.
X1, X2, and X3 are the random positive numbers you entered on the Terminal, which should be
greater than 9.
Y1, Y2, and Y3 are the total sum that your program calculated. Your program should calculate
the correct total sum.
Do not embed X1, X2, X3, Y1, Y2, and Y3 in your C-program (No hard-coding).
Your program should accept your input (a number greater than 9) in the Terminal and calculate
the total sum three times before returning to the Terminal as shown below.
Let's calculate the total sum.
=> Please enter a positive number greater than 9:
X1
You entered X1.
The total sum is Y1,
=> Please enter a positive number greater than 9:
X2
You entered X2.
The total sum is Y2,
=> Please enter a positive number greater than 9:
X3
You entered X3.
The total sum is Y3,
Three (3) tests have been completed.
C PROGRAM:
#include<stdio.h>
int main()
{
//variables declaration
int x,sum;
printf("Let's calculate the total sum.\n");
//loop for 3 tests
for (int i=0;i<3;i++)
{
//every time sum equals 0 before calculation
sum=0;
printf("=>Please enter a positive number greater than 9\n");
scanf("%d",&x);
//checking if number is less than 9
if(x<=9)
{
printf("number is not greater than 9");
//exit loop
break;
}
printf("You entered %d\n",x);
// calculation of 1+2+3+..... up to x.
for(int j=1;j<=x;j++)
sum=sum +j;
//printing sum
printf("The total sum is %d,\n",sum);
//termination statement
if(i==2)
printf("Three (3) tests have been completed");
}
return 0;
}
OUTPUT: