0

# Write C program to find the sum of first 100 numbers?

Updated: 8/10/2023

Wiki User

15y ago

#include<stdio.h> #include<conio.h> void main() { int n; printf("Enter any number"); scanf("%d",&n); sum=((n*(n+1))/2); printf("The sum of first 100 natural numbers is\t%d",sum); }

Wiki User

15y ago

Wiki User

7y ago

#include<stdio.h>

#include<math.h>

bool is_prime (int num) {

int factor, max_factor;

if (num<0) n*= (-1); // make num positive

if (num<2) return false; // 2 is the lowest positive prime

if ((num%2)==0) return num==2; // 2 is the only even prime

max_factor = sqrt (num) + 1; // if no factors less than max_factor, then none greater either

for (factor=3; factor<=max_factor; factor+=2) // test all odd factors in half-closed range [3:max_factor)

if ((num%factor)==0) return false; // num is composite

return true; // num has no factors, so it must be prime

}

int main (void)

{

const int min = 100;

const int max = 500;

int sum=0;

for (int num=min; num<=max; ++num) if (is_prime (num)) sum+=num;

printf ("Sum of all prime numbers from %d to %d is %d\n", min, max, sum);

return 0;

}

EDIT: Previous answer produced correct result but was highly inefficient. The modified code above is more efficient but could be improved by ignoring all even values, thus the range becomes 101 through 499, inclusive, and increments in steps of 2 instead of 1 (e.g., num += 2 instead of ++num).

The is_prime(n) algorithm has a worst-case time complexity of O(sqrt(n)/2) with a constant space complexity O(1). While adequate for this otherwise trivial program, the algorithm would need to be improved to cope with substantially larger values. A lookup table is one solution since all prime numbers are constant variables and, if the table is saved to disk, would only need to be generated once. A binary search of the table can be achieved in O(log n) time, you simply load the table into an array of appropriate size as and when it is needed.

Wiki User

14y ago

#include <stdio.h>

main()

{

int i;

for (i=0;i<100;++i)

printf("%d\n",2*i+1);

}

Earn +20 pts
Q: Write C program to find the sum of first 100 numbers?
Submit
Still have questions?
Related questions

### How do you write an assembly language program to find the sum of n numbers using array?

write an assembly language program to find sum of N numbers

### How to write a C program to find largest 2 numbers using pointers?

program to find maximum of two numbers using pointers

### How do you write a VBnet program to find the prime numbers between 100 to 200?

VBnet program to find the prime numbers between 100 to 200?

### How do you write a program to find out the square and cube of first ten natural numbers in gw basic?

First you will need to have some basic programming knowledge. You can use this to help make the program that is needed.

### How do you write socket program in c?

For first find an example program.

k

### Write a program to print first 100 alternative prime numbers?

This would require some computer knowledge. It can make it easier to find out the prime numbers without figuring it out in your head.

### How. to. write an. algorthim. to find the. sum of. first. 15 natural. numbers?

Write an. Algorthim. To. Find the. Sum. Of. First15 natural. Numbers

### Write a program to find the product of two numbers using Halving and Doubling method?

i need this answer