Beruflich Dokumente
Kultur Dokumente
1. #include <stdio.h>
2.
3. int main()
4. {
5. printf("Hello World\n");
6. return 0;
7. }
Output of program:
"Hello World"
1. #include <stdio.h>
2.
3. int main()
4. {
5. int x;
6.
7. printf("Input an integer\n");
8. scanf("%d", &x); // %d is used for an integer
9.
10. printf("The integer is: %d\n", x);
Partner of Sarvodaya Fusion with the Collaboration of Cisco Networking Academy and Microsoft Imagine Academy
Page 2 of 23 Processed by BIT Computer Services
11.
12. return 0;
13. }
Output:
Input an integer
7897
The integer is: 7897
1. #include <stdio.h>
2.
3. int main()
4. {
5. int x = 1;
6.
7. if (x == 1)
8. printf("x is equal to one.\n");
9. else
10. printf("For comparison use '==' as '=' is the
assignment operator.\n");
11.
12. return 0;
13. }
Output:
x is equal to one.
Partner of Sarvodaya Fusion with the Collaboration of Cisco Networking Academy and Microsoft Imagine Academy
Page 3 of 23 Processed by BIT Computer Services
1. #include <stdio.h>
2.
3. int main()
4. {
5. int c = 1; // Initializing variable
6.
7. while (c <= 10) // While loop will execute till the
condition is true
8. {
9. printf("%d ", c); // Note the space after %d for
gap in the numbers we want in output
10. c++;
11. }
12.
13. return 0;
14. }
Output:
1 2 3 4 5 6 7 8 9 10
1. #include <stdio.h>
2.
3. int main()
4. {
5. int n, c;
6.
7. printf("Enter a number\n");
8. scanf("%d", &n);
Partner of Sarvodaya Fusion with the Collaboration of Cisco Networking Academy and Microsoft Imagine Academy
Page 4 of 23 Processed by BIT Computer Services
9.
10. if (n == 2)
11. printf("Prime number.\n");
12. else
13. {
14. for (c = 2; c <= n - 1; c++)
15. {
16. if (n % c == 0)
17. break;
18. }
19. if (c != n)
20. printf("Not prime.\n");
21. else
22. printf("Prime number.\n");
23. }
24. return 0;
25. }
1. #include <stdio.h>
2.
3. int main(int argc, char *argv[])
4. {
5. int c;
6.
7. printf("Number of command line arguments passed:
%d\n", argc);
8.
9. for (c = 0; c < argc; c++)
10. printf("%d argument is %s\n", c + 1, argv[c]);
Partner of Sarvodaya Fusion with the Collaboration of Cisco Networking Academy and Microsoft Imagine Academy
Page 5 of 23 Processed by BIT Computer Services
11.
12. return 0;
13. }
This program prints the number of arguments passed, and the arguments
which are passed to it.
1. #include <stdio.h>
2.
3. int main()
4. {
5. int array[100], n, c;
6.
7. printf("Enter number of elements in array\n");
8. scanf("%d", &n);
9.
10. printf("Enter %d elements\n", n);
11.
12. for (c = 0; c < n; c++)
13. scanf("%d", &array[c]);
14.
15. printf("The array elements are:\n");
16.
17. for (c = 0; c < n; c++)
18. printf("%d\n", array[c]);
19.
20. return 0;
21. }
Partner of Sarvodaya Fusion with the Collaboration of Cisco Networking Academy and Microsoft Imagine Academy
Page 6 of 23 Processed by BIT Computer Services
1. #include <stdio.h>
2.
3. void my_function(); // Declaring a function
4.
5. int main()
6. {
7. printf("Main function.\n");
8.
9. my_function(); // Calling the function
10.
11. printf("Back in function main.\n");
12.
13. return 0;
14. }
15.
16. // Defining the function
17. void my_function()
18. {
19. printf("Welcome to my function. Feel at home.\n");
20. }
1. #include <stdio.h>
2.
3. int main()
4. {
5. // Single line comment in a C program
6.
Partner of Sarvodaya Fusion with the Collaboration of Cisco Networking Academy and Microsoft Imagine Academy
Page 7 of 23 Processed by BIT Computer Services
1. #include <stdio.h>
2. #include <string.h>
3.
4. struct game
5. {
6. char game_name[50];
7. int number_of_players;
8. }; // Note the semicolon
9.
10. int main()
11. {
12. struct game g;
13.
14. strcpy(g.game_name, "Cricket");
15. g.number_of_players = 11;
Partner of Sarvodaya Fusion with the Collaboration of Cisco Networking Academy and Microsoft Imagine Academy
Page 8 of 23 Processed by BIT Computer Services
16.
17. printf("Name of game: %s\n", g.game_name);
18. printf("Number of players:
%d\n", g.number_of_players);
19.
20. return 0;
21. }
1. #include <stdio.h>
2.
3. int main()
4. {
5. int n, first = 0, second = 1, next, c;
6.
7. printf("Enter the number of terms\n");
8. scanf("%d", &n);
9.
10. printf("First %d terms of Fibonacci series
are:\n", n);
11.
12. for (c = 0; c < n; c++)
13. {
14. if (c <= 1)
15. next = c;
16. else
17. {
18. next = first + second;
19. first = second;
20. second = next;
Partner of Sarvodaya Fusion with the Collaboration of Cisco Networking Academy and Microsoft Imagine Academy
Page 9 of 23 Processed by BIT Computer Services
21. }
22. printf("%d\n", next);
23. }
24.
25. return 0;
26. }
1. #include <graphics.h>
2. #include <conio.h>
3.
4. int main()
5. {
6. int gd = DETECT, gm;
7.
8. initgraph(&gd, &gm,"C:\\TC\\BGI");
9.
10. outtextxy(10, 20, "Graphics programming is fun!");
11.
12. circle(200, 200, 50);
13.
14. setcolor(BLUE);
15.
16. line(350, 250, 450, 50);
17.
18. getch();
19. closegraph( );
20. return 0;
21. }
Partner of Sarvodaya Fusion with the Collaboration of Cisco Networking Academy and Microsoft Imagine Academy
Page 10 of 23 Processed by BIT Computer Services
1. #include <stdio.h>
2. #include <conio.h>
3.
4. int main()
5. {
6. int c;
7.
8. for (c = 1; c <= 10; c++)
9. printf("%d\n", c);
10.
11. getch();
12. return 0;
13. }
Above program includes a header file <conio.h> and uses function getch,
but this file is Borland specific, so it works in Turbo C compiler but not in GCC.
The program for GCC should be like:
1. #include <stdio.h>
2.
3. int main()
4. {
5. int c;
6.
7. /* for loop */
8.
9. for (c = 1; c <= 10; c++)
10. printf("%d\n", c);
11.
Partner of Sarvodaya Fusion with the Collaboration of Cisco Networking Academy and Microsoft Imagine Academy
Page 11 of 23 Processed by BIT Computer Services
12. return 0;
13. }
Print an integer in C
C program to print an integer
1. #include <stdio.h>
2.
3. int main()
4. {
5. int a;
6.
7. printf("Enter an integer\n");
8. scanf("%d", &a);
9.
10. printf("The integer is %d\n", a);
11.
12. return 0;
13. }
Output of the program:
C program to print first hundred integers [1, 100] using a for loop:
1. #include <stdio.h>
2.
3. int main()
4. {
5. int c;
6.
7. for (c = 1; c <= 100; c++)
8. printf("%d ", c);
9.
10. return 0;
11. }
Partner of Sarvodaya Fusion with the Collaboration of Cisco Networking Academy and Microsoft Imagine Academy
Page 12 of 23 Processed by BIT Computer Services
Partner of Sarvodaya Fusion with the Collaboration of Cisco Networking Academy and Microsoft Imagine Academy
Page 13 of 23 Processed by BIT Computer Services
13. return 0;
14. }
C program to add two numbers repeatedly
1. #include <stdio.h>
2.
3. int main()
4. {
5. int a, b, c;
6. char ch;
7.
8. while (1) {
9. printf("Inut two integers\n");
10. scanf("%d%d", &a, &b);
11. getchar();
12.
13. c = a + b;
14.
15. printf("(%d) + (%d) = (%d)\n", a, b, c);
16.
17. printf("Do you wish to add more numbers
(y/n)\n");
18. scanf("%c", &ch);
19.
20. if (ch == 'y' || ch == 'Y')
21. continue;
22. else
23. break;
24. }
25.
26. return 0;
27. }
Output of program:
1. Inut two integers
2. 2 6
3. (2) + (6) = (8)
4. Do you wish to add more numbers (y/n)
5. y
6. Inut two integers
7. 2 -6
Partner of Sarvodaya Fusion with the Collaboration of Cisco Networking Academy and Microsoft Imagine Academy
Page 15 of 23 Processed by BIT Computer Services
Partner of Sarvodaya Fusion with the Collaboration of Cisco Networking Academy and Microsoft Imagine Academy
Page 17 of 23 Processed by BIT Computer Services
Partner of Sarvodaya Fusion with the Collaboration of Cisco Networking Academy and Microsoft Imagine Academy
Page 19 of 23 Processed by BIT Computer Services
C programming code
1. #include <stdio.h>
2.
3. int main()
4. {
5. int first, second, add, subtract, multiply;
6. float divide;
7.
8. printf("Enter two integers\n");
9. scanf("%d%d", &first, &second);
10.
11. add = first + second;
12. subtract = first - second;
13. multiply = first * second;
14. divide = first / (float)second; //typecasting
15.
16. printf("Sum = %d\n", add);
17. printf("Difference = %d\n", subtract);
18. printf("Multiplication = %d\n", multiply);
19. printf("Division = %.2f\n", divide);
20.
21. return 0;
22. }
Partner of Sarvodaya Fusion with the Collaboration of Cisco Networking Academy and Microsoft Imagine Academy
Page 20 of 23 Processed by BIT Computer Services
Partner of Sarvodaya Fusion with the Collaboration of Cisco Networking Academy and Microsoft Imagine Academy
Page 21 of 23 Processed by BIT Computer Services
7. printf("Input a character\n");
8. scanf("%c", &ch);
9.
10. switch(ch)
11. {
12. case 'a':
13. case 'A':
14. case 'e':
15. case 'E':
16. case 'i':
17. case 'I':
18. case 'o':
19. case 'O':
20. case 'u':
21. case 'U':
22. printf("%c is a vowel.\n", ch);
23. break;
24. default:
25. printf("%c isn't a vowel.\n", ch);
26. }
27.
28. return 0;
29. }
1. #include <stdio.h>
2.
3. int main()
4. {
5. char ch;
6.
7. printf("Input a character\n");
8. scanf("%c", &ch);
9.
10. if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' &&ch <= '
Z')) {
Partner of Sarvodaya Fusion with the Collaboration of Cisco Networking Academy and Microsoft Imagine Academy
Page 22 of 23 Processed by BIT Computer Services
Partner of Sarvodaya Fusion with the Collaboration of Cisco Networking Academy and Microsoft Imagine Academy
Page 23 of 23 Processed by BIT Computer Services
Partner of Sarvodaya Fusion with the Collaboration of Cisco Networking Academy and Microsoft Imagine Academy