-->
Showing posts with label 4. Show all posts
Showing posts with label 4. Show all posts

program to get sum of 1,2,3,4,5,.............100 using function

using codeblock
--------------------------------------------------------------------------------------------------


//program get sum of  1,2,3,4,5,.............100 using function
#include<stdio.h>                      //header file
void sum_series();                          // function prototype/declaration with return type zero.
int main()                              //main function
{
sum_series();                                     // function calling.
return 0;                                       //returns 0 value
}
void sum_series()                              // function body line ; also called function definition
{
int sum=0;           //initialization of sum to zero
 int i;                    // variable declaration 
for(i=1;i<=100;i++)       //loop running from 1 to 100 with increment 1 each time
{
    sum=sum+i;           // summing all the numbers repetitively
}
printf("sum=%d\n",sum);      //output
}


----------------------------------------------------------------------------------------------------------
using turboc++
-------------------------------------------------------------------------------------------------
//program get sum of  1,2,3,4,5,.............100 using function
#include<stdio.h>                      //header file
#include<conio.h>
void sum_series();                          // function prototype/declaration with return type zero.
void main()                              //main function
{
sum_series();                                     // function calling.
getch();                                       //returns 0 value
}
void sum_series()                              // function body line ; also called function definition
{
int sum=0;           //initialization of sum to zero
 int i;                    // variable declaration 
for(i=1;i<=100;i++)       //loop running from 1 to 100 with increment 1 each time
{
    sum=sum+i;           // summing all the numbers repetitively
}
printf("sum=%d\n",sum);      //output
}


/* here we have used top down approach. i.e calling from top and writing its body line down.
 we can also reverse the process.*/

program to generate series 1,2,3,4,5,.............100 using function

using codeblock
----------------------------------------------------------------------------------------
// to generate 1,2,3,4,5,....100 series using function.
#include<stdio.h>                      //header file
void series();                          // function prototype/declaration with return type zero.
int main()                              //main function
{
series();                                     // function calling.
return 0;                                       //returns 0 value
}
void series()                              // function body line ; also called function definition
{
 int i;                    // variable declaration
for(i=1;i<=100;i++)       //loop running from 1 to 100 with increment 1 each time
{
    printf("%d\n",i);      //output
}
}

----------------------------------------------------------------------------------------------------

using turboc++
---------------------------------------------------------------------------------------------------
// to generate 1,2,3,4,5,....100 series using function.
#include<stdio.h>                      //header file
#include<conio.h>
void series();                          // function prototype/declaration with return type zero.
void main()                              //main function
{
series();                                     // function calling.
getch();                                       //holds the output
}
void series()                              // function body line ; also called function definition
{
 int i;                    // variable declaration
for(i=1;i<=100;i++)       //loop running from 1 to 100 with increment 1 each time
{
    printf("%d\n",i);      //output
}
}


/* here we have used top down approach. i.e calling from top and writing its body line down.
 we can also reverse the process.*/

program to print/display 2,4,6,8,10,12... to 15th term.

//program to print/display 2,4,6,8,10,12... to 15th term.

#include<stdio.h>
#include<conio.h>
void main()
{
int k=2;

while(k<=15)
   {
      printf("%d,",k);
      k=k+2;
   }
getch();
}

Or
you can use following method.

first term(a)=2
common difference(d)=2
number of terms(n)=15
here  we can see that given series is in arithmetic progression so nth term=a+(n-1)*d.It gives us nth term=30 i.e.
last term will be 30.
We put 30 in while loop

//program to print/display 2,4,6,8,10,12... to 15th term.
#include<stdio.h>
#include<conio.h>
void main()
{
int k=2;

while(k<=30)
   {
      printf("%d,",k);
      k=k+2;
   }
getch();
}