main
functionadd
if it is behind the fuction that calls it. No requirement of the declaration otherwise.#include <stdio.h>
// This is the main program
int main()
{
int a,b,sum;
int add(int x,int y);
a=10;
b=24;
sum=add(a,b);
printf("sum= %d\n",sum);
return 0;
}
/* This function calculates the sum of x and y */
int add(int x,int y)
{ int z;
z=x+y;
return(z);
}
sum = +3+5+7+...+20
while
to deal with loopint main()
{
// sum = 1+3+5+7+...+20
int t,sum;
sum = 0;
t = 1;
while(t<=20){
sum += t;
t += 2;
}
printf("1+3+5+7+...+ 20 = %d\n",sum);
return 0;
}
for
to deal with loopbreak
or early break
the loopint main()
{
// sum = 1!+2!+3!+...+8!
int i;
long t,sum;
sum = 0;
t = 1;
for(i=1;i<=8;i++){
sum += t;
t *= i+1;
}
printf("1!+2!+3!+...+ 8! = %ld\n",sum);
return 0;
}
sum = 1/2+3/4+5/8+7/16+...
int main()
{
// sum = 1/2+3/4+5/8+7/16+...
double denominator,numerator,sum;
sum = 0.0;
numerator = 1.0;
denominator = 2.0;
while(numerator/denominator>=1e-6){
sum += numerator/denominator;
numerator += 2.0;
denominator *= 2;
}
printf("1/2+3/4+5/8+7/16+... = %f\n",sum);
return 0;
}