Sunday, 8 January 2012

C PROGRAMMING EXAMPLES


c program to check whether input alphabet is a vowel or not

This code checks whether an input alphabet is a vowel or not. Both lower-case and upper-case are checked.

C programming code


#include<stdio.h>
 
main()
{
      char ch;
 
      printf("Enter a character\n");
      scanf("%c",&ch);
 
      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 not a vowel.\n", ch);
 
      return 0;
}

Output:
check charater case output

Check vowel using switch statement


#include<stdio.h>
 
main()
{
   char ch;
 
   printf("Enter a character\n");
   scanf("%c",&ch);
 
   switch(ch)
   {
      case 'a':
      case 'A':
      case 'e':
      case 'E':
      case 'i':
      case 'I':
      case 'o':
      case 'O':
      case 'u':
      case 'U':
            printf("%c is a vowel.\n", ch);
            break;
      default:
            printf("%c is not a vowel.\n", ch);
   }                     
 
   return 0;
}

Function to check vowel


int check_vowel(char a)
{
    if ( a >= 'A' && a <= 'Z' )
       a = a + 'a' - 'A';   /* Converting to lower case */
 
    if ( a == 'a' || a == 'e' || a == 'i' || a == 'o' || a == 'u')
       return 1;
 
    return 0;
}

C PROGRAMMING EXAMPLES


add digits of number in c

C program to add digits of a number: Here we are using modulus operator to extract individual digits of number and adding them.

C programming code


#include<stdio.h>
 
main()
{
   int n, sum = 0, remainder;
 
   printf("Enter an integer\n");
   scanf("%d",&n);
 
   while( n != 0 )
   {
      remainder = n % 10;
      sum = sum + remainder;
      n = n / 10;
   }
 
   printf("Sum of digits of entered number = %d\n",sum);
 
   return 0;
}

Output of program:
digit program output

C PROGRAMMING EXAMPLES


c program to check leap year

c program to check leap year: c code to check leap year, year will be entered by the user.

C programming code


#include<stdio.h>
 
main()
{
      int year;
 
      printf("Enter a year to check if it is a leap year\n");
      scanf("%d", &year);
 
      if ( year%400 == 0)
         printf("%d is a leap year.\n", year);
      else if ( year%100 == 0)
         printf("%d is not a leap year.\n", year);
      else if ( year%4 == 0 )
         printf("%d is a leap year.\n", year);
      else
         printf("%d is not a leap year.\n", year);   
 
      return 0;
}

C PROGRAMMING EXAMPLES


C program to perform addition, subtraction, multiplication and division

C program to perform basic arithmetic operations i.e. add , subtract, multiply and divide two numbers. Numbers are assumed to be integers and will be entered by the user.

C programming code


#include<stdio.h>
 
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;
}

Arithmetic operations.
Output:
arithmetic operations program

Comments

#1 Guest : superb

i like this post, was searching this on net from last 1 hours

#2 Guest : but what if the user enters integers in the order 5 and 10?

the difference in that case would come out to be negative which is incorrect because difference is always positive..its the greater - smaller.

#3 adminPs : Use long data type

For large numbers you may use long int data type.
Declare variable as
long var;
and use %ld as conversion specifier.

#4 Guest : hi

very nice

#5 Guest : multiplication

how to multiply two numbers without using multiplication sign ?

#6 adminPs : c program to multiply numbers without using multiplication sign


#include<stdio.h>
 
main()
{
      int x,  y, c, temp, temp1, result = 0;
 
      scanf("%d%d",&x,&y);
      temp = x;
      temp1 = y;
 
      if ( temp < 0 )
         temp = -temp;       /* If x is negative*/
 
      for ( c = 1 ; c <= temp ; c++)
         result = result + temp1;
 
      if( x < 0 && y < 0 )   /* to print proper sign */
         result = -result;
 
      printf("%d\n",result);
      return 0;
 
}

C PROGRAMMING EXAMPLES


c program to add two numbers

C program to add two numbers: This c language program perform the basic arithmetic operation of addition on two numbers and then prints the sum on the screen. For example if the user entered two numbers as 5, 6 then 11 ( 5 + 6 ) will be printed on the screen.

C programming code


#include<stdio.h>
 
main()
{
   int a, b, c;
 
   printf("Enter two numbers to add\n");
   scanf("%d%d",&a,&b);
 
   c = a + b;
 
   printf("Sum of entered numbers = %d\n",c);
 
   return 0;
}

Add numbers program executable.

Output of program

add numbers

Addition without using third variable


#include<stdio.h>
 
main()
{
   int a = 1, b = 2;
 
   /* Storing result of addition in variable a */
 
   a = a + b;
 
   /* Not recommended because original value of a is lost  
    * and you may be using it some where in code considering it 
    * as it was entered by the user. 
    */
 
   printf("Sum of a and b = %d\n", a);
 
   return 0;
}

C program to add two numbers repeatedly


#include<stdio.h>
 
main()
{
   int a, b, c;
   char ch;
 
   while(1)
   {
      printf("Enter values of a and b\n");
      scanf("%d%d",&a,&b);
 
      c = a + b;
 
      printf("a + b = %d\n", c);
 
      printf("Do you wish to add more numbers(y/n)\n");
      scanf(" %c",&ch);
 
      if ( ch == 'y' || ch == 'Y' )
         continue;
      else
     break;
   }
 
   return 0;
}

Adding numbers in c using function

We have used long data type as it can handle large numbers.
#include<stdio.h>
 
long addition(long, long);
 
main()
{
   long first, second, sum;
 
   scanf("%ld%ld", &first, &second);
 
   sum = addition(first, second);
 
   printf("%ld\n", sum);
 
   return 0;
}
 
long addition(long a, long b)
{
   long result;
 
   result = a + b;
 
   return result;
}

C PROGRAMMING EXAMPLES


Example 1 - C hello world program
/* A very simple c program printing a string on screen*/
#include<stdio.h>
 
main()
{
    printf("Hello World\n");
    return 0;
}

Output of above program:
"Hello World"
Example 2 - c program to take input from user using scanf
#include<stdio.h>
 
main()
{
   int number;
 
   printf("Enter a number\n");
   scanf("%d",&number);
 
   printf("Number entered by you is %d\n", number);
 
   return 0;
}

Output:
Enter a number
5
Number entered by you is 5
Example 3 - using if else control instructions
#include<stdio.h>
 
main()
{
   int x = 1;
 
   if ( x == 1 )
      printf("x is equal to one.\n");
   else
      printf("For comparison use == as = is the assignment operator.\n");
 
   return 0;
}

Output:
x is equal to one.
Example 4 - loop example
#include<stdio.h>
 
main()
{
   int value = 1;
 
   while(value<=3)
   {
      printf("Value is %d\n", value);
      value++;
   }
 
   return 0;
}
Output:
Value is 1
Value is 2
Value is 3
Example 5 - c program for prime number
#include<stdio.h>
 
main()
{
   int n, c;
 
   printf("Enter a number\n");
   scanf("%d", &n);
 
   if ( n == 2 )
      printf("Prime number.\n");
   else
   {
       for ( c = 3 ; c <= n - 1 ; c++ )
       {
           if ( n % c == 0 )
              break;
       }
       if ( c != n )
          printf("Not prime.\n");
       else
          printf("Prime number.\n");
   }
   return 0;
}
Example 6 - command line arguments
#include<stdio.h>
 
main(int argc, char *argv[])
{
   int c;
 
   printf("Number of command line arguments passed: %d\n", argc);
 
   for ( c = 0 ; c < argc ; c++)
      printf("%d. Command line argument passed is %s\n", c+1, argv[c]);
 
   return 0;
}

Above c program prints the number and all arguments which are passed to it.
Example 7 - Array program
#include<stdio.h>
 
main() 
{
    int array[100], n, c;
 
    printf("Enter the 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("Array elements entered by you are:\n");
 
    for ( c = 0 ; c < n ; c++ ) 
        printf("array[%d] = %d\n", c, array[c]);
 
    return 0;
}
Example 8 - function program
#include<stdio.h>
 
void my_function();
 
main()
{
   printf("Main function.\n");
 
   my_function();
 
   printf("Back in function main.\n");
 
   return 0;
}
 
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 c source code
 
   printf("Writing comments is very useful.\n");
 
   /*
    * Multiline comment syntax
    * Comments help us to understand code later easily.
    * Will you write comments while developing programs ?
    */
 
   printf("Good luck\n"); 
 
   return 0;
}
Example 10 - using structures in c programming
#include<stdio.h>
 
struct programming
{
    float constant;
    char *pointer;
};
 
main()
{
   struct programming variable;
   char string[] = "Programming in Software Development.";
 
 
   variable.constant = 1.23;
   variable.pointer = string;
 
   printf("%f\n", variable.constant);
   printf("%s\n", variable.pointer);
 
   return 0;
}
Example 11 - c program for fibonacci series
#include<stdio.h>
#include<conio.h>
 
main()
{
   int n, first = 0, second = 1, next, c;
 
   printf("Enter the number of terms ");
   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);
   }
 
   getch();
   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 source code example.");
 
    circle(200, 200, 50);
 
    setcolor(BLUE);
 
    line(350, 250, 450, 50);
 
    getch();
    closegraph( );
    return 0;
}

For gcc compiler users

If you are using gcc compiler on linux operating system then you need to modify programs. For example consider the following programs which prints first ten natural numbers
#include<stdio.h>
#include<conio.h>
 
main()
{
    int c;
 
    for ( c = 1 ; c <= 10 ; c++ )
        printf("%d\n", c);
 
    getch();
    return 0;
}
Above source code 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. So the code for gcc should be like