Thursday 24 April 2014

IMPORTATNT PROGRAMS OF C LANGUAGE





C language programming is considered to be the basic s of any programming language. so any developer or programmer is assumed to know some basic of C language programming. When we face any interview, the chances are quite high that the interviewer will ask some basic programs of C language because apart from programming skills, it also checks the problem solving skills of an individual.

So here are few important C language programs which are frequently asked in many interviews.

1. FIBONACCI SERIES

/* Fibonacci Series in C language */

#include<stdio.h>  /* header file */

int main()  /*initiate program*/
{
   int n, first = 0, second = 1, next, c;

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

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

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

   return 0;  /*display output*/
}




2. PALINDROME

#include <stdio.h>
#include <string.h>

int main()
{
   char a[100], b[100];

   printf("Enter the string to check if it is a palindrome\n");
   gets(a);

   strcpy(b,a);
   strrev(b);

   if( strcmp(a,b) == 0 )
      printf("Entered string is a palindrome.\n");
   else
      printf("Entered string is not a palindrome.\n");

   return 0;
}


3. ARMSTRONG NUMBER

#include <stdio.h>

int main()
{
   int number, sum = 0, temp, remainder;

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

   temp = number;

   while( temp != 0 )
   {
      remainder = temp%10;
      sum = sum + remainder*remainder*remainder;
      temp = temp/10;
   }

   if ( number == sum )
      printf("Entered number is an armstrong number.\n");
   else
      printf("Entered number is not an armstrong number.\n");

   return 0;
}


4. FACTORIAL OF A NUMBER

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


5. PRIME NUMBER OF A NUMBER

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

No comments:

Post a Comment