Thursday, 20 March 2014

/* C program to find hcf  of  2 number */
#include<stdio.h>
#include<conio.h>

int hcf(int a,int b);

int main(){
int a,b;
printf("Enter two numbers to find hcf = ");
scanf("%d%d",&a,&b);
printf("\n\n The HCF of a and b is = %d",hcf(a,b));


}

return 0;
}


int hcf(int a,int b){
int facta,factb;

facta=abs(a);
factb=abs(b);

while(1){

    if(facta==factb) return facta;

    if(facta >factb ){
        facta--;
        while(a%facta)
        facta--;
    }else{
        factb--;
        while(b%factb)
        factb--;
    }

}

}


Tuesday, 18 March 2014

/*
 C code to find  perfect number..

 A no. said to be perfect, if sum of all its factor is equal to twice the number
e.g 6 is perfect no.
because factors are 6,3,2,1  , sum 12 ie. 2 * 6,,
while 4 is not perfect


factor of a number is perfect divisior of that number ..
i.e. at divion no remainder should left

evern no. has finite factors0 ,,

except 1, every no. has at least 2 factors , no itself and 1 ,,

*/


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

 int perfectNo(int n);

 int main(){

 int n;
 printf("Enter no. you want to check for perfect ");
 scanf("%d",&n);
 if(perfectNo(n))
 printf(" \n\n %d is perfect number ",n);
 else
 printf(" \n\n %d is not perfect number ",n);

 return 0;}


 int perfectNo(int n){
 int ff;  //ff means factor finder
 int sumf=0; // hold sum of all the factors

 for(ff=n;ff!=0;ff--){
    if(!(n%ff))   // i.e at true factor is found else not found
    sumf+=ff;
 }

 if((n*2)==sumf)  //  to check no. is perfect or not,,
 return 1;
 return 0;
 }


/* C code to find LCM of 2 numbers ,, by method 1 */

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

main(){
    int a,b;
printf("Enter no. you want lcm");
scanf("%d%d",&a,&b);
printf("\n LCM of a and b is = %d",lcm(a,b));
return 0;
}

int lcm(int a,int b){
        int tempa,tempb;
        a=abs(a); // to ignore negative sign ,,
        b=abs(b);

        tempa=a;
        tempb=b;


        // if one of input zero(0) then lcm is always 0

        if(a==0 || b==0)
        return 0;

        while(1){      // here is main logic,,  read and get it,,
            if(tempa==tempb) return tempa;

            if(tempa<tempb)
                tempa+=a;
                else
                tempb+=b;

        }
}