Posted on Leave a comment

‘C’ Graphics Programming

‘C’ Graphics Programming

  1. #include <graphics.h>
  2. #include <conio.h>
  3. int main()
  4. {
  5.     int gd = DETECT, gm;
  6.     initgraph(&gd, &gm,”C:\\TC\\BGI”);
  7.     outtextxy(10, 20, “Graphics programming is fun!”);
  8.     circle(200, 200, 50);
  9.     setcolor(BLUE);
  10.     line(350, 250, 450, 50);
  11.     getch();
  12.     closegraph( );
  13.     return 0;
  14. }
Posted on Leave a comment

‘C’ Program for Fibonacci series

 ‘C’ program for Fibonacci series

  1. #include <stdio.h>
  2. int main()
  3. {
  4.   int n, first = 0, second = 1, next, c;
  5.   printf(“Enter the number of terms\n”);
  6.   scanf(“%d”, &n);
  7.   printf(“First %d terms of Fibonacci series are:\n”, n);
  8.   for (c = 0; c < n; c++)
  9.   {
  10.     if (c <= 1)
  11.       next = c;
  12.     else
  13.     {
  14.       next = first + second;
  15.       first = second;
  16.       second = next;
  17.     }
  18.     printf(“%d\n”, next);
  19.   }
  20.   return 0;
  21. }
Posted on Leave a comment

‘C’ using structures in C programming

‘C’ using structures in C programming

  1. #include <stdio.h>
  2. #include <string.h>
  3. struct game
  4. {
  5.   char game_name[50];
  6.   int number_of_players;
  7. };  // Note the semicolon
  8. int main()
  9. {
  10.   struct game g;
  11.   strcpy(g.game_name, “Cricket”);
  12.   g.number_of_players = 11;
  13.   printf(“Name of game: %s\n”, g.game_name);
  14.   printf(“Number of players: %d\n”, g.number_of_players);
  15.   return 0;
  16. }
Posted on Leave a comment

‘C’ Using comments in a program

‘C’ Using comments in a program

  1. #include <stdio.h>
  2. int main()
  3. {
  4.    // Single line comment in a C program
  5.    printf(“Writing comments is very useful.\n”);
  6.    /*
  7.     * Multi-line comment syntax
  8.     * Comments help us to understand program later easily.
  9.     * Will you write comments while writing programs?
  10.     */
  11.    printf(“Good luck C programmer.\n”);
  12.    return 0;
  13. }
Posted on Leave a comment

‘C’ function program

‘C’ function program

  1. #include <stdio.h>
  2. void my_function();  // Declaring a function
  3. int main()
  4. {
  5.   printf(“Main function.\n”);
  6.   my_function();  // Calling the function
  7.   printf(“Back in function main.\n”);
  8.   return 0;
  9. }
  10. // Defining the function
  11. void my_function()
  12. {
  13.   printf(“Welcome to my function. Feel at home.\n”);
  14. }
Posted on Leave a comment

‘C’ Array program

‘C’ Array program

  1. #include <stdio.h>
  2. int main()
  3. {
  4.     int array[100], n, c;
  5.     printf(“Enter number of elements in array\n”);
  6.     scanf(“%d”, &n);
  7.     printf(“Enter %d elements\n”, n);
  8.     for (c = 0; c < n; c++)
  9.         scanf(“%d”, &array[c]);
  10.     printf(“The array elements are:\n”);
  11.     for (c = 0; c < n; c++)
  12.         printf(“%d\n”, array[c]);
  13.     return 0;
  14. }
Posted on Leave a comment

‘C’ program check if an integer is prime or not

C program check if an integer is prime or not

  1. #include <stdio.h>
  2. int main()
  3. {
  4.   int n, c;
  5.   printf(“Enter a number\n”);
  6.   scanf(“%d”, &n);
  7.   if (n == 2)
  8.     printf(“Prime number.\n”);
  9.   else
  10.   {
  11.     for (c = 2; c <= n – 1; c++)
  12.     {
  13.       if (n % c == 0)
  14.         break;
  15.     }
  16.     if (c != n)
  17.       printf(“Not prime.\n”);
  18.      else
  19.        printf(“Prime number.\n”);
  20.   }
  21.   return 0;
  22. }
Posted on Leave a comment

How use While loop example in ‘C’

How use While loop example in ‘C’

  1. #include <stdio.h>
  2. int main()
  3. {
  4.   int c = 1;  // Initializing variable
  5.   while (c <= 10)  // While loop will execute till the condition is true
  6.   {
  7.     printf(“%d “, c);  // Note the space after %d for gap in the numbers we want in output
  8.     c++;
  9.   }
  10.   return 0;
  11. }

Output:

***********************************
1 2 3 4 5 6 7 8 9 10

***********************************

Posted on Leave a comment

Using if & else control instructions

using if else control instructions

  1. #include <stdio.h>
  2. int main()
  3. {
  4.    int x = 1;
  5.    if (x == 1)
  6.       printf(“x is equal to one.\n”);
  7.    else
  8.       printf(“For comparison use ‘==’ as ‘=’ is the assignment operator.\n”);
  9.    return 0;
  10. }

Output:

**********************************************
x is equal to one.

**********************************************

Posted on Leave a comment

‘C’ Program to get input from a user using scanf command

Example: ‘C’ Program to get input from a user using scanf command

  1. #include <stdio.h>
  2. int main()
  3. {
  4.   int x;
  5.   printf(“Input an integer\n”);
  6.   scanf(“%d”, &x); // %d is used for an integer
  7.   printf(“The integer is: %d\n”, x);
  8.   return 0;
  9. }

Output:
Input an integer
7897
The integer is: 7897