Tuesday, 23 June 2015

C PROGRAM TO FIND HCF OF TWO NUMBERS



#include<stdio.h>
int main()
{
    int a,b;
    printf("Enter two integer values : ");
    scanf("%d %d",&a,&b);
    printf("HCF of %d and %d is ",a , b);
    while(a!=b)
    {
        if(a>b)
            a -= b;
        else
            b -= a;
    }
    printf("%d",a);
    return 0;
}


OUTPUT
Enter two integer values : 23
48
HCF of 23 and 48 is 1

C PROGRAM TO PRINT CHARACTERS FROM A TO Z



#include<stdio.h>
void main()
{
    char a;
    for(a='A'; a<='Z'; ++a)
       printf("%c ",a);
    return 0;
}

OUTPUT
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

C PROGRAM TO FIND L.C.M OF TWO NUMBERS



#include<stdio.h>
int main()
{
    int n1,n2,p1,p2;
    printf("Enter two positive integers: ");
    scanf("%d %d",&n1,&n2);
    p1=n1;
    p2=n2;
    while(p1!=p2)
    {
        if(p1>p2)
            p1-=p2;
        else
            p2-=p1;
    }
    printf("LCM of two numbers %d and %d is %d", n1, n2, (n1*n2)/p1);
    return 0;
}

OUTPUT
Enter two positive integers : 5
6
LCM of two numbers 5 and 6 is 30

C PROGRAM TO FIND FACTORIAL OF A NUMBER



#include <stdio.h>
int main()
{
    int i, count;
    unsigned long long int fc=1;        
    printf("Enter an integer value : ");
    scanf("%d",&i);
    if ( i< 0)
        printf("Error!!! Factorial of negative number doesn't exist.");
    else
    {
       for(count=1;count<=i;++count)  
       {
          fc*=count;            
       }
    printf("Factorial = %lu",fc);
    }
    return 0;
}

OUTPUT
Enter an integer value : 8
Factorial = 40320

C PROGRAM TO CALCULATE SUM OF NATURAL NUMBERS



#include <stdio.h>
int main()
{
    int a, count, sum;
    sum=0;
    printf("Enter an integer value : ");
    scanf("%d",&a);
    for(count=1;count<=a;++count)
    {
        sum+=count;              
    }
    printf("Sum = %d",sum);
    return 0;
}

OUTPUT
Enter an integer value : 5
Sum = 15

C PROGRAM TO FIND WETHER THE GIVEN CHARACTER IS AN ALPHABET OR NOT



#include<stdio.h>
int main()
{
    char ch;
    printf("Enter a character : ");
    scanf("%c",&ch);
    if( (ch>='a'&& ch<='z') || (ch>='A' && ch<='Z'))
       printf("%c is an alphabet.",ch);
    else
       printf("%c is not an alphabet.",ch);
    return 0;
}


OUTPUT
Enter a character : d
d is an alphabet.