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

Sunday, February 22, 2009

Write a C program to generate the Fibonocci series using Recursion.

#include(stdio.h) // place this '<' & '>' instead of '(' & ')' before stdio.h
#include(conio.h)
int fibno(int); // function declaration.
void main( )
{
int ct, no, disp;
clrscr( );
printf("Enter the no. of terms:");
scanf("%d", &no);
printf("\n The Fibonocci series:\n");
for( ct=0; ct<=n-1; ct++) {
disp= fibno(ct); //calling function.
printf("%5d", disp);
}
getch( );
}

int fibno( int n)
{
int x, y;
if(n==0)
return 0;
else if(n==1)
return 1;
else
{
x= fibno( n-1);
y= fibno( n-2);
return (x+y);
}
}

Here the 'main' function variables
'ct' is to count the no. of values displayed in output.
'no' is for total no.of terms.
'disp' is for display the fibonocci numbers.

Here the loop 'ct<= n-1' displays the values when 'ct' equals to 'n-1'.
Suppose, Let us assume n=10 (ie., no. of terms )
Then the loop terminates, when ct==9.

Output:- Enter the no. of terms: 10

The Fibonocci series:
0 1 1 2 3 5 8 13 21 34

Note:- If u have any doubt regarding this program or logic, please feel free to contact me.

Friday, February 20, 2009

Write a C program to determine the given integer is Palindrome or not.

#include(stdio.h) // place this '<' & '>' instead of '(' & ')' before stdio.h
#include(conio.h)
void main( )
{
int no, rem, rev=0, temp;
clrscr( );
printf("Enter the required integer:");
scanf("%d", &no);
temp=no;
while(temp>0)
{
rem=temp%10;
rev=(10*rev)+rem;
temp=temp/10;
}
if(no==rev)
printf("\n%d is Palindrome number", no);
else
printf("\n%d is not a palindrome string", no);
getch();
}

Output:-
Enter the required number: 141
141 is palindrome number.

Note:- If u have any doubt regarding this program or logic, please feel free to contact me.

Write a C program to calculate the sum of factors of a number.

#include(stdio.h) // place this '<' & '>' instead of '(' & ')' before stdio.h
#include(conio.h)
void main( )
{
int no, sum=0, x;
clrscr( );
printf("Enter the required number :");
scanf("%d", &no);
for( x=1; x<=no; x++) {
if(no%x==0) sum=sum+x;
}
printf("\nSum of the factors of %d is: %d", no, sum);
getch( );
}

Output:-
Enter the required number: 10

Sum of the factors of 10 is: 18.
ie., 1+2+5+10 =18

Note:- If u have any doubt regarding this program or logic, please feel free to contact me.