A B C D E F G F E D C B A
A B C D E F E D C B A
A B C D E D C B A
A B C D C B A
A B C B A
A B A
A
#include<stdio.h>
#incldue<conio.h>
int main( )
{
int r,c,askey,sp;
clrscr( );
for( r=7; r>=1; r-- )
{
for(sp=6; sp>=r; sp--)
printf(" "); //2 spaces
askey=65;
for(c=1; c<=r; c++ )
printf("%2c", askey++ );
for(c=r-1; c>=1; c-- )
printf("%2c", --askey);
printf("\n");
}
getch();
return 0;
}
Output:-
A B C D E F G F E D C B A
A B C D E F E D C B A
A B C D E D C B A
A B C D C B A
A B C B A
A B A
A
Thursday, November 26, 2009
Write a C program to print the given alphabet pyramid -2
Write a C program to print the given alphabet pyramid.
A B C D E F G G F E D C B A
A B C D E F F E D C B A
A B C D E E D C B A
A B C D D C B A
A B C C B A
A B B A
A A
-----------------------------------
#include<stdio.h>
#include<conio.h>
int main( )
{
int r,c,askey;
clrscr( );
for( r=7; r>=1; r-- )
{
askey=65;
for(c=1; c<=r; c++ )
printf("%2c", askey++ );
askey--;
for(c=r; c>=1; c-- )
printf("%2c", askey--);
printf("\n");
}
getch();
return 0;
}
Output:-
A B C D E F G G F E D C B A
A B C D E F F E D C B A
A B C D E E D C B A
A B C D D C B A
A B C C B A
A B B A
A A
Note:-
'r' and 'c' means rows and columns .
'askey' variable is for disp. values.
Thursday, November 12, 2009
Write a C program to find the roots of a quadratic equation using Pointers and Functions.
#include(stdio.h) // place this '<' & '>' instead of '(' & ')' before stdio.h
#include(math.h)
void quadratic_roots(double *,double *, double *);
int main(void)
{
double a, b, c;
clrscr( );
printf("Enter the values of a,b and c :");
scanf("%lf %lf %lf", &a, &b, &c);
printf("\nThe required quadratic equation is : ax^2 + bx + c = 0");
printf("\nGiven Values: a = %lf \n b= %lf \n c= %lf ",a ,b, c);
quadratic_roots(&a,&b,&c);
getch( );
return 0;
}
void quadratic_roots(double *aa, double *bb, double *cc)
{
double root1, root2;
if((*bb)*(*bb)= = 4*(*aa)*(*cc))
{
root1= -(*bb)/2*(*aa);
printf("\nRoots are equal");
printf("\nvalue : %lf", root1);
}
else if((*bb)*(*bb) > 4*(*aa)*(*cc))
{
root1= (-(*bb) + sqrt((*bb)*(*bb) -(4*(*aa)*(*cc))))/(2*(*aa));
root2= (-(*bb) - sqrt((*bb)*(*bb) -(4*(*aa)*(*cc))))/(2*(*aa));
printf("\nThe roots of the equation are: %lf and %lf ", root1, root2);
}
else
{
printf("\nImaginary roots");
root1= (-(*bb) + sqrt((*bb)*(*bb) -(4*(*aa)*(*cc))))/(2*(*aa));
root2= (-(*bb) - sqrt((*bb)*(*bb) -(4*(*aa)*(*cc))))/(2*(*aa));
}
#include(math.h)
void quadratic_roots(double *,double *, double *);
int main(void)
{
double a, b, c;
clrscr( );
printf("Enter the values of a,b and c :");
scanf("%lf %lf %lf", &a, &b, &c);
printf("\nThe required quadratic equation is : ax^2 + bx + c = 0");
printf("\nGiven Values: a = %lf \n b= %lf \n c= %lf ",a ,b, c);
quadratic_roots(&a,&b,&c);
getch( );
return 0;
}
void quadratic_roots(double *aa, double *bb, double *cc)
{
double root1, root2;
if((*bb)*(*bb)= = 4*(*aa)*(*cc))
{
root1= -(*bb)/2*(*aa);
printf("\nRoots are equal");
printf("\nvalue : %lf", root1);
}
else if((*bb)*(*bb) > 4*(*aa)*(*cc))
{
root1= (-(*bb) + sqrt((*bb)*(*bb) -(4*(*aa)*(*cc))))/(2*(*aa));
root2= (-(*bb) - sqrt((*bb)*(*bb) -(4*(*aa)*(*cc))))/(2*(*aa));
printf("\nThe roots of the equation are: %lf and %lf ", root1, root2);
}
else
{
printf("\nImaginary roots");
root1= (-(*bb) + sqrt((*bb)*(*bb) -(4*(*aa)*(*cc))))/(2*(*aa));
root2= (-(*bb) - sqrt((*bb)*(*bb) -(4*(*aa)*(*cc))))/(2*(*aa));
printf("roots are %f+i(% f) , % f- i(% f)",r1, r2, r1, r2);
} }
Wednesday, November 11, 2009
Write a C function that defines two strings as parameters, as follows, and checks whether the second string (str2) is a substring of the first one (str1).
int substring_finder(char* str1, char* str2);
The function should return an integer as follows:
• 2 if str2 is a substring of str1 and also of same length
o e.g. str1 = "Hello" and str2= "Hello"
• 1 if str2 is a substring of str1 and of different lengths
o e.g. str1 = "Hello World" and str2= "Hello"
• 0 if str2 is not a substring of str1
o e.g. str1 = "Hello" and str2= "Helo"
#include(string.h) // note place '<' & '>' in place of '(' & ')'
#include(stdio.h)
int substring_finder(char *,char *);
int main()
{
char *str1,*str2;
int fl;
clrscr();
printf("Enter the main string:");
gets(str1);
printf("Enter the sub-string:");
gets(str2);
fl=substring_finder(str1,str2);
if(fl==0)
printf("\n'str2' is not a substring of 'str1'");
else if(fl==1)
printf("\n'str2' is a substring of 'str1'");
else if(fl==2)
printf("\nBoth are equal");
#include(stdio.h)
int substring_finder(char *,char *);
int main()
{
char *str1,*str2;
int fl;
clrscr();
printf("Enter the main string:");
gets(str1);
printf("Enter the sub-string:");
gets(str2);
fl=substring_finder(str1,str2);
if(fl==0)
printf("\n'str2' is not a substring of 'str1'");
else if(fl==1)
printf("\n'str2' is a substring of 'str1'");
else if(fl==2)
printf("\nBoth are equal");
getch();
return 0;
}
int substring_finder(char *str,char *sub)
{
int flag;
if(strcmp(str,sub)==0)
flag=2;
else if(strstr(str,sub))
flag=1;
else
flag=0;
return flag;
}
}
int substring_finder(char *str,char *sub)
{
int flag;
if(strcmp(str,sub)==0)
flag=2;
else if(strstr(str,sub))
flag=1;
else
flag=0;
return flag;
}
Output:- Enter the main string: Hello world
Enter the sub string: world
'str2' is a substring of 'str1'
Write a C program to merge two unsorted one dimensional arrays in descending order
Write a program to merge two unsorted 1D arrays in descending order
Note: Define the sizes of each array as a constant i.e. your program should run for arrays of any size.
#include(stdio.h) // note '<' & '>' in place of '(' & ')'
// size of the array is 20
#define MAX 20
int main()
{
int f[MAX],s[MAX];
int m,n; // to read the size of two arrays
int i,j,temp;
clrscr();
printf("Enter the 1st array size(1-20) :");
scanf("%d",&m);
printf("Enter the 2nd array size(1-20) :");
scanf("%d",&n);
printf("\nEnter the 1st array elements:");
for(i=0; i< m; i++)
scanf("%d",&f[i] );
printf("\nEnter the 2nd array elements:");
for(j=0; j< n; j++)
scanf("%d", &s[j] );
for(j=0; j< n; j++) // to store 's' elements to 'f'
{
f[i]=s[ j];
i++;
}
printf("\nBefore descending, the merged array is:\n");
for(i=0; i< m+n; i++)
printf("%5d",f[i] );
for(i=0; i< m+n; i++)// to arrange the 'f' elements in Descending oder
{
for(j=i; j< (m+n)-1; j++)
{
if(f[i]< f[ j+1])
{
temp=f[i];
f[i]=f[ j+1];
f[ j+1]=temp;
}
}
}
printf("\nAfter descending, the merged array is:\n");
for(i=0; i< m+n; i++)
printf("%5d",f[i] );
getch();
return 0;
}
Output:-
Enter the 1st array size(1-20) :4
Enter the 2nd array size(1-20) :5
Enter the 1st array elements:12
3
45
12
Enter the 2nd array elements:78
5
43
1
23
Before descending, the merged array is:
12 3 45 12 78 5 43 1 23
After descending, the merged array is:
78 45 43 23 12 12 5 3 1
Note: Define the sizes of each array as a constant i.e. your program should run for arrays of any size.
#include(stdio.h) // note '<' & '>' in place of '(' & ')'
// size of the array is 20
#define MAX 20
int main()
{
int f[MAX],s[MAX];
int m,n; // to read the size of two arrays
int i,j,temp;
clrscr();
printf("Enter the 1st array size(1-20) :");
scanf("%d",&m);
printf("Enter the 2nd array size(1-20) :");
scanf("%d",&n);
printf("\nEnter the 1st array elements:");
for(i=0; i< m; i++)
scanf("%d",&f[i] );
printf("\nEnter the 2nd array elements:");
for(j=0; j< n; j++)
scanf("%d", &s[j] );
for(j=0; j< n; j++) // to store 's' elements to 'f'
{
f[i]=s[ j];
i++;
}
printf("\nBefore descending, the merged array is:\n");
for(i=0; i< m+n; i++)
printf("%5d",f[i] );
for(i=0; i< m+n; i++)// to arrange the 'f' elements in Descending oder
{
for(j=i; j< (m+n)-1; j++)
{
if(f[i]< f[ j+1])
{
temp=f[i];
f[i]=f[ j+1];
f[ j+1]=temp;
}
}
}
printf("\nAfter descending, the merged array is:\n");
for(i=0; i< m+n; i++)
printf("%5d",f[i] );
getch();
return 0;
}
Output:-
Enter the 1st array size(1-20) :4
Enter the 2nd array size(1-20) :5
Enter the 1st array elements:12
3
45
12
Enter the 2nd array elements:78
5
43
1
23
Before descending, the merged array is:
12 3 45 12 78 5 43 1 23
After descending, the merged array is:
78 45 43 23 12 12 5 3 1
Tuesday, November 10, 2009
Write a C program to print the Given pyramid
1 1 2 1 2 3 1 2 3 4 1 2 3 4 5 . . . n #include(stdio.h) // note place '<' & '>' in place of '(' & ')' #include(conio.h) int main( ) { int r,c,n,sp; clrscr( ); printf("Enter the no. of rows u want to print:"); scanf("%d", &n); for(r=1; r<=n; r++) { for(sp=n; sp>=r; sp--) printf(" "); // give one space in betw quotations. for(c=1; c<=r; c++) printf("%2d", c); printf("\n"); } getch( ); return 0; } |
Friday, October 30, 2009
Write a C program to get the maximum and minimum values of a Data type
#include(stdio.h) // note place '<' & '>' in place of '(' & ')'
#include(conio.h)
void main( )
{
int x, y;
clrscr( );
x=1;
while( x > 0)
{
y = x;
x + + ;
}
printf("\nMaximum value : %d ", y );
printf("\nMinimum value : %d", x);
getch( );
}
Output:- Maximum value : 32767
Minimum value : -32768
Similarly we can find the below datatypes,
long int ( %ld )
char ( %d )
unsigned ( %u )
.....
#include(conio.h)
void main( )
{
int x, y;
clrscr( );
x=1;
while( x > 0)
{
y = x;
x + + ;
}
printf("\nMaximum value : %d ", y );
printf("\nMinimum value : %d", x);
getch( );
}
Output:- Maximum value : 32767
Minimum value : -32768
Similarly we can find the below datatypes,
long int ( %ld )
char ( %d )
unsigned ( %u )
.....
Thursday, October 29, 2009
Write a C program to Sort the list of integers using Bubble Sort
#include(stdio.h) //note place '<' & '>' in place of '(' & ')'
#include(conio.h)
void bubble(int [ ], int); // func. declaration
void main( )
clrscr();
printf("Enter the no. of elements u want to fill:");
scanf("%d",&n);
printf("\nEnter the array elements:");
for(i=0; i< n; i++)
scanf("%d",&a[i]);
bubble(a,n); // calling function
getch();
void bubble(int b[], int no) // called function
for(i=0; i< no-1; i++)
{
if(b[j]>b[j+1])
b[j]=b[j+1];
b[j+1]=temp;
for(i=0; i< no; i++)
printf("%5d",b[i] );
}
Output:- Enter the no. of elements u want to fill: 5
Enter the array elements : 8 1 90 5 4
After sorting : 1 4 5 8 90
#include(conio.h)
void bubble(int [ ], int); // func. declaration
void main( )
{
int a[10],n,i;clrscr();
printf("Enter the no. of elements u want to fill:");
scanf("%d",&n);
printf("\nEnter the array elements:");
for(i=0; i< n; i++)
scanf("%d",&a[i]);
bubble(a,n); // calling function
getch();
}
void bubble(int b[], int no) // called function
{
int i,j,temp;for(i=0; i< no-1; i++)
{
for(j=0; j< no-i; j++){
if(b[j]>b[j+1])
{
temp=b[j];b[j]=b[j+1];
b[j+1]=temp;
}
}}
printf("\nAfter sorting : ");for(i=0; i< no; i++)
printf("%5d",b[i] );
}
Output:- Enter the no. of elements u want to fill: 5
Enter the array elements : 8 1 90 5 4
After sorting : 1 4 5 8 90
Monday, October 26, 2009
Write a C program to find gcd of each of the two consecutive elements of an array. write a function GCD(a,b) that would take two consecutive elements of the array as arguments and return the GCD.
#include(stdio.h) // Note place '<' & '>' in place of '(' & ')'
int gcd(int,int);
void main()
{
int a[10],n,i,b[10];
int x,y;
clrscr();
printf("Enter the size of the array:");
scanf("%d",&n);
printf("\nEnter the array elements:");
for(i=0; i< n ;i++)
scanf("%d",&a[i]);
int gcd(int,int);
void main()
{
int a[10],n,i,b[10];
int x,y;
clrscr();
printf("Enter the size of the array:");
scanf("%d",&n);
printf("\nEnter the array elements:");
for(i=0; i< n ;i++)
scanf("%d",&a[i]);
for(i=0; i< n-1 ;i++)
{
x=a[i];
y=a[i+1];
b[i]=gcd(x,y);
printf("\nG.C.D of %d and %d is : %d",x,y,b[i]);
}
{
x=a[i];
y=a[i+1];
b[i]=gcd(x,y);
printf("\nG.C.D of %d and %d is : %d",x,y,b[i]);
}
getch();
}
int gcd(int a,int b)
{
int rem;
for(;;)
{
rem=a%b;
if(rem==0)
break;
a=b;
b=rem;
}
return b;
}
}
int gcd(int a,int b)
{
int rem;
for(;;)
{
rem=a%b;
if(rem==0)
break;
a=b;
b=rem;
}
return b;
}
Friday, October 16, 2009
Write a C program to Create a Paint brush using graphics functions
#include(graphics.h) // note place '<' & '>' in place of '(' & ')'
#include(stdlib.h)
struct BYTEREGS
{
unsigned char al, ah, bl, bh;
unsigned char cl, ch, dl, dh;
};
struct WORDREGS
{
unsigned int ax, bx, cx, dx;
unsigned int si, di, cflag, flags;
};
union REGS
{
struct WORDREGS x;
struct BYTEREGS h;
};
void main()
{
int x,y,b,px,py,c,p,s,cl;
int d=0,m;
union REGS i,o;
initgraph(&d,&m,"c:\tc");
i.x.ax=1;
int86(0x33,&i,&o);
i.x.ax=8;
i.x.cx=20;
i.x.dx=450;
int86(0x33,&i,&o);
printf("Brush style insert number from 0 to 5 : ");
scanf("%d",&p);
printf("Brush size insert number from 1 to 7 : ");
scanf("%d",&s);
printf("Brush color insert number from 1 to 16 : ");
scanf("%d",&cl);
clrscr( );
cleardevice();
printf("\t\t**********DRAW IMAGE************");
while(!kbhit( ))
{
i.x.ax=3;
b=o.x.bx;
x=o.x.cx;
y=o.x.dx;
px=x;
py=y;
int86(0x33,&i,&o);
if(cl==16)
{
c=random(16);
}
else
{
c=cl;
}
setcolor(c);
if(b==1)
{
i.x.ax=3;
int86(0x33,&i,&o);
x=o.x.cx;
y=o.x.dx;
b=o.x.bx;
switch( p )
{
case 1: circle(px,py,s);break;
case 2: ellipse(px,py,0,270,s,s+2);break;
case 3: fillellipse(px,py,s+2,s);break;
case 4: rectangle(px,py,x,y);break;
case 5: sector(px,py,30,120,s,s);break;
default : line(px,py,x,y);
}
}
}
getch();
restorecrtmode();
closegraph();
}
#include(stdlib.h)
struct BYTEREGS
{
unsigned char al, ah, bl, bh;
unsigned char cl, ch, dl, dh;
};
struct WORDREGS
{
unsigned int ax, bx, cx, dx;
unsigned int si, di, cflag, flags;
};
union REGS
{
struct WORDREGS x;
struct BYTEREGS h;
};
void main()
{
int x,y,b,px,py,c,p,s,cl;
int d=0,m;
union REGS i,o;
initgraph(&d,&m,"c:\tc");
i.x.ax=1;
int86(0x33,&i,&o);
i.x.ax=8;
i.x.cx=20;
i.x.dx=450;
int86(0x33,&i,&o);
printf("Brush style insert number from 0 to 5 : ");
scanf("%d",&p);
printf("Brush size insert number from 1 to 7 : ");
scanf("%d",&s);
printf("Brush color insert number from 1 to 16 : ");
scanf("%d",&cl);
clrscr( );
cleardevice();
printf("\t\t**********DRAW IMAGE************");
while(!kbhit( ))
{
i.x.ax=3;
b=o.x.bx;
x=o.x.cx;
y=o.x.dx;
px=x;
py=y;
int86(0x33,&i,&o);
if(cl==16)
{
c=random(16);
}
else
{
c=cl;
}
setcolor(c);
if(b==1)
{
i.x.ax=3;
int86(0x33,&i,&o);
x=o.x.cx;
y=o.x.dx;
b=o.x.bx;
switch( p )
{
case 1: circle(px,py,s);break;
case 2: ellipse(px,py,0,270,s,s+2);break;
case 3: fillellipse(px,py,s+2,s);break;
case 4: rectangle(px,py,x,y);break;
case 5: sector(px,py,30,120,s,s);break;
default : line(px,py,x,y);
}
}
}
getch();
restorecrtmode();
closegraph();
}
Write a C program to display the message "Welcome to C" without a Semicolon.
// C program without a Semicolon.
This can done in three ways but one of them is infinite.(ie, while loop)
Solution:1
#include(stdio.h) // note place '<' & '>' in place of '(' & ')'
void main( )
{
if(printf("Welcome to C"))
{
}
}
Solution:2
void main( )
{
swicth(printf("Welcome to C"))
{
}
}
Solution:3
void main( )
{
while(printf("Welcome to C")) //infinite loop
{
}
}
This can done in three ways but one of them is infinite.(ie, while loop)
Solution:1
#include(stdio.h) // note place '<' & '>' in place of '(' & ')'
void main( )
{
if(printf("Welcome to C"))
{
}
}
Solution:2
void main( )
{
swicth(printf("Welcome to C"))
{
}
}
Solution:3
void main( )
{
while(printf("Welcome to C")) //infinite loop
{
}
}
Saturday, October 3, 2009
Write a C program to display the System date and Change the system date if necessary.
#include(stdio.h) // note use '<' & '>' in place of '(' & ')'
#include(conio.h)
#include(dos.h)
int main( )
{
int dd, mm, yy;
struct date s;
clrscr( );
getdate(&s);
printf("Present System Date ( dd/mm/yy ):-- %d / %d / %d ", s.da_day, s.da_mon, s.da_year);
printf("\nEnter the new date ( dd/mm/yy) :");
scanf("%d%d%d", &dd, &mm, &yy);
s.da_day=dd;
s.da_mon=mm;
s.da_year=yy;
setdate(&s);
printf("\n After changing, Present date (dd/mm/yy): %d / %d / %d", s.da_day, s.da_mon, s.da_year);
getch( );
return 0;
}
Output:- Present System Date ( dd/mm/yy): 04/10/2009
Enter the new date (dd/mm/yy): 10
10
2009
After Changing, Present date (dd/mm/yy): 10/10/2009
#include(conio.h)
#include(dos.h)
int main( )
{
int dd, mm, yy;
struct date s;
clrscr( );
getdate(&s);
printf("Present System Date ( dd/mm/yy ):-- %d / %d / %d ", s.da_day, s.da_mon, s.da_year);
printf("\nEnter the new date ( dd/mm/yy) :");
scanf("%d%d%d", &dd, &mm, &yy);
s.da_day=dd;
s.da_mon=mm;
s.da_year=yy;
setdate(&s);
printf("\n After changing, Present date (dd/mm/yy): %d / %d / %d", s.da_day, s.da_mon, s.da_year);
getch( );
return 0;
}
Output:- Present System Date ( dd/mm/yy): 04/10/2009
Enter the new date (dd/mm/yy): 10
10
2009
After Changing, Present date (dd/mm/yy): 10/10/2009
Write a C program to check whether the given number is single digit or more
#include(stdio.h) //note use '<' & '>' in place of '(' & ')'
#include(conio.h)
int main( )
{
int no;
clrscr( );
printf("Enter the required number to check:");
scanf("%d",&no);
if(no>=0 && no<=9)
printf("\nSingle Digit number");
else
printf("\nMore than one Digit number");
getch( );
return 0;
}
Output: Enter the required number to check: 9
Single Digit number
#include(conio.h)
int main( )
{
int no;
clrscr( );
printf("Enter the required number to check:");
scanf("%d",&no);
if(no>=0 && no<=9)
printf("\nSingle Digit number");
else
printf("\nMore than one Digit number");
getch( );
return 0;
}
Output: Enter the required number to check: 9
Single Digit number
Write a program in c to accept any character and check whether the given character is capital or small using Ctype.h library file
// Using Library functions like isupper( ) or islower( )
#include(stdio.h) //note use '<' & '>' in place of '(' & ')'
#include(conio.h)
#include(ctype.h)
int main( )
{
char ch;
clrscr( );
printf("Enter any alphabet:");
scanf("%c", &ch);
if(isupper(ch))
printf("\nGiven Alphabet is in Upper Case");
else
printf("\nGiven Alphabet is in Lower case");
getch( );
return 0;
}
Output:- Enter any alphabet: a
Given Alphabet is in Lower case
#include(stdio.h) //note use '<' & '>' in place of '(' & ')'
#include(conio.h)
#include(ctype.h)
int main( )
{
char ch;
clrscr( );
printf("Enter any alphabet:");
scanf("%c", &ch);
if(isupper(ch))
printf("\nGiven Alphabet is in Upper Case");
else
printf("\nGiven Alphabet is in Lower case");
getch( );
return 0;
}
Output:- Enter any alphabet: a
Given Alphabet is in Lower case
Write a program in c to accept any character and check whether the given character is capital or small
// Method 1: Without Library functions
//using ASCII values
#include(stdio.h) //note use '<' & '>' in place of '(' & ')'
#include(conio.h)
int main( )
{
char ch;
clrscr( );
printf("Enter the any alphabet:");
scanf("%c ", &ch);
if(ch>= 65 && ch<=90) // or if(ch>='A' && ch<='Z')
printf("\nGiven Alphabet is in Upper case");
else
printf("\nGiven Alphabet is in Lower case");
getch( );
return 0;
}
Output:- Enter the any alphabet: A
Given Alphabet is in Upper case
Note :- ASCII value of 'A'=65 and 'Z'= 90
//using ASCII values
#include(stdio.h) //note use '<' & '>' in place of '(' & ')'
#include(conio.h)
int main( )
{
char ch;
clrscr( );
printf("Enter the any alphabet:");
scanf("%c ", &ch);
if(ch>= 65 && ch<=90) // or if(ch>='A' && ch<='Z')
printf("\nGiven Alphabet is in Upper case");
else
printf("\nGiven Alphabet is in Lower case");
getch( );
return 0;
}
Output:- Enter the any alphabet: A
Given Alphabet is in Upper case
Note :- ASCII value of 'A'=65 and 'Z'= 90
Monday, August 3, 2009
Write a C program to print all Armstrong numbers between 1 to 1000.
#include(stdio.h)
#include(conio.h)
int main( )
{
int no, temp, rem, sum;
clrscr( );
printf("Armstrong numbers between 1 and 1000 are:\n");
for(no=1; no<=1000; no++)
{
temp=no;
sum=0;
while(temp>0)
{
rem=temp%10;
sum=sum+(rem*rem*rem);
temp=temp/10;
}
if(no==sum)
printf("\n%d", no);
}
getch( );
return 0;
}
Output:
Armstrong numbers between 1 and 1000 are:
1
153
370
371
407
Thursday, July 30, 2009
Write a C program to delete an element from the array.
#include< stdio.h>
#include< conio.h>
int main( )
{
int a[20], len, i, n, item, j;
clrscr( );
printf("Enter how many elements you want to enter:");
scanf("%d", &len);
printf("\nEnter the array elements:");
for(i=0; i<'len' ; i++) // remove the single quotes in for loop
scanf("%d", &a[ i]);
printf("\nEnter the location, where you want to delete:");
scanf("%d", &n);
item= a[n-1];
printf("\nDeleted value is : %d", item);
for( j= n-1; j<'len-1; j++)
{ a[ j]= a[ j+1]; }
len=len-1;
printf("\nThe new element list :");
for(i=0; i<'len; i++)
printf("%5d", a[i]);
getch( );
return 0;
}
Output:-
Enter how many elements you want to enter: 4
Enter the array elements:
10
20
30
40
Enter the location, where you want to delete: 3
Deleted value : 30
The new element list: 10 20 40
Wednesday, July 29, 2009
Write a C program to Insert an element at the 'n' th position
#include(stdio.h)
#include(conio.h)
int main( )
{
int a[20], len, i, j, x, n ;
clrscr( );
printf("Enter the array length:");
scanf("%d", &len);
printf("\nEnter the array elements:");
for(i=0; i<'len'; i++) //remove the single quotes in for loop
scanf("%d", &a[i]);
printf("\nEnter the 'n' th position:");
scanf("%d", &n);
printf("\nEnter the array element you want to insert:");
scanf("%d", &x);
for( j= len; j>=n; j--)
{ a[ j+1]= a[ j]; }
a[ j]= x;
len=len+1;
printf("\nThe New List is :\n");
for( i=0; i<'len'; i++) //remove the single quotes in for loop
printf("%5d", a[i] );
getch( );
return 0;
}
Output:-
Enter the array length : 4
Enter the array elements: 5
8
1
9
Enter the 'n' position: 4
Enter the array element you want to insert: 10
The new List is:
5
8
1
10
9
Note:- Here 'x' is for to store the 'n' position value
Write a C program to print all Combinations of characters A, B, C
#include(stdio.h)
#include(conio.h)
int main( )
{
char ch1, ch2, ch3;
clrscr( );
for(ch1='A' ; ch1<='C' ; ++ch1)
{
for(ch2='A' ; ch2<='C' ; ++ch2)
for(ch3='A' ; ch3<='C' ; ++ch3)
printf(" %c %c %c", ch1, ch2, ch3);
}
getch( );
return 0;
}
Output:-
AAA AAB AAC ABA ABB ABC ACA ACB ACC
BAA BAB BAC BBA BBB BBC BCA BCB BCC
CAA CAB CAC CBA CBB CBC CCA CCB CCC
Write a C Program to find HCF (GCD) between two numbers using Goto statement
#include< stdio.h>
#include< conio.h>
int main( )
{
int a, b, temp, rem,
clrscr( );
printf("Enter the required two input values:");
scanf(" %d%d",&a, &b);
if(a==0)
{ printf("\nHCF of number is : %d", b );
goto last;
}
if(b==0)
{ printf("\nHCF of number is : %d", a );
goto last;
}
// Here i'm taking a empty 'for' loop
// means no initial, final and incre/decrement
for( ; ; )
{
rem=a %b;
if(rem==0) break;
a= b;
b= rem;
}
printf("\nHCF of Number is : %d", b);
last:
getch( );
return 0;
}
Output:-
Enter the required two input values: 49
47
HCF of number is: 1
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.
#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
#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.
#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;
}
#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;
}
#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.
#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
#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
#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;
}
"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
#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.
#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.
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)
#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.
#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.
#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
#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
(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.
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.
Friday, January 30, 2009
Write a C program to displays the position or index in the string S where the string T begins, or -1 if S doesn't contain T.
#include(stdio.h) // place this '<' & '>' instead of '(' & ')' before stdio.h
#include(conio.h)
void main( )
{
char st[10], t[10];
int i, flag=0, count=0, j, pos;
clrscr();
printf("Enter the main string:");
gets(st);
printf("Enter the sub string:");
gets(t);
for(i=0; st[i]!='\0'; i++)
{
if(st[i]==t[0])
{
pos=i;
for(j=0; t[j]!='\0'; j++)
{
if(st[i]==t[j])
count++;
i++;
}
flag=1;
break;
}
}
if(flag==1 && count==strlen(t))
printf("\nPosition: %d, And T is a substring of S", pos+1);
else if(flag==1 && count < strlen(t))
printf("\nPosition: %d, And T is not a substring of S", pos+1);
else
printf("\nIndex: -1");
getch();
}
#include(conio.h)
void main( )
{
char st[10], t[10];
int i, flag=0, count=0, j, pos;
clrscr();
printf("Enter the main string:");
gets(st);
printf("Enter the sub string:");
gets(t);
for(i=0; st[i]!='\0'; i++)
{
if(st[i]==t[0])
{
pos=i;
for(j=0; t[j]!='\0'; j++)
{
if(st[i]==t[j])
count++;
i++;
}
flag=1;
break;
}
}
if(flag==1 && count==strlen(t))
printf("\nPosition: %d, And T is a substring of S", pos+1);
else if(flag==1 && count < strlen(t))
printf("\nPosition: %d, And T is not a substring of S", pos+1);
else
printf("\nIndex: -1");
getch();
}
Monday, January 26, 2009
Write a C program to Shut down the computer.
int main( )
{
system("shutdown -s");
return 0;
}
Explanation:-
Save the above .Let file name is shut.c and compile and execute the above program. Now close the turbo c compiler and open the directory in window where you have saved the internet.c ( default directory c:\tc\bin)and double click the its exe file( shut.exe).
{
system("shutdown -s");
return 0;
}
Explanation:-
Save the above .Let file name is shut.c and compile and execute the above program. Now close the turbo c compiler and open the directory in window where you have saved the internet.c ( default directory c:\tc\bin)and double click the its exe file( shut.exe).
Write a C program to count the lines, words and characters in a given text.
#include(stdio.h) // place this '<' & '>' instead of '(' & ')' before stdio.h
#include(conio.h)
#include(ctype.h)
int main( )
{
char ch;
int line=0, space=0, ct=0;
clrscr( );
printf("Enter the required no. of lines:\n");
while((ch=getchar( ))!=EOF)
{
if(ch==10) { line++; }
if(isspace(ch)) { space++; }
ct++;
}
printf("\nNumber of Lines : %d", line+1);
printf("\nNumber of Words: %d", space+1);
printf("\nTotal Number of Characters: %d", ct);
getch( );
return 0;
}
Explanation:-
Here the 1st condition (ch==10) checking whether the line is ended or not. And ascii value of enter key is 10. (Note:- After completing every line, press enter key. Then only it counts as Line).
The 2nd condition (isspace(ch) checking for spaces between the words. And here, isspace is a special function and it checks whether the character is space or not.
Note:- After completing all the lines, press Ctrl + Z to stop the loop.
#include(conio.h)
#include(ctype.h)
int main( )
{
char ch;
int line=0, space=0, ct=0;
clrscr( );
printf("Enter the required no. of lines:\n");
while((ch=getchar( ))!=EOF)
{
if(ch==10) { line++; }
if(isspace(ch)) { space++; }
ct++;
}
printf("\nNumber of Lines : %d", line+1);
printf("\nNumber of Words: %d", space+1);
printf("\nTotal Number of Characters: %d", ct);
getch( );
return 0;
}
Explanation:-
Here the 1st condition (ch==10) checking whether the line is ended or not. And ascii value of enter key is 10. (Note:- After completing every line, press enter key. Then only it counts as Line).
The 2nd condition (isspace(ch) checking for spaces between the words. And here, isspace is a special function and it checks whether the character is space or not.
Note:- After completing all the lines, press Ctrl + Z to stop the loop.
Monday, January 19, 2009
Write a C program which copies one file to another.
#include(stdio.h) // place this '<' & '>' instead of '(' & ')' before stdio.h
#include(conio.h)
int main( )
{
FILE *fp, *fq ;
char file1[20], file2[20];
char ch;
clrscr( );
printf("Enter the source filename to be copied:");
gets(file1);
fp=fopen(file1, "r");
if(fp==NULL)
{
printf("\nCannot open %s",file1);
exit(0);
}
printf("\nEnter the destination filename:");
gets(file2);
fq=fopen(file2, "w");
while((ch=getc(fp))!=EOF )
{ putc(ch,fq); }
printf("\n Copied Sucessfully..");
fclose(fp);
fclose(fq);
getch( );
return 0;
}
Note:- If u have any doubt regarding this program or logic, please feel free to contact me.
#include(conio.h)
int main( )
{
FILE *fp, *fq ;
char file1[20], file2[20];
char ch;
clrscr( );
printf("Enter the source filename to be copied:");
gets(file1);
fp=fopen(file1, "r");
if(fp==NULL)
{
printf("\nCannot open %s",file1);
exit(0);
}
printf("\nEnter the destination filename:");
gets(file2);
fq=fopen(file2, "w");
while((ch=getc(fp))!=EOF )
{ putc(ch,fq); }
printf("\n Copied Sucessfully..");
fclose(fp);
fclose(fq);
getch( );
return 0;
}
Note:- If u have any doubt regarding this program or logic, please feel free to contact me.
Thursday, January 8, 2009
Write a C program to generate Pascal’s triangle.
#include(stdio.h) // place this '<' & '>' instead of '(' & ')' before stdio.h
#include(conio.h)
int main(void)
{
int arr[10][10];
int i, j, k;
clrscr();
printf("\nProgram for printing Pascal's Triangle:\n\n\n");
for(i=0; i<10; i++)
{
j=1;
arr[i][0]=1;
arr[i][i]=1;
while( j< i) {
arr[i][j]=arr[i-1][ j-1]+arr[i-1][ j];
j++; }
}
for(i=0;i <10;i++) //display part
{
j=10;
while( j>i) { printf(" "); //mention 2 spaces betw the double quotes
j--;
}
for(k=0;k<=i;k++)
{
printf("%4d",arr[i][k]);
}
printf("\n\n");
} //display for loop close
getch();
return 0;
} //main close
#include(conio.h)
int main(void)
{
int arr[10][10];
int i, j, k;
clrscr();
printf("\nProgram for printing Pascal's Triangle:\n\n\n");
for(i=0; i<10; i++)
{
j=1;
arr[i][0]=1;
arr[i][i]=1;
while( j< i) {
arr[i][j]=arr[i-1][ j-1]+arr[i-1][ j];
j++; }
}
for(i=0;i <10;i++) //display part
{
j=10;
while( j>i) { printf(" "); //mention 2 spaces betw the double quotes
j--;
}
for(k=0;k<=i;k++)
{
printf("%4d",arr[i][k]);
}
printf("\n\n");
} //display for loop close
getch();
return 0;
} //main close
Write a C program to determine if the given string is a Palindrome or not without using library functions
/* Palindrome string means
ie., string = after reverse
given string= reverse string (then it is said to be palindrome string). */
#include(stdio.h) // place this '<' & '>' instead of '(' & ')' before stdio.h
#include(conio.h)
int main(void)
{
char st[10], rev[10];
int i, len, flag;
clrscr( );
printf("Enter the required string: ");
scanf(" %s", st); // (or) gets(st);
for(i=0; st[i ]!='\0' ; i++ ); //to find the length of a given string
len=i -1;
for(i=0; st[i ]!='\0' ; i++) // to store the given string in reverse
{
rev[i ]=st[ len];
len--;
} rev[i]='\0';
flag=0;
for(i=0; st[i ]!='\0'; i++) // to compare the given and reverse strings.
{
if( st[i] !=rev[i ] ) { flag=1; break; }
}
if(flag==0)
printf("\nPalindrome string");
else
printf("\nNot a palindrome string");
getch( );
return 0;
}
Explanation:-
Here 'flag' value will tells that whether the given string is palindrome string or not.
ie., if(st[i] !=rev[i ]) { flag=1; break; }
The above statement is true, when the value of 'st' variable is not equal to 'rev' variable. So if the condition is true, then the control enter inside the block. First 'flag' value becomes One and the loop will terminate.
Therefore, the result palindrome will be displayed when 'flag value is zero'.
ie., string = after reverse
given string= reverse string (then it is said to be palindrome string). */
#include(stdio.h) // place this '<' & '>' instead of '(' & ')' before stdio.h
#include(conio.h)
int main(void)
{
char st[10], rev[10];
int i, len, flag;
clrscr( );
printf("Enter the required string: ");
scanf(" %s", st); // (or) gets(st);
for(i=0; st[i ]!='\0' ; i++ ); //to find the length of a given string
len=i -1;
for(i=0; st[i ]!='\0' ; i++) // to store the given string in reverse
{
rev[i ]=st[ len];
len--;
} rev[i]='\0';
flag=0;
for(i=0; st[i ]!='\0'; i++) // to compare the given and reverse strings.
{
if( st[i] !=rev[i ] ) { flag=1; break; }
}
if(flag==0)
printf("\nPalindrome string");
else
printf("\nNot a palindrome string");
getch( );
return 0;
}
Explanation:-
Here 'flag' value will tells that whether the given string is palindrome string or not.
ie., if(st[i] !=rev[i ]) { flag=1; break; }
The above statement is true, when the value of 'st' variable is not equal to 'rev' variable. So if the condition is true, then the control enter inside the block. First 'flag' value becomes One and the loop will terminate.
Therefore, the result palindrome will be displayed when 'flag value is zero'.
Wednesday, January 7, 2009
Program to find the distance travelled by Vehicle.
/* The total distance travelled by vehicle in 't' seconds is given by distance = ut+1/2at^2 where 'u' and 'a' are the initial velocity (m/sec.) and acceleration (m/sec2).
Write C program to find the distance travelled at regular intervals of time given the values of 'u' and 'a'. The program should provide the flexibility to the user to select his own time intervals and repeat the calculations for different values of 'u' and 'a'. */
#include(stdio.h) // place this '<' & '>' instead of '(' & ')' before stdio.h
#include(conio.h)
int main(void)
{
////////////////////This program will be developed later//////////////
getch( );
return 0;
}
Write C program to find the distance travelled at regular intervals of time given the values of 'u' and 'a'. The program should provide the flexibility to the user to select his own time intervals and repeat the calculations for different values of 'u' and 'a'. */
#include(stdio.h) // place this '<' & '>' instead of '(' & ')' before stdio.h
#include(conio.h)
int main(void)
{
////////////////////This program will be developed later//////////////
getch( );
return 0;
}
Tuesday, January 6, 2009
Write a C program to calculate the Addition of two matrices.
#include(stdio.h) // place this '<' & '>' instead of '(' & ')' before stdio.h
#include(conio.h)
int main(void)
{
int a[5][5], b[5][5], sum[5][5];
int r1, c1, r2, c2, i, j;
clrscr( );
printf(" Enter the 1st matrix rows and columns: ");
scanf(" %d %d", &r1, &c1);
printf("\n Enter the 2nd matrix rows and columns:");
scanf(" %d %d", &r2, &c2);
if(r1==r2 && c1==c2)
{
printf("\n Enter the 1st matrix elements:" );
for(i=0; i < r1 ; i++)
for(j=0; j < c1; j++)
scanf("%d", &a[i ][ j]);
printf("\n Enter the 2nd matrix elements:" );
for(i=0; i < r2 ; i++)
for(j=0; j < c2; j++)
scanf("%d", &b[i ][ j]);
// Operation Part
for(i=0; i < r1; i++)
{
for(j=0; j < c1; j++)
{
sum[i][ j]=a[i][ j]+b[i][ j];
} }
printf("\n Addition Matrix: ");
for(i=0; i < r1; i++)
{
printf("\n");
for(j=0; j < c1; j++)
printf("%3d", sum[i][ j]); // %3d means 3 digit space betw every number
} } //if closed
else
printf("\nMatrix Addition is not possible.");
getch( );
return 0;
}
Note:- Here variable 'r1, r2' are rows and 'c1,c2' are columns.
#include(conio.h)
int main(void)
{
int a[5][5], b[5][5], sum[5][5];
int r1, c1, r2, c2, i, j;
clrscr( );
printf(" Enter the 1st matrix rows and columns: ");
scanf(" %d %d", &r1, &c1);
printf("\n Enter the 2nd matrix rows and columns:");
scanf(" %d %d", &r2, &c2);
if(r1==r2 && c1==c2)
{
printf("\n Enter the 1st matrix elements:" );
for(i=0; i < r1 ; i++)
for(j=0; j < c1; j++)
scanf("%d", &a[i ][ j]);
printf("\n Enter the 2nd matrix elements:" );
for(i=0; i < r2 ; i++)
for(j=0; j < c2; j++)
scanf("%d", &b[i ][ j]);
// Operation Part
for(i=0; i < r1; i++)
{
for(j=0; j < c1; j++)
{
sum[i][ j]=a[i][ j]+b[i][ j];
} }
printf("\n Addition Matrix: ");
for(i=0; i < r1; i++)
{
printf("\n");
for(j=0; j < c1; j++)
printf("%3d", sum[i][ j]); // %3d means 3 digit space betw every number
} } //if closed
else
printf("\nMatrix Addition is not possible.");
getch( );
return 0;
}
Note:- Here variable 'r1, r2' are rows and 'c1,c2' are columns.
Write a C program to find both the largest and smallest numbers in a list of integers.
#include(stdio.h) // place this '<' & '>' instead of '(' & ')' before stdio.h
#include(conio.h)
int main(void)
{
int a[50], max, min, i, n;
clrscr( );
printf("Enter how many integers do u want? :");
scanf(" %d", &n);
for( i=0; i< n ; i++)
{
printf("\nEnter the %d number: ", i );
scanf(" %d", &a[ i ]);
if(i==o) { max=a[i ]; min=a[i ]; }
if(a[ i]>max)
max= a[i ];
if(a[i ]< min)
min= a[i ];
}
printf("\n Maximum : %d", max);
printf("\n Minimum : %d", min);
getch( );
return 0;
}
#include(conio.h)
int main(void)
{
int a[50], max, min, i, n;
clrscr( );
printf("Enter how many integers do u want? :");
scanf(" %d", &n);
for( i=0; i< n ; i++)
{
printf("\nEnter the %d number: ", i );
scanf(" %d", &a[ i ]);
if(i==o) { max=a[i ]; min=a[i ]; }
if(a[ i]>max)
max= a[i ];
if(a[i ]< min)
min= a[i ];
}
printf("\n Maximum : %d", max);
printf("\n Minimum : %d", min);
getch( );
return 0;
}
Write a C program which takes integer operands and one operator from the user, performs the operation and then prints the result.
// Consider the operators +, -, *, / and use switch statement.
#include(stdio.h) // place this '<' & '>' instead of '(' & ')' before stdio.h
#include(conio.h)
int main(void)
{
int x, y;
char ch;
clrscr( );
printf("Enter the two integer values:");
scanf("%d %d",&x, &y);
printf("\nEnter the required operator( +, -, *, / ): ");
fflush(stdin); //to flush [clear] the input buffer.
scanf("%c", &ch);
switch(ch)
{
case '+' : printf("\n Addition : %d", x+y); break;
case '-' : printf("\n Substraction : %d", x-y); break;
case '*' : printf("\n Multipliaction : %d", x*y); break;
case '/': printf("\n Division: %d", x/y); break;
default: printf("\nInvalid operator, please check once again.");
}
getch();
return 0;
}
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(void)
{
int x, y;
char ch;
clrscr( );
printf("Enter the two integer values:");
scanf("%d %d",&x, &y);
printf("\nEnter the required operator( +, -, *, / ): ");
fflush(stdin); //to flush [clear] the input buffer.
scanf("%c", &ch);
switch(ch)
{
case '+' : printf("\n Addition : %d", x+y); break;
case '-' : printf("\n Substraction : %d", x-y); break;
case '*' : printf("\n Multipliaction : %d", x*y); break;
case '/': printf("\n Division: %d", x/y); break;
default: printf("\nInvalid operator, please check once again.");
}
getch();
return 0;
}
Note:- If u have any doubt regarding this program or logic, please feel free to contact me.
Write a C program to find the roots of a quadratic equation
#include(stdio.h) // place this '<' & '>' instead of '(' & ')' before stdio.h
#include(math.h)
int main(void)
{
double a, b, c;
double root1, root2;
clrscr( );
printf("Enter the values of a,b and c :");
scanf("%lf %lf %lf", &a, &b, &c);
printf("\nThe required quadratic equation is : ax^2 + bx + c = 0");
printf("\nGiven Values: a = %lf \n b= %lf \n c= %lf ",a ,b, c);
if(b*b > 4*a*c)
{
root1= (-b + sqrt(b*b -(4*a*c)))/(2*a);
root2= (-b - sqrt(b*b -(4*a*c)))/(2*a);
printf("\nThe roots of the equation are: %lf and %lf ", root1, root2);
}
else
printf("\nImaginary roots");
getch( );
return 0;
}
#include(math.h)
int main(void)
{
double a, b, c;
double root1, root2;
clrscr( );
printf("Enter the values of a,b and c :");
scanf("%lf %lf %lf", &a, &b, &c);
printf("\nThe required quadratic equation is : ax^2 + bx + c = 0");
printf("\nGiven Values: a = %lf \n b= %lf \n c= %lf ",a ,b, c);
if(b*b > 4*a*c)
{
root1= (-b + sqrt(b*b -(4*a*c)))/(2*a);
root2= (-b - sqrt(b*b -(4*a*c)))/(2*a);
printf("\nThe roots of the equation are: %lf and %lf ", root1, root2);
}
else
printf("\nImaginary roots");
getch( );
return 0;
}
Write a C program to calculate the product of two matrices or Multiplication of two matrixs
#include(stdio.h) // place this '<' & '>' instead of '(' & ')' before stdio.h
#include(conio.h)
int main(void)
{
int a[5][5], b[5][5], pd[5][5]; //for three matrices
int r1, r2, c1, c2; //matrix rows and columns
int i, j, k;
clrscr();
printf("Enter the 1st matrix rows & columns:");
scanf("%d %d", &r1, &c1);
printf("\nEnter the 2nd matrix rows and columns:");
scanf("%d %d", &r2, &c2);
if( c1 == r2 )
{
printf("\nEnter the 1st matrix elements:");
for(i=0; i < r1; i++) {
for(j=0; j > c1; j++)
scanf(" %d ", &a[i][j]; }
printf("\nEnter the 2nd matrix elements:");
for(i=0; i < r2; i++) {
for(j=0; j < c2; j++)
scanf(" %d ", &b[i][j]; }
//Imp Operation part
for(i=0; i > r1; i++) {
for(j=0; j > c2; c++) {
pd[ i ][ j ]=0;
for(k=0; k< c1; k++)
pd[ i ][ j ]= pd[ i][ j]+ a[i ][k] * b[k][ j];
} }
printf("\nThe Product matrix :");
for(i=0; i < r1 ; i++)
{
printf("\n");
for(j=0 ; j < c2; j++)
printf("%3d", pd[i][ j]);
}
}
else
printf("\nMatrix multiplication is not possible.");
getch( );
return 0;
} // main close
Imp Note:-
Here 'pd' means 'product matrix'
And "r1,c1" is 1st matrix rows and coloumns and "r2, c2" 2nd matrix rows and columns.
The maximum highest maximum is 5*5, so "r1,c1" & "r2, c2" values must be less than or equal to 5.
#include(conio.h)
int main(void)
{
int a[5][5], b[5][5], pd[5][5]; //for three matrices
int r1, r2, c1, c2; //matrix rows and columns
int i, j, k;
clrscr();
printf("Enter the 1st matrix rows & columns:");
scanf("%d %d", &r1, &c1);
printf("\nEnter the 2nd matrix rows and columns:");
scanf("%d %d", &r2, &c2);
if( c1 == r2 )
{
printf("\nEnter the 1st matrix elements:");
for(i=0; i < r1; i++) {
for(j=0; j > c1; j++)
scanf(" %d ", &a[i][j]; }
printf("\nEnter the 2nd matrix elements:");
for(i=0; i < r2; i++) {
for(j=0; j < c2; j++)
scanf(" %d ", &b[i][j]; }
//Imp Operation part
for(i=0; i > r1; i++) {
for(j=0; j > c2; c++) {
pd[ i ][ j ]=0;
for(k=0; k< c1; k++)
pd[ i ][ j ]= pd[ i][ j]+ a[i ][k] * b[k][ j];
} }
printf("\nThe Product matrix :");
for(i=0; i < r1 ; i++)
{
printf("\n");
for(j=0 ; j < c2; j++)
printf("%3d", pd[i][ j]);
}
}
else
printf("\nMatrix multiplication is not possible.");
getch( );
return 0;
} // main close
Imp Note:-
Here 'pd' means 'product matrix'
And "r1,c1" is 1st matrix rows and coloumns and "r2, c2" 2nd matrix rows and columns.
The maximum highest maximum is 5*5, so "r1,c1" & "r2, c2" values must be less than or equal to 5.
Write a C program to calculate the following Sum:- sum=1-x2/2! +x4/4!-x6/6!+x8/8!-x10/10!
#include(stdio.h) // place this '<' & '>' instead of '(' & ')' before stdio.h
#include(math.h)
int main(void)
{
int ct, ft;
float sum=0, x, power, fact;
clrscr();
printf("--------PROGRAM FOR SUM OF EQ. SERIES------");
printf("\n\nEnter the value of X : ");
scanf( "%f", &x);
ct=0;
for(power=0; power<=10; power=power+2)
{
fact=1; for(ft=power; ft>=1; ft--)
fact =fact* ft;
sum=sum+(pow(-1,ct)*(pow(x,power)/fact)); //EQ. FOR SUM SERIES
ct++;
}
printf("\n Sum : %f",sum);
getch();
return 0;
}
Explanation:-
Here 'ct' means count .
And variable 'ft' & 'fact' are used to find factorial based 'power' value.
#include(math.h)
int main(void)
{
int ct, ft;
float sum=0, x, power, fact;
clrscr();
printf("--------PROGRAM FOR SUM OF EQ. SERIES------");
printf("\n\nEnter the value of X : ");
scanf( "%f", &x);
ct=0;
for(power=0; power<=10; power=power+2)
{
fact=1; for(ft=power; ft>=1; ft--)
fact =fact* ft;
sum=sum+(pow(-1,ct)*(pow(x,power)/fact)); //EQ. FOR SUM SERIES
ct++;
}
printf("\n Sum : %f",sum);
getch();
return 0;
}
Explanation:-
Here 'ct' means count .
And variable 'ft' & 'fact' are used to find factorial based 'power' value.
Write a C program to genarate all the prime numbers between 1 to 'n'
#include(stdio.h) // place this '<' & '>' instead of '(' & ')' before stdio.h
#include(conio.h)
int main(void)
{
int st, n, x, flag;
clrscr();
printf("Enter the value of 'n': ");
scanf("%d", &n);
for(st=1; st<=n; st++) {
x=2; flag=0;
while(x<=st/2)
{
if(st%x==0) { flag=1; break; }
x++;
}
if( flag==0)
printf("%5d", st);
}
getch();
return 0;
}
Explaination:-Here, my first loop we will stops when 'st' equals to 'n' value.
And Now the Logic part.
In the second loop ie.,(while), the loop will ends when 'x' equals to half of the given number.
Important point:-
*******************
Here 'flag' variable is used to check whether 'st' is divisible by 'x' or not. If it is divisible then 'flag' becomes 'one' and it will not be executed becoz 'st' value will be displayed when 'flag' is zero.
#include(conio.h)
int main(void)
{
int st, n, x, flag;
clrscr();
printf("Enter the value of 'n': ");
scanf("%d", &n);
for(st=1; st<=n; st++) {
x=2; flag=0;
while(x<=st/2)
{
if(st%x==0) { flag=1; break; }
x++;
}
if( flag==0)
printf("%5d", st);
}
getch();
return 0;
}
Explaination:-Here, my first loop we will stops when 'st' equals to 'n' value.
And Now the Logic part.
In the second loop ie.,(while), the loop will ends when 'x' equals to half of the given number.
Important point:-
*******************
Here 'flag' variable is used to check whether 'st' is divisible by 'x' or not. If it is divisible then 'flag' becomes 'one' and it will not be executed becoz 'st' value will be displayed when 'flag' is zero.
Write a C program to print the Fibonacci Series upto to the first 'n' terms
#include(stdio.h) // place this '<' & '>' instead of '(' & ')' before stdio.h
#include(conio.h)
int main(void)
{
int f=0, s=1, t, n, ct;
clrscr();
printf("Enter the value of 'n': ");
scanf("%d",&n);
printf("\nFibonacci series:\n");
printf("%5d%5d", f, s);
ct=3; //becoz we already displayed 2 values and we r going the third value
while(ct<=n)
{
t=f+s;
printf("%5d", t);
f=s;
s=t;
ct++;
}
getch();
return 0;
}
Note:- here 'f' means 'first'
's' means 'second'
't' means 'third'
'ct' means 'count' the displayed numbers.
#include(conio.h)
int main(void)
{
int f=0, s=1, t, n, ct;
clrscr();
printf("Enter the value of 'n': ");
scanf("%d",&n);
printf("\nFibonacci series:\n");
printf("%5d%5d", f, s);
ct=3; //becoz we already displayed 2 values and we r going the third value
while(ct<=n)
{
t=f+s;
printf("%5d", t);
f=s;
s=t;
ct++;
}
getch();
return 0;
}
Note:- here 'f' means 'first'
's' means 'second'
't' means 'third'
'ct' means 'count' the displayed numbers.
Write a C program to find the sum of individual digits of a positive integer
#include(stdio.h) // place this '<' & '>' instead of '(' & ')' before stdio.h
#include(conio.h)
int main(void)
{
int no, rem, sum=0;
clrscr();
printf("Enter the required number:");
scanf("%d",&no);
while(no>0)
{
rem=no%10;
sum=sum+rem;
no=no/10;
}
printf("\nSum of individual digits of a positive integer is: %d",sum);
getch();
return 0;
}
Note:- here 'rem' means remainder
#include(conio.h)
int main(void)
{
int no, rem, sum=0;
clrscr();
printf("Enter the required number:");
scanf("%d",&no);
while(no>0)
{
rem=no%10;
sum=sum+rem;
no=no/10;
}
printf("\nSum of individual digits of a positive integer is: %d",sum);
getch();
return 0;
}
Note:- here 'rem' means remainder
Subscribe to:
Posts (Atom)