Tuesday, 30 June 2015

C Program To Find Factorial Of A Number

For those who are looking for an easy program to find out the factorial of a number using C language, Here we have the right answer for you. Just try out our code given below:


#include<stdio.h>
main()
{
    long long int i,j=1,k;
    printf("Enter a number: ");
    scanf("%d", &i);
    for(k=1; k<=i; k++)
    {
        j=j*k;
    }
    printf("The factorial of given value is: %lld", j);
}



For those who are unfamiliar with the term long long int, let me tell you that it is used to hold bigger integer values i.e., up to 15 digits and uses 8 bytes of memory and %lld is the symbol used to use the values. If you face any problem, then do comment and we will be happy to serve you.
Posted on 08:50 | Categories:

Sunday, 28 June 2015

Solution to Project Euler Problem 5 in C

Even I got stuck at one point while solving this problem but finally got it right. So here's what the fifth problem said on Project Euler:

2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder. What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?

And here's the 22 line solution to this:


#include <stdio.h>
main()
{
    long int i,j,a;
    for(i=21; i<999999999; i++)
    {
        a=0;
        for(j=2; j<21; j++)
        {
            if(i%j != 0)
            {
                a=1;
                break;
            }
        }
        if(a==0)
        {
            printf("Number is %ld", i);
            break;
        }

    }
}



It took just 2.007 seconds for the program to execute giving the final answer 232792560.

Solution To Project Euler Problem 3 in C

For those who are unable to solve the third problem of Project Euler which says:


The prime factors of 13195 are 5, 7, 13 and 29.

What is the largest prime factor of the number 600851475143 ?

Here's the simple solution to in C language which merely took  25 lines.

#include <stdio.h>
main()
{
    long long int i,j,b,a=0;
    for(i=3; i<=600851475143/2; i++)
    {
        if(600851475143%i == 0)
        {
            for(j=2; j<=i/2; j++)
            {
                if(i%j == 0)
                {
                    a=1;
                    break;
                }            
            }
            if(a==0)
            {
                b=i;
            }
        }
      
    }
    printf("The sum of all multiples is %lld", b);
}



Although since this program involves large numbers, it too around 1 hour to execute.

The final answer which will come is  6857.

Hope it helps you guys.

Saturday, 27 June 2015

Solution to Project Euler Problem 1 in C

Here's the simple solution of the first problem of Project Euler which said:

If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.

Find the sum of all the multiples of 3 or 5 below 1000.

The code is as follows and took just 13 lines:

#include<stdio.h>
main()
{
    int i,j=0;
    for(i=3; i<1000; i++)
    {
        if(i%3==0 || i%5==0)
        {
            j=j+i;
        }
    }
    printf("The sum of all multiples is %d", j);
}


Execution time was  0.01055 seconds

The answer to this question is 233168.

For any queries, you are free to comment.