« Previous                                                                 Next Chapter » 
Control Structures

The structure, which controls the execution of the program, can be called as control structure. We can control the program execution in two different ways.

  • By Executing or skipping the required statements by checking some condition. 
  • By re executing some selected statements required number of times.
We have two different types of control structures. They are

1. Conditional Statement.

2. Looping Structures.

1. Conditional Statement

If any statement is useful to check small condition based  on the   result of the condition. If we are able to execute or skip some set of statements such a one can be called as conditional  statement or conditional structure. We have 4 types of

1.1 Simple if

1.2 If else

1.3 If else if (else if ladder)

1.4 Nested if 

1.1 Simple if

This is the first conditional statement which is useful to check  small condition. Based on the result of the condition we can execute some statements or skip them.

Syntax: 

              if (condition)

              {

               statement 1;

               statement 2;

                      .

                      .

               statement n;

               } 

1.2 If else

This is the second conditional statement which provides more facility when compared to simple if. In simple if we are unable to perform any action  if the condition we checked results false. Where as here we have else part to perform some action if the condition we checked results false.

Syntax:

             if (condition)

             {

             statement 1;

             statement 2;

                    .

                    .

             statement n;

             }

             else

             {

             statement 1;

             statement 2;

                    .

                    .

             statement n; 

             }

If the condition results true the statements in the if block will be executed and the statements in the else block will be skipped from the execution. If the condition fails then the statements in the if block will be skipped from the execution and the statements in the else block will be executed.

1.3 If else if

This is the  third conditional statement which provides more facility  than if else. In if else we are unable to check one more condition in else part if the condition we checked where as here we can check any number of conditions as per our requirement.

Syntax: 

             if (condition 1)

             {

             block 1;

             } 

             else if (condition 2)

             {

             block 2;

             }

             else if (condition 3)

             {

             block 3;

             }

             .

             .

             else if (condition n)

             {

             block n;

             }

             else

             {

             statements;

             }

1.4 Nested if

If any if contains one more if in it such an if can be called as nested if. A simple if may contain simple if or simple if may contain if else or if else may contain simple if  or simple if may contain if else if etc.

2. Looping Structures

A loop is nothing but a repetitive structure which is useful to re execute  some set of statements again and again as per our  requirement. We have two different types of loops in C language.

2.1 Top testing loop or Entry control loop.

2.2 Bottom testing loop or Exit control loop.

2.1 Top testing or Entry control loop

In any loop first if we are checking  a condition based on the result of the condition later if we are able to re execute or skip some statements such a loop can be called as top testing or entry control loop. May be top testing we have two different loops.

2.1.1 While loop

2.1.2 For loop

2.2 Bottom testing or Exit control loop

In any loop first  if we are executing  the loop statements later if we are checking a condition such a loop can be called as bottom testing loop or exit control loop. May be bottom testing we have one loop.

2.2.1 Do while loop

2.1.1 While loop

While loop is a top testing loop or entry control loop means in this loop first the condition  will be checked  later based on the result of the condition the loop statements will be executed or skipped.

Syntax:  

              While(condition)

              {

              statement 1;

              statement 2;

                      .

                      .

              statement n;

              } 

Whenever the control is entering into the loop first the condition will be checked. If the condition is false then all the loop statements will be skipped from the execution and the rest of the program will be executed. If the condition is true then all the loop statements will be executed. Whenever the control reaches the end of the loop automatically it will be transfer to condition part again. The same will be repeated until the condition becomes false. To work with the while loop we have to concentrate on three areas.

  • Even though our program doesn't require the help of any variable there should be at least one variable which can be called as loop variable.
  • The loop variable must be initialized with  proper value.
  • It should be incremented or decremented  to make condition at somewhere false.

2.2.1 Do while loop

Do while is a bottom testing loop or exit control loop means in this loop first all the statements will be executed later the condition will be checked. If the condition results false then the rest of the program will be executed. If the condition is true then the control will be transferred to the first statement in the loop. Once again all the loop statements will be executed. When it reaches the end the condition will be checked .The same thing will be repeated until the condition will become false.

Syntax:  

              Do

              {

              statement 1;

              statement 2;

              statement 3;

                     .

                     .

              statement n;

              } while(Condition); 

There is a major difference between   do while and   while loop. In while loop the minimum numbers of executions are zero if the condition fails for the first time. Where as in the do while the minimum numbers of executions are at least one even if the condition fails for first time.

2.1.2 For loop

For loop is a entry control loop or top testing loop means in this loop first the condition will be checked before all the loop statements are going to be executed. It decreases the length of  program and provides more facility to the user when compared to while and do while. In while and do while the initialization and  condition checking and increment or decrement all the three has to be placed in three different places. Where as in for loop all the three has to be placed in the single line. So there is no chance of missing any one of them.

Syntax:

                for (initialization; condition; increment  or decrement)

                {

                statement 1;

                statement 2;

                statement 3;

                       .

                       .

                statement n;

                }

when ever the control is entering into the for loop first the initialization part will be executed . Once it is over the condition will be checked. If the condition results false then all the statements in the loop will be skipped from the execution and the rest of the program will be executed. If the condition results true then all the statements in the loop will be executed. Whenever the control reaches the end of the loop automatically it will be transfer to the increment or decrement part. After that  the condition will be checked again if it  results true the statements will be executed again the same will happen until the condition results false.

   

More Examples
Simple if
/* program to find positive value */
#include<stdio.h>
#include<conio.h>
main()
{
int x;
clrscr();
printf(" enter the value ");
scanf("%d",&x);
if(x>=0)
{
printf("it is a positive value ");
}
}

/* program to find leap year */
#include<stdio.h>
#include<conio.h>
main()
{
int x;
clrscr();
printf(" enter the value ");
scanf("%d",&x);
if(x%4==0)
{
printf(" leap year ");
}
}

/* program to find major person */
#include<stdio.h>
#include<conio.h>
main()
{
int x;
clrscr();
printf(" enter the value ");
scanf("%d",&x);
if(x>=18)
{
printf(" major ");
}
}

/* program to find enter key */
#include<stdio.h>
#include<conio.h>
main()
{
char x;
clrscr();
printf(" enter the character ");
scanf("%c",&x);
if(x==10)
{
printf(" enter key ");
}
}

If else:

/* program to read days and find leap year or not */
#include<stdio.h>
#include<conio.h>
main()
{
int x;
clrscr();
printf(" enter the days ");
scanf("%d",&x);
if(x==366)
{
printf(" leap year ");
}
else
{
printf(" not a leap leap year");
}
}

/* program to find even number or odd number */
#include<stdio.h>
#include<conio.h>
main()
{
int x;
clrscr();
printf(" enter the value ");
scanf("%d",&x);
if(x%2==0)
{
printf(" even number ");
}
else
{
printf(" odd number ");
}
}

/* program to find whether it is space or not */
#include<stdio.h>
#include<conio.h>
main()
{
char x;
clrscr();
printf(" enter the value ");
scanf("%c",&x);
if(x==32)
{
printf(" space ");
}
else
{
printf(" not a space ");
}
}

/* program to find whether it is digit or not */
#include<stdio.h>
#include<conio.h>
main()
{
int x;
clrscr();
printf(" enter the value ");
scanf("%d",&x);
if(x>=0&&x<=9)
{
printf(" digit ");
}
else
{
printf(" not a digit ");
}
}

/* program to find whether first value is big or not */
#include<stdio.h>
#include<conio.h>
main()
{
int a,b;
clrscr();
printf(" enter the values of a,b ");
scanf("%d %d",&a,&b);
if(a>b)
{
printf(" first value is big ");
}
else
{
printf(" first value is not big ");
}
}

/* program to find whether he got profit or not */
#include<stdio.h>
#include<conio.h>
main()
{
int oa,sa;
clrscr();
printf(" enter the values of oa,sa ");
scanf("%d %d",&oa,&sa);
if(oa<sa)
{
printf(" profit ");
}
else
{
printf(" no profit ");
}
}

If else if:

/* program to find relation between two values */
#include<stdio.h>
#include<conio.h>
main()
{
int a,b;
clrscr();
printf("enter the values of a,b");
scanf("%d %d",&a,&b);
if(a>b)
{
printf("a is big");
}
else if(b>a)
{
printf("b is big");
}
else
{
printf("both are equal");
}
}

/* program to find whether person got profit or loss */
#include<stdio.h>
#include<conio.h>
main()
{
int oa,sa;
clrscr();
printf("enter the values of oa,sa");
scanf("%d %d",&oa,&sa);
if(sa>oa)
{
printf("profit");
}
else if(oa>sa)
{
printf("loss");
}
else
{
printf("no profit,no loss");
}
}

/* program to find the biggest of 3 numbers */
#include<stdio.h>
#include<conio.h>
main()
{
int a,b,c;
clrscr();
printf("enter the values of a,b,c");
scanf("%d %d %d",&a,&b,&c);
if((a>b)&&(a>c))
{
printf("a is big");
}
else if(b>c)
{
printf("b is big");
}
else
{
printf("c is big");
}
}

/* program to find the character */
#include<stdio.h>
#include<conio.h>
main()
{
char x;
clrscr();
printf("enter the character");
x=getchar();
if((x>=65)&&(x<=90))
{
printf("capital letter");
}
else if((x>=97)&&(x<=122))
{
printf("small letter");
}
else if((x>=48)&&(x<=57))
{
printf("digit");
}
else if(x==32)
{
printf("space");
}
else
{
printf("other special character");
}
}

Nested if:

/* example for nested if */
#include<stdio.h>
#include<conio.h>
main()
{
int m1,m2,m3,tot;
float avg;
clrscr();
printf("enter the three subject marks");
scanf("%d %d %d",&m1,&m2,&m3);
tot=m1+m2+m3;
avg=tot/3;
if((m1>=35)&&(m2>=35)&&(m3>=35))
{
if((avg>=35)&&(avg<50))
{
printf(" THIRD CLASS ");
}
else if((avg>=50)&&(avg<60))
{
printf(" SECOND CLASS ");
}
else if(avg>=60)
{
printf(" FIRST CLASS ");
}
}
else
{
printf(" FAIL ");
}
}

/* example for goto statement */
#include<stdio.h>
#include<conio.h>
main()
{
int i=1;
clrscr();
ding:
printf(" HAI\n");
i++;
if(i<=5)
goto ding;
}

While loop:

/* program to print a message 5 times */
#include<stdio.h>
#include<conio.h>
main()
{
int i=1;
clrscr();
while(i<=5)
{
printf("HAI\n");
i++;
}
}

/* program to print a message n times */
#include<stdio.h>
#include<conio.h>
main()
{
int i=1,n;
clrscr();
printf("enter the no of times to be printed");
scanf("%d",&n);
while(i<=n)
{
printf("HAI\n");
i++;
}
}

/* program to print first 5 natural numbers */
#include<stdio.h>
#include<conio.h>
main()
{
int i=1;
clrscr();
while(i<=5)
{
printf("%d ",i);
i++;
}
}

/* program to print first n natural numbers */
#include<stdio.h>
#include<conio.h>
main()
{
int i=1,n;
clrscr();
printf("enter the no of numbers to be printed");
scanf("%d",&n);
while(i<=n)
{
printf("%d ",i);
i++;
}
}

/* program to print all even numbers up to n */
#include<stdio.h>
#include<conio.h>
main()
{
int i=1,n;
clrscr();
printf("enter the maximum limit");
scanf("%d",&n);
while(i<=n)
{
if(i%2==0)
{
printf("%d ",i);
}
i++;
}
}

/* program to print all odd numbers up to n */
#include<stdio.h>
#include<conio.h>
main()
{
int i=1,n;
clrscr();
printf("enter the maximum limit");
scanf("%d",&n);
while(i<=n)
{
if(i%2==1)
{
printf("%d ",i);
}
i++;
}
}

/* program to find the sum of n natural numbers */
#include<stdio.h>
#include<conio.h>
main()
{
int i=1,n,sum=0;
clrscr();
printf("enter the maximum limit");
scanf("%d",&n);
while(i<=n)
{
sum=sum+i;
i++;
}
printf("the sum is %d\n",sum);
}

/* program to find the sum of all even numbers up to n */
#include<stdio.h>
#include<conio.h>
main()
{
int i=1,n,sum=0;
clrscr();
printf("enter the maximum limit");
scanf("%d",&n);
while(i<=n)
{
if(i%2==0)
{
sum=sum+i;
}
i++;
}
printf("the sum is %d\n",sum);
}

/* program to find the sum of all odd numbers up to n */
#include<stdio.h>
#include<conio.h>
main()
{
int i=1,n,sum=0;
clrscr();
printf("enter the maximum limit");
scanf("%d",&n);
while(i<=n)
{
if(i%2==1)
{
sum=sum+i;
}
i++;
}
printf("the sum is %d\n",sum);
}

/* program to find sum of all,even,odd numbers up to n */
#include<stdio.h>
#include<conio.h>
main()
{
int i=1,n,sum=0,sume=0,sumo=0;
clrscr();
printf("enter the maximum limit");
scanf("%d",&n);
while(i<=n)
{
sum=sum+i;
if(i%2==0)
{
sume=sume+i;
}
else
{
sumo=sumo+i;
}
i++;
}
printf("the sum of all natural numbers is %d\n",sum);
printf("the sum of all even natural numbers is %d\n",sume);
printf("the sum of all odd natural numbers is %d\n",sumo);
}

/* program to print the factors of given number */
#include<stdio.h>
#include<conio.h>
main()
{
int i=1,n;
clrscr();
printf("enter the number");
scanf("%d",&n);
while(i<=n)
{
if(n%i==0)
{
printf("%d ",i);
}
i++;
}
}

/* program to print even factors of the given number */
#include<stdio.h>
#include<conio.h>
main()
{
int i=1,n;
clrscr();
printf("enter the number");
scanf("%d",&n);
while(i<=n)
{
if((n%i==0)&&(i%2==0))
{
printf("%d ",i);
}
i++;
}
}

/* program to print odd factors of the given number */
#include<stdio.h>
#include<conio.h>
main()
{
int i=1,n;
clrscr();
printf("enter the number");
scanf("%d",&n);
while(i<=n)
{
if(n%i==0)
{
if(i%2==1)
{
printf("%d ",i);
}
}
i++;
}
}

/* program to find sum of the factors of given number */
#include<stdio.h>
#include<conio.h>
main()
{
int i=1,n,sum=0;
clrscr();
printf("enter the value of n");
scanf("%d",&n);
while(i<=n)
{
if(n%i==0)
{
sum=sum+i;
}
i++;
}
printf("the sum of the factors is %d\n",sum);
}

/* program to find sum of all,even and odd factors of number */
#include<stdio.h>
#include<conio.h>
main()
{
int i=1,n,sum=0,sume=0,sumo=0;
clrscr();
printf("enter the value of n");
scanf("%d",&n);
while(i<=n)
{
if(n%i==0)
{
sum=sum+i;
if(i%2==0)
sume=sume+i;
else
sumo=sumo+i;
}
i++;
}
printf("the sum of the factors is %d\n",sum);
printf("the sum of the even factors is %d\n",sume);
printf("the sum of the odd factors is %d\n",sumo);
}

/* program to find no of factors of a given number */
#include<stdio.h>
#include<conio.h>
main()
{
int i=1,n,count=0;
clrscr();
printf("enter the n");
scanf("%d",&n);
while(i<=n)
{
if(n%i==0)
{
count++;
}
i++;
}
printf("the no of factors %d ",count);
}

/* program to find no of all,even and odd factors of number */
#include<stdio.h>
#include<conio.h>
main()
{
int i=1,n,count=0,counte=0,counto=0;
clrscr();
printf("enter the value of n");
scanf("%d",&n);
while(i<=n)
{
if(n%i==0)
{
count++;
if(i%2==0)
counte++;
else
counto++;
}
i++;
}
printf("the no of all factors is %d\n",count);
printf("the no of even factors is %d\n",counte);
printf("the no of odd factors is %d\n",counto);
}

/* program to find whether the given number is prime or not */
#include<stdio.h>
#include<conio.h>
main()
{
int i=1,n,count=0;
clrscr();
printf("enter the n");
scanf("%d",&n);
while(i<=n)
{
if(n%i==0)
count++;
i++;
}
if(count==2)
printf("it is a prime number");
else
printf("it is not a prime number");
}

/* second method */
#include<stdio.h>
#include<conio.h>
main()
{
int i=2,n,count=0;
clrscr();
printf("enter the n");
scanf("%d",&n);
while(i<n)
{
if(n%i==0)
count++;
i++;
}
if(count==0)
printf("it is a prime number");
else
printf("it is not a prime number");
}

/* third method */
#include<stdio.h>
#include<conio.h>
main()
{
int i=1,n,prod=1;
clrscr();
printf("enter the n");
scanf("%d",&n);
while(i<=n)
{
if(n%i==0)
prod=prod*i;
i++;
}
if(prod==n)
printf("it is a prime number");
else
printf("it is not a prime number");
}

/* fourth method */
#include<stdio.h>
#include<conio.h>
main()
{
int i=1,n,sum=0;
clrscr();
printf("enter the n");
scanf("%d",&n);
while(i<=n)
{
if(n%i==0)
sum+=i;
i++;
}
if(sum-1==n)
printf("it is a prime number");
else
printf("it is not a prime number");
}

/* program to find whether the given number is perfect or not */
#include<stdio.h>
#include<conio.h>
main()
{
int i=1,n,sum=0;
clrscr();
printf("enter the n");
scanf("%d",&n);
while(i<n)
{
if(n%i==0)
sum+=i;
i++;
}
if(sum==n)
printf("it is a perfect number");
else
printf("it is not a perfect number");
}

/* program to find the product of n natural numbers */
#include<stdio.h>
#include<conio.h>
main()
{
int i=1,n,prod=1;
clrscr();
printf("enter the n");
scanf("%d",&n);
while(i<=n)
{
prod=prod*i;
i++;
}
printf("the product is %d\n",prod);
}

/* program to find factorial of the given number */
#include<stdio.h>
#include<conio.h>
main()
{
int n,fact=1;
clrscr();
printf("enter the n");
scanf("%d",&n);
while(n>0)
{
fact=fact*n;
n--;
}
printf("the factorial is %d\n",fact);
}

/* program to find power of given base,exp */
#include<stdio.h>
#include<conio.h>
main()
{
int base,exp,i=1,res=1;
clrscr();
printf("enter the base,exp");
scanf("%d %d",&base,&exp);
while(i<=exp)
{
res=res*base;
i++;
}
printf("the result is %d\n",res);
}

/* second method */
#include<stdio.h>
#include<conio.h>
main()
{
int base,exp,res=1;
clrscr();
printf("enter the base,exp");
scanf("%d %d",&base,&exp);
while(exp>0)
{
res=res*base;
exp--;
}
printf("the result is %d\n",res);
}

/* program to print multiplication table of a given number */
#include<stdio.h>
#include<conio.h>
main()
{
int n,l,i=1;
clrscr();
printf("enter the n,l");
scanf("%d %d",&n,&l);
while(i<=l)
{
printf("%d * %2d = %d\n",n,i,n*i);
i++;
}
}

/* program to detach the number in to digits */
#include<stdio.h>
#include<conio.h>
main()
{
int n,r;
clrscr();
printf("enter the n");
scanf("%d",&n);
while(n>0)
{
r=n%10;
printf("%d ",r);
n=n/10;
}
}

/* program to find sum of all,even and odd digits of given number */
#include<stdio.h>
#include<conio.h>
main()
{
int n,r,sum=0,sume=0,sumo=0;
clrscr();
printf("enter the n");
scanf("%d",&n);
while(n>0)
{
r=n%10;
sum=sum+r;
if(r%2==0)
{
sume=sume+r;
}
else
{
sumo=sumo+r;
}
n=n/10;
}
printf("the sum of all digits %d\n",sum);
printf("the sum of all even digits is %d\n",sume);
printf("the sum of all odd digits is %d\n",sumo);
}

/* program to find count of all,even and odd digits of given number */
#include<stdio.h>
#include<conio.h>
main()
{
int n,r,count=0,counte=0,counto=0;
clrscr();
printf("enter the n");
scanf("%d",&n);
while(n>0)
{
r=n%10;
count++;
if(r%2==0)
{
counte++;
}
else
{
counto++;
}
n=n/10;
}
printf("the count of all digits %d\n",count);
printf("the count of all even digits is %d\n",counte);
printf("the count of all odd digits is %d\n",counto);
}

/* program to find reverse of the given number */
#include<stdio.h>
#include<conio.h>
main()
{
int n,r,sum=0;
clrscr();
printf("enter the n");
scanf("%d",&n);
while(n>0)
{
r=n%10;
sum=sum*10+r;
n=n/10;
}
printf("the reverse of the number is %d\n",sum);
}

/* program to find whether the given number is palindrome or not */
#include<stdio.h>
#include<conio.h>
main()
{
int n,r,sum=0,dup;
clrscr();
printf("enter the n");
scanf("%d",&n);
dup=n;
while(n>0)
{
r=n%10;
sum=sum*10+r;
n=n/10;
}
if(sum==dup)
printf("it is a palindrome");
else
printf("it is not a palindrome");
}

/* program to find whether the given number is unique or not(9801) */
#include<stdio.h>
#include<conio.h>
main()
{
int n,r,sum=0,dup;
clrscr();
printf("enter the n");
scanf("%d",&n);
dup=n;
while(n>0)
{
r=n%10;
sum=sum*10+r;
n=n/10;
}
if(sum*9==dup)
printf("it is a unique number");
else
printf("it is not a unique number");
}

/* program to find whether given number is armstrong or not */
#include<stdio.h>
#include<conio.h>
main()
{
int n,r,sum=0,dup;
clrscr();
printf("enter the n");
scanf("%d",&n);
dup=n;
while(n>0)
{
r=n%10;
sum=sum+(r*r*r);
n=n/10;
}
if(sum==dup)
{
printf("it is a armstrong");
}
else
{
printf("it is not a armstrong");
}
}

/* program to find whether the given number is strong or not */
#include<stdio.h>
#include<conio.h>
main()
{
int n,r,sum=0,dup,fact;
clrscr();
printf("enter the n");
scanf("%d",&n);
dup=n;
while(n>0)
{
r=n%10;
fact=1;
while(r>0)
{
fact=fact*r;
r--;
}
sum=sum+fact;
n=n/10;
}
if(sum==dup)
{
printf("it is a strong");
}
else
{
printf("it is not a strong");
}
}

Do while loop:

/* program to find the first n natural numbers */
#include<stdio.h>
#include<conio.h>
main()
{
int n,i=1;
clrscr();
printf("enter the n");
scanf("%d",&n);
do
{
printf("%d ",i);
i++;
}while(i<=n);
}

/* program to print the febonacci series up to n */
#include<stdio.h>
#include<conio.h>
main()
{
int n,a=0,b=1,c;
clrscr();
printf("enter the n");
scanf("%d",&n);
printf("%d %d ",a,b);
do
{
c=a+b;
printf("%d ",c);
a=b;
b=c;
}while(c<n);
}

/* program to print febonacci series between two limits */
#include<stdio.h>
#include<conio.h>
main()
{
int l1,l2,a=0,b=1,c;
clrscr();
printf("enter the two limits");
scanf("%d %d",&l1,&l2);
if(l1==0)
printf("%d %d ",a,b);
else if(l1==1)
printf("%d ",b);
do
{
c=a+b;
if((c>=l1)&&(c<=l2))
printf("%d ",c);
a=b;
b=c;
}while(c<l2);
}

/* program to print prime numbers in febanacci series between two limits */
#include<stdio.h>
#include<conio.h>
main()
{
int l1,l2,a=0,b=1,c,i,count;
clrscr();
printf("enter the two limits");
scanf("%d %d",&l1,&l2);
do
{
c=a+b;
if((c>=l1)&&(c<=l2))
{
i=1;
count=0;
while(i<=c)
{
if(c%i==0)
{
count++;
}
i++;
}
if(count==2)
printf("%d ",c);
}
a=b;
b=c;
}while(c<l2);
}

/* program to find the lucky digit of a given number */
#include<stdio.h>
#include<conio.h>
main()
{
int n,r,sum;
clrscr();
printf("enter the number");
scanf("%d",&n);
do
{
sum=0;
while(n>0)
{
r=n%10;
sum=sum+r;
n=n/10;
}
n=sum;
}while(sum>9);
printf("the lucky digit is %d\n",sum);
}

For loop:

/* program to print n natural numbers using for loop */
#include<stdio.h>
#include<conio.h>
main()
{
int n,i;
clrscr();
printf("enter the number");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("%d ",i);
}
}

/* program to find 1+x+x^2+x^3+........+x^n */
#include<stdio.h>
#include<conio.h>
main()
{
int x,n,sum=1,term=1,i;
clrscr();
printf("enter the x,n values");
scanf("%d %d",&x,&n);
for(i=1;i<=n;i++)
{
term=term*x;
sum=sum+term;
}
printf("sum of terms in the series is %d\n",sum);
}

/* program to find the value of 1+x^2+x^4+.......+x^n */
#include<stdio.h>
#include<conio.h>
main()
{
int x,n,sum=1,term=1,i;
clrscr();
printf("enter the x,n");
scanf("%d %d",&x,&n);
for(i=1;i<=n;i++)
{
term=term*x;
if(i%2==0)
{
sum=sum+term;
}
}
printf("sum of the even power terms is %d\n",sum);
}

/* program to find the value of x+x^3+x^5+.......+x^n */
#include<stdio.h>
#include<conio.h>
main()
{
int x,n,sum=1,term=1,i;
clrscr();
printf("enter the x,n");
scanf("%d %d",&x,&n);
for(i=1;i<=n;i++)
{
term=term*x;
if(i%2==1)
{
sum=sum+term;
}
}
printf("sum of the odd power terms is %d\n",sum);
}

/* program to find value of 1+(1/x)+(2/x^2)+.....(n/x^n) */
#include<stdio.h>
#include<conio.h>
main()
{
int x,n,term=1,i;
float sum=1;
clrscr();
printf("enter the x,n");
scanf("%d %d",&x,&n);
for(i=1;i<=n;i++)
{
term=term*x;
sum=sum+(float)i/term;
}
printf("sum of the power terms is %f\n",sum);
}

/* program to find value of 1+(2/x^2)+(4/x^4)+.....*/
#include<stdio.h>
#include<conio.h>
main()
{
int x,n,term=1,i;
float sum=1;
clrscr();
printf("enter the x,n");
scanf("%d %d",&x,&n);
for(i=1;i<=n;i++)
{
term=term*x;
if(i%2==0)
{
sum=sum+(float)i/term;
}
}
printf("sum of the even power terms is %f\n",sum);
}

/* program to find value of 1+(1/x)+(3/x^3)+......*/
#include<stdio.h>
#include<conio.h>
main()
{
int x,n,term=1,i;
float sum=1;
clrscr();
printf("enter the x,n");
scanf("%d %d",&x,&n);
for(i=1;i<=n;i++)
{
term=term*x;
if(i%2==1)
{
sum=sum+(float)i/term;
}
}
printf("sum of the odd power terms is %f\n",sum);
}

/* program to find the value of 1+(x)+(x^2/2)+(x^3/3)+.....*/
#include<stdio.h>
#include<conio.h>
main()
{
int x,n,term=1,i;
float sum=1;
clrscr();
printf("enter the x,n");
scanf("%d %d",&x,&n);
for(i=1;i<=n;i++)
{
term=term*x;
sum=sum+(float)term/i;
}
printf("sum of the power terms is %f\n",sum);
}

/* program to find value of 1-x+x^2-x^3+x^4.....*/
#include<stdio.h>
#include<conio.h>
main()
{
int x,n,sum=1,term=1,i;
clrscr();
printf("enter the x,n");
scanf("%d %d",&x,&n);
for(i=1;i<=n;i++)
{
term=term*-x;
sum=sum+term;
}
printf("sum of the power terms is %d\n",sum);
}

/* program to find value of 1+x-x^2+x^3-x^4.....*/
#include<stdio.h>
#include<conio.h>
main()
{
int x,n,sum=1,term=1,i;
clrscr();
printf("enter the x,n");
scanf("%d %d",&x,&n);
for(i=1;i<=n;i++)
{
term=term*x;
sum=sum+term;
term=term*-1;
}
printf("sum of the power terms is %d\n",sum);
}




  « Previous                                                                  Next Chapter »