Saturday, 28 April 2012

Check leap year


c program to check leap year: c code to check leap year, year will be entered by the user.

#include<stdio.h>
#include<conio.h>
 
void 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);   
 
      getch();
}

Add digits

                    C program to add digits of a number: Here we are using modulus operator(%) to extract individual digits of number and adding them.
#include<stdio.h>
 void main()
{
   int n, sum = 0, remainder;
   printf("Enter an integer\n");
   scanf("%d",&n);
    while( n != 0 )
   {
      remainder = n % 10;
      sum = sum + remainder;
      n = n / 10;
   }

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

   getch();
}
output :-


Add digits using recursion :-

#include <stdio.h>
#include<conio.h>

int add_digits(int);
 
int main() 
{
  int n, result;
  scanf("%d", &n);
  result = add_digits(n);
  printf("%d\n", result);
  getch();
}
 int add_digits(int n) 
{
  static int sum = 0;
   if (n == 0) 
{
    return 0;
  }
  sum = n%10 + add_digits(n/10);
 return sum;
}

Factorial



       Factorial system in c: c value to discover and list factorial of a variety, three techniques are given, first one uses a for cycle, second uses a operate to discover factorial and third using recursion. Factorial is showed using !, so five factorial will be published as 5!, n factorial as n!. Also
n! = n*(n-1)*(n-2)*(n-3)...3.2.1 and zero factorial is determined as one i.e. 0!=1.


#include<stdio.h>
#include<conio.h>

void 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);

   getch();
 
}

output :-
              Enter a number to it's factorial=6
              Factorial of 6=720


Factorial program in c using function :-

#include<stdio.h>
#include<conio.h>

long factorial(int);

void main()
{
   int number;
   long fact = 1;

   printf("Enter a number to calculate it's factorial\n");
   scanf("%d",&number);

   printf("%d! = %ld\n",number, factorial(number));

   getch();
}

long factorial(int n)
{
   int c;
   long result = 1;

   for( c = 1 ; c <= n ; c++ )
         result= result*c;

   return(result);
}

Factorial program in c using recursion :-

#include<stdio.h>
 #inculde<conio.h>

long factorial(int);

void main()
{
   int num;
   long f;

   printf("ENTER A NUMBER TO FIND FACTORIAL :");
   scanf("%d",&num);

   if(num<0)
      printf("NEGATIVE NUMBERS ARE NOT ALLOWED");
   else
   {
      f = factorial(num);
      printf("%d!=%ld",num,f);
   }
   return(0);
}

long factorial(int n)
{
   if(n==0)
      return(1);
   else
      return(n*factorial(n-1));
}

Add n numbers

                         
                                  This c system add n statistics which will be joined by the individual. First of all individual will get into a variety showing how many statistics individual desires to add and then individual will get into n statistics. In our c system to add statistics we are not using an array, if you wish you can use an array.


#include<stdio.h>
#include<conio.h>

void main()
{
   int n, sum = 0, c, var;

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

   printf("Enter %d numbers\n",n);

   for ( c = 1 ; c <= n ; c++ )
   {
      scanf("%d",&var);
      sum = sum + var;
   }

   printf("Sum of Enter number = %d\n",sum);

   getch();
}

Swaping

          
                          C program to swap two numbers with and without using third variable, swapping in c using pointers and functions (Call by reference) , swapping means interchanging. For example if in your c program you have taken two variable a and b where a = 4 and b = 5, then before swapping a = 4, b = 5 after swapping a = 5, b = 4
In our c program to swap numbers we will use a temp variable to swap two numbers. Swapping is used in sorting that is when we wish to arrange numbers in a particular order either in ascending order or in descending order.


   swaping of two number in c :-


#include<stdio.h>
#include<conio.h>
void main()
{
   int x, y, temp;
   printf("Enter the value of x and y ");
   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);
   getch();
}


Swapping of two numbers without third variable:-

#include<stdio.h>
#include<conio.h>

void main()
{
   int a, b;

   printf("Enter two numbers to swap ");
   scanf("%d%d",&a,&b);

   a = a + b;
   b = a - b;
   a = a - b;
    
printf("a = %d\nb = %d\n",a,b);

   getch();
}

swap two number using pointers :-


#include<stdio.h>
#include<conio.h>
 void main()
{
   int x, y, *a, *b, temp;
   printf("Enter the value of x and y ");
   scanf("%d%d",&x,&y);
   printf("Before Swapping\nx = %d\ny = %d\n", x, y);
   a = &x;
   b = &y;
   temp = *b;
   *b = *a;
   *a = temp;
   printf("After Swapping\nx = %d\ny = %d\n", x, y);
  getch();
}


 swapping number using call by reference:-

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

void main()
{
   int x, y;

   printf("Enter the value of x and y\n");
   scanf("%d%d",&x,&y);

   printf("Before Swapping\nx = %d\ny = %d\n", x, y);

   swap(&x, &y);

   printf("After Swapping\nx = %d\ny = %d\n", x, y);

   getch();
}

void swap(int *a, int *b)
{
   int temp;

   temp = *b;
   *b = *a;
   *a = temp;
}

output :-

Reverse a number

                 C System to opposite a variety :- The offer opposite the variety joined by the individual and then printing the changed variety on the screen. For example if individual get into 123 as feedback then 321 is produced as result. In our program we use modulus(%) owner to obtain the numbers of a variety. To change variety look at it and write it from other or the result of value is a variety acquired by writing unique variety from right to left. To opposite thousands use lengthy information kind or lengthy long information kind if your compiler can handle it, if you still have thousands then use post or other information structure.



#include<stdio.h>
#include<conio.h>

void main()
{
   int n, reverse = 0;

   printf("Enter a number to reverse\n");
   scanf("%d",&n);

   while( n != 0 )
   {
      reverese = reverese * 10;
      reverese = reverese + n%10;
      n = n/10;
   }

   printf("Reverse of enter No. is = %d\n", reverse);

   getch();
}

output:-

Enter the number to reverse
1234
reverse of entered number is=4321

Palindrome numbers

Palindrome number algorithm :-

1. Get the variety from user.
2. Reverse it.
3. Evaluate it with the number entered by the usar.
4. If both are same then list palindrome number
5. Else list not a palindrome number.

Palindrome number program c

#include<stdio.h>
 #include<conio.h>

void main()
{
   int n, reverse = 0, temp;

   printf("Enter a No. 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);

   getch();
}