google.com, pub-4617457846989927, DIRECT, f08c47fec0942fa0 Learn to enjoy every minute of your life.Only I can change my life.: January 2015

Thursday, January 1, 2015

C Programs



1.C program to check odd or even using modulus operator

#include<stdio.h>
main()
{
   int n;
   printf("Enter an integer\n");
   scanf("%d",&n);
    if ( n%2 == 0 )
      printf("Even\n");
   else
      printf("Odd\n");
    return 0;
}


2. Fibonacci Series c language

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


3.Palindrome number program c

#include <stdio.h>
 int main()
{
   int n, reverse = 0, temp;
   printf("Enter a number to check if it is a palindrome or not\n");
   scanf("%d",&n);
    temp = n;
    while( temp != 0 )
   {
      reverse = reverse * 10;
      reverse = reverse + temp%10;
      temp = temp/10;
   }

   if ( n == reverse )
      printf("%d is a palindrome number.\n", n);
   else
      printf("%d is not a palindrome number.\n", n);

   return 0;
}


4.C program to check leap year

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


5.Prime number program in c language

#include<stdio.h>
 int main()
{
   int n, i = 3, count, c;
    printf("Enter the number of prime numbers required\n");
   scanf("%d",&n);

   if ( n >= 1 )
   {
      printf("First %d prime numbers are :\n",n);
      printf("2\n");
   }

   for ( count = 2 ; count <= n ;  )
   {
      for ( c = 2 ; c <= i - 1 ; c++ )
      {
         if ( i%c == 0 )
            break;
      }
      if ( c == i )
      {
         printf("%d\n",i);
         count++;
      }
      i++;
   }
    return 0;
}


6.C program for prime number or not

#include<stdio.h>
main()
{
   int n, c = 2;
    printf("Enter a number to check if it is prime\n");
   scanf("%d",&n);

   for ( c = 2 ; c <= n - 1 ; c++ )
   {
      if ( n%c == 0 )
      {
         printf("%d is not prime.\n", n);
     break;
      }
   }
   if ( c == n )
      printf("%d is prime.\n", n);
    return 0;
}


7.C program to reverse 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;
}



8.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;
}


9.Factorial program in c using for loop

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


10. Print
    *
  ***
*****
  ***
    *

#include <stdio.h>
int main()
{
  int n, c, k, space = 1;
   printf("Enter number of rows\n");
  scanf("%d", &n);
   space = n - 1;
   for (k = 1; k <= n; k++)
  {
    for (c = 1; c <= space; c++)
      printf(" ");
     space--;
     for (c = 1; c <= 2*k-1; c++)
      printf("*");
     printf("\n");
  }
   space = 1;
   for (k = 1; k <= n - 1; k++)
  {
    for (c = 1; c <= space; c++)
      printf(" ");
     space++;
     for (c = 1 ; c <= 2*(n-k)-1; c++)
      printf("*");
     printf("\n");
  }
   return 0;
}

अच्छे विचार करे विचार

  पहचान की नुमाईश, जरा कम करें... जहाँ भी "मैं" लिखा है, उसे "हम" करें... हमारी "इच्छाओं" से ज़्यादा "सुन...