Subscribe

RSS Feed (xml)

Wednesday, July 29, 2009

Write a C program to find the roots of a Quadratic equation

#include(stdio.h) //note use '<' & '>' in place of '(' & ')'
#include(conio.h)
#include(math.h)
int main( )
{
int a , b, c, d;
float x1, x2;
ab:
printf("Enter the value of a, b, c:");
scanf("%d%d%d", &a, &b, &c);
if(a==0)
{
printf("\nIt is a Linear Equation");
prinft("\nRe-Enter the data again.");
goto ab;
}
d=(b*b)-(4*a*c);
if(d==0)
{
printf("\nRoots are real and equal");
x1= x2=(-b)/(2*a);
printf("\nRoots are: %f \t %f ", x1, x2);
}
else
{
if(d<0)
printf("\nRoots are imaginary");
else
{
x1=((-b)+sqrt(d))/(2*a);
x2=((-b)-sqrt(d))/(2*a);
printf("\nRoots are real");
printf("\nRoots are : %f \t %f", x1, x2);
}
}
getch( );
}
Output:-
Enter the values of a, b,c : 5
2
4
Roots are imaginary.

Write a C program to Convert any Decimal number into Binary coded decimal

#include(stdio.h)
#include(conio.h)
int main( )
{
int a[20], n, ct=0, rem , i=0;
clrscr( );
printf("Enter the required decimal number:");
scanf("%d", &n);
printf("\nBinary Number is :");
while(n>=1)
{
rem= n%2;
a[i]= rem;
i++; // to increase the array location
ct++; // to count the no. of bits storing in an array
n=n/2;
}
for(i=ct ; i>=1; i-- )
printf("%3d", a[i] );
getch( );
return 0;
}

Output:
Enter the required decimal number: 5
Binary Number is : 1 0 1

Write a C program to Calculate the sum of the series sum=1+1/x+1/x2+1/x3+...........+1/xn

#include(stdio.h)
#include(conio.h)
#include(math.h)
int main( )
{
int i, x, n;
float sum, p;
clrscr( );
printf("Enter the base value 'x':");
scanf("%d", &x);
printf("\nEnter the power value 'n':");
scanf(" %d", &n);
sum=1;
for( i=1; i<=n ; i++)
{
p=pow(x, i);
sum=sum+(1/p);
}
printf("\nSum of the series: %f", sum);
getch( );
return 0;
}

Note:- if u have any douts regarding this program, please feel free to contact.

Tuesday, July 21, 2009

Write a c program to obtain the Sum of the first and last digit of the number, if a four digit number is inputted through the keyboard

/* Program to obtain the Sum of first and last digit of the number without using loops*/
#include(stdio.h)
#include(conio.h)
int main( )
{
int no, sum, first, last;
clrscr( );
printf("Enter the required four digit number");
scanf("%d", &no);
first = no/1000;
last= no%10;
sum= first + last;
printf("\n Sum of first and last digit : %d", sum);
getch( );
return 0;
}

Monday, July 20, 2009

Write a C program to convert Fahrenheit degrees into Centigrade and Vice-Versa

#include(stdio.h)
#include(conio.h)
int main( )
{
float f, c;
clrscr( );
printf("Enter the fahrenheit degrees:");
scanf("%f", &f);
c= (f-32)/1.8 ;
printf("\nAfter Fahrenheit degrees, Centigrade Degrees = %f", c);
f= 1.8*c + 32.0;
printf("\nAfter Centigrade degrees, Fahrenheit degrees = %f", f);
getch( );
return 0;
}

Tuesday, July 14, 2009

Write a C program to check whether the given number is positive, negative or zero

#include(stdio.h) // place '><' in place of '(' & ')'
#include(conio.h)
int main( )
{
int no;
clrscr( );
printf("Enter the required number to check:");
scanf("%d", &no);
if(no>0)
printf("\n %d is positive number",no);
else if(no<0)
printf("\n%d is negative number",no);
else
printf("\nEntered Number is ZERO");
getch( );
return 0;
}

Note:- If u have any doubts regarding this program or my website programs, just contact through my email-id.

Friday, June 19, 2009

Write a C program to find the greatest number between four numbers using if-else ladder

#include(stdio.h)
#include(conio.h)
int main( )
{
int a, b, c, d;
clrscr( );
printf("Enter the four different numbers:")l
scanf(" %d %d %d %d", &a, &b, &c, &d);
if(a>b && a>c && a>d)
printf("\n%d is greatest", a);
else if(b>a && b>c && b>d)
printf("\n%d is greatest", b);
else if(c>a && c>b && c>d)
printf("\n%d is greatest", c);
else
printf("\n %d is greatest", d);
getch( );
}
Note:- if u have any doubts regarding this program, feel free to contact my email id

Tuesday, June 16, 2009

Write a 'C' program to perform the selected arithmetic operation by taking two integer values using Switch Statement

#include(stdio.h)
#include(conio.h)
int main( )
{
int x,y;
char ch;
clrscr( );
printf("Enter the two integer values:);
scanf("%d%d",&x,&y);
printf("\nEnter the required arithmetic operator(+,-,*,/):");
fflush(stdin);
scanf("%c",&ch);
switch(ch)
{
case '+' : printf("\nAddition: %d", x+y); break;
case '-' : printf("\nSubstraction : %d", x-y); break;
case '*' : printf("\nMultiplication ; %d', x*y); break;
case '/': printf("\nDivision: %d", x/y); break;
default: printf("\nInvalid Arithmetic operator.");
}
getch( );
return 0;
}
Output:- Enter the two integer values: 30
20
Enter the required arithmetic operator(+,-,*,/): +
Addition: 50

Thursday, June 11, 2009

Write a Program to print a Histogram showing the frequencies of different word length

"stdio.h"
"ctype.h"
"string.h" // used header files
int display(int );
void main( )
{
char st[200];
int i, count;
clrscr( );
printf("Enter the required sentence( Press Ctrl + Z to stop the sentence:");
for(i=0; (st[i]=getchar())!=EOF; i++);
printf("\n\n<---------------------Histogram fequencies--------------->");
printf("\n\n 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 ..................");
for(i=0, count=0; ; i++)
{
if(st[i]==EOF)
break;
if(isspace(st[i])
count=display(count);
else
count++;
}
count= display(count);
printf("\n<----------------------------End---------------------->");
getch( );
}
int display(int ct)
{
int i;
printf("\n");
for(i=0; i<(ct*2); i++) // (ct*2) is applied becoz 1 alpha= 2 ascii syb
{
putchar(177);
}
printf("(%d)",ct);
return 0;
}

Friday, May 22, 2009

Write a C program to find the factors of a given integer.

#include(stdio.h>
#include(conio.h)
void main( )
{
int no,x;
clrscr( );
printf("Enter the required number:");
scanf("%d",&no);
printf("\nThe factors are:");
for(x=1; x<=no; x++)
{
if(no%x==0)
printf("\n%d",x);
}
getch( );
}

Output:-
Enter the required number: 27
The factors are:
1
3
9
27