Saturday, December 28, 2013

C Program to find HCF/GCD of two numbers

Definition of HCF (Highest common factor):

HFC is also called greatest common divisor (gcd). HCF of two numbers is a largest positive numbers which can divide both numbers without any remainder.  For example HCF of two numbers 4 and 8 is 2 since 2 is the largest positive number which can dived 4 as well as 8 without a remainder. 

Logic of HCF or GCD of any two numbers:

In HCF we try to find any largest number which can divide both the number.
For example: HCF or GCD of 20 and 30
Both number 20 and 30 are divisible by 1, 2,5,10.

HCF=max (1, 2, 3, 4, 10) =10

#include<stdio.h>
int main(){
    int x,y,m,i;
    printf("Insert any two number: ");
    scanf("%d%d",&x,&y);
    if(x>y)
         m=y;
    else
         m=x;
    for(i=m;i>=1;i--)
    {
         if(x%i==0 && y%i==0)
        {
             printf("\nHCF of two number is : %d",i) ;
             break;
         }
    }
    return 0;
}

LCM program in c with two numbers:

Definition of LCM (Least common multiple):

LCM of two integers is a smallest positive integer which is multiple of both integers that it is divisible by the both of the numbers.
For example: LCM of two integers 2 and 5 is 10 since 10 is the smallest positive numbers which is divisible by both 2 and 5.

#include<stdio.h>
int main(){
  int n1,n2,x,y;
  printf("\nEnter two numbers:");
  scanf("%d %d",&n1,&n2);
  x=n1,y=n2;
  while(n1!=n2)
  {
      if(n1>n2)
           n1=n1-n2;
      else
           n2=n2-n1;
  }
  printf("L.C.M=%d",x*y/n1);
  return 0;
}

Write a c program to check whether a number is strong or not

Definition of strong number:

A number is called strong number if sum of the factorial of its digit is equal to number itself. For example: 145 since

1! + 4! + 5! = 1 + 24 + 120 = 145

#include<stdio.h>
int main(){
  int num,i,f,r,sum=0,temp;

  printf("Enter a number: ");
  scanf("%d",&num);
 
  temp=num;
  while(num){
      i=1,f=1;
      r=num%10;

      while(i<=r){
         f=f*i;
        i++;
      }
      sum=sum+f;
      num=num/10;
  }
  if(sum==temp)
      printf("%d is a strong number",temp);
  else
      printf("%d is not a strong number",temp);

  return 0;
}

Sample output:
Enter a number: 145
145 is a strong number

C program for prime numbers between 1 to n

#include<stdio.h>

int main(){

    int num,i,count,n;
    printf("Enter max range: ");
    scanf("%d",&n);
    for(num = 1;num<=n;num++)
    {
         count = 0;
         for(i=2;i<=num/2;i++)
           {
             if(num%i==0)
              {
                 count++;
                 break;
              }
        }
         if(count==0 && num!= 1)
             printf("%d ",num);
    }
  
   return 0;
}

Sample output:
Enter max range: 50

2 3 5 7 11 13 17 19 23 29 31 37 41 43 47

WACP to find whether number is prime or not.

Logic for prime number in c

We will take a loop and divide number from 2 to number/2. If the number is not divisible by any of the numbers then we will print it as prime number.


Example of prime numbers : 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199 etc.

int main(){
    int num,i,count=0;
    printf("Enter a number: ");
    scanf("%d",&num);
    for(i=2;i<=num/2;i++)
    {
        if(num%i==0)
        {
         count++;
         break;
        }
    }
   if(count==0 && num!= 1)
        printf("%d is a prime number",num);
   else
      printf("%d is not a prime number",num);
   return 0;
}

Wednesday, December 25, 2013

Write a C Program to find perfect number.

Definition of perfect number or what is perfect number? 

Perfect number is a positive number which sum of all positive divisors excluding that number is equal to that number. For example 6 is perfect number since divisor of 6 are 1, 2 and 3.  Sum of its divisor is
1 + 2+ 3 =6

Note: 6 is the smallest perfect number.

Next perfect number is 28 since 1+ 2 + 4 + 7 + 14 = 28
Some more perfect numbers: 496, 8128


#include<stdio.h>
int main(){
  int n,i=1,sum=0;

  printf("Enter a number: ");
  scanf("%d",&n);

  while(i<n)
  {
      if(n%i==0) 
           sum=sum+i;
          i++;
  }
  if(sum==n)
      printf("%d is a perfect number",n);
  else
      printf("%d is not a perfect number",n);

  return 0;
}


Sample output:
Enter a number: 6

6 is a perfect number

Sum of Fibonacci series in c

#include<stdio.h>
int main(){
    int k,r;
    long int i=0,j=1,f;
    long int sum = 1;

    printf("Enter the number range: ");
    scanf("%d",&r);

    for(k=2;k<r;k++)
   {
         f=i+j;
         i=j;
         j=f;
         sum = sum + j;
    }

    printf("Sum of Fibonacci series is: %ld",sum);
  
    return 0;
}

Sample output:
Enter the number range: 4

Sum of Fibonacci series is: 4

Write a C program to print n terms of Fibonacci series

Definition of Fibonacci numbers:
We assume first two Fibonacci are 0 and 1
A series of numbers in which each sequent number is sum of its two previous numbers is known as Fibonacci series and each numbers are called Fibonacci numbers. So Fibonacci numbers is

Algorithm for Fibonacci series 

Fn = Fn-2 + Fn-1

Example of Fibonacci series:

0 , 1 ,1 , 2 , 3 , 5 , 8 , 13 , 21 , 34 , 55  ...

#include<stdio.h>
int main(){
    int k,r;
    long int i=0,j=1,f;

    //Taking maximum numbers form user
    printf("Enter the number range:");
    scanf("%d",&r);

    printf("FIBONACCI SERIES: ");
    printf("%ld %ld",i,j); //printing first two values.

    for(k=2;k<r;k++)
    {
         f=i+j;
         i=j;
         j=f;
         printf(" %ld",j);
    }
  
    return 0;
}

Sample output:
Enter the number range: 15

FIBONACCI SERIES: 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377

Write a C program to reverse a number

Description-This program reverse the number entered by the user and then prints the reversed number on the screen.
For example if user enter 123 as input then 321 is printed as output. In our program we use modulus (%) operator to obtain the digits of a number. To invert number look at it and write it from opposite direction or the output of code is a number obtained by writing original number from right to left. To reverse or invert large numbers use long data.

Source Code:
#include <stdio.h>
int main()
{
int n,rem, reverse = 0;
printf("Enter a number to reverse\n");
scanf("%d",&n);
while (n != 0)
{
rem=n%10;
reverse = reverse *10+ rem;
n = n/10;
}
printf("Reverse of entered number is = %d\n", reverse);
return 0;

}

Program to display even numbers between 1-20

Source Code:
#include<stdio.h>
#include<conio.h>
int main( )
{
 int i;
 clrscr( );
 printf("\n The even numbers are:");
 for(i=1;i<=20;i++)
 {
if(i%2==0)
{
 printf("\n\t%d",i);
}
 }
getch();
return 0;

Tuesday, December 24, 2013

Write a C program to find Armstrong number

Description-Armstrong number c program: c programming code to check whether a number is armstrong or not. A number is Armstrong if the sum of
cubes of individual digits of a number is equal to the number itself. For example 371 is an armstrong number as 33 + 73 + 13 = 371. Some
other armstrong numbers are: 0, 1, 153, 370, 407.

Source Code:
#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;

}

Write a C program to find and print factorial of a number

Source Code:
#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;

}

Write a C program to add digits of a number

Description-Here we are using modulus operator (%) to extract individual digits of number and adding them.

Source Code:
#include <stdio.h>
int 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);
return 0;

}

Write a C program to check whether input alphabet is a vowel or not (Using switch)

Source Code:
 #include<stdio.h>
 #include<conio.h>

int main( )
{
char ch;
clrscr( );
printf("\n Enter a character::");
scanf("%c",&ch);
switch(ch)
{
case 'a':
case 'A': printf("\n vowel entered");
 break;
case 'e':
case 'E': printf("\nvowel entered");
 break;
 case 'i':
 case 'I': printf("\n vowel entered");
break;
 case 'o':
 case 'O': printf("\n vowel entered");
break;
 case 'u':
 case 'U': printf("\n vowel entered");
 break;
default: printf("\n Consonant entered");
 }
getch();
return 0;

Write a C program to Check vowel using switch statement

Source Code:
#include <stdio.h>
int main()
{
char ch;
printf("Input a character\n");
scanf("%c", &ch);
switch(ch)
{
case 'a':
case 'A':
case 'e':
case 'E':
case 'i':
case 'I':
case 'o':
case 'O':
case 'u':
case 'U':
printf("%c is a vowel.\n", ch);
break;
default:
printf("%c is not a vowel.\n", ch);
}
return 0;

}

Write a C program to Check vowel using switch statement

Source Code:
#include <stdio.h>
int main()
{
char ch;
printf("Input a character\n");
scanf("%c", &ch);
switch(ch)
{
case 'a':
case 'A':
case 'e':
case 'E':
case 'i':
case 'I':
case 'o':
case 'O':
case 'u':
case 'U':
printf("%c is a vowel.\n", ch);
break;
default:
printf("%c is not a vowel.\n", ch);
}
return 0;

}

Program to calculate the net salary of a person

Source Code:
#include<conio.h>
#include<stdio.h>
int main()
{
int b=0,s,H,D,t;
clrscr();
printf("\n enter the basic salary:");
scanf("%d",&b);
printf("\n the basic salary is:%d",b);
if(b<10000)
{
 H=(b*15)/100;
 D=(b*85)/100;
 t=(b*12)/100;
 s=b+H+D+t;
 printf("\n the net salary is :%d",s);
}
else
{
 H=(b*20)/100;
 D=(b*92)/100;
 t=(b*22)/100;
 s=b+H+D+t;
 printf("\n the net salary is :%d",s);
 }
getch();
return 0;

Program to find the largest of three numbers using Logical AND (&&)

#include<conio.h>
#include<stdio.h>
int main()
{
 int a=0,b=0,c=0;
printf("\n enter the numbers:");
scanf("%d%d%d",&a,&b,&c);
if ( a>b && a> c)
     printf("\n %d is largest:",a);
else if ( b>a && b> c)
     printf("\n %d is largest:",b);
else
     printf("\n %d is largest:",c);
getch();
return 0;

Program to find the smallest number without using logical AND

Source Code:
#include<stdio.h>
#include<conio.h>
int main()
{
int a,b,c,d,e,s;
printf("\n enter the five numbers:");
scanf("%d%d%d%d%d",&a,&b,&c,&d,&e);
s=a;
if(s>b)
s=b;
else if(s>c)
s=c;
else if(s>d)
s=d;
else if(s>e)
s=e;
printf("\n the smallest number is:%d",s);
getch();
return 0;

Program to find the largest of three numbers

Source Code:
#include<conio.h>
#include<stdio.h>
int main()
{int a=0,b=0,c=0;
printf("\n enter the numbers:");
scanf("%d%d%d",&a,&b,&c);
if(a>b)
{
if(b>c)
 printf("\n %d is largest:",a);
 else
 printf("\n %d is largest:",c);
}
else if(b>c)
printf("\n %d is largest:",b);
else
printf("\n %d is largest:",c);
getch();
return 0;
}