Best Answer

#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); }

More answers

#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.

#include <stdio.h>

main()

{

int i;

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

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

}

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

Write your answer...

Submit

Still have questions?

Continue Learning about Engineering

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

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

#include<iostream> int main() { int i=0; while(i++<10) std::cout<<i*i<<std::endl; }

Just write a method or function that calculates the LCM for two numbers at a time. Then calculate the LCM for the first two numbers, get the LCM of the result with the third number, etc.Just write a method or function that calculates the LCM for two numbers at a time. Then calculate the LCM for the first two numbers, get the LCM of the result with the third number, etc.Just write a method or function that calculates the LCM for two numbers at a time. Then calculate the LCM for the first two numbers, get the LCM of the result with the third number, etc.Just write a method or function that calculates the LCM for two numbers at a time. Then calculate the LCM for the first two numbers, get the LCM of the result with the third number, etc.

write a vb program to find the magic square

Related questions

write an assembly language program to find sum of N numbers

program to find maximum of two numbers using pointers

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

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

For first find an example program.

k

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

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

i need this answer

Please visit http://talentsealed.blogspot.com/2009/10/to-find-sqaure-of-numbers-using-c.htmlfor the answer.

Since there is an infinite set of prime numbers the answer would be infinity.

#include<iostream> int main() { int i=0; while(i++<10) std::cout<<i*i<<std::endl; }