Blog Moved

This website completely moved to new domain. For latest content, visit www.programmingposts.com

Search This Blog

17 Aug 2012

C Program to find Factorial of given Number using Function


/* C Program to find Factorial of given Number using Function*/
#include <stdio.h>
//#include<conio.h>
int main()
{
    int num;
    //clrscr();
    printf("\n >> PROGRAM TO FIND FACTORIAL OF GIVEN NUMBER
                   using Function <<\n");
    printf("\n Enter the Number whose Factorial you want: ");
    scanf("%d",&num);
    printf("\n The factorial of %d is %d.\n\n", 
                                 num,factorial(num));
    //factorial(num) prints the value returned by the function
    //getch();
    return 0;

}

int factorial(num1)
{
    int i,fact=1;
    for(i=num1; i>=2 ; i--)
    {
        fact = fact * i;
    }
    return fact; //returning fact to main function
}
---------------------------------------------------------------------------------------------
Sample Output:
( using GNU GCC Compiler with code::blocks IDE, hence no need of clrscr(); and getch(); )


C Program to find the Factorial of Given Number


/** C Program to find the Factorial of Given Number **/
#include <stdio.h>
//#include<conio.h>
int main()
{
  int num,i,fact=1;
  //clrscr();
  printf("\n >> PROGRAM TO FIND FACTORIAL OF GIVEN NUMBER <<\n");
  printf("\n Enter the Number whose Factorial you want: ");
  scanf("%d",&num);
  for(i=num; i>=2 ; i--) //i is intializing with num value and  is decremented till 2
  {
    fact = fact * i; //fact is updating with changing value of i
  }

  printf("\n The factorial of %d is %d.\n\n",num,fact);
  //getch();

  return 0; //indicate end of program to OS

}
---------------------------------------------------
Sample Output:
( using GNU GCC Compiler with code::blocks IDE, hence no need of clrscr(); and getch(); )


C Program to Print Multiplication Table of a given Number


/** C Program to Print Multiplication Table of a given Number **/
#include<stdio.h>
//#include<conio.h>
void main()
{
 int num,i;
 //clrscr();
 printf("\n >>> PROGRAM TO PRINT MULTIPLICATION TABLE <<<");
 printf("\n Enter the Number: ");
 scanf("%d",&num);
 printf("\n %d Multiplication Table",num);
 printf("\n ----------------------");
 for(i=1;i<=10;i++) //printing upto 10..
 {
     printf("\n %d * %d =  %d",num,i,(num*i));
     //the product of entered num and i value
 }
 printf("\n\n");
 //getch();
}

Sample Output:
 ( using GNU GCC Compiler with code::blocks IDE, hence no need of clrscr(); and getch(); )


15 Aug 2012

C Program To Find The Number Palindrome or Not


/* To Find Whether Number is Palindrome or Not */
#include<stdio.h>
//#include<conio.h>
void main()
{
    int num,rem,sum=0,temp;
    //clrscr();
    printf("\n /** To Find Reverse Of a Number **/ \n");
    printf("\n Enter a number: ");
    scanf("%d",&num);
    temp=num;
    while(num)
    {
     rem=num%10;  //for getting remainder by dividing with 10
     num=num/10; //for getting quotient by dividing with 10
     sum=sum*10+rem; /*multiplying the sum with 10 and adding
                           remainder*/
    }
    printf("\n The Reversed Number is: %d \n",sum);
    if(temp==sum) //checking whether the reversed number is
                    equal to entered number
    {
      printf("\n Number is Palindrome \n\n" );
    }
    else
    {
      printf("\n Number is not a palindrome \n\n");
    }
    //getch();
    return 0;
}


Sample Output:( using GNU GCC Compiler with code::blocks IDE, hence no need of clrscr(); and getch(); )



9 Aug 2012

C Program To Find The Reverse Of a Given Number

/** To Find Reverse Of a Number **/
 
#include<stdio.h>
//#include<conio.h>
void main()
{
    int num,rem,sum=0,rev;
    //clrscr();
    printf("\n /** To Find Reverse Of a Number **/ \n");
    printf("\n Enter a number: ");
    scanf("%d",&num);
    while(num)
    {
         rem=num%10;  //for getting remainder by dividing with 10
         num=num/10; //for getting quotient by dividing with 10
         sum=sum*10+rem;
    }
    rev=sum;
    printf("\n The Reversed Number is: %d \n\n",rev);
    //getch();
    return 0;
}




Output: ( using GNU GCC Compiler with code::blocks IDE, hence no need of clrscr(); and getch(); )



7 Aug 2012

C program to print the Prime Numbers Between The two Numbers / Given range

/* c program to print prime numbers between given range */
#include<stdio.h>
//#include<conio.h>
int main()
{

  int i,j,count,min,max;
  //clrscr();
  printf(" Enter min range: ");
  scanf("%d",&min);
  printf(" Enter max range: ");
  scanf("%d",&max);
  if(min==0) //since zero is not a prime number
    min=min+1;
  printf("\n The Prime Numbers Between %d And %d 

           are: \n",min,max);
  for(i = min;i<=max;i++)
  {
    count = 0;
    for(j=2;j<=i/2;j++)
    {
      if(i%j==0)
      {
       count++;
       break; //to come out of if loop..
      }
    }
    if(count==0 && i!= 1 ) //since 1 is not a prime number
     printf(" %d",i);
  }
   printf("\n\n");
   //getch();
   return (0);

}

Output: ( using GNU GCC Compiler with code::blocks IDE, hence no need of clrscr(); and getch(); ) 


C program to print Prime Numbers Between 1 and n

/ * c program to print Prime Numbers Between 1 and N * /
#include<stdio.h>
void main()
{
int num,i=1,j,count;
//clrscr();
printf("Enter Num value To Print Prime Numbers between 1 and Num: ");
scanf("%d",&num);
printf("Prime Numbers upto %d :\n \n",num);

while(i<=num)
{
count=0;
for(j=1;j<=i;j++)
{
if(i%j==0) //checking whether num is dvisible by j
count++;
}
if(count==2)  //if num is divisible by 2 numbers,then it is prime
printf("%d ",i);
i++;
}
printf("\n\n");

//getch(); 
}
Output: ( using GNU GCC Compiler with code::blocks IDE, hence no need of clrscr(); and getch(); )



C program to Find the Number is Prime or Not

/ * C program to find the Number is Prime or not-Prime */
#include<stdio.h>
main()
{
 int n,r=1,d=2;
 printf("\n Enter the Number : ");
 scanf("%d",&n);
 if(n==2)
 {
   printf("\n %d is a Prime Number \n",n);
   exit(0); //to exit from program
 }
 r=n%d; /*we have to do this before while also becuuse
 if while condition fails then r=n%d inside while is not executed*/

 while(d<=n/2)
 {
  r=n%d;
  if(r==0)
  break; //if r==0, then to come out of while loop
  d++; //incrementing d value if(r==0) fails..
}
if(r==0||n==1) // since 1 is not a prime number
printf("\n %d is not a Prime Number \n",n);
else
printf("\n %d is a Prime Number \n",n);
return(0);
}


Output: (using GNU GCC Compiler with Code::Blocks Compiler)

6 Aug 2012

C program to find Greatest Of Two Numbers Using Conditional Operator

/* C Program to find Greatest of Two Numbers Using Conditional Operator.. */
#include<stdio.h>
main()
{
 int a,b,c;
 printf("\n Enter two Numbers: ");
 scanf("%d %d",&a,&b);
 c=(a>b)?a:b;  /* (a>b) is true then c=a else c=b */
 printf("\n The Greatest number is : %d",c);
 return(0);
}

Sample Output: ( using GNU GCC Compiler with Code Blocks IDE )

5 Aug 2012

C program to print Fibonacci series upto N th term


/* C program to generate and print Fibonacci series */
#include<stdio.h>
main()
{
int n,i,a=0,b=1,c=0;
printf("Enter Nth term of Fibonacci series : ");
scanf("%d",&n);
printf("\n %d %d ",a,b); //for printing first two numbers
for(i=2;i<n;i++)  // for printing from 3rd number in series
{
 c=a+b;
 a=b;
 b=c;
 printf("%d ",c);
}
 printf("\n\n");
return(0);
}

Output: ( using GNU GCC Compiler with Code Blocks IDE )

2 Aug 2012

C Program to Swap Two Numbers using Temp/Third Variable


/* C Program to Swap Two Numbers using Temp/Third Variable */

#include <stdio.h>

main()
{
   int a, b, temp;

   printf("\n Enter the values of a and b: ");
   scanf("%d %d", &a, &b);  // Taking The values of x and y from user

   printf("\n Before Swapping: \n a = %d b = %d \n",a,b);

   temp = a; //temp stores value of a
   a = b;    //a stores b value
   b = temp; //b stores temp value

   printf("\n After Swapping: \n a = %d b = %d \n",a,b);

   return 0;
}

Output:  ( using GNU GCC Compiler with Code Blocks IDE )

C Program to Add N integers using Array


/* C program to add n integers using array */

PROGRAM:

#include <stdio.h>

int main()
{
   int n,i,value[100];  //declaring value as array whose length is 100
   int sum=0;  // declaring sum to zero , otherwise it stores garbage values

   printf("\n Enter the number of integers you want to add: ");
   scanf("%d", &n);

   printf("\n Enter %d integers: ",n);

   for (i = 1; i <= n; i++)
   {
      scanf("%d",&value[i]);
      sum = sum + value[i];  //adding all entered integers to sum
   }

   printf("\n Sum of entered integers = %d \n",sum);

   return 0;
}

Output:  ( using GNU GCC Compiler with Code Blocks IDE )

C Program to Add n Integers without Array


/* c program to add n integers without using array */ 

PROGRAM:

#include <stdio.h>

int main()
{
   int n,i,value;
   int sum=0;  // declaring sum to zero , otherwise it stores garbage values

   printf("\n Enter the number of integers you want to add: ");
   scanf("%d", &n);

   printf("\n Enter %d integers: ",n);

   for (i = 1; i <= n; i++)
   {
      scanf("%d",&value);
      sum = sum + value;  //adding all entered integers to sum 
   }

   printf("\n Sum of entered integers = %d \n",sum);

   return 0;
}

Output:  ( using GNU GCC Compiler with Code Blocks IDE )


1 Aug 2012

sizeof operator in C

C Program To Find the Size of Data Types using sizeof operator

#include<stdio.h>
void main()
{
  printf("\n size of int is: %d",sizeof(int));
  printf("\n size of float is: %d",sizeof(float));
  printf("\n size of double is: %d",sizeof(double));
  printf("\n size of char is: %d",sizeof(char));
}


Program can also be written using sizeof operator with variables as given below.

#include<stdio.h>
void main()
{
  int i;
  float j;
  double k;
  char l;
  printf("\n size of int variable is: %d",sizeof(i));
  printf("\n size of float variable is: %d",sizeof(j));
  printf("\n size of double variable is: %d",sizeof(k));
  printf("\n size of char variable is: %d",sizeof(l));
}
 



Output: ( using GNU GCC Compiler with Code Blocks IDE )
Note: In borland C or Turbo C compilers size of int is 2.