Question

In: Computer Science

Write a program that will calculate the sum of the first n odd integers, and the...

Write a program that will calculate the sum of the first n odd integers, and the

sum of the first n even integers.

Use a Do while loop

Use a for loop

Here is a sample of how the program should run:

How many odd integers would you like to add? 5

Count Odd Integer Sum

1 1 1

2 3 4

3 5 9

4 7 16

5 9 25

The sum of the first 5 odd integers is 25

How many even integers would you like to add? 7

Count Even Integer Sum

1 2 2

2 4 6

3 6 12

4 8 20

5 10 30

6 12 42

7 14 56

The sum of the first 7 even integers is 56

Do you want to add again? Press y or n

y

How many odd integers would you like to add?

Make sure your program works with any input value from 1 to 1000.

REMEMBER! The sum of the first 5 odd integers is 1 + 3 + 5 + 7 + 9. It is NOT the

sum of the odd integers up to 5

Solutions

Expert Solution

Code:

#include<stdio.h>
int main(){
   int i=1, sum=0, upto;
   char again;
   while(again!='n'){
       printf("How many odd integers would you like to add?");
       scanf("%d",&upto);
       int j=1;
       printf("count odd integer sum\n");
       do {
           sum=sum+i;
           printf("%d %d %d\n",j,i,sum);
           j++;
           i=i+2;
       }while(j<=upto);
      
       printf("The sum of the first %d odd integers is %d\n",upto,sum);
      
       printf("How many even integers would you like to add?");
       scanf("%d",&upto);
       sum=0;
       j=2;
       printf("count even integer sum\n");
       for(i=1;i<=upto;i++){
           j=i*2;
           sum=sum+j;
           printf("%d %d %d\n",i,j,sum);
       }
      
       printf("The sum of the first %d even integers is %d\n",upto,sum);
       printf("Do you want to add again? Press y or n\n");
       scanf("%s",&again);
   }
  
   return 0;
}

Output:

Code with explanation:


Related Solutions

Write an assembly program In MASM assembler to calculate the sum of odd numbers only stored...
Write an assembly program In MASM assembler to calculate the sum of odd numbers only stored in a 16-bit integers array. The size of the array is provided in a separate variable. Use the following design: Sum = 0 FOR i = 0 TO SizeOfArray - 1     IF Arr[i] % 2 = 1           Sum = Sum + Arr[i]     END IF END FOR
Program – version 1: Sum of Range Algorithm Write a program that will sum the integers...
Program – version 1: Sum of Range Algorithm Write a program that will sum the integers between a given range (limit your range from 0 to 50). For example, if the user want to add the integers between (and including) 1 and 10, then the program should add: 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 Algorithm: Program title/description Ask the user to input a start value in the...
Write a program in C++ that generates and displays the first N three digit odd numbers....
Write a program in C++ that generates and displays the first N three digit odd numbers. Whereas the number N is provided by the user.
write in c plus plus Write a program that computes the sum of the odd numbers...
write in c plus plus Write a program that computes the sum of the odd numbers and the sum of the even numbers between two values a and b. For example a=1 and b=10
Write a program in C++ that computes the sum of odd numbers between 1 and 117....
Write a program in C++ that computes the sum of odd numbers between 1 and 117. Execute the program and submit a screen capture of the program and its results.
Write a program in java which store 10 numbers and find the sum of odd and...
Write a program in java which store 10 numbers and find the sum of odd and even numbers. Create a program that uses a two dimensional array that can store integer values inside. (Just create an array with your own defined rows and columns). Make a method called Square, which gets each of the value inside the array and squares it. Make another method called ShowNumbers which shows the squared numbers. Write a program in java which has an array...
Write a program in java which store 10 numbers and find the sum of odd and...
Write a program in java which store 10 numbers and find the sum of odd and even numbers. Create a program that uses a two dimensional array that can store integer values inside. (Just create an array with your own defined rows and columns). Make a method called Square, which gets each of the value inside the array and squares it. Make another method called ShowNumbers which shows the squared numbers.
How could I mathematically prove these statements? 1. The sum of the first n positive odd...
How could I mathematically prove these statements? 1. The sum of the first n positive odd numbers is square. 2. Two positive numbers have the same set of common divisors as do the smallest of them and their absolute difference. 3. For every prime p > 3, 12|(p 2 − 1).
Prove that the following is true for all positive integers n: n is odd if and...
Prove that the following is true for all positive integers n: n is odd if and only if n2 is odd.
Write a MIPS Assembly program that computes the sum of all the odd numbers from 1...
Write a MIPS Assembly program that computes the sum of all the odd numbers from 1 ..99 and print out the answer.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT