Wednesday, 3 February 2016

C program to swapping the two numbers

#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;
}

OUTPUT:
        Enter the value of x and y:
        4
        5
        Before Swapping
        x=4
        y=5
        After Swapping
        x=5
        y=4

Tuesday, 12 January 2016

C program to find Fibonacci series using loop

#include<stdio.h>
#include<conio.h>
 
int 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);
   }
 
   return 0;
}

Output:

       Enter the number of terms:5
       First 5 terms of Fibonacci series are:
       0
       1
       1
       2
       3

Monday, 11 January 2016

C program to convert the Decimal value to Binary value

#include <stdio.h>
 
int main()
{
  int n, c, k;
 
  printf("Enter an integer in decimal number system:");
  scanf("%d", &n);
 
  printf("%d in binary number system is:", n);
 
  for (c = 31; c >= 0; c--)
  {
    k = n >> c;
 
    if (k & 1)
      printf("1");
    else
      printf("0");
  }
 
  printf("\n");
 
  return 0;
}

Output:
       Enter an integer in decimal number system:100
       100 in binary number system is:00000000000000000000000001100100

Friday, 8 January 2016

C Program to find Factorial using loop

#include <stdio.h>
#include <conio.h>
 
int 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);
 
  return 0;
}

Output: 

   Enter a number to calculate it's factorial:6
   Factorial of 6=720