150 Programming and Data Structures
fo r (x=0;x<8;x++)
I
k++;
fo r (a=0;a<3;a++)
I
b=y%2;
gotoxy(j-=3,k);
printf (“%d",b);
y=y/2;
if (b==l)
C++;
if (c%2~0)
I
gotoxy(25,k);
printf ("%d",l);
1
else
I
gotoxy(25,k);
printf ("%d",0);
OUTPUT:
Binary Bits Parity Bits
0 0 0
0 0 1
0 1 0
O i l
1 0 0
1 0 1
1 1 0
1 1 1
0
0
0
0
1
Loop Control Statements 151
Explanation In the above program two for loops are used. The inner for loop generates three
binary bits for each octal number. The 0 to 7 octal numbers are taken from the outer for loop. If the
bit is 1 counter ' c ' is increased. The value of %c ' is checked for even or odd condition. If ' c # is
odd the parity bit ' 0 ' is displayed otherwise 11 # .The output is displayed.
6.36 Write a program to evaluate the series given in comment
/* sum of series of x-x3/3!+x5/5!-x7/7!+...x“/n! * /
# include <stdio.h>
# include <math.h>
main()
I
int n,i,x^=3,f=l,l;
float sum;
clrscri);
printf ("Enter x& n :");
scanf ("%d %d",/kx,&cn);
sum=x;
for (i=3,n<=nfl+=2)
{
M ;
if(c%2!=0)
I
for (l=l;l<=i;l++)
sum=sum-pow(x,i)lf,
I
else
I
for (l=l;l<=i;l++)
f = n
sum=sutn+pow(x,i)lfi
I
C + + ;
}
printf ("nSum of series Numbers :% f,sum);
getcheO;
I
OUTPUT:
Enter x & n : 2 5
Sum of series Numbers :0.9333
152 Programming and Data Structures
Explanation Through the keyboard values of 'x ' and 'n ' are entered, where numerical constant
values are assigned to *x ' and ' n '. Throughout the series 'n ' decides how long the series should
continue. The series contains alternate + and - terms. The two fo r loops within i f are used for
calculating the factorials. The i f statement decides the positive or negative term depending upon
odd or even value of % c '.
The following are the programs based on series. The logic of the programs is simple for understanding.
6.37 Write a program to evaluate the series given in comment
/* x+x2/2!+xV4!+xV6!+...xn/n! *1
# in clud e < std io .h >
# in clu d e <math.h>
m ain()
{
in t f=>l , 1 , i , x , y ;
f lo a t sum;
c lr s c r O ;
printf ("Enter the value o fx & y
scanf ("%d %d",&cx,&cy);
sum = x;
for (i=2;i<=y,i+=2)
I
f= l;
fo r (l-l;l<=i;l++)
f= n ;
sum = sum + pow(x,i)lf;
I
printf ("rt Sum of Series : %f', sum);
getcheO;
I
QU 1 PUT:
Lnter the value of x & y : 4 4
Sum of Series :22.6666
6 38 Write a program to evaluate the series given in comment
I* l-l/l!+2/2!-3/3!...n/n! */
# include <stdio,h>
# include <conio.h>
# include <math.h>
Loop Control Statements 153
void mainO
{
in t n , i ,c = 3 , l ;
flo at sum=0,f=l,k;
c l r s c r ();
printf ("Enter value ofn
scanf ("%d",&n);
sum=l;
for (i=lrf<=w;i++)
I
f= l;
if(c%2!=0)
I
fo r (l-l;l<=i;l++)
k=i/f;
sum=sum-k;
I
else
(
fo r (l=l;l<=i;l++)
f= ru
sum=sum+(i/f);
I
C + + ;
I
printf ("nSum of series Numbers :%f',sunt);
OUTPUT:
Enter value of n : 3
Sum of series Numbers : 0.5000
6.39 Write a program to detect the Armstrong numbers in three digits from 100 to 999. If sum
of cubes of each digits of the number is equal to number itself, then the number is called
as an Armstrong number. ( For example 153 = l 3 * 53 * 33 = 153 )
# include <Stdio.h>
# include <conio.h>
154 Programming and Data Structures
# include <math.h>
mainO
{
in t n ,d ,x ;
int k,i,cu be«0;
c l r s c r () ;
printf ("n The Following Numbers are Armstrong numbers.
fo r (k=100;k<=999;k++)
I
cube=0;
x-1;
d=3;
n=k;
while (x<=d)
{
i=n%10;
cube=cube+pow(IJ);
n=n/10;
}
if(cube==k)
printf ("nt %d ",k);
I
i
OUTPUT:
The Following Numbers are Armstrong numbers.
153
370
371
470
Explanation The program separates individual digit of a number and calculates the cubes of each
digit. If the sum of cubes of individual digits of a number is equal to that number then the number
is called as Armstrong numbers.
6.40 Write a program to display the stars as shown below.
* * *
* * * if
* * * * *
# include <stdio.h>
main()
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.172.38