Sunday, 8 January 2012

C PROGRAMMING EXAMPLES


c program to add two complex numbers

c program to add two complex numbers :- This program calculate the sum of two complex numbers which will be entered by the user and then printed their sum. User will have to enter the real and imaginary parts of two complex numbers. Then on our program we will add real parts and imaginary parts of complex numbers and then we add them and prints the complex number, i is the symbol used for iota. For example if user entered two complex numbers as
(1 + 2i) and (4 + 6 i) then output of program will be (5+8i).

C programming code


#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
 
struct complex
{
   int real;
   int img;
};
 
main()
{
   struct complex a, b, c;
 
   printf("Enter a and b where a + ib is the first complex number.");
   printf("\na = ");
   scanf("%d", &a.real);
   printf("b = ");
   scanf("%d", &a.img);
   printf("Enter c and d where c + id is the second complex number.");
   printf("\nc = ");
   scanf("%d", &b.real);
   printf("d = ");
   scanf("%d", &b.img);
 
   c.real = a.real + b.real;
   c.img = a.img + b.img;
 
   if ( c.img >= 0 )
      printf("Sum of two complex numbers = %d + %di",c.real,c.img);
   else
      printf("Sum of two complex numbers = %d %di",c.real,c.img);
 
   getch();
   return 0;
}



output of c program to add two complex numbers

C PROGRAMMING EXAMPLES


Factorial program in c

Factorial program in c: c code to find and print factorial of a number, three methods are given, first one uses a for loop, second uses a function to find factorial and third usingrecursion. Factorial is represented using !, so five factorial will be written as 5!, n factorial as n!. Also
n! = n*(n-1)*(n-2)*(n-3)...3.2.1 and zero factorial is defined as one i.e. 0!=1.

Factorial program in c using for loop

: Here we find factorial using for loop.
#include<stdio.h>
#include<conio.h>
 
main()
{
   int c, n, fact = 1;
 
   printf("Enter a number to calculate it's factorial\n");
   scanf("%d",&n);
 
   for( c = 1 ; c <= n ; c++ )
         fact = fact*c;
 
   printf("Factorial of %d = %d\n",n,fact);
 
   getch();
   return 0;
}

Factorial
Output of code:

factorial code output

Factorial program in c using function


#include<stdio.h>
 
long factorial(int);
 
main()
{
   int number;
   long fact = 1;
 
   printf("Enter a number to calculate it's factorial\n");
   scanf("%d",&number);
 
   printf("%d! = %ld\n", number, factorial(number));
 
   return 0;
}
 
long factorial(int n)
{
   int c;
   long result = 1;
 
   for( c = 1 ; c <= n ; c++ )
         result = result*c;
 
   return ( result );
}

Factorial program in c using recursion


#include<stdio.h>
 
long factorial(int);
 
main()
{
   int num;
   long f;
 
   printf("ENTER A NUMBER TO FIND FACTORIAL :");
   scanf("%d",&num); 
 
   if(num<0)
      printf("NEGATIVE NUMBERS ARE NOT ALLOWED");
   else
   {
      f = factorial(num);
      printf("%d!=%ld",num,f);
   }
   return(0);
}
 
long factorial(int n)
{
   if(n==0)
      return(1);
   else
      return(n*factorial(n-1));
}

C PROGRAMMING EXAMPLES


c program to compare two strings

This c program compares two strings using strcmpwithout strcmp and using pointers. For comparing strings without using library function see another code below.

C program to compare two strings using strcmp


#include<stdio.h>
#include<string.h>
 
main()
{
   char a[100], b[100];
 
   printf("Enter the first string\n");
   gets(a);
 
   printf("Enter the second string\n");
   gets(b);
 
   if( strcmp(a,b) == 0 )
      printf("Entered strings are equal.\n");
   else
      printf("Entered strings are not equal.\n");
 
   return 0;
}

C program to compare two strings without using strcmp

Here we create our own function to compare strings.
int compare(char a[], char b[])
{
   int c = 0;
 
   while( a[c] == b[c] )
   {
      if( a[c] == '\0' || b[c] == '\0' )
         break;
      c++;
   }
   if( a[c] == '\0' && b[c] == '\0' )
      return 0;
   else
      return -1;
}

C program to compare two strings using pointers

In this method we will make our own function to perform string comparison, we will use character pointers in our function to manipulate string.
#include<stdio.h>
 
int compare_string(char*, char*);
 
main()
{
    char first[100], second[100], result;
 
    printf("Enter first string\n");
    gets(first);
 
    printf("Enter second string\n");
    gets(second);
 
    result = compare_string(first, second);
 
    if ( result == 0 )
       printf("Both strings are same.\n");
    else
       printf("Entered strings are not equal.\n");
 
    return 0;
}
 
int compare_string(char *first, char *second)
{
   while(*first==*second)
   {
      if ( *first == '\0' || *second == '\0' )
         break;
 
      first++;
      second++;
   }
   if( *first == '\0' && *second == '\0' )
      return 0;
   else
      return -1;
}

C PROGRAMMING EXAMPLES


c program to swap two numbers

C program to swap two numbers with and without using third variable, swapping in c using pointers and functions (Call by reference) , swapping means interchanging. For example if in your c program you have taken two variable a and b where a = 4 and b = 5, then before swapping a = 4, b = 5 after swapping a = 5, b = 4
In our c program to swap numbers we will use a temp variable to swap two numbers. Swapping is used in sorting that is when we wish to arrange numbers in a particular order either in ascending order or in descending order.

Swaping of two numbers in c


#include<stdio.h>
#include<conio.h>
 
main()
{
   int x, y, temp;
 
   printf("Enter the value of x and y ");
   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);
 
   getch();
   return 0;
}

Swapping of two numbers without third variable

You can also swap two numbers without using temp or temporary or third variable. In that case c program will be as shown :-

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

To understand above logic simply choose a as 7 and b as 9 and then do what is written in program. You can choose any other combination of numbers as well. Sometimes it's a good way to understand a program.

Swap two numbers using pointers


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

Swapping numbers using call by reference

In this method we will make a function to swap numbers.
#include<stdio.h>
 
void swap(int*, int*);
 
main()
{
   int x, y;
 
   printf("Enter the value of x and y\n");
   scanf("%d%d",&x,&y);
 
   printf("Before Swapping\nx = %d\ny = %d\n", x, y);
 
   swap(&x, &y); 
 
   printf("After Swapping\nx = %d\ny = %d\n", x, y);
 
   return 0;
}
 
void swap(int *a, int *b)
{
   int temp;
 
   temp = *b;
   *b = *a;
   *a = temp;   
}

Output of code:
swap numbers

C PROGRAMMING EXAMPLES


c program to add n numbers

This c program add n numbers which will be entered by the user. Firstly user will enter a number indicating how many numbers user wishes to add and then user will enter n numbers.In our c program to add numbers we are not using an array, if you wish you can use an array.

C programming code


#include<stdio.h>
#include<conio.h>
 
main()
{
   int n, sum = 0, c, var;
 
   printf("Enter the number of integers you want to add\n");
   scanf("%d",&n);
 
   printf("Enter %d numbers\n",n);
 
   for ( c = 1 ; c <= n ; c++ )
   {
      scanf("%d",&var);
      sum = sum + var;
   }
 
   printf("Sum of entered numbers = %d\n",sum);
 
   getch();
   return 0;
}

Add n numbers.

Output:

n numbers

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