Sie sind auf Seite 1von 23

Page 1 of 23 Processed by BIT Computer Services

1st Floor, Narammala Road, Siyambalagaskotuwa,


Phone: 077-665-9080, 037-2247-699, Reg. No.22/0297, TVEC Reg .No P-12/0224

Example 1 - C hello world program


/** My first C program */

1. #include <stdio.h>
2.
3. int main()
4. {
5. printf("Hello World\n");
6. return 0;
7. }

Output of program:
"Hello World"

Example 2 - C program to get input from a user using scanf

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

Example 3 - using if else control instructions

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

Example 4 - while loop example

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

Example 5 - C program check if an integer is prime or not

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. }

Example 6 - command line arguments

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.

Example 7 - Array program

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

Example 8 - function program

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. }

Example 9 - Using comments in a program

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

7. printf("Writing comments is very useful.\n");


8.
9. /*
10. * Multi-line comment syntax
11. * Comments help us to understand program later
easily.
12. * Will you write comments while writing programs?
13. */
14.
15. printf("Good luck C programmer.\n");
16.
17. return 0;
18. }

Example 10 - using structures in C programming

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. }

Example 11 - C program for Fibonacci series

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. }

Example 12 - C graphics programming

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

How to compile C programs with GCC compiler?

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

C program to store an integer in a string


1. #include <stdio.h>
2.
3. int main ()
4. {
5. char n[1000];
6.
7. printf("Input an integer\n");
8. scanf("%s", n);
9.
10. printf("%s", n);
11.
12. return 0;
13. }
Output of program:
1. Input an integer
2. 12345678909876543210123456789
3. 12345678909876543210123456789

Partner of Sarvodaya Fusion with the Collaboration of Cisco Networking Academy and Microsoft Imagine Academy
Page 13 of 23 Processed by BIT Computer Services

Addition of two numbers in C


Addition program in C
1. #include<stdio.h>
2.
3. int main()
4. {
5. int a, b, c;
6.
7. printf("Enter two numbers to add\n");
8. scanf("%d%d", &a, &b);
9.
10. c = a + b;
11.
12. printf("Sum of the numbers = %d\n", c);
13.
14. return 0;
15. }
Output of C addition program:

Addition without using third variable


1. #include<stdio.h>
2.
3. main()
4. {
5. int a = 1, b = 2;
6.
7. /* Storing result of addition in variable a */
8.
9. a = a + b;
10.
11. printf("Sum of a and b = %d\n", a);
12.
Partner of Sarvodaya Fusion with the Collaboration of Cisco Networking Academy and Microsoft Imagine Academy
Page 14 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

8. (2) + (-6) = (-4)


9. Do you wish to add more numbers (y/n)
10. y
11. Inut two integers
12. -5 3
13. (-5) + (3) = (-2)
14. Do you wish to add more numbers (y/n)
15. y
16. Inut two integers
17. -5 -6
18. (-5) + (-6) = (-11)
19. Do you wish to add more numbers (y/n)
20. n
C program for addition of two numbers using a
function
We can calculate sum of two integers using a function.
1. #include<stdio.h>
2.
3. long addition(long, long);
4.
5. main()
6. {
7. long first, second, sum;
8.
9. scanf("%ld%ld", &first, &second);
10.
11. sum = addition(first, second);
12.
13. printf("%ld\n", sum);
14.
15. return 0;
16. }
17.
18. long addition(long a, long b)
19. {
20. long result;
21.
22. result = a + b;
23.
Partner of Sarvodaya Fusion with the Collaboration of Cisco Networking Academy and Microsoft Imagine Academy
Page 16 of 23 Processed by BIT Computer Services

24. return result;


25. }

Partner of Sarvodaya Fusion with the Collaboration of Cisco Networking Academy and Microsoft Imagine Academy
Page 17 of 23 Processed by BIT Computer Services

Even or odd program in C

Odd or even program in C using modulus


operator
1. #include <stdio.h>
2.
3. int main()
4. {
5. int n;
6.
7. printf("Enter an integer\n");
8. scanf("%d", &n);
9.
10. if (n%2 == 0)
11. printf("Even\n");
12. else
13. printf("Odd\n");
14.
15. return 0;
16. }

C program to find odd or even using bitwise


operator
1. #include <stdio.h>
2.
3. int main()
4. {
5. int n;
6.
7. printf("Input an integer\n");
8. scanf("%d", &n);
9.
10. if (n & 1 == 1)
11. printf("Odd\n");
12. else
13. printf("Even\n");
14.
15. return 0;
16. }
Partner of Sarvodaya Fusion with the Collaboration of Cisco Networking Academy and Microsoft Imagine Academy
Page 18 of 23 Processed by BIT Computer Services

C program to check even or odd using


conditional operator
1. #include <stdio.h>
2.
3. int main()
4. {
5. int n;
6.
7. printf("Input an integer\n");
8. scanf("%d", &n);
9.
10. n%2 == 0 ? printf("Even\n") : printf("Odd\n");
11.
12. return 0;
13. }

C program to check odd or even without using


bitwise or modulus operator
1. #include <stdio.h>
2.
3. int main()
4. {
5. int n;
6.
7. printf("Enter an integer\n");
8. scanf("%d", &n);
9.
10. if ((n/2)*2 == n)
11. printf("Even\n");
12. else
13. printf("Odd\n");
14.
15. return 0;
16. }

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 program to perform addition, subtraction, multiplication and


division

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

C program to check whether a character is vowel or consonant

Check vowel using if else


1. #include <stdio.h>
2.
3. int main()
4. {
5. char ch;
6.
7. printf("Enter a character\n");
8. scanf("%c", &ch);
9.
10. // Checking both lower and upper case
11.
12. if (ch == 'a' || ch == 'A' || ch == 'e' || ch == 'E'
|| ch == 'i' || ch == 'I' || ch =='o' || ch=='O' || c
h =='u' || ch == 'U')
13. printf("%c is a vowel.\n", ch);
14. else
15. printf("%c isn't a vowel.\n", ch);
16.
17. return 0;
18. }
Output of program:

Check vowel using switch statement


1. #include <stdio.h>
2.
3. int main()
4. {
5. char ch;
6.

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. }

C program to check vowel or consonant using if


else
In this program we will check whether a character is a vowel or consonant or a
special character

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

11. if (ch=='a' || ch=='A' || ch=='e' || ch=='E' || ch


=='i' || ch=='I' || ch=='o' || ch=='O' || ch== 'u' ||c
h=='U')
12. printf("%c is a vowel.\n", ch);
13. else
14. printf("%c is a consonant.\n", ch);
15. }
16. else
17. printf("%c is neither a vowel nor a
consonant.\n", ch);
18.
19. return 0;
20. }

Function to check vowel


1. int check_vowel(char a)
2. {
3. if (a >= 'A' && a <= 'Z')
4. a = a + 'a' - 'A'; /* Converting to lower
case or use a = a + 32 */
5.
6. if (a == 'a' || a == 'e' || a == 'i' || a == 'o' |
| a == 'u')
7. return 1;
8.
9. return 0;
10. }

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

Das könnte Ihnen auch gefallen