Subscribe

RSS Feed (xml)

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.

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.

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.

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.

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.

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.

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.

Saturday, February 7, 2009

Write a C program to find the GCD (greatest common divisor) of two given integers without using recusive function

(a). Without using Recursive function.

#include(stdio.h) // place this '<' & '>' instead of '(' & ')' before stdio.h
#include(conio.h)
void gcd (int, int); //func. declaration.
void main( )
{
int a, b;
clrscr( );
printf("Enter the two integer values:");
scanf("%d%d", &a, &b);
printf("\nGiven numbers are: %d and %d", a, b);
gcd(a, b); // calling function.
getch( );
}
void gcd( int x, int y) //called function.
{
int z;
for( ; ; ) //This is empty for loop.
{
z= x%y;
if( z==0) break;
x=y;
y=z;
}
printf("\nThe GCD is: %d", y);
}

Note:- Take the values as 23 and 3 , then GCd is 3.
This logic works, when a>b. (ie., 23>3 )
I'm not mentioned the logic when b>a. ( for that, we need if-else statement)

Explanation:-
Here i'm taking empty for loop becoz i dont have initial value , condition and increment or decrement.
This for loop will ends, when 'z' becomes zero. ie.,(z==0)

Write a C program to delete n Characters from a given position in a given string.

//without using functions.

#include(stdio.h) // place this '<' & '>' instead of '(' & ')' before stdio.h
#include(string.h)
void main()
{
char st[20],temp[20];
int pos,i,j,ct=0,n;
clrscr();
printf("Enter the string:");
gets(st);
printf("\nEntre the index position:");
scanf("%d",&pos);
printf("\nEnter the no.of characters:");
scanf("%d",&n);
if(pos<=strlen(st)) {
for(i=0;i<=strlen(st);i++) {
if(i==pos)
{
for(j=0;st[i]!='\0';j++) // to store the 'st' in 'temp' from a giv pos
{
temp[j]=st[i];
i++;
}
temp[j]='\0';
i=pos;
//to delete the 'n' char and after restore the temp in st.
for(j=0;temp[j]!='\0';j++)
{
ct++;
if(ct>n)
{
st[i]=temp[j];
i++;
}
}

st[i]='\0';
}
}
printf("\n\nAfter deletion the string: %s",st);
}
else
printf("\nSorry, we cannot delete from that position.");
getch();
}

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

Write a C program to insert a sub-string in to given main string from a given position.

//Without using Functions.

#include(stdio.h) // place this '<' & '>' instead of '(' & ')' before stdio.h
#include(string.h)
void main( )
{
char st[20],sub[10],temp[10];
int pos, i, j;
clrscr( );
printf("Enter the main string:");
gets(st);
printf("\nEnter the substring to insert:");
gets(sub);
printf("Enter the index position:");
scanf("%d",&pos);
if(pos<=strlen(st)) {
for(i=0;i<=strlen(st);i++) {
if(i==pos)
{
for(j=0;st[i]!='\0';j++) // to store the 'st' to 'temp' from given position.
{
temp[j]=st[i];
i++;
}
temp[j]='\0';
i=pos;
for(j=0;sub[j]!='\0';j++) // to insert a sub-str to main string.
{
st[i]=sub[j];
i++;
}
for(j=0;temp[j]!='\0';j++) // Lastly to insert the 'temp' to 'st' after sub-str.
{
st[i]=temp[j];
i++;
}
st[i]='\0';
}}
printf("\nAfter adding the sub-string: %s",st);
}
else
printf("\nSorry, it is not possible to insert a substring in that position.");
getch();
}

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

Thursday, February 5, 2009

Write a C program to read in two numbers, x and n, and then compute the sum of this geometric progression: 1+x+x2+x3+………….+xn

#include(stdio.h) // place this '<' & '>' instead of '(' & ')' before stdio.h
#include(conio.h)
#include(math.h)
int main( )
{
int x, n, sum,power;
clrscr( );
printf("Enter the values of x and n:");
scanf("%d%d", &x, &n);
if(n<0 style="font-weight: bold; color: rgb(255, 0, 0);">
{
printf("\nSorry, the formula does not make sense for negative exponents & values");
printf("Enter the values of x and n:");
scanf("%d%d", &x, &n);
sum=1;
for(power=1; power <=n; power++) {
sum=sum+ pow(x,power); }
printf("X value is: %d \nN value is: %d", x, n);
printf("\nSum of the given geometric progression: %d", sum);
}
else
{
sum=1;
for(power=1; power <=n; power++) {
sum=sum+ pow(x,power); }
printf("X value is: %d \nN value is: %d", x, n);
printf("\nSum of the given geometric progression: %d", sum);
}
getch( );
return 0;
}

Output:-
case 1: Suppose x=5, n=3
X value is: 5
N value is: 3
Sum of the given geometric progression: 156
ie., 1+ 5 + 25 + 125 = 156

case 2: Suppose n=-2 or x=-5 then the following message will be displays and it ask for another new values,

Sorry, the formula does not make sense for negative exponents & values.
Enter the values of x and n:5
3
X value is: 5
N value is: 3
Sum of the given geometric progression: 156

Write C programs that use both recursive and non-recursive functions to find the factorial of a given integer.

(i). to find the factorial of a given integer
(a).without using recursive function
.


#include(stdio.h) // place this '<' & '>' instead of '(' & ')' before stdio.h
#include(conio.h)
int fact( int); // function declaration
int main( )
{
int no,result;
clrscr( );
printf("Enter the required number:");
scanf("%d", &no);
result = fact( no);
printf("\n %d Factorial is : %d", no, result);
getch( );
return 0;
}
int fact(int n)
{
int ft,
for( ft=1; n>=1; n--)
ft=ft*n;
return ft;
}

Output:- 4 Factorial is : 24

(a).using recursive function.

#include(stdio.h) // place this '<' & '>' instead of '(' & ')' before stdio.h
#include(conio.h)
int fact( int); // function declaration
int main( )
{
int no,result;
clrscr( );
printf("Enter the required number:");
scanf("%d", &no);
result = fact( no);
printf("\n %d Factorial is : %d", no, result);
getch( );
return 0;
}

int fact(int n)
{
int ft,
if( n==1)
return 1;
else
ft= n*fact (n-1);
return ft;
}

Output:- 4 Factorial is : 24

Write a C program to construct a pyramid of numbers.

/*
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
*/

#include(stdio.h) // place this '<' & '>' instead of '(' & ')' before stdio.h
#include(conio.h)
int main( )
{
int r, c, num=1 ,len;
clrscr( );
printf("Enter the required no. of rows:");
scanf("%d", &len);
for( r=1; r< =len; r++ ) {
printf("\n");
for(c=1 ; c<=r; c++) {
printf("%2d", num);
num++; }
}
getch( );
return 0;
}
Explanation:-
Here 'r' indicates row,
'c' indicates column and 'len' indicates length of the pyramid.
And lastly 'num=1' for generating pyramid of numbers.
This 'c<=r' is required becoz when row is incremented, column value is also incremented.
Note:- If U have any doubts regarding this program, contact with me through my email-id.