#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
Friday, May 22, 2009
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.
#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.
#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.
#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.
Monday, February 16, 2009
Write a C program to detect whether the given integer is Armstrong number or not.
Armstrong number:- Sum of the cubes of a individual number is known as Armstrong number.
Example:- Suppose take 153,
153= 1 + 125 + 27
ie., 153 = 1 cube + 5 cube + 3 cube
153 = 153, Then we say '153' is Armstrong number
#include(stdio.h) // place this '<' & '>' instead of '(' & ')' before stdio.h
#include(conio.h)
void main( )
{
int rem, sum=0, n, temp;
clrscr( );
printf("Enter the required number:");
scanf("%d",&n);
temp= n;
while(n>0)
{
rem=n%10;
sum = sum + (rem * rem * rem);
n=n/10;
}
if(temp == sum)
printf("\nArmstrong Number.");
else
printf("\nNot a Armstrong number.");
getch( );
}
Output:-
Enter the required number: 153
Armstrong Number.
Note:- If u have any doubt regarding this program or logic, please feel free to contact me.
Example:- Suppose take 153,
153= 1 + 125 + 27
ie., 153 = 1 cube + 5 cube + 3 cube
153 = 153, Then we say '153' is Armstrong number
#include(stdio.h) // place this '<' & '>' instead of '(' & ')' before stdio.h
#include(conio.h)
void main( )
{
int rem, sum=0, n, temp;
clrscr( );
printf("Enter the required number:");
scanf("%d",&n);
temp= n;
while(n>0)
{
rem=n%10;
sum = sum + (rem * rem * rem);
n=n/10;
}
if(temp == sum)
printf("\nArmstrong Number.");
else
printf("\nNot a Armstrong number.");
getch( );
}
Output:-
Enter the required number: 153
Armstrong Number.
Note:- If u have any doubt regarding this program or logic, please feel free to contact me.
Write a C program to check whether the given integer is Magic number or not.
Magic number:- A number which is divisible by 9 and also after reverse if it is divisible by 9, then it is said to Magic number.
Example:- Suppose take 18
We know '18' is divisible by 9 (ie., 9 * 2 = 18)
Now After reverse '18' becomes 81.
Here '81' is also is divisible by 9 (ie., 9 * 9 =81 )
So we say '18' is a Magic number.
#include(stdio.h) // place this '<' & '>' instead of '(' & ')' before stdio.h
#include(conio.h)
void main( )
{
int rem, rev=0, n;
clrscr( );
printf("Enter the required number:");
scanf("%d",&n);
while(n>0)
{
rem=n%10;
rev=(10*rev)+ rem;
n=n/10;
}
if(rev%9== 0)
printf("\nMagic Number.");
else
printf("\nNot a Magic number.");
getch( );
}
Output:-
Enter the required number: 27
Magic Number.
Note:- If u have any doubt regarding this program or logic, please feel free to contact me.
Example:- Suppose take 18
We know '18' is divisible by 9 (ie., 9 * 2 = 18)
Now After reverse '18' becomes 81.
Here '81' is also is divisible by 9 (ie., 9 * 9 =81 )
So we say '18' is a Magic number.
#include(stdio.h) // place this '<' & '>' instead of '(' & ')' before stdio.h
#include(conio.h)
void main( )
{
int rem, rev=0, n;
clrscr( );
printf("Enter the required number:");
scanf("%d",&n);
while(n>0)
{
rem=n%10;
rev=(10*rev)+ rem;
n=n/10;
}
if(rev%9== 0)
printf("\nMagic Number.");
else
printf("\nNot a Magic number.");
getch( );
}
Output:-
Enter the required number: 27
Magic Number.
Note:- If u have any doubt regarding this program or logic, please feel free to contact me.
Write a C program to determine the given integer is Perfect number or not.
Perfect number:- Sum of the factorials of a individual digit is known as Perfect number
Ex:- 145 = 1! + 4! + 5!
=> 145 = 1 + 24 + 120
==> 145 = 145
So '145' is a Perfect number.
#include(stdio.h) // place this '<' & '>' instead of '(' & ')' before stdio.h
#include(conio.h)
void main( )
{
int no, rem, sum=0, temp, fact;
clrscr( );
printf("Enter the required number:");
scanf("%d", &no);
temp=no;
while(no>0)
{
fact=1;
for( rem= no%10 ; rem>=1; rem--) { fact=fact*rem; }
sum = sum+ fact;
no = no/10;
}
if(temp == sum)
printf("\n%d is Perfect number", temp);
else
printf("\n%d is not a Perfect number", temp);
getch();
}
Output:-
Enter the required number: 145
145 is Perfect number.
Note:- If u have any doubt regarding this program or logic, please feel free to contact me.
Ex:- 145 = 1! + 4! + 5!
=> 145 = 1 + 24 + 120
==> 145 = 145
So '145' is a Perfect number.
#include(stdio.h) // place this '<' & '>' instead of '(' & ')' before stdio.h
#include(conio.h)
void main( )
{
int no, rem, sum=0, temp, fact;
clrscr( );
printf("Enter the required number:");
scanf("%d", &no);
temp=no;
while(no>0)
{
fact=1;
for( rem= no%10 ; rem>=1; rem--) { fact=fact*rem; }
sum = sum+ fact;
no = no/10;
}
if(temp == sum)
printf("\n%d is Perfect number", temp);
else
printf("\n%d is not a Perfect number", temp);
getch();
}
Output:-
Enter the required number: 145
145 is Perfect number.
Note:- If u have any doubt regarding this program or logic, please feel free to contact me.
Friday, February 13, 2009
Write a C program to check whether the given integer Prime number or not.
Method: 1
#include(stdio.h) // place this '<' & '>' instead of '(' & ')' before stdio.h
#include(conio.h)
int main( )
{
int no, flag=0, x=2;
clrscr( );
printf("Enter the required number to check:");
scanf("%d", &no);
while(x<=no/2) {
if(no%x==0)
{
flag=1;
break;
}
x++;
}
if(flag==0)
printf("\nPrime number.");
else
printf("\nNot a Prime number.");
getch( );
return o;
}
Output:-
Enter the required number: 23
Prime number.
Method: 2 // Prime number or not
#include(stdio.h) // place this '<' & '>' instead of '(' & ')' before stdio.h
#include(conio.h)
int main( )
{
int no, ct=0 , x=1;
clrscr( );
printf("Enter the required number to check:");
scanf("%d", &no);
while(x<=no)
{
if(no%x==0)
{ ct++; }
x++;
}
if(flag==0)
printf("\nPrime number.");
else
printf("\nNot a Prime number.");
getch( );
return o;
}
Output:-
Enter the required number: 21
Not a Prime number.
Note:- If u have any doubt regarding this program or logic, please feel free to contact me.
#include(stdio.h) // place this '<' & '>' instead of '(' & ')' before stdio.h
#include(conio.h)
int main( )
{
int no, flag=0, x=2;
clrscr( );
printf("Enter the required number to check:");
scanf("%d", &no);
while(x<=no/2) {
if(no%x==0)
{
flag=1;
break;
}
x++;
}
if(flag==0)
printf("\nPrime number.");
else
printf("\nNot a Prime number.");
getch( );
return o;
}
Output:-
Enter the required number: 23
Prime number.
Method: 2 // Prime number or not
#include(stdio.h) // place this '<' & '>' instead of '(' & ')' before stdio.h
#include(conio.h)
int main( )
{
int no, ct=0 , x=1;
clrscr( );
printf("Enter the required number to check:");
scanf("%d", &no);
while(x<=no)
{
if(no%x==0)
{ ct++; }
x++;
}
if(flag==0)
printf("\nPrime number.");
else
printf("\nNot a Prime number.");
getch( );
return o;
}
Output:-
Enter the required number: 21
Not a Prime number.
Note:- If u have any doubt regarding this program or logic, please feel free to contact me.
Wednesday, February 11, 2009
Write a C program to solve the Towers of Hanoi problem using Recursive function.
#include(stdio.h) // place this '<' & '>' instead of '(' & ')' before stdio.h
#include(conio.h)
#include(math.h)
void hanoi(int x, char from, char to, char aux)
{
if(x==1)
printf("Move Disk From %c to %c\n",from,to);
else
{
hanoi(x-1,from,aux,to);
printf("Move Disk From %c to %c\n",from,to);
hanoi(x-1,aux,to,from);
}
}
void main( )
{
int disk;
int moves;
clrscr();
printf("Enter the number of disks you want to play with:");
scanf("%d",&disk);
moves=pow(2,disk)-1;
printf("\nThe No of moves required is=%d \n",moves);
hanoi(disk,'A','C','B');
getch( );
}
Output:-
Enter the number of disks you want to play with: 3
The No of moves required is=7
Move Disk from A to C
Move Disk from A to B
Move Disk from C to B
Move Disk from A to C
Move Disk from B to A
Move Disk from B to C
Move Disk from A to C
Note:- If u have any doubt regarding this program or logic, please feel free to contact me.
#include(conio.h)
#include(math.h)
void hanoi(int x, char from, char to, char aux)
{
if(x==1)
printf("Move Disk From %c to %c\n",from,to);
else
{
hanoi(x-1,from,aux,to);
printf("Move Disk From %c to %c\n",from,to);
hanoi(x-1,aux,to,from);
}
}
void main( )
{
int disk;
int moves;
clrscr();
printf("Enter the number of disks you want to play with:");
scanf("%d",&disk);
moves=pow(2,disk)-1;
printf("\nThe No of moves required is=%d \n",moves);
hanoi(disk,'A','C','B');
getch( );
}
Output:-
Enter the number of disks you want to play with: 3
The No of moves required is=7
Move Disk from A to C
Move Disk from A to B
Move Disk from C to B
Move Disk from A to C
Move Disk from B to A
Move Disk from B to C
Move Disk from A to C
Note:- If u have any doubt regarding this program or logic, please feel free to contact me.
Monday, February 9, 2009
Write a C program to find the GCD (greatest common divisor) of two given integers using Recursive function.
#include(stdio.h) // place this '<' & '>' instead of '(' & ')' before stdio.h
#include(conio.h)
int gcd (int, int); //func. declaration.
void main( )
{
int a, b, res;
clrscr( );
printf("Enter the two integer values:");
scanf("%d%d", &a, &b);
res= gcd(a, b); // calling function.
printf("\nGCD of %d and %d is: %d", a, b, res);
getch( );
}
int gcd( int x, int y) //called function.
{
int z;
z=x%y;
if(z==0)
return y;
gcd(y,z); //recursive function
}
Note:- If u have any doubt regarding this program or logic, please feel free to contact me.
#include(conio.h)
int gcd (int, int); //func. declaration.
void main( )
{
int a, b, res;
clrscr( );
printf("Enter the two integer values:");
scanf("%d%d", &a, &b);
res= gcd(a, b); // calling function.
printf("\nGCD of %d and %d is: %d", a, b, res);
getch( );
}
int gcd( int x, int y) //called function.
{
int z;
z=x%y;
if(z==0)
return y;
gcd(y,z); //recursive function
}
Note:- If u have any doubt regarding this program or logic, please feel free to contact me.
Subscribe to:
Posts (Atom)