132 Programming and Data Structures
scanf ("%d",&cn);
for (i=l;i<=w;i++)
I
x=x+(l/pozv(i,2));
y=y+(llpow(i,3));
i
printf ("Value of x =
printf ("nValue o fy = %f',y);
getcheO;
OUTPUT:
Enter Value of n: 2
Value of x = 1.2500
Value of Y = 1.12500
Explanation Initially it is assumed sum of the two series 1 and 2 are zero. So, the variables ' x 7 and
xy 7 are set to zero. The denominators of the first and second equations are squares and cubes of
numbers from 1 to % n 7 respectively. Where, the user enters value o f ' n 7 and it determines how long
the series should be continued. Every time in the loop square and cube of variable ' i 7 are calculated
using the library function pow (). They are added to the variables ' x 7 and ' y 7 respectively
6.15 Write a program to generate triangular number.
Note: Triangular number is nothing but summation of 1 to given number.
For example, when entered number is 5 it's triangular number would be (l+2+3+4+5)=15.
# include <stdio.h>
# include <conio.h>
void main()
{
in t n , j , tri_num=0;
c l r s c r ();
printf ("What Triangular number do you want
scanf ("%d",&cn);
for (j=l;j<=n;++j)
tri_n um=tri_num+ j;
printf ("Triangular number of% d is %d ",n,tri_num);
I
Loop Control Statements 133
OUTPUT;
What Triangular number do you w ant: 5
Triangular number of 5 is 15
Explanation In the above program a number is entered whose triangular number is to be calculated.
The fo r loop is initialized from 1 to ' n ' . In each iteration of fo r loop, the value of ' j ' is added to
't r i n u m ' variable. When the loop terminates the 't r i n u m ' contains triangular number of
entered number.
6.16 Write a program to find sum of the following series.
1+2+3+.. n
l 2+22+23+..n2
# in clu d e < std io .h >
# in clu d e < conio.h>
void mainO
{
in t sum=0, ssum=0, i , j ;
c l r s c r ( ) ;
printf ("Enter Number
scanf ("%d", &j);
clrscrO;
printf (" Numbers:");
for (i=l,*!<=/,*i++)
printf C%5d",i);
printf (" Squares:");
for (i=l,-i<=j;f++)
{
printf ("%5d",i*i);
sum=sum+i;
ssum=ssum+i*i;
}
printf (" Sum o f Numbers from 1 to %d :%d”,j,sum);
printf (" Sum o f Squares o f l to %d Numbers :%d,j,ssum);
I
OUTPUT:
Enter Number 5
Numbers: 1 2 3 4 5
134 Programming and Data Structures
Squares: 1 4 9 16 25
Sum of Numbers from 1 to 5:15
Sum of Squares of 1 to 5 Numbers: 55
Explanation In the above cited program sum and square of sum are assumed to be zero. The first
for loop prints number from 1 to the entered number. The second for loop does the sum and
squares of the numbers froml to the entered number. The variable ' sum7 and ' ssum' are used for
displaying the final results.
6.17 Write a program to find the perfect squares from 1 to 500.
#include <etdio.h>
#include <math.h>
#include <conio.h>
void main()
{
in t i , c o u n t,x ;
£ lo a t c ;
c l r s c r ( ) ;
printf ("nn");
printf (" Perfect squares from 1 to 500n");
count=0;
for (i=l,-i<=500;i++)
{
c=sqrt(i);
x=floor(c); I* For rounding up floorO is used. */
if (c==x)
printf Ct%5d,i);
count++;
i
printf ("nn Total Perfect Squares =%d ",count);
getchO;
§
OUTPUT:
1 4 9 16 25 36 49 64 81 121 144
169 196 225 256 289 324 361 400 441
484
Total Perfect Squares = 22
Explanation Numbers like 2,4,9,25,. . . are perfect squares. In the above program a fo r loop is
used from 1 to 500. We can use sqrt () function for finding the square root of the numbers. The
Loop Control Statements 135
square root obtained is stored in variable ' c ' and it is rounded off and stored in another variable ' x '.
The value of xc ' remains unchanged. Now the comparison between 'x ' and * c' are made. If both of
them are same then the number becomes a perfect square root. Perfect square roots are only integers
and not floats. Hence, f lo o r () function dose not effect integer values.
6.18 Write a program to detect the largest number out of five numbers and display it.
main()
{
i n t a,b,c,d,o ,sum=0,i;
clrsc r() ;
printf ("nEntcr Five numbers
scanf ("%d %d %d %d %d",&za,8zb,&i:c,&cd,&Le);
sum=a+b+c+d+e;
for (i=sutn; i<=sutnrf~)
{
if (i==a I I i==b I I i==c I I i==d I I i==e)
{
printf ("The Largest Number: %d”,i);
exit();
I
I
OUTPUT:
Enter Five numbers : 5 2 3 7 3
The Largest Number : 7
Explanation Through the keyboard five numbers are entered and their sum is stored in variable
% sum '. The f o r loop is used up to value of ' sum' in the reverse order (- - ) . While decreasing the
value of sum, the same is tested each time as to whether the decreased sum is equal to one of the
entered numbers. In case the condition is satisfied, the largest number is displayed and e x it ()
terminates the program. The largest number entered through the keyboard appears first while
decreasing the value of 'sum' (sum =a+b+c+d+e).
6.19 Write a program to detect the smallest number out of five numbers and display it.
main ()
{
in t a ,b ,c ,d ,e ,s u m = 0 ,i;
c l r s c r ( ) ;
printf ("nEnter Five numbers
scanf ("%d %d %d %d
sum=a+b+c+d+e;
for (i=l; i<=sum;i++)
I
if (i==a I I i~ b I I i==c I I i==d I I i==e)
I
136 Programming and Data Structures
printf ("The Smallest Number : %d",i);
exitO;
}
}
OUTPUT:
Enter Five numbers : 5 2 3 7 3
The Smallest Number: 2
Explanation The logic used in the above program is same as explained in the previous program.
The only difference is that the for loop is used in increasing order (++). While increasing the
number from one to the ' sum7 the smallest number appears first. When match occurs, the smallest
number is displayed.
6.20 Write a program to print the five entered numbers in ascending order,
m ain()
{
in t a ,b ,c,d ,e,su m = 0,i;
c l r s c r ();
printf (" iEtiter Five numbers
scanf ("%d %d %d %d %d",&:a,&b,&x,&:d,&e);
printf ("n Numbers in ascending order
sum=a+b+c+d+e;
for (i=l; k=s«m;/++)
I
if (i==a I I i- - b I I f==c I I i==d I I i==e)
I
printf ("%3d”,i);
I
OUTPUT:
Enter Five numbers : 5 8 7 4 1
Numbers in ascending order : 1 4 5 7 8
Explanation The logic used^is same as in the previous program. Here, after satisfying the i f
condition the program doesn't terminate and searches for the next number. This procedure is
continued till the value of xi ' reaches to 'sum'. The numbers are sorted and displayed in the
increasing order.
For descending order the.for should be " fo r (i=sum; i<=sum;i--) ".
..................Content has been hidden....................

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