Tuesday, November 22, 2011

C Program to Calculate Factorial of a Number

It is a sample c program to calculate factorial of a number.

Program Source Code


#include"stdio.h"
#include"conio.h"
void main()
{
int fact=1,i,n;
clrscr();
printf("\n ***FIND THE FACTORIAL NUMBER OF ANY
NUMBER***");
printf("\n Enter the value of n:--");
scanf("\n %d",&n);
for(i=1;i<=n;i++)
{
fact=fact*i;
printf("\n factorialis:-- %d",fact);
}
getch();
}

Output


Calculate Factorial of a Number

C Program to Check Largest Number in Two Variable

It is a sample C program to check largest number in two variable.

Program Source Code


#include"stdio.h"
#include"conio.h"
void main()
{
int a,b;
clrscr();
printf("***PRINT LARGEST VALUE AMONG TWO OR MORE
NUMBER***");
printf("Enter the value of a:--");
scanf("%d",&a);
printf("Enter the value of b:--")
scanf("%d",&b);
if(a>b)
{
printf("a is greater");
}
else
{
printf("b is greater");
}
getch();
}

Output


Largest Number in Two Variable in C Program

Program in C to Find Even or Odd Number

It is a sample c program to find even or odd number.

Program Source Code


#include"stdio.h"
#include"conio.h"
void main()
{
int n ;
clrscr();
printf("\n ***Check 'even' ** or ** 'odd'**** ");
printf("\n Enter the number to check:-- ");
scanf("%d",&n);
if (n%2==0)
printf("\n Given no is even:--");
else
printf("\n Given no is odd:--");
getch();
}

Output


Even or Odd Number in C Program

C Program to Check String is Palindrome or Not

It is a sample C language example. It is a string palindrome program which accept a string from user.
It checks that accepted string is a palindrome string or not. If string or string reverse is as same as then this string is a palindrome string. It means read string same in both forward or backward direction.



Aim - C program to check whether the accepted string is palindrome or not.


Program Source Code
//C Program to check accepted string is palindrome or not
#include"stdio.h"
#include"conio.h"
int isPallindrome(char *);
void main()
{
  int a;
  char str1[30];
  clrscr();
  printf("\n******String Palindrome******\n");
  printf("Enter a string: ");
  gets(str1); // accept string from user
  a=isPallindrome(str1);  /*function calls, accept string as argument and return flag value*/

  if(a)
   printf("\nstring: %s is a palindrome string",str1);
  else
   printf("\nstring: %s is not a palindrome string",str1);
  getch();

}


int isPallindrome(char *str2)
{
   int i,j,flag=1;

   for(i=0,j=strlen(str2)-1; i<j; i++, j--)
 {
    if(str2[i]!=str2[j]) /*starts checking from ends of string to middle*/
  {
    flag=0; /*if reverse of string is not same than return 0*/
    break;
  }

 }
   return(flag);
}


Output1
******String Palindrome******
Enter a string: MADAM
string: MADAM is a palindrome string

Output2
******String Palindrome******
Enter a string: HELLO
string: HELLO is not a palindrome string