/***************************************************************** * Find the sum of all the numbers from 20 to 2000, and * * print it. This program uses a for loop to count through * * the numbers. You also could have used a while loop to do * * it. Notice that you don't need to read anything in for * * this program. * *****************************************************************/ #include int main(void) { int count, sum = 0; for (count=20; count <=2000; count = count + 1) sum = sum + count; printf("The sum is %d\n", sum); return (0); } /***************************************************************** * Read in two numbers and tell whether their sum is greater * * than 500. There are no loops in this program; just an * * if statement. * *****************************************************************/ #include int main(void) { int number_1,number_2; printf("Enter a number: "); scanf("%d", &number_1); printf("Enter another number: "); scanf("%d", &number_2); if (number_1 + number_2 > 500) printf("The sum of %d and %d is greater then 500\n", number_1,number_2); else printf("The sum of %d and %d is not greater than 500\n", number_1,number_2); return (0); } /***************************************************************** * Read in floats until end of file. Print out all numbers * * that are greater than 100 or less than 50. I used a while * * loop to read through the numbers, as usual. I use the OR * * operation to check the values. The values need to be * * printed as soon as they are read in. * *****************************************************************/ #include int main(void) { float number; int input_status; input_status = scanf("%f",&number); while( input_status != EOF) { if (number > 100 || number < 50) printf("%f is greater than 100 or less than 50\n", number); input_status = scanf("%f",&number); } return (0); } /*****************************************************************. * Read in integers until end of file. Count how many * * numbers are positive and how many numbers are negative. * * Print out the counts. This program uses one variable to * * count positive numbers and another variable to count * * negative numbers. If a 0 is entered, it is just ignored * * because it is not positive or negative. The counters are * * incremented inside the loop, and the counts are printed out * * after the loop. * *****************************************************************/ #include int main(void) { int number, count_positive = 0, count_negative = 0; int input_status; input_status = scanf("%d",&number); while( input_status != EOF) { if (number > 0) count_positive = count_positive + 1; else if (number < 0) count_negative = count_negative + 1; input_status = scanf("%d",&number); } printf("There were %d positive numbers\n", count_positive); printf("There were %d negative numbers\n", count_negative); return (0); } /*****************************************************************. * Read in numbers until you get two consecutive zeroes. * * than the first number you read in. Print out the sum of the * * numbers. The while loop does not check for an end of * * file in this program; instead it checks for two consecutive * * zeroes. The only printf statement is after the loop. * *****************************************************************/ #include #define YES 1 #define NO 0 int main(void) { int number; /* number you read in */ int sum = 0; /* sum of all numbers read in */ int prev_zero; /* Was last number a 0? */ prev_zero = NO; scanf("%d", &number); while( number != 0 || prev_zero == NO ) { sum = sum + number; if (number == 0) prev_zero = YES; else prev_zero = NO; scanf("%d", &number); } printf("The sum is %d\n", sum); return (0); }