The Loop Control Structure | Let us C solution with details description and tutorials | yashwant kanetkar
[B] Attempt the following:
(a)Write a program to calculate overtime pay of 10 employees. Overtime is paid at the rate of Rs. 12.00 per hour for every hour worked above 40 hours. Assume that employees do not work for fractional part of an hour.
Show Solutions
Hide Solutions
/*b (a)Write a program to calculate overtime pay of 10
employees. Overtime is paid at the rate of Rs. 12.00
per hour for every hour worked above 40 hours. Assume
that employees do not work for fractional part of an hour.
*/
#include <stdio.h> //header file
#include <conio.h> //header file
int main() //main function.. starting of c code
{
int hour,employee,overtime,overtime_pay;
employee=1;
while (employee<=10)
{
printf("\nEnter the no.of Hrs done by employee %d:",employee);
scanf ("%d",&hour);
if(hour>40)
{
overtime = hour-40;
overtime_pay =12*overtime;
printf ("\nYour overtime pay is:%d\n\n", overtime_pay);
}
else if (hour<=40)
{
printf("\nYou won't get overtime pay.\n\n");
}
employee++;
}
getch();
return 0;
}
/*
\ is used to join 2 line.If \ is not use syntax error will be.
*/
(b)Write a program to find the factorial value of any number entered through the keyboard.
Show Solutions
Hide Solutions
/*b (b)Write a program to find the factorial value of
any number entered through the keyboard.
*/
#include <stdio.h> //header file
#include <conio.h> //header file
int main() //main function.. starting of c code
{
int number,factorial,fact;
printf("Enter the number for calculating factorial: ");
scanf ("%d",&number);
fact=number;
factorial=1;
while(fact>=1)
{
factorial=factorial*fact;
fact--;
}
printf("The factorial of entered number is :%d", factorial);
getch();
return 0;
}
/*
logic of problem: number = fact
initially factorial = number as (number =fact)
in next loop factorial =number* (number-1) as (fact=fact-1)
......
loop should continue till fact>=1.. finally number will
be multiplied upto 1.
Example: factorial = 5(fact)
factorial = 5(fact) * 4 (fact-1);
factorial = 5(fact) * f (fact-1)*(fact-2)...*1
..................
\ is used to join 2 line.If \ is not use syntax error will be.
*/
(c)Two numbers are entered through the keyboard. Write a program to find the value of one number raised to the power of another.
Show Solutions
Hide Solutions
/*b (c)Two numbers are entered through the keyboard. Write
a program to find the value of one number raised to the
power of another.
*/
#include <stdio.h> //header file
#include <conio.h> //header file
int main() //main function.. starting of c code
{
int number1,number2,a=1,b=1;
printf("Enter the number1 to raise power on number2 ");
scanf ("%d",&number1);
printf("Enter the number2: ");
scanf ("%d",&number2);
while(a<=number1)//loops exists number1 times.
{
b = number2*b; //number2 is multiplied number1 times.
a++;
}
printf("number1 raising power on number2 gives %d",b);
getch();
return 0;
}
/*
logic of problem: number2 should be multiplied number1 time.
so while(a<=number1) executive number1 times.
inside while loop number2 is multiplied number1 times as
a<=number1
eg. number1=3
number2=2
in 1st loop b=2*1=2;
in 2nd loop b=2*2=4;
in 3rd loop b=4(2*2)*2=8;
loops fails as a>number1.
so results 8.
*/
(d)Write a program to print all the ASCII values and their equivalent characters using a while loop. The ASCII values vary from 0 to 255.
Show Solutions
Hide Solutions
/*b (d)Write a program to print all the ASCII values and
their equivalent characters using a while loop. The
ASCII values vary from 0 to 255.
*/
#include <stdio.h> //header file
#include <conio.h> //header file
int main() //main function.. starting of c code
{
int num=0;
while(num<=255)
{
printf("\nascii= %d character = %c",num,num);
num++;
}
getch();
return 0;
}
/*note:
%c gives character corresponding to integer num.
*/
(e)Write a program to print out all Armstrong numbers between 1 and 500. If sum of cubes of each digit of the number is equal to the number itself, then the number is called an Armstrong number. For example, 153 = ( 1 * 1 * 1 ) + ( 5 * 5 * 5 ) + ( 3 * 3 * 3 )
Show Solutions
Hide Solutions
/*b (e)Write a program to print out all Armstrong numbers
between 1 and 500. If sum of cubes of each digit of the
number is equal to the number itself, then the number is
called an Armstrong number. For example,
153 = ( 1 * 1 * 1 ) + ( 5 * 5 * 5 ) + ( 3 * 3 * 3 ) .
*/
#include <stdio.h> //header file
#include <conio.h> //header file
int main() //main function.. starting of c code
{
int i,no,r,t=0;
i=1;
while(i<=500) //loop for number 1 to 100
{
t=0; //for every i initially t=0
no=i;
while(no!=0)//loop for cube of remainder of a i.
{ //loop executive no of digit times
r=no%10;
t = t + r*r*r;//sum of cube of remainder
no=no/10;
}
if(t==i) //testing sum of cube of all digit==no. itself
printf("%d\n",i);//case of validation we print
i++;
}
getch();
return 0;
}
/*note: the while(no!=0) fails when sum of cube of remainder
is calculated.
for eg. for no 500, In 1st loop no(INT) will be 50.0 i,e 5
In 2nd loop no(INT) will be 5.0 i,e 5
In 3rd loop no(INT) will be 0.5 i,e 0
in 4th loop no(INT)==0 so loop breaks.
no of digit in 500 is 3 & no of loop execution =3.
finally we calculated sum of cube of all digit and test with
number itself for Armstrong number.
*/
(f)Write a program for a matchstick game being played between the computer and a user. Your program should ensure that the computer always wins. Rules for the game are as follows: ? There are 21 matchsticks. ? The computer asks the player to pick 1, 2, 3, or 4 matchsticks. ? After the person picks, the computer does its picking. ? Whoever is forced to pick up the last matchstick loses the game.
Show Solutions
Hide Solutions
/* b (f)Write a program for a matchstick game being played between
the computer and a user.Your program should ensure that the
computer always wins. Rules for the game are as follows:
-There are 21 matchsticks.
-The computer asks the player to pick 1, 2, 3 or 4 matchsticks.
-After the person picks, the computer does its picking.
-Whoever is forced to pick up the last matchstick loses the game.
*/
#include <stdio.h>
#include <conio.h>
int main()
{
int matchstk,com_slt,total=0,count,sel_times=0;
for(matchstk=1;matchstk<=21;matchstk++)
/*
total matchstick is 21 so loop is used which executes until
matchstick total selected will be 21...inside loop there is
count of total no. matchstick selected which will be assign
to matchstk.use of loop doesn't make exucution 21 times because
matchstk gets value as per total no of matchstick selected inside
loop.so loop continues till matchstick selected will be 21.
*/
{
if(total==20) /*for last condition when only one matchstick is
left which should be selected by player.*/
{ // use of'\' at end of line is used to join 2 line
printf("only one matchstick is left that you must select.\
\n!!!!! so you loose!!!!");
break;
}
printf("\nenter the no of matchstick to pick from 1 2 3 or 4");
scanf("%d",&count);
if(count==1||count==2||count==3||count==4)
{
//selected by player as per rule do nothing
//rule is "select only 1 or 2 or 3 or 4 matchstk...."
//else below condition executes.
}
else //for case not selected as per rule.
{
printf("please select from 1-4 only.. ");
matchstk=matchstk-1;
/*if not selected as per rule
message come.and return to loop
without increasement of matchstk no.
by matchstk-1 reduces matchstk no by 1
due to continue 1 increases and matchstk no
remains constant as matchstk-1+1.Otherwise matchstick
selected will be counted more without selection*/
continue;
}
com_slt=5-count; /*this is for computer selection.
computer select such that total will be multiply of 5.*/
printf("\nyou select %d",count);
printf("\ncomputer select %d",com_slt);
total=total+count+com_slt;
matchstk=total;
printf("\ntotal no of matchstk selected =%d ",matchstk);
/*below is used for how many time player or computer select*/
sel_times=sel_times+1;
printf("\n%d times selection completes \n\n\n",sel_times);
}
getch();
return 0;
}
/*logic of winning by computer: first player should pick.
then computer pick such that total will be 5 or multiply
of 5. due to whichtotal no of matchstick selected will be
20 in player turn. so he is forced to pick last matchstick.
in 1st selection ..if u choose 2 then computer select 3 total=5
in 2nd slection ..if u choose 4 then computer select 1 total=10
in 3rd selection ..if u choose 1 then computer select 4 total=15
in 4th selection ..if u choose 3 then computer select 2 total=20
now your turn u must select last one as no matchstick remaining..
*/
(g)Write a program to enter the numbers till the user wants and at the end it should display the count of positive, negative and zeros entered.
Show Solutions
Hide Solutions
/*b (g)Write a program to enter the numbers till the user wants
and at the end it should display the count of positive,
negative and zeros entered.
*/
#include <stdio.h>
#include <conio.h>
int main()
{
int num,to_neg=0,to_pos=0,to_zero=0;
char another='y';
while(another=='y')//takes user input till user desire
{
printf("\nenter number: ");
scanf("%d",&num);
if(num==0)
to_zero++;
else if(num>0)
to_pos++;
else if(num<0)
to_neg++;
printf("another no y/n");
fflush(stdin);//to remove enter key from buffer. see note 1:
scanf("%c",&another);
}
printf("\n");
printf("to.-ve=%d to.+ve=%d to.zero=%d",to_neg,to_pos,to_zero);
getch();
return 0;
}
/*
note 1:
when u pree enter key for entering number, enter key
will be in buffer. In next scanf enter key will be
there.so to remove enter key from buffer and
type our desire key we use fflush(stdin) function.
*/
(h)Write a program to find the octal equivalent of the entered number.
Show Solutions
Hide Solutions
/*b (h)Write a program to find the octal equivalent of the
entered number.
*/
#include <stdio.h>
#include <conio.h>
int main()
{
int rem,num,total=0,i=1;
//note 1 for about octal number.
printf("Enter any number for octal equivalent: ");
scanf("%d",&num);
while(num!=0) //loop exists no of digit times.
{
rem=num%8; //remainder for octal number.
total=total+rem*i;//finds octal number.
//finally it find octal number.
i=i*10; //in 2nd loop rem should be multiplied by
//10 to find number. and 100 in 3rd loop...
num=num/8;
}
printf("Octal equivalent is %d",total);
getch();
return 0;
}
/*
note1: octal number are 0,1,2,3,4,5,6,7.
so we convert entered number into octal number by
dividing by 8 and we collect remainder and reversely
we find out total number which is octal equivalent.
for example:
you entered number 123 then.
in 1st loop rem=3, total=3.
in 2nd loop rem=7, total=3+7*10=73;
in 3rd loop rem=1, total=73(3+7*10)+1*100=173(octal)
in 4th loop loop breaks.....
*/
(i)Write a program to find the range of a set of numbers. Range is the difference between the smallest and biggest number in the list.
Show Solutions
Hide Solutions
/*b (i)Write a program to find the range of a set of numbers.
Range is the difference between the smallest and biggest
number in the list.
*/
#include <stdio.h>
#include <conio.h>
int main()
{
int i,a=2,b=3,c=20,d=5,e=9,great,least,num;
i=1;
while(i<=5)
{
if(i==1)
num=a;
else if(i==2)
num=b;
else if(i==3)
num=c;
else if(i==4)
num=d;
else if(i==5)
num=e;
//for greatest number
if(num>=a&&num>=b&&num>=c&&num>=d&&num>=e) //see note 1
{
great=num;
}
//for least number
if(num<=a&&num<=b&&num<=c&&num<=d&&num<=e)
{
least=num;
}
i++;
}
printf("least number=%d\n greatest number=%d",least,great);
printf("\n Range=%d",great-least);
getch();
return 0;
}
/*
note 1:
in 1st loop num=a;
we check a or num is greatest or not.
if a is greatest then we assign to great variable
if a is least then we assign to least variable
in 2nd loop num=b;
and so on....
there r five number so we use loop 5 times for
finding great and least number.
*/
------------------------------------------------
for, break, continue, do-while
[E] Attempt the following:
(a)Write a program to print all prime numbers from 1 to 300. (Hint: Use nested loops, break and continue)
Show Solutions
Hide Solutions
/*e (a)write a program to print prime number from 1 to 300*/
#include <stdio.h>
#include <conio.h>
int main()
{
int no,i,rem,j,flag=0;
for(no=2;no<=300;no++)
{
flag=0;/*read at last..flag should initialize to 0.because
once remainder=0, flag=1 will became, then if condition
(flag!=1) will never satisfied although prime no is that
number .*/
j=no/2; /*j takes 50% of the no.because if no. isn't
divided by upto 50% of it's no. then no. willn't be
divided by any further no. eg 13. if 13 isn't
divided by upto(13/2=6 AS INTEGER) then 13 will n't be
divided by any further no.so, why to divide to further
...u can divide also.*/
for(i=j;i>=2;i--)
{
rem=no%i;/*no is divided upto 50%of no. to find remainder
is 0 or not if remainder =0 then that no. will
not be prime. so flag=1 will be. which prevents
the print of no as the condition of (flag!=1)will
be dissatisfied once remainder is 0 it is not prime
so break is used.no necessary to divide further.
*/
if(rem==0)
{
flag=1;
break;
}
}
if(flag!=1)
printf("%d\n",no);
}
getch();
return 0;
}
/*
logic. if no is divisible by any no then that no is n't prime no.
So, that number shouldn't be printed. so if no is divided by any
no. then if condition (if(flag!=1)) fails as flag=1 will be if
number is divided by any no.
*/
(b)Write a program to fill the entire screen with a smiling face. The smiling face has an ASCII value 1.
Show Solutions
Hide Solutions
/*e b) Write a program to fill the entire screen with a smiling face.
The smiling face has an ASCII value 1. */
#include <stdio.h>
#include <conio.h>
int main()
{
int smiling=1,i;
for(i=1;i<=2000;i++) //2000 times smiling face gets printed.
{
printf("%c",smiling); /*%c prints character corresponding to
ascii value 1.*/
}
getch();
return 0;
}
(c)Write a program to add first seven terms of the following series using a for loop: 1/1! + 2/2! + 3/3! + .........
Show Solutions
Hide Solutions
/*e (c)Write a program to add first seven terms of the following
series using a for loop: 1/1! + 2/2! + 3/3! + ......... */
#include <stdio.h>
#include <conio.h>
int main()
{
int factorial,fact,i;
float sum=0.0;
i=0;
while(i<=7)
{
fact=i;//for finding factorial see note: 1.
factorial=1;
while(fact>=1)
{
factorial=factorial*fact;
fact--;
}
sum=sum+(float)i/factorial;//int/int gives integer value so
// we converted into float value.
i++;
}
printf("sum upto 7th term is %f",sum);
getch();
return 0;
}
/*
note: 1
for understanding factorial see problem (B) b
*/
(d)Write a program to generate all combinations of 1, 2 and 3 using for loop.
Show Solutions
Hide Solutions
/*(d)Write a program to generate all combinations of
1, 2 and 3 using for loop. */
#include <stdio.h>
#include <conio.h>
int main()
{
int i,j,k,l,count=0;
for(i=1;i<=4;i++)
{
for(j=1;j<=4;j++)
{
for(k=1;k<=4;k++)
{
for(l=1;l<=4;l++)
{
if(i!=j&&j!=k&&i!=k&&i!=l&&j!=l&&k!=l)
{
printf("\n%d %d %d %d",i,j,k,l);
count++;
}
}
}
}
}
printf("\ncount = %d",count);
getch();
return 0;
}
/*
note: 1
for understanding factorial see problem (B) b
*/
(e) According to a study, the approximate level of intelligence of a person can be calculated using the following formula: i = 2 + ( y + 0.5 x ) Write a program, which will produce a table of values of i, y and x, where y varies from 1 to 6, and, for each value of y, x varies from 5.5 to 12.5 in steps of 0.5.
Show Solutions
Hide Solutions
/*e (e) According to a study, the approximate level of
intelligence of a person can be calculated using the
following formula: i = 2 + ( y + 0.5 x ) Write a program,
which will produce a table of values of i, y and x, where
y varies from 1 to 6, and, for each value of y, x varies
from 5.5 to 12.5 in steps of 0.5. */
#include <stdio.h>
#include <conio.h>
int main()
{
int y;
float i,x;
for(y=1;y<=6;y++)
{
for(x=5.5;x<=12.5;)//x varies from5.5 to 12.5 so
{
i=2+(y+0.5*x); //formula
printf("i=%f y=%d x=%f\n",i,y,x);//displaying
x=x+0.5; //x increase in step of 0.5 by question
}
}
getch();
return 0;
}
(f)Write a program to produce the following output:
A B C D E F G F E D C B A
A B C D E F F E D C B A
A B C D E E D C B A
A B C D D C B A
A B C C B A
A B B A
A A
Show Solutions
Hide Solutions
/*e (f)Write a program to produce the following output:
A B C D E F G F E D C B A
A B C D E F F E D C B A
A B C D E E D C B A
A B C D D C B A
A B C C B A
A B B A
A A
*/
#include <stdio.h>
#include <conio.h>
int main()
{
int i,j,k,count1,count2=0;//count1 and count 2 for spaces
for(i=104;i>=97;i--)
{
for(j=97;j<=104;j++)
{
if(i>=j)/*In 1st loop print character from 97-104
In 2nd loop print character from 97-103
{i(103)>=j(104) fails }
and so on.
*/
printf("%c ",j);//prints characters
}
for(count1=0;count1<count2;count1++)
/*for managing space
in 1st line we don't need space
so count1(0<count2(0) fails and
spaces don't get printed
*/
{
if(i==103) // in 2nd line we need 1 space for each loop.
// so i==103 condition of 2nd line
printf(" ");
if(i!=103)//2 spaces are needed. and spaces
// increases by 2 for each line after 2nd line
printf(" ");
}
for(k=104;k>=97;k--)
{
if(k<=i&&k!=104)
printf("%c ",k);
}
if(i==103) //for managing spaces
count2=count2+1;
else //for managing spaces
count2=count2+2;
printf("\n"); //new line for each i.
}
getch();
return 0;
}
(g)Write a program to fill the entire screen with diamond and heart alternatively. The ASCII value for heart is 3 and that of diamond is 4.
Show Solutions
Hide Solutions
/*e (g)Write a program to fill the entire screen with diamond
and heart alternatively. The ASCII value for heart is 3 and
that of diamond is 4.
*/
#include <stdio.h>
#include <conio.h>
int main()
{
int i;
for(i=0;i<=600;i++)//u can use any limit of i.
{
printf(" %c %c",3,4);
}
getch();
return 0;
}
/*
*/
(h)Write a program to print the multiplication table of the number entered by the user. The table should get displayed in the following form. 29 * 1 = 29 29 * 2 =58 ....
Show Solutions
Hide Solutions
/*(h)Write a program to print the multiplication table of the
number entered by the user. The table should get displayed in
the following form. 29 * 1 = 29 29 * 2 =58 ....
*/
#include <stdio.h>
#include <conio.h>
int main()
{
int no,i,multiply;
printf("enter number for multiplication table:");
scanf("%d",&no);
for(i=1;i<=9;i++)//we can make upto any number.
{
multiply=no*i;
printf("\n%d * %d = %d",no,i,multiply);
}
getch();
return 0;
}
(i) Write a program to produce the following output:
1
2 3
4 5 6
7 8 9 10
Show Solutions
Hide Solutions
/*e (i) Write a program to produce the following output:
1
2 3
4 5 6
7 8 9 10
*/
#include <stdio.h>
#include <conio.h>
int main()
{
int i,j,k,cnt=1,icpy,icpy1,space,count=10;
for(i=1;i<=5;i++)
{
for(space=count;space>=1;space--)
/*this is for managing spaces. atfirst there will be 10 spaces
which gradually decreases by 1.In output 2 is at 1
space left of 1.so, we decrease by 1*/
{
printf(" ");
}
count=count-1;
icpy=i;/*we have to increase i for above output
so we assign for increasing because i
is in the outer loop we can't increase i below*/
for(j=1;j<=i;j++)/*loop continue i times.
in 1st loop we have to print only 1 no.
in 2nd loop we have to print 2 no.
so for this, this loop required.
which coutinue i times
*/
{
printf("%d ",icpy);
icpy++;/*increase and printed till i times as this loop valids
*/
}
printf("\n");
}
getch();
return 0;
}
(j)Write a program to produce the following output:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
Show Solutions
Hide Solutions
/*e (j) Write a program to produce the following output:
1
2 3
4 5 6
7 8 9 10
(it is a pascal triangle)
*/
#include <stdio.h>
#include <conio.h>
int main()
{
//note1: read at last comment for understanding
int a,b,c,line,space,i,j,n,r,ncr,fact_r,fact_n,factn_r;
printf("please enter the number of lines of the pascal triangle: \n");
scanf("%d",&line); //take no. of line we want
printf("\n");
for(i=0;i<=line;i++)//outer loop for the value of n
{
//n,r, n-r have special meaning in permuntation and combination
fact_n=1;// initializing of factorial of n.
fact_r=1;// initializing of factorial of r.
factn_r=1;// initializing of factorial of n-r.
for(space=line;space>=i;space--) //for managing spaces.
{
printf(" ");
}
for(j=0;j<=i;j++) //loop for row element.
//element in row is equal to i.
{
n=i; /*for ncr or c(n,r) we need n and r which are different for
each i and j so we assign here.*/
r=j;
fact_n=1;// below we calculate fact_n.so to avoid below fact_n value.
fact_r=1;// below we calculate fact_n.so to avoid below fact_n value.
factn_r=1;// below we calculate fact_n.so to avoid below fact_n value.
//for calculating of factorial of fact_n,fact_r,factn_r
//factorial of fact_n
if(n==0) //we know factorial of 0 =1
fact_n=1;
else
{
for(a=n;a>=1;a--) //factorial calculation of n is n!=0
fact_n=fact_n*a;
}
//factorial of fact_r.
if(r==0)
fact_r=1;
else
{
for(b=r;b>=1;b--) //factorial calculation of r if r!=0
fact_r=fact_r*b;
}
//factorial of factn_r.
if((n-r)==0)
factn_r=1;
else
{
for(c=(n-r);c>=1;c--) //factorial calculation of n-r if (n-r)!=0
factn_r=factn_r*c;
}
ncr=fact_n/(fact_r*factn_r);//c(n,r) calculation.this is formula for c(n,r)
//ncr and c(n,r) are same.
printf("%2d",ncr); //2 space for each ncr
}
printf("\n"); //new line after each row.
}
getch();
return 0;
}
/*
note1:
pascal triangle is generated by using formula.
c(n,r)=n!/((n-r)!*r!)
1st row: c(0,0) will be.
2nd row: c(1,0),c(1,1) will be
3rd row: c(2,0),c(2,1),c(2,2) will be
.................
nth row: c(n,0)....c(n,n);
so we use two loop. for each n. there should be
r value ranging from 0 to n.
*/
(k) A machine is purchased which will produce earning of Rs. 1000 per year while it lasts. The machine costs Rs. 6000 and will have a salvage of Rs. 2000 when it is condemned. If 12 percent per annum can be earned on alternate investments what would be the minimum life of the machine to make it a more attractive investment compared to alternative investment?
Show Solutions
Hide Solutions
/*e (k)A machine is purchased which will produce earning of
Rs. 1000 per year while it lasts. The machine costs Rs. 6000
and will have a salvage of Rs. 2000 when it is condemned. If
12 percent per annum can be earned on alternate investments
what would be the minimum life of the machine to make it a
more attractive investment compared to alternative investment?
*/
#include <stdio.h>
#include <conio.h>
int main()
{
int year=0,inv,altn;
while(altn>inv)
{
year++;
altn=120*year; // 12%of 1000 = 120
inv=(1000*year)-4000;
}
printf("The minimum year is %d",year);
getch();
return 0;
}
(l) When interest compounds q times per year at an annual rate of r % for n years, the principle p compounds to an amount a as per the following formula
a = p ( 1 + r / q ) ^ nq
Write a program to read 10 sets of p, r, n & q and calculate the corresponding as.
Show Solutions
Hide Solutions
/*e (l) When interest compounds q times per year at an annual rate
of r % for n years, the principle p compounds to an amount a as
per the following formula a = p ( 1 + r / q ) ^ nq
Write a program to read 10 sets of p, r, n & q and calculate
the corresponding as.
*/
#include <stdio.h>
#include <conio.h>
int main()
{
int p,r,n,q,a,i,j,product;
float amt=1;
for(i=0;i<10;i++)
{
amt=1;
printf("enter the values of p,r,n and q: ");
scanf("%d%d%d%d",&p,&r,&n,&q);
product=n*q;
for(j=1;j<=product;j++)/*for calculating power value.
function also can be use but this is loop
chapter so we try to make use of loop*/
{
amt = amt * ( 1 +(float) r / q );/* r/q gives integer.(float) changes
to float value.*/
}
amt= p *amt;
printf("%f",amt);
}
getch();
return 0;
}
(m)The natural logarithm can be approximated by the following series.
(x-1)/x + 1/2((x-1)/2)^2 + 1/2((x-1)/2)^3 + ...
If x is input through the keyboard, write a program to calculate the sum of first seven terms of this series.
Show Solutions
Hide Solutions
/*e (m)The natural alogarithm can be approximated by the
following series.(x-1)/x + 1/2((x-1)/2)^2 + 1/2((x-1)/2)^3 + ...
If x is input through the keyboard, write a program to
calculate the sum of first seven terms of this series.
*/
#include <stdio.h>
#include <conio.h>
int main()
{
int x,i,j;
float sum=0,power=1;
printf("enter x for sum upto 7th term: ");
scanf("%d",&x);
for(i=1;i<=6;i++)//loop for adding upto last 6 terms
{
power=1;
for(j=0;j<=i;j++) //for power.term multiplying as power term
{
power = power * ((x-1.0)/2.0);
}
//printf(" %f ",power);
sum = (1.0/2) * power + sum;/*from 2-7 term.1/2 is common so we
multiply at last at once.*/
}
sum=sum + (float)(x-1.0)/x;/* 1st term + sum of last 6 term
(calculated from above loop) */
printf("%f",sum);
getch();
return 0;
}
Comments
Post a Comment