Saturday, January 11, 2020

C Language Programs


 program check if an integer is prime or not
#include <stdio.h>

int main()
{
  int n, c;

  printf("Enter a number\n");
  scanf("%d", &n);

  if (n == 2)
    printf("Prime number.\n");
  else
  {
    for (c = 2; c <= n-1; c++)
    {
      if (n % c == 0)
        break;
    }
    if (c != n)
      printf("Not prime.\n");
     else
       printf("Prime number.\n");
  }
  return 0;
}

Arithmetic  program.

#include <stdio.h>
 
int main()
{
   int first, second, add, subtract, multiply;
   float divide;
 
   printf("Enter two integers\n");
   scanf("%d%d", &first, &second);
 
   add = first + second;
   subtract = first - second;
   multiply = first * second;
   divide = first / (float)second;   //typecasting
 
   printf("Sum = %d\n", add);
   printf("Difference = %d\n", subtract);
   printf("Multiplication = %d\n", multiply);
   printf("Division = %.2f\n", divide);
 
   return 0;
}


 

Swapping of two numbers in C

#include <stdio.h>
 
int main()
{
   int x, y, temp;
 
   printf("Enter the value of x and y\n");
   scanf("%d%d", &x, &y);
 
   printf("Before Swapping\nx = %d\ny = %d\n",x,y);
 
   temp = x;
   x    = y;
   y    = temp;
 
   printf("After Swapping\nx = %d\ny = %d\n",x,y);
 
   return 0;
}




C example
#include <stdio.h>
 
int main () {
 
   /* local variable definition */
   int a = 10;
 
   /* while loop execution */
   while( a < 20 ) {
      printf("value of a: %d\n", a);
      a++;
   }
 
   return 0;
 
output:
value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 15
value of a: 16
value of a: 17
value of a: 18
value of a: 19



do while example
#include <stdio.h>
 
int main () {
 
   /* local variable definition */
   int a = 10;
 
   /* do loop execution */
   do {
      printf("value of a: %d\n", a);
      a = a + 1;
   }while( a < 20 );
 
   return 0;
}
Output:
value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 15
value of a: 16
value of a: 17
value of a: 18
value of a: 19

break example
#include <stdio.h>
 
int main () {
 
   /* local variable definition */
   int a = 10;
 
   /* while loop execution */
   while( a < 20 ) {
   
      printf("value of a: %d\n", a);
      a++;
                 
      if( a > 15) {
         /* terminate the loop using break statement */
         break;
      }
   }
 
   return 0;
}
result −
value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 15

Continue
#include <stdio.h>
 
int main () {
 
   /* local variable definition */
   int a = 10;
 
   /* do loop execution */
   do {
   
      if( a == 15) {
         /* skip the iteration */
         a = a + 1;
         continue;
      }
                 
      printf("value of a: %d\n", a);
      a++;
   
   } while( a < 20 );
 
   return 0;
}
result −
value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 16
value of a: 17
value of a: 18
value of a: 19
Syntax
The syntax for a goto statement in C is as follows −
goto label;
..
.
label: statement;

#include <stdio.h>
 
int main () {
 
   /* local variable definition */
   int a = 10;
 
   /* do loop execution */
   LOOP:do {
   
      if( a == 15) {
         /* skip the iteration */
         a = a + 1;
         goto LOOP;
      }
                 
      printf("value of a: %d\n", a);
      a++;
 
   }while( a < 20 );
 
   return 0;
}
result −
value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 16
value of a: 17
value of a: 18
value of a: 19


check vowel or consonant using if else

#include <stdio.h>
 
int main()
{
  char ch;
 
  printf("Input a character\n");
  scanf("%c", &ch);
 
  if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' &&ch <= 'Z')) {
    if (ch=='a' || ch=='A' || ch=='e' || ch=='E' || ch=='i' || ch=='I' || ch=='o' || ch=='O' || ch== 'u' || ch=='U')
      printf("%c is a vowel.\n", ch);
    else
      printf("%c is a consonant.\n", ch);
  }
  else
    printf("%c is neither a vowel nor a consonant.\n", ch);
 
  return 0;
}
sum of digits of a number:
#include <stdio.h>
 
int main()
{
   int n, t, sum = 0, remainder;
 
   printf("Enter an integer\n");
   scanf("%d", &n);
 
   t = n;
 
   while (t != 0)
   {
      remainder = t % 10;
      sum       = sum + remainder;
      t         = t / 10;
   }
 
   printf("Sum of digits of %d = %d\n", n, sum);
 
   return 0;
}
Output of program:
For example, if the input is 98, the variable sum is 0 initially
98%10 = 8 (% is modulus operator which gives us remainder when 98 is divided by 10).
sum = sum + remainder
so sum = 8 now.
98/10 = 9 because in C language whenever we divide an integer by an another integer we get an integer.
9%10 = 9
sum = 8 (previous value) + 9
sum = 17
9/10 = 0.
So finally n = 0, the loop ends we get the required sum.
find HCF and LCM:
#include <stdio.h>
 
int main() {
  int a, b, x, y, t, gcd, lcm;
 
  printf("Enter two integers\n");
  scanf("%d%d", &x, &y);
 
  a = x;
  b = y;
 
  while (b != 0) {
    t = b;
    b = a % b;
    a = t;
  }
 
  gcd = a;
  lcm = (x*y)/gcd;
 
  printf("Greatest common divisor of %d and %d = %d\n", x, y, gcd);
  printf("Least common multiple of %d and %d = %d\n", x, y, lcm);
 
  return 0;
}
Download HCF and LCM program.
Output of program:

C program to find reverse of a number

#include <stdio.h>
 
int main()
{
   int n, reverse = 0;
 
   printf("Enter a number to reverse\n");
   scanf("%d", &n);
 
   while (n != 0)
   {
      reverse = reverse * 10;
      reverse = reverse + n%10;
      n       = n/10;
   }
 
   printf("Reverse of entered number is = %d\n", reverse);
 
   return 0;
}

Array program
#include <stdio.h>

main()
{
    int array[100], n, c;

    printf("Enter number of elements in array\n");
    scanf("%d", &n);

    printf("Enter %d elements\n", n);

    for (c = 0; c < n; c++)
        scanf("%d", &array[c]);

    printf("The array elements are:\n");

    for (c = 0; c < n; c++)
        printf("%d\n", array[c]);

    return 0;
}
Example 8 - function program
#include <stdio.h>

void my_function();  // Declaring a function

main()
{
  printf("Main function.\n");

  my_function();  // Calling the function

  printf("Back in function main.\n");

  return 0;
}

// Defining the function
void my_function()
{
  printf("Welcome to my function. Feel at home.\n");
}
Example 9 - Using comments in a program
#include <stdio.h>

main()
{
   // Single line comment in a C program

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

   /*
    * Multi-line comment syntax
    * Comments help us to understand program later easily.
    * Will you write comments while writing programs?
    */

   printf("Good luck C programmer.\n");

   return 0;
}
Example 10 - using structures in C programming
#include <stdio.h>
#include <string.h>

struct game
{
  char game_name[50];
  int number_of_players;
};  // Note the semicolon

int main()
{
  struct game g;

  strcpy(g.game_name, "Cricket");
  g.number_of_players = 11;

  printf("Name of Game: %s\n", g.game_name);
  printf("Number of players: %d\n", g.number_of_players);

  return 0;
}
Example 11 - C program for Fibonacci series
#include <stdio.h>

main()
{
  int n, first = 0, second = 1, next, c;

  printf("Enter the number of terms\n");
  scanf("%d", &n);

  printf("First %d terms of Fibonacci series are:\n", n);

  for (c = 0; c < n; c++)
  {
    if (c <= 1)
      next = c;
    else
    {
      next = first + second;
      first = second;
      second = next;
    }
    printf("%d\n", next);
  }

  return 0;
}
Example 12 - C graphics programming
#include <graphics.h>
#include <conio.h>

main()
{
    int gd = DETECT, gm;

    initgraph(&gd, &gm,"C:\\TC\\BGI");

    outtextxy(10, 20, "Graphics programming is fun!");

    circle(200, 200, 50);

    setcolor(BLUE);

    line(350, 250, 450, 50);

    getch();
    closegraph( );
    return 0;
}
Compiling C programs with GCC compiler
If you are using GCC on Linux operating system, then you may need to modify the programs. For example, consider the following program which prints first ten natural numbers
#include <stdio.h>
#include <conio.h>

int main()
{
    int c;

    for (c = 1; c <= 10; c++)
        printf("%d\n", c);

    getch();
    return 0;
}
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:
#include <stdio.h>

int main()
{
    int c;

    /* for loop */

    for (c = 1; c <= 10; c++)
        printf("%d\n", c);

    return 0;
}