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)+1)) to new value of sum.
for(i=0;i<n;i++)
{
sum = sum+((i*2)+1);
}
printf("Sum of %d odd 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)+1)) to new value of sum. for(i=0;i<n;i++) { sum = sum+((i*2)+1); } printf("Sum of %d odd natural numbers are %d",n,sum); }
Output1
Sum of how many numbers:5
Sum of 5 odd natural numbers are 25.
Sum of 5 odd natural numbers are 25.
Output2
Sum of how many numbers:10
Sum of 10 odd natural numbers are 100.
Sum of 10 odd natural numbers are 100.
Output3
Sum of how many numbers:15
Sum of 15 odd natural numbers are 225.
Sum of 15 odd natural numbers are 225.
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)+1);
i++;
}
printf("Sum of first %d odd 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)+1); i++; } printf("Sum of first %d odd 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)+1);
i++;
}while(i<=n)
printf("Sum of first %d odd 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)+1); i++; }while(i<=n) printf("Sum of first %d odd natural numbers are %d",n,sum); }
defining a function 'sum' 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*2)+1);
}
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*2)+1); } return sum; }
Using above define function 'sum_of_odd':
void main()
{
int n,result;
printf("How many numbers of sum you want: ");
scanf("%d",&n);
result = sum_of_odd(5);
printf("Sum of first %d odd numbers are %d",n,result);
}
void main() { int n,result; printf("How many numbers of sum you want: "); scanf("%d",&n); result = sum_of_odd(5); printf("Sum of first %d odd numbers are %d",n,result); }
Output1
Sum of how many numbers:5
Sum of 5 odd natural numbers are 25.
Sum of 5 odd natural numbers are 25.
Using above define function 'sum_of_odd':
void main()
{
int n,result;
printf("How many numbers of sum you want: ");
scanf("%d",&n);
result = sum_of_odd(10);
printf("Sum of first %d odd numbers are %d",n,result);
}
void main() { int n,result; printf("How many numbers of sum you want: "); scanf("%d",&n); result = sum_of_odd(10); printf("Sum of first %d odd numbers are %d",n,result); }
Output2
Sum of how many numbers:10
Sum of 10 odd natural numbers are 100.
Sum of 10 odd natural numbers are 100.
Using above define function 'sum_of_odd':
void main()
{
int n,result;
printf("How many numbers of sum you want: ");
scanf("%d",&n);
result = sum_of_odd(15);
printf("Sum of %d odd 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_odd(15); printf("Sum of %d odd natural numbers are %d",n,result); }
Output3
Sum of how many numbers:15
Sum of 15 odd natural numbers are 225.
Sum of 15 odd natural numbers are 225.
0 Comments
Post a Comment