28.5k views
3 votes
For all the following assignments, you must define one or more functions in C (1) Write a program which asks the user for the value of N, the program will print out the sum of Sum = 1 + 2 + + N Try your program with N = 100 and 1000, 000

User Mlabraca
by
5.0k points

1 Answer

3 votes

Answer:

// here is code in C.

#include <stdio.h>

// main function

int main(void) {

// variable

long long int n;

printf("Enter the value of N:");

// read the value of n

scanf("%llu",&n);

// calculate the sum from 1 to N

long long int sum=n*(n+1)/2;

// print the sum

printf("\\sum of all number from 1 to %llu is: %llu",n,sum);

return 0;

}

Step-by-step explanation:

Read the value of n from user.Then find the sum of all number from 1 to N with the formula sum of first N natural number.That is (n*(n+1)/2). This will give the sum from 1 to N.

Output:

Enter the value of N:100

sum of all number from 1 to 100 is: 5050

Enter the value of N:1000000

sum of all number from 1 to 1000000 is: 500000500000

User Davor Zubak
by
4.7k points