Practice Program
Solution : through for loop
void main()
{
int n,i,sum = 0;;
printf("Sum of how many numbers: ");
scanf("%d",&n);
//at every iteration we are assigning
//(current_value + (i *2)) to new value of sum.
for(i=0; i<n; i++)
{
sum = sum+(i*2);
}
printf("Sum of %d even natural numbers are %d",n,sum);
}
void main() { int n,i,sum = 0;; printf("Sum of how many numbers: "); scanf("%d",&n); //at every iteration we are assigning //(current_value + (i *2)) to new value of sum. for(i=0; i<n; i++) { sum = sum+(i*2); } printf("Sum of %d even natural numbers are %d",n,sum); }
Output1
Sum of how many numbers:5
Sum of 5 even natural numbers are 20.
Sum of 5 even natural numbers are 20.
Output2
Sum of how many numbers:10
Sum of 15 even natural numbers are 90.
Sum of 15 even natural numbers are 90.
Output3
Sum of how many numbers:15
Sum of 15 even natural numbers are 210.
Sum of 15 even natural numbers are 210.
Solution : through while loop
void main()
{
int n,i,sum = 0;;
printf("Sum of how many numbers: ");
scanf("%d",&n);
i=1;
//at every iteration we are assigning
//(current_value + i ) to new value of sum.
while(i<=n)
{
sum = sum+(i*2);
i++;
}
printf("Sum of first %d natural numbers are %d",n,sum);
}
void main() { int n,i,sum = 0;; printf("Sum of how many numbers: "); scanf("%d",&n); i=1; //at every iteration we are assigning //(current_value + i ) to new value of sum. while(i<=n) { sum = sum+(i*2); i++; } printf("Sum of first %d natural numbers are %d",n,sum); }
Solution: from do-while loop
void main()
{
int n,i,sum = 0;;
printf("Sum of how many numbers: ");
scanf("%d",&n);
i=1;
//at every iteration we are assigning
//(current_value + i ) to new value of sum.
do
{
sum = sum+(i*2);
i++;
}while(i<=n)
printf("Sum of first %d natural numbers are %d",n,sum);
}
void main() { int n,i,sum = 0;; printf("Sum of how many numbers: "); scanf("%d",&n); i=1; //at every iteration we are assigning //(current_value + i ) to new value of sum. do { sum = sum+(i*2); i++; }while(i<=n) printf("Sum of first %d natural numbers are %d",n,sum); }
defining a function 'sum_of_even' which will return sum of n numbers.
int sum_of_even(int n)
{
int i,sum=0;
//at every iteration we are assigning
//(current_value + i ) to new value of sum.
for(i=1;i<=n;i++)
{
sum = sum+i;
}
return sum;
}
int sum_of_even(int n) { int i,sum=0; //at every iteration we are assigning //(current_value + i ) to new value of sum. for(i=1;i<=n;i++) { sum = sum+i; } return sum; }
Using above define function 'sum_of_even':
void main()
{
int n,result;
printf("How many numbers of sum you want: ");
scanf("%d",&n);
result = sum_of_even(5);
printf("Sum of first %d numbers are %d",n,result);
}
void main() { int n,result; printf("How many numbers of sum you want: "); scanf("%d",&n); result = sum_of_even(5); printf("Sum of first %d numbers are %d",n,result); }
Output1
Sum of how many numbers:5
Sum of 5 even natural numbers are 20.
Sum of 5 even natural numbers are 20.
Using above define function 'sum_of_even':
void main()
{
int n,result;
printf("How many numbers of sum you want: ");
scanf("%d",&n);
result = sum_of_even(10);
printf("Sum of first %d numbers are %d",n,result);
}
void main() { int n,result; printf("How many numbers of sum you want: "); scanf("%d",&n); result = sum_of_even(10); printf("Sum of first %d numbers are %d",n,result); }
Output2
Sum of how many numbers:10
Sum of 10 even natural numbers are 90.
Sum of 10 even natural numbers are 90.
Using above define function 'sum_of_even':
void main()
{
int n,result;
printf("How many numbers of sum you want: ");
scanf("%d",&n);
result = sum_of_even(15);
printf("Sum of %d even natural numbers are %d",n,result);
}
void main() { int n,result; printf("How many numbers of sum you want: "); scanf("%d",&n); result = sum_of_even(15); printf("Sum of %d even natural numbers are %d",n,result); }
Output3
Sum of how many numbers:15
Sum of 15 even natural numbers are 210.
Sum of 15 even natural numbers are 210.
0 Comments
Post a Comment