Tuesday, 4 October 2011

Program to find roots of quadratic equation


#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int a,c,b;
float d,r1,r2;
clrscr();

printf("\n Enter values of coefficient of x^2, x , and constant : \n");
scanf ( "%d%d%d" ,&a,&b,&c);

d = sqrt((b*b) - (4*a*c));

r1= (-b+d)/(2*a);
r2= (-b-d)/(2*a);

printf("\n Roots are : %f and %f ",  r1, r2);
getch();
}

Program to print fibonacci series of 1 to 20 nos.


#include<stdio.h>
#include<conio.h>
void main()
{
int fib=0, a=1,b=0, i ;
clrscr();

printf("\n Fibonacii sereis of 1st 20 nos. is :");

for( i =0 ; i <20; i++)
{
printf("\n%d . %5d",i+1,fib);
fib = a + b;
a= b;
b=fib;
}
getch();
}

Program to print factorial of a number by recursion


#include<stdio.h>
#include<conio.h>
void main()
{
int n,fact;
clrscr();
printf("\nEnter a number: ");
scanf("%d",&n);
fact = rec(n);
printf("\n Factorial is : %d ",fact);
getch();
}
rec(int n)
{
int f;
if( n==1)
return 1;
else
return f= n * rec(n-1);

}

Program to print GCD of two nos. by method 2


#include<stdio.h>
#include<conio.h>
void main()
{
int n1,n2;
clrscr();
printf("\nEnter two nos. : \n ");
scanf("%d%d",&n1,&n2);
while(n1!=n2)
{
if(n1>=n2-1)
n1=n1-n2;
else
n2=n2-n1;
}
printf("\n GDCD of is : %d ",n1);
getch();
}

Program to print GCD of two nos.


#include<stdio.h>
#include<conio.h>
void main()
{
int x,y, i,m ;
clrscr();
printf("enter two nos.: \n");
scanf("%d%d",&x,&y);
if ( x>y )
m= y ;
else
m=x;
for(i=m;i>=1;i--)
if(x%i==0 && y%i==0)
{
printf("\n GCD is :%d ",i);
break;
}
getch();
}

Program to print given number in words


#include<stdio.h>
#include<conio.h>
void main()
{
int number,reverse=0;
clrscr();
printf("\n Enter a number :");
scanf("%d",&number);
for(;number>0;number/=10)
{
reverse = reverse*10+ number%10;
}
for(;reverse>0;reverse/=10)
{
switch(reverse%10)
{
case 0:printf("Zero ");break;
case 1:printf("One ");break;
case 2:printf("Two ");break;
case 3:printf("Three ");break;
case 4:printf("Four ");break;
case 5:printf("Five ");break;
case 6:printf("Six ");break;
case 7:printf("Seven ");break;
case 8:printf("Eight ");break;
case 9:printf("Nine ");break;
}
}
getch();
}

Program to print reverse number of given number


#include<stdio.h>
#include<conio.h>
void main()
{
int num , rev = 0 , x;
clrscr();

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

while ( num > 0)
{
x= num %10;
rev = rev* 10 + x;
num= num /10;
}

printf("\n Reverse of Given no. is : %d ",rev);
getch();
}


Program 4 from term work FE


#include<stdio.h>
#include<conio.h>
#include<ctype.h>
void main()
{
char c[100];
int i,vowel=0, a=0;
clrscr();

printf("\n Ener a string : ");
gets(c);

for(i=0;c[i]!='\0';i++)
{
switch(tolower(c[i]))
{
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
vowel ++ ;
}
switch(tolower(c[i]))
{
case 'a' :  a++;
}
}

printf("\n\n You have entered : %s", c);
printf("\n\n number of charcters in given string : %d",i);
printf("\n\n Number of vowels in given string is : %d",vowel);
printf("\n\n Number of accurance of \'a'\ is : %d",a);
getch();
}

Program to check type of triangle.......


#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
printf("\n Enter lengths of 3 sides of triangle :   " );
scanf("%d%d%d",&a,&b,&c);


if( a==b && b==c)
{
printf("\n This is an equilateral triangle");
}
else if((a*a)== (b*b) + (c*c) ||(b*b) == (a*a) + (c*c) || (c*c)== (a*a) +(b*b))
{
printf("\n This is a right angled triangle");
}
else if(a==b || b==c || a==c)
{
printf("\n This triangle is isosceles triangle");
}
else
{
printf("\n This triangle is neither equilateral nor isosceles and not rigt angled triangle");
}
getch();
}

Program to find numbers divisible by 4 between 1 to 100 and their sum...


#include<stdio.h>
#include<conio.h>
void main()
{
int i, sum = 0 ;
clrscr();
for ( i =0 ; i <= 100 ; i++)
{
if(i%4 == 0)
{
printf("\n %d",i);
sum+=i;
}
}
printf("\n Sum = %d",sum);
getch();
}

C program to find given year is leap year or not.......


#include<stdio.h>
#include<conio.h>

void main()
{
int yr;
clrscr();
printf("\n Enter a year: ");
scanf("%d",&yr);
if ( yr %100 == 0)
{
if( yr %400 == 0)
{
printf("\n Leap Year ");
}
else
{
printf("\n Not Leap Year ");
}
}
else
{
if (yr %4 ==0)
{
  printf("\n Leap Year ");
}
else
{
printf("\n Not Leap Year ");
}
}
getch();
}