Upcoming SlideShare
×

# Simple c program

1,668 views
1,590 views

Published on

Published in: Education, Technology
2 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

Views
Total views
1,668
On SlideShare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
95
0
Likes
2
Embeds 0
No embeds

No notes for slide

### Simple c program

1. 1. 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(); }
2. 2. Output : My first program in C !_
3. 3. 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(); }
4. 4. Output : Number is : 19 !_
5. 5. 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(); }
6. 6. Output : Enter any number :12 Number is : 12_
7. 7. Operators
8. 8. 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(); }
9. 9. Output : Enter any 2 numbers : 12 14 Addition is : 26_
10. 10. 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(); }
11. 11. Output : Enter any 2 numbers : 8 6 Subtraction is : 2_
12. 12. 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(); }
13. 13. Output : Enter any 2 numbers : 12 5 Division is : 2_
14. 14. 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(); • }
15. 15. Output : Enter any 2 numbers : 8 12 Multiplication is : 96_
16. 16. 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(); • }
17. 17. Output : Enter any 2 numbers : 11 5 Modulus is : 1_
18. 18. Pyramid in c
19. 19. 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(); • }
20. 20. Output : * * * * * * * * * * * * * * * * * * * * * * * * *_
21. 21. 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(); • }
22. 22. Output : * * * * * * * * * * * * * * *_
23. 23. 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(); • }
24. 24. Output : * * * * * * * * * * * * * * *_
25. 25. 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(); • }
26. 26. Output : * * * * * * * * * * * * * * *_
27. 27. 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(); • }
28. 28. Output : * * * * * * * * * * * * * * *_
29. 29. 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(); • }
30. 30. Output : * * * * * * * * * * * * * * *_
31. 31. 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(); • } •
32. 32. Output : * * * * * * * * * * * * * * * * * * * * * * * * *_
33. 33. 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(); • }
34. 34. Output : Enter number of rows: 5 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15_
35. 35. 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(); • }
36. 36. 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_
37. 37. 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(); • }
38. 38. Output : 1 2 2 3 3 3 4 4 4 4 5 5 5 5 5_
39. 39. 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(); • }
40. 40. Output : 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5_
41. 41. 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(); • }
42. 42. 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_
43. 43. 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(); • }
44. 44. 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_
45. 45. 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(); • }
46. 46. 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_
47. 47. Addition of two matrices :
48. 48. 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("nt Enter First Matrix : "); • for(i=0;i<3;i++) • { • for(j=0;j<3;j++) • { • scanf("%d",&a[i][j]); • } • } • printf("nt 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("nt Matrix Addition is : nn"); • for(i=0;i<3;i++) • { • for(j=0;j<3;j++) • { • printf("t %d",c[i][j]); • } • printf("n"); • } • getch(); • }
49. 49. Output : Enter First Matrix : 2 1 2 3 2 1 2 3 3 Enter Second Matrix : 2 1 0 2 3 0 1 1 2 Matrix Addition is : 4 2 2 5 5 1 3 4 5_
50. 50. 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("nt Enter First Matrix : "); • for(i=0;i<3;i++) • { • for(j=0;j<3;j++) • { • scanf("%d",&a[i][j]); • } • } • printf("nt 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("nt Matrix Subtraction is : nn"); • for(i=0;i<3;i++) • { • for(j=0;j<3;j++) • { • printf("t %d",c[i][j]); • } • printf("n"); • } • getch(); • }
51. 51. Output : Enter First Matrix : 2 1 2 3 2 1 2 3 3 Enter Second Matrix : 2 1 0 2 3 0 1 1 2 Matrix Subtraction is : 0 0 2 1 -1 1 1 2 1_
52. 52. Multiplication of two matrices :• Program : • /* Program to demonstrate multiplication of two matrices. Creation Date : 25 Nov 2011 11:50:11 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,k; • clrscr(); • printf("nt Enter First Matrix : "); • for(i=0;i<3;i++) • { • for(j=0;j<3;j++) • { • scanf("%d",&a[i][j]); • } • } • printf("nt 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("nt Matrix Multiplication is : nn"); • for(i=0;i<3;i++) • { • for(j=0;j<3;j++) • { • printf("t %d",c[i][j]); • } • printf("n"); • } • getch(); • }
53. 53. Output : Enter First Matrix : 1 2 1 3 2 1 1 2 1 Enter Second Matrix : 3 3 3 1 2 1 1 1 1 Matrix Multiplication is : 6 8 6 12 14 12 6 8 6_
54. 54. 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("nt Enter Matrix : "); • for(i=0;i<3;i++) • { • for(j=0;j<3;j++) • { • scanf("%d",&a[i][j]); • } • } • printf("nt Transpose of Matrix is : nn"); • for(i=0;i<3;i++) • { • for(j=0;j<3;j++) • { • printf("t%d",a[j][i]); • } • printf("n"); • } • getch(); • }
55. 55. Output : Enter Matrix : 2 1 2 3 2 1 2 3 3 Transpose of Matrix is : 2 3 2 1 2 3 2 1 3_
56. 56. 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("nt 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("nn Lower triangular matrix !"); • else • printf("nn Not lower triangular matrix !"); • getch(); • }
57. 57. Output : Enter 3*3 Matrix : 1 0 0 1 1 0 2 2 3 Lower triangular matrix !_
58. 58. 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("nt 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("nn Upper triangular matrix !"); • else • printf("nn Not Upper triangular matrix !"); • getch(); • }
59. 59. Output : Enter 3*3 Matrix : 1 2 3 0 1 1 0 0 3 Upper triangular matrix !_
60. 60. 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("nt 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("nn Unit/Identity matrix !"); • else • printf("nn Not Unit/Identity matrix !"); • getch(); • }
61. 61. Output : Enter 3*3 Matrix : 1 0 0 0 1 0 0 0 1 Unit/Identity matrix !_
62. 62. 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("nt 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("nn Zero/Null matrix !"); • else • printf("nn Not Zero/Null matrix !"); • getch(); • }
63. 63. Output : Enter 3*3 Matrix : 0 0 0 0 0 0 0 0 0 Zero/Null matrix !_
64. 64. 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("nt 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("nn Diagonal matrix !"); • else • printf("nn Not Diagonal matrix !"); • getch(); • } •
65. 65. Output : Enter 3*3 Matrix : 2 0 0 0 5 0 0 0 1 Diagonal matrix !_