Simple C Program :
Program :/* Simple C Program .
Creation Date : 12:34 PM 07/11/2010
Author : R.K.Singh*/
#include <stdio.h>#include <conio.h>void main(){
clrscr();printf("\n My first program in C !");getch();
}
Output :
My first program in C !_
Program to print value of existing number :
Program :/* Program to print value of existing number .
Creation Date : 12:38 PM 07/11/2010
Author :R.K.Singh */
#include <stdio.h>#include <conio.h>void main(){
int no = 19;clrscr();printf("\n Number is : %d",no);getch();
}
Output :
Number is : 19 !_
Program to demonstrate scanf() and printf() : 3.C :
Program :/* Program to demonstrate scanf() and printf().
Creation Date : 12:41 PM 07/11/2011
Author : R.K.Singh */
#include <stdio.h>#include <conio.h>void main(){
int no;clrscr();printf("\n Enter any number :");scanf("%d",&no);printf("\n Number is : %d",no);getch();
}
Output :
Enter any number :12
Number is : 12_
Operators
Program to calculate addition of two numbers :
Program :/* Program to calculate addition of two numbers.
Creation Date : 09:51 PM 21/11/2011
Author : R.K.Singh */
#include <stdio.h>#include <conio.h>void main(){
int a,b,c;clrscr();printf("\n Enter any 2 numbers : ");scanf("%d %d",&a,&b);c = a + b;printf("\n Addition is : %d",c);getch();
}
Output :
Enter any 2 numbers : 12 14
Addition is : 26_
Program to calculate subtraction of two numbers :
/* Program to calculate subtraction of two numbers.
Creation Date : 10:43 PM 21/11/2011
Author : R.K.Singh */
#include <stdio.h>#include <conio.h>void main(){
int a,b,c;clrscr();printf("\n Enter any 2 numbers : ");scanf("%d %d",&a,&b);c = a - b;printf("\n Subtraction is : %d",c);getch();
}
Output :
Enter any 2 numbers : 8 6
Subtraction is : 2_
Program to calculate division of two numbers :
Program :/* Program to calculate division of two numbers.
Creation Date : 10:58 PM 21/11/2011
Author : R.K.Singh */
#include <stdio.h>#include <conio.h>void main(){
int a,b,c;clrscr();printf("\n Enter any 2 numbers : ");scanf("%d %d",&a,&b);c = a / b;printf("\n Division is : %d",c);getch();
}
Output :
Enter any 2 numbers : 12 5
Division is : 2_
Program to calculate multiplication of two numbers :
• Program :• /* Program to calculate multiplication of two numbers.
• Creation Date : 10:51 PM 21/11/2011
• Author : R.K.Singh */
• #include <stdio.h>• #include <conio.h>• void main()• {• int a,b,c;• clrscr();• printf("\n Enter any 2 numbers : ");• scanf("%d %d",&a,&b);• c = a * b;• printf("\n Multiplication is : %d",c);• getch();• }
Output :
Enter any 2 numbers : 8 12
Multiplication is : 96_
Program to calculate modulus of two numbers :• Program :• /* Program to calculate modulus of two numbers.
• Creation Date : 11:01 PM 21/11/2011
• Author : R.K.Singh */
• #include <stdio.h>• #include <conio.h>• void main()• {• int a,b,c;• clrscr();• printf("\n Enter any 2 numbers : ");• scanf("%d %d",&a,&b);• c = a % b;• printf("\n Modulus is : %d",c);• getch();• }
Output :
Enter any 2 numbers : 11 5
Modulus is : 1_
Pyramid in c
Program to print pyramid in C : Pattern 1 :
• Program :• /* Program to print pyramid pattern in C : Pattern 1
• Creation Date : 12:36 AM 22/11/2011
• Author : R.K.Singh */
• #include <stdio.h>• #include <conio.h>• void main()• {• int i,j;• clrscr();• for(i=0; i<5; i++)• {• for(j=0; j<5; j++)• {• printf(" * ");• }• printf("\n");• }• getch();• }
Output :
* * * * * * * * * * * * * * * * * * * * * * * * *_
Program to print pyramid in C : Pattern 2 :
• Program :• /* Program to print pyramid pattern in C : Pattern 2
• Creation Date : 12:43 AM 22/11/2011
• Author : R.K.Singh */
• #include <stdio.h>• #include <conio.h>• void main()• {• int i,j;• clrscr();• for(i=0; i<5; i++)• {• for(j=0; j<=i; j++)• {• printf(" * ");• }• printf("\n");• }• getch();• }
Output :
* * * * * * * * * * * * * * *_
Program to print pyramid in C : Pattern 3 :• /* Program to print pyramid pattern in C : Pattern 3 • Creation Date : 12:28 AM 22/11/2011• Author : R.K.Singh */
• #include <stdio.h>• #include <conio.h>• void main()• {• int i,j,k;• clrscr();• for(i=1; i<=5; i++)• {• for(j=5; j>=i; j--)• {• printf(" ");• }• for(k=1; k<=i; k++)• {• printf("*");• }• printf("\n");• }• getch();• }
Output :
* * * * * * * * * *
* * * * *_
Program to print pyramid in C : Pattern 4 :• /* Program to print pyramid pattern in C : Pattern 4
• Creation Date : 12:54 AM 22/11/2011
• Author : R.K.Singh */
• #include <stdio.h>• #include <conio.h>• void main()• {• int i,j,k,samp=1;• clrscr();• for(i=5; i>=1; i--)• {• for(k=samp; k>=0; k--)• {• printf(" "); // only 1 space• }• for(j=i; j>=1; j--)• {• printf("*");• }• samp = samp + 1;• printf("\n");• }• getch();• }
Output :
* * * * * * * * * * * * * * *_
Program to print pyramid in C : Pattern 5 :
• Program :• /* Program to print pyramid pattern in C : Pattern 5
• Creation Date : 01:08 AM 22/11/2011
• Author : R.K.Singh */
• #include <stdio.h>• #include <conio.h>• void main()• {• int i,j;• clrscr();• for(i=5; i>=1; i--)• {• for(j=1; j<=i; j++)• {• printf(" * ");• }• printf("\n");• }• getch();• }
Output :
* * * * * * * * * * * * * * *_
Program to print pyramid in C : Pattern 6 :
• Program :• /* Program to print pyramid pattern in C : Pattern 6
• Creation Date : 01:52 AM 22/11/2011
• Author : R.K.Singh */
• #include <stdio.h>• #include <conio.h>• void main()• {• int i,j,k,t=0;• clrscr();• for(i=1; i<=5; i++)• {• for(k=t; k<5; k++)• {• printf(" ");• }• for(j=0; j< i; j++)• {• printf(" * ");• t = t + 1;• }• printf("\n");• }• getch();• }
Output :
** ** * * * * * *
* * * * *_
Program to print pyramid in C : Pattern 7 :• Program :• /* Program to print pyramid pattern in C : Pattern 7
• Creation Date : 01:13 AM 22/11/2011
• Author : R.K.Singh */
• #include <stdio.h>• #include <conio.h>• void main()• {• int i,j,k,samp=1;• clrscr();• for(i=1; i<=5; i++)• {• for(k=samp; k<=5; k++)• {• printf(" ");• }• for(j=0; j< i; j++)• {• printf("*");• }• samp = samp + 1;• printf("\n");• }• samp = 1;• for(i=4; i>=1; i--)• {• for(k=samp; k>=0; k--)• {• printf(" ");• }• for(j=i; j>=1; j--)• {• printf("*");• }• samp = samp + 1;• printf("\n");• }• getch();• }•
Output : * * * * * * * * * * * * * * * * * * * * * * * * *_
Program to print pyramid in C : Pattern 8 :
• Program :• /* Program to print pyramid pattern in C : Pattern 8
• Creation Date : 02:39 PM 01/10/2011
• Author : R.K.Singh */
• #include <stdio.h>• #include <conio.h>• void main()• {• int rw, c, no=1 ,len;• clrscr();• printf("Enter number of rows: ");• scanf("%d," &len);• for(rw=1; rw<=len; rw++)• {• printf("\n");• for(c=1; c<=rw; c++)• {• printf(" %2d ", no);• no++;• }• }• getch();• }
Output :
Enter number of rows: 5 12 34 5 67 8 9 1011 12 13 14 15_
Program to print pyramid in C : Pattern 9 :• Program :• /* Program to print pyramid pattern in C : Pattern 9
• Creation Date : 03:19 PM 01/10/2011
• Author : R.K.Singh */
• #include <stdio.h>• #include <conio.h>• void main()• {• int no,i,y,x=35;• clrscr();• printf("Enter number of rows: ");• scanf("%d," &no);• for(y=0;y<=no;y++)• {• goto(x,y+1);• for(i=0-y; i<=y; i++)• {• printf(" %3d ", abs(i));• x=x-3;• }• }• getch();• }
Output :Enter number of rows: 5
0
1 0 1 2 1 0 1 2 3 2 1 0 1 2 3
4 3 2 1 0 1 2 3 4 5 4 3 2 1 0 1 2 3 4 5_
Program to print pyramid in C : Pattern 10 :• Program :• /* Program to print pyramid pattern in C : Pattern 10
• Creation Date : 03:14 PM 01/10/2011
• Author : R.K.Singh */
• #include <stdio.h>• #include <conio.h>• void main()• {• int i, j=5, k, x;• clrscr();• for(i=1;i<=5;i++)• {• for(k=1;k<=j;k++)• {• printf(" ");• }• for(x=1;x<=i;x++)• {• printf("%d",i);• printf(" ");• }• printf("\n");• j=j-1;• }• getch();• }
Output :
1 2 2 3 3 3 4 4 4 4
5 5 5 5 5_
Program to print pyramid in C : Pattern 11 :• Program :• /* Program to print pyramid pattern in C : Pattern 11
• Creation Date : 03:24 PM 01/10/2011
• Author : R.K.Singh */
• #include <stdio.h>• #include <conio.h>• void main()• {• int rw,c,no,spc;• clrscr();• printf("Enter number of rows : ");• scanf("%d", &no);• for(rw=1; rw<=no; rw++)• {• for(spc=no; spc>=rw; spc--)• {• printf(" ");• }• for(c=1; c<=rw; c++)• {• printf("%2d",c);• }• printf("\n");• }• getch();• }
Output :
1 1 2 1 2 3 1 2 3 4
1 2 3 4 5_
Program to print pyramid in C : Pattern 12 :• Program :• /* Program to print pyramid pattern in C : Pattern 12
• Creation Date : 03:24 PM 01/10/2011
• Author : R.K.Singh */
• #include <stdio.h>• #include <conio.h>• void main()• {• int i,j,k;• clrscr();• for(i=1; i<=5; i++)• {• for(j=1; j<=5-i; j++)• {• printf(" ");• }• for(k=1; k<=2*i-1; k++)• {• printf(" %d ",k);• }• printf("\n");• }• getch();• }
Output :
1 1 2 3
1 2 3 4 5 1 2 3 4 5 6 7
1 2 3 4 5 6 7 8 9_
Program to print pyramid in C : Pattern 13 :• Program :• /* Program to print pyramid pattern in C : Pattern 13
• Creation Date : 04:24 PM 01/10/2011
• Author : R.K.Singh */
• #include <stdio.h>• #include <conio.h>• void main()• {• int i,j,asci,spc;• clrscr();• for(i=7; i>=1; i--)• {• for(spc=6; spc>=i; spc--)• {• printf(" ");• }• asci=65;• for(j=1; j<=i; j++)• {• printf("%2c",asci++);• }• for(j=i-1; j>=0; j--)• {• printf("%2c",--asci);• }• printf("\n");• }• getch();• }
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_
Program to all Combinations of characters A,B,C in C : Pattern 14 :
• Program :• /* Program to print all Combinations of characters
A, B, C : Pattern 14
• Creation Date : 11:33 PM 01/10/2011
• Author : R.K.Singh */
• #include <stdio.h>• #include <conio.h>• void 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();• }
Output :
AAA AAB AAC ABA ABB ABC ACA ACB ACC BAA BAB BAC BBA BBBBBC BCA BCB BCC CAA CAB CAC CBA CBB CBC CCA CCB CCC_
Addition of two matrices :
Program : /* Program to demonstrate addition of two matrices. Creation Date : 25 Nov 2011 11:35:00 PM Author : R.K.Singh */
• #include <stdio.h>• #include <conio.h>• • void main()• {• int a[3][3],b[3][3],c[3][3],i,j;• clrscr();• printf("\n\t Enter First Matrix : ");• for(i=0;i<3;i++)• {• for(j=0;j<3;j++)• {• scanf("%d",&a[i][j]);• }• }• printf("\n\t Enter Second Matrix : ");• for(i=0;i<3;i++)• {• for(j=0;j<3;j++)• {• scanf("%d",&b[i][j]);• }• }• for(i=0;i<3;i++)• {• for(j=0;j<3;j++)• {• c[i][j]=a[i][j]+b[i][j];• }• }• printf("\n\t Matrix Addition is : \n\n");• for(i=0;i<3;i++)• {• for(j=0;j<3;j++)• {• printf("\t %d",c[i][j]);• }• printf("\n");• }• getch();• }
Output :
Enter First Matrix : 2 1 23 2 12 3 3Enter Second Matrix : 2 1 02 3 01 1 2Matrix Addition is :4 2 25 5 13 4 5_
Subtraction of two matrices :• Program :• • /* Program to demonstrate subtraction of two matrices.
• Creation Date : 25 Nov 2011 11:42:08 PM
• Author : R.K.Singh */
• #include <stdio.h>• #include <conio.h>• • void main()• {• int a[3][3],b[3][3],c[3][3],i,j;• clrscr();• printf("\n\t Enter First Matrix : ");• for(i=0;i<3;i++)• {• for(j=0;j<3;j++)• {• scanf("%d",&a[i][j]);• }• }• printf("\n\t Enter Second Matrix : ");• for(i=0;i<3;i++)• {• for(j=0;j<3;j++)• {• scanf("%d",&b[i][j]);• }• }• for(i=0;i<3;i++)• {• for(j=0;j<3;j++)• {• c[i][j]=a[i][j]-b[i][j];• }• }• printf("\n\t Matrix Subtraction is : \n\n");• for(i=0;i<3;i++)• {• for(j=0;j<3;j++)• {• printf("\t %d",c[i][j]);• }• printf("\n");• }• getch();• }
Output :
Enter First Matrix : 2 1 23 2 12 3 3
Enter Second Matrix : 2 1 02 3 01 1 2
Matrix Subtraction is :
0 0 21 -1 11 2 1_
Multiplication of two matrices :• Program :• /* Program to demonstrate multiplication of two matrices.
Creation Date : 25 Nov 2011 11:50:11 PMAuthor : R.K.Singh */#include <stdio.h>
• #include <conio.h>• void main()• {• int a[3][3],b[3][3],c[3][3],i,j,k;• clrscr();• printf("\n\t Enter First Matrix : ");• for(i=0;i<3;i++)• {• for(j=0;j<3;j++)• {• scanf("%d",&a[i][j]);• }• }• printf("\n\t Enter Second Matrix : ");• for(i=0;i<3;i++)• {• for(j=0;j<3;j++)• {• scanf("%d",&b[i][j]);• }• }• for(i=0;i<3;i++)• {• for(j=0;j<3;j++)• {• c[i][j]=0;• for(k=0;k<3;k++)• {• c[i][j]=c[i][j]+a[i][k]*b[k][j];• }• }• }• printf("\n\t Matrix Multiplication is : \n\n");• for(i=0;i<3;i++)• {• for(j=0;j<3;j++)• {• printf("\t %d",c[i][j]);• }• printf("\n");• }• getch();• }
Output :
Enter First Matrix : 1 2 13 2 11 2 1
Enter Second Matrix : 3 3 31 2 11 1 1
Matrix Multiplication is :
6 8 612 14 126 8 6_
Transpose of matrix :• Program :• • /* Program to demonstrate transpose of matrix.
• Creation Date : 26 Nov 2011 12:03:59 PM
• Author : R.K.Singh */
• #include <stdio.h>• #include <conio.h>• • void main()• {• int a[3][3],i,j;• clrscr();• printf("\n\t Enter Matrix : ");• for(i=0;i<3;i++)• {• for(j=0;j<3;j++)• {• scanf("%d",&a[i][j]);• }• }• printf("\n\t Transpose of Matrix is : \n\n");• for(i=0;i<3;i++)• {• for(j=0;j<3;j++)• {• printf("\t%d",a[j][i]);• }• printf("\n");• }• getch();• }
Output :
Enter Matrix : 2 1 23 2 12 3 3
Transpose of Matrix is :
2 3 21 2 32 1 3_
Check matrix is lower triangular or not :• Program :• • /* Program to check lower triangular matrix or not.
• Creation Date : 26 Nov 2011 01:14:54 PM
• Author : R.K.Singh */
• #include <stdio.h>• #include <conio.h>• • void main()• {• int a[3][3],i,j,flg=0;• clrscr();• printf("\n\t Enter 3*3 Matrix : ");• for(i=0;i<3;i++)• {• for(j=0;j<3;j++)• {• scanf("%d",&a[i][j]);• }• } • for(i=0;i<3;i++)• { • for(j=0;j<3;j++)• {• if(a[i]<a[j] && a[i][j]==0)• {• flg=flg+1;• }• }• }• if(flg==3)• printf("\n\n Lower triangular matrix !");• else• printf("\n\n Not lower triangular matrix !");• getch();• }
Output :
Enter 3*3 Matrix : 1 0 01 1 02 2 3
Lower triangular matrix !_
Check matrix is upper triangular or not :• Program :• • /* Program to check upper triangular matrix or not.
• Creation Date : 26 Nov 2011 01:14:54 PM
• Author : R.K.Singh */
• #include <stdio.h>• #include <conio.h>• • void main()• {• int a[3][3],i,j,flg=0;• clrscr();• printf("\n\t Enter 3*3 Matrix : ");• for(i=0;i<3;i++)• {• for(j=0;j<3;j++)• {• scanf("%d",&a[i][j]);• }• } • for(i=0;i<3;i++)• { • for(j=0;j<3;j++)• {• if(a[i]>a[j] && a[i][j]==0)• {• flg=flg+1;• }• }• }• if(flg==3)• printf("\n\n Upper triangular matrix !");• else• printf("\n\n Not Upper triangular matrix !");• getch();• }
Output :
Enter 3*3 Matrix : 1 2 30 1 10 0 3
Upper triangular matrix !_
Check matrix is unit/identity matrix or not :• Program :• • /* Program to check unit/identity matrix or not.
• Creation Date : 26 Nov 2011 08:28:08 PM
• Author : R.K.Singh */
• #include <stdio.h>• #include <conio.h>• • void main()• {• int a[3][3],i,j,flg=0,flg2=0,flg3=0;• clrscr();• printf("\n\t Enter 3*3 Matrix : ");• for(i=0;i<3;i++)• {• for(j=0;j<3;j++)• {• scanf("%d",&a[i][j]);• }• } • for(i=0;i<3;i++)• { • for(j=0;j<3;j++)• {• if(a[i]<a[j] && a[i][j]==0)• {• flg=flg+1;• }• if(a[i]>a[j] && a[i][j]==0)• {• flg2=flg2+1;• }• if(a[i][j]==1)• {• flg3=flg3+1;• }• }• }• if(flg==3 && flg2==3 && flg3==3)• printf("\n\n Unit/Identity matrix !");• else• printf("\n\n Not Unit/Identity matrix !");• getch();• }
Output :
Enter 3*3 Matrix : 1 0 00 1 00 0 1
Unit/Identity matrix !_
Check matrix is zero/null matrix or not :• Program :• • /* Program to check zero/null matrix or not.
• Creation Date : 26 Nov 2011 02:09:04 PM
• Author : R.K.Singh */
• #include <stdio.h>• #include <conio.h>• • void main()• {• int a[3][3],i,j,flg=0;• clrscr();• printf("\n\t Enter 3*3 Matrix : ");• for(i=0;i<3;i++)• {• for(j=0;j<3;j++)• {• scanf("%d",&a[i][j]);• }• } • for(i=0;i<3;i++)• { • for(j=0;j<3;j++)• {• if(a[i][j]==0)• {• flg=flg+1;• }• }• }• if(flg==9)• printf("\n\n Zero/Null matrix !");• else• printf("\n\n Not Zero/Null matrix !");• getch();• }
Output :
Enter 3*3 Matrix : 0 0 00 0 00 0 0
Zero/Null matrix !_
Check matrix is diagonal or not :• Program :• • /* Program to check diagonal matrix or not.
• Creation Date : 26 Nov 2011 01:14:54 PM
• Author : R.K.Singh */
• #include <stdio.h>• #include <conio.h>• • void main()• {• int a[3][3],i,j,flg=0,flg2=0;• clrscr();• printf("\n\t Enter 3*3 Matrix : ");• for(i=0;i<3;i++)• {• for(j=0;j<3;j++)• {• scanf("%d",&a[i][j]);• }• } • for(i=0;i<3;i++)• { • for(j=0;j<3;j++)• {• if(a[i]<a[j] && a[i][j]==0)• {• flg=flg+1;• }• if(a[i]>a[j] && a[i][j]==0)• {• flg2=flg2+1;• }• }• }• if(flg==3 && flg2==3)• printf("\n\n Diagonal matrix !");• else• printf("\n\n Not Diagonal matrix !");• getch();• }
Output :
Enter 3*3 Matrix : 2 0 00 5 00 0 1
Diagonal matrix !_