Powered By Blogger

Wednesday, August 15, 2012

Programs For Placements

// Factorial using Recursion

#include<stdio.h>
#include<conio.h>
long int fact(long int);
void main()
{
 long int n,res;
 clrscr();
 printf("\n Enter the n value:");
 scanf("%ld",&n);
 res=fact(n);
 printf("\n The Result is %ld",res);
 getch();
}
long int fact(long int n)
{
 long int f=1,res;
 if(n==0 || n==1)
          return 1;
 else
         res=n*fact(n-1);
   return res;
}

// Factorial without Recursion

#include<stdio.h>
#include<conio.h>
void main()
{
 int n,fact=1,i;
 clrscr();
 printf("\n Enter the n value:");
 scanf("%d",&n);
 if(n==1 || n==0)
 {
  n=1;
  printf("\n The result is : %d",n);
 }
 for(i=1;i<=n;i++)
                fact=fact*i;
 printf("The Result is =%d",fact);
getch();
}

// Program for Fibonacci series for a=0 and b=1 fixed values

#include<stdio.h>
#include<conio.h>
void main()
{
 int a=0,b=1,c,n,i;
 clrscr();
 printf("\n Enter the range of the series:");
 scanf("%d",&n);
 printf("\n The Series is %d \t %d",a,b);
 for(i=2;i<n;i++)
 {
  c=a+b;
  a=b;
  b=c;
  printf("\t %d \t",c);
 }
 getch();
}

// Program for Fibonacci Series asking a and b values from user

#include<stdio.h>
#include<conio.h>
void main()
{
 int a,b,c,n,i;
 clrscr();
 read:printf("\n Enter the values of a and b:");
 scanf("%d %d",&a,&b);
 if(a==0 && b==0)
 goto read;
 printf("\n Enter the range:");
 scanf("%d",&n);
 printf("The Series is %d\t%d",a,b);
 for(i=2;i<n;i++)
 {
  c=a+b;
  a=b;
  b=c;
  printf("\t%d",c);
 }
getch();
}

// Prime numbers from 2 to N

#include<stdio.h>
#include<conio.h>
void main()
{
 int n,i,j,flag=0;
 clrscr();
 printf("\n Enter the range:");
 scanf("%d",&n);
 for(i=2;i<=n;i++)
 {
  for(j=2;j<=i;j++)
  {
   if(i%j==0)
    flag++;
  }
  if(flag==1)
  printf("\n The Prime nubers are : %d",i);
  flag=0;
 }
 getch();
}

// List of Prime Numbers Between Two Integer values.

#include<stdio.h>
#include<conio.h>
void main()
{
 int m,n,i,j,flag=0;
 clrscr();
 read: printf("\n Enter the range between M and N:");
 scanf("%d %d",&m,&n);

 if(m>n)
 goto read;
 for(i=m;i<=n;i++)
 {
  for(j=2;j<=i;j++)
  {
   if(i%j==0)
    flag++;
  }
  if(flag==1)
  printf("\n The Prime nubers are : %d",i);
  flag=0;
 }
 getch();
}

//Sum of the individual digits. [Eg: 145 – 1+4+5=10]

#include<stdio.h>
#include<conio.h>
void main()
{
 int n,rem,i,sum=0;
 clrscr();
 printf("\n Enter the digit:");
 scanf("%d",&n);
 while(n!=0)
 {
  rem=n%10;
  n=n/10;
  sum=sum+rem;
  }
 printf("The Result is %d",sum);
 getch();
}

// swapping of 2 numbers with temporary (Call by Reference)

#include<stdio.h>
#include<conio.h>
void swap(int *,int *);

void main()
{
 int a,b,c;
 clrscr();
 printf("\n Enter the values of a and b:");
 scanf("%d %d",&a,&b);
 printf("\n \n Before swapping the values are %d \t %d \n",a,b);
 swap(&a,&b);
 printf("\n The After swapping the values are %d \t %d \n",a,b);
 getch();
}
void swap(int *x,int *y)
{
 int t;
 t=*x;
 *x=*y;
 *y=t;
}

//Swapping without temporary variable

#include<stdio.h>
#include<conio.h>
void main()
{
 int a,b;
 clrscr();
 printf("\n Enter the values of a and b:");
 scanf("%d %d",&a,&b);
 printf("\n Before swapping %d and %d\n",a,b);
 a=a+b;
 b=a-b;
 a=a-b;
 printf("\n After swapping %d and %d",a,b);
 getch();
}

// Reversing a Digit Eg: if a = 321 the result should be 123

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
 int num,i,count=0,res,sum=0,mo;
 clrscr();
 printf("\n Enter the number:");
 scanf("%d",&num);
 res=num;
 while(num!=0)
 {
 num=num/10;
 count++;
 }
  for(i=count-1;res!=0;i--)
  {
  mo=res%10;
  sum = sum+(mo*pow(10,i));
  res=res/10;
  }
  printf("Final Result = %d \n \t",sum);
  getch();
 }

// Reversing the digit in another way but it is not the correct one so max avoid this program

#include<stdio.h>
#include<conio.h>
void main()
{
 int a,b;
 printf("Enter the number to be reversed:");
 scanf("%d",&a);
 if(a<0)
  a=-a;
 printf("Reverse number is");
 do
 {
  b=a%10;
  printf("%d\n \t",b);
  a=a/10;
  } while(a>0);
  getch();
 }

More Programs Yet to Come