c program to find the factorial of a number without recursion


Description


This program finds the factorial of a given number. The program does not use the concept of recursion. The value for n is read into the variable n. The program finds the factorial by multiplying each numbers form n to 1, each time decrementing the value for n.


Example


Factorial of 5

5*4*3*2*1=120

 Program


#include<stdio.h>

#include<conio.h>

void main()

{

int n,i,sum=1,f;

clrscr();

printf("Enter the number");

scanf("%d",&n);

f=n;

i=n;

while(i>=1)

{

sum=sum*f;

i=i-1;

f=f-1;

}

printf("Factorial is %d",sum);

getch();

}


C program to find the factorial using recursion

c program to print the triangle star(*) pattern

 Description


This program prints the pattern given below. The number of lines in the pattern is read into the variable j. Then the program uses while loop to print the pattern line by line.

//* * * * *
//* * * *
//* * *
//* *
//*

Program


//C program for pattern printing



//C program for pattern printing using asterisk.



//C program for pattern printing using while loop.



#include<stdio.h>

#include<conio.h>

void main()

{

clrscr();

int i=1,j;

printf("Enter the value for n");

scanf("%d",&j);

l:

while(i<=j)

{

printf("* ");

i=i+1;

}

if(j<0)

{

printf("\n");

j=j-1;

i=1;

goto l;

}

getch();

}


c program to find the biggest among 3 numbers

Description


This program finds the biggest among three numbers. The three numbers are read as input and stored in the variables a, b and c. Then the biggest among these values is find using  if elseif statement.

Program


//Program to find the biggest among three numbers

//C program to demonstrate if elseif statement. 



#include<stdio.h>

void main()

{

int a,b,c;

printf("Enter the numbers");

scanf("%d%d%d",&a,&b,&c);

if(a>b)

{

if(a>c)

printf("%d is greater",a);

}

else

{

printf("%d is greater",c);

}}

elseif(b>c)

{

printf("%d is greater",b);

}

else

{

printf("%d is greater",c);

}

getch();

}

c program to find the average and range of n numbers

Description


This program finds the average and range of n numbers. The value for n is read into the variable n. The program uses while loop to repeatedly read n values. The variables l and s is used to store the smallest and largest values, that can be used to find the range. 



Program



#include<stdio.h>

#include<conio.h>

void main()

{

int n,a,l=0,flag=1,sum=0,range,i=1,s,avg;

clrscr();

printf("enter the value of n: ");

scanf("%d",&n);

printf("enter the numbers");

while(i<=n)

{

scanf("%d",&a);

if(flag==1)

{

s=a;

flag=0;

}

if(a>l)

{

l=a;

}

if(a<s)

{

s=a;

}

sum=sum+a;

i++;

}

avg=sum/n;

range=l-s;

printf("Average=%d",avg);

printf("Range=%d",range);

getch();

}

c program to find the average of n numbers using goto

Description


This program is used to find the average of n numbers using goto statement. The value for n is read into the variable n and the value of i is initially set to 1. Then the numbers are read one by one each time adding it to the variable sum and incrementing the value of i. This process continues until the value of i reaches n. Then the average is calculated by using the statement avg=sum/n;


Program


#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int a;
float n,sum=0,avg,i=1;
printf("enter the value of n");
scanf("%f",&n);
printf("enter the numbers");
l:
scanf("%d",&a);
sum=sum+a;
i=i+1;
if(i<=n)
{
goto l;
}
else
{
avg=sum/n;
printf("average is %f",avg);
}
getch();
}

c program to print febonacci series

Description

 This program prints the febonacci series upto the given limit. A febonacci series is generated by successively adding the last two numbers in a series. This program checks the condition that the value for n is greater than 1. Then the program uses while loop to print the febonacci series.

Example

Febonacci series with linit 7, starting with 0
0, 1, 1, 2, 3, 5, 8

Program

#include<stdio.h>

#include<conio.h>

void main()

{

clrscr();

int f1=0,f2=1,f3,i=1,n;

printf("Enter the value of n");

l:

scanf("%d",&n);

if(n<2)

{

printf("n should be greater than 1\n");

goto l;

}

else

{

printf("\n %d\n%d",f1,f2);

while((i<=(n-2))

{

f3=f1+f2;

printf("\n %d",f3);

f1=f2;

f2=f3;

i++;

}}

getch();

}