Brijmohan lal Sahu - Facebook
Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu
Showing posts with label using for loop. Show all posts
Showing posts with label using for loop. Show all posts

Sunday, December 7, 2014

Program to calculate sum of this special series using for loop in C.


sum of special series

sum of special series formula



Code Box
----------------------------------------------

/*Program to calculate sum of series

 1/(1*2)+1/(2*3)+1/(3*4)+.......+1/((n-1)*n)

 where n is the value entered by user.

 =>Explicit type conversion
 =>Nested loop
*/

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int i,n;
float sum,mul;
clrscr();
sum=0;


printf("\nEnter limit number : ");
scanf("%d",&n);


printf("\nSeries=");

printf("\n---------------------\n");

for(i=1;i<n;i++)
{

mul=i*(i+1);
printf(" +1/(%d*%d) ",i,i+1);


sum=sum+(float)1/mul;  //Explicit type conversion
}

printf("\n---------------------\n");

printf("\nSum of series= %.3f",sum);

getch();
}

Friday, December 5, 2014

Program to print factorial of any input number using for loop in C.



factorial


Code Box
----------------------------------------------------------

/*Program to print factorial of any input number*/

#include<stdio.h>
#include<conio.h>
void main()
{
int num,i;

float fact;

/* If we take fact as integer, it can not be able
to store factorial of value greater than 9 */

fact=1.0;

clrscr();

printf("\nEnter any number:");
scanf("%d",&num);

clrscr();  //for clear screen

printf("\n-------------------------------");
printf("\nNumber entered is: %d",num);
printf("\n-------------------------------");
printf("\nFactorial of %d is:",num);
printf("\n-----------------------------\n");


if(num==0)
{
printf("\n1"); //factorial of 0 is 1
}
else
{
for(i=1;i<=num;i++)
{
fact=fact*num;
num--;
}
}
printf("\n%.0f",fact);

printf("\n------------------------------");

getch();
}