Project 1 Write a program that will compute the sum of the f
Project 1:
Write a program that will compute the sum of the first n positive odd integers. For example, if n is 5, you should compute 1 +3 + 5 +7 + 9. Use for loop to implement the project. The program accept integer n from keyboard.
Project 2:
Rewrite the program by using the while loop. And show me the output/resut for both.
please separate project 1 and project 2 thank you...
Solution
Project 1:
Answer:
#include <stdio.h>
int main()
{
int i, n, sum=0;
/* Reads range to find sum of odd numbers */
printf(\"Enter any number: \");
scanf(\"%d\", &n);
/* Finds the sum of all odd number */
for(i=1; i<=n; i+=2)
{
sum += i;
}
printf(\"\ Sum of all odd number between 1 to %d = %d\", n, sum);
return 0;
}
Project 2:
Answer:
#include <stdio.h>
int main()
{
int i=1, n, sum=0;
/* Reads range to find sum of odd numbers */
printf(\"Enter any number: \");
scanf(\"%d\", &n);
/* Finds the sum of all odd number */
while(i<=n){
sum += i;
i+=2;
}
printf(\"\ Sum of all odd number between 1 to %d = %d\", n, sum);
return 0;
}

