C Program To Find Prime Factors of a Number

A positive integer is entered through the keyboard. Write a function to obtain the prime factors of this number.

For Example: Prime factors of 24 are 2, 2, 2 and 3. Whereas prime factors of 35 are 5 and 7.

Related Read:
C Program to Find Factors of a Number
C Program To Find Prime Number or Not using While Loop

Factors of a Number: All the numbers which perfectly divide a given number are called as Factors of that number.

Prime Number: Any natural number which is greater than 1 and has only two factors i.e., 1 and the number itself is called a prime number.

Note: The user entered number need not be a prime number. But the factors of the number must be prime number.

For example, 24 and 35 are not prime numbers. But the prime factors of 24 are 2, 2, 2, and 3. Here both 2 and 3 are prime numbers. Similarly, prime factors of 35 are 5 and 7. Both 5 and 7 are prime numbers.

Video Tutorial: C Program To Find Prime Factors of a Number using Function


[youtube https://www.youtube.com/watch?v=QAxnHPxicgY]

YouTube Link: https://www.youtube.com/watch?v=QAxnHPxicgY [Watch the Video In Full Screen.]


Source Code: C Program To Find Prime Factors of a Number using Function

#include<stdio.h>

void primefactors(int num)
{
    int count;

    printf("\nPrime Factors of %d are ..\n", num);
    for(count = 2; num > 1; count++)
    {
        while(num % count == 0)
        {
            printf("%d ", count);
            num = num / count;
        }
    }
    printf("\n");
}

int main()
{
    int num;

    printf("Enter a positive integer\n");
    scanf("%d", &num);

    primefactors(num);

    return 0;
}

Output 1:
Enter a positive integer
24

Prime Factors of 24 are ..
2 2 2 3

Output 2:
Enter a positive integer
35

Prime Factors of 35 are ..
5 7

Output 3:
Enter a positive integer
315

Prime Factors of 315 are ..
3 3 5 7

Output 4:
Enter a positive integer
24024

Prime Factors of 24024 are ..
2 2 2 3 7 11 13

Output 5:
Enter a positive integer
510

Prime Factors of 510 are ..
2 3 5 17

Logic To Find Prime Factors of a Number, using Function

We ask the user to enter a positive integer number and store it inside variable num. We pass this value to a function primefactors().

Inside primefactors() function we write a for loop. We initialize the loop counter to 2 – which is the smallest prime number. We exit the for loop when num is less than or equal to 1.

Inside for loop we write a while loop to check if the number is perfectly divisible by the value of count. If yes, then we display the value of count and divide the num by count and store it back into variable num.

Since we continuously modulo divide and divide the number by 2, other numbers(which are multiples of 2) can not divide the number. Similarly, we divide the number by 3, so other numbers(which are multiples of 3) can not divide the number. Next we divide the number by 5, 7, 11, etc.

Note: Function primefactors() doesn’t return anything so its return type is void. It accepts 1 integer type argument.

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

C Program To Calculate One Number Raised To Another using Function

Write a function power( a, b ), to calculate the value of a raised to b.

Note: In today’s video tutorial lets see 2 methods of calculating value of a raised to b.
1. In first method lets write the entire logic ourselves.
2. In second method lets use the built in method pow() which is present in math.h library file.

Problem Statement

To clarify, the problem statement is: User inputs two numbers through the keyboard. Write a C program to find the value of one number raised to the power of another, using function / method.

Related Read:
Function / Methods In C Programming Language
Calculate Power of a Number: C Program
Calculate Power of a Number using pow(): C Program

Video Tutorial: C Program To Calculate One Number Raised To Another using Function


[youtube https://www.youtube.com/watch?v=NN_m-I_2auE]

YouTube Link: https://www.youtube.com/watch?v=NN_m-I_2auE [Watch the Video In Full Screen.]


Source Code: C Program To Calculate One Number Raised To Another using Function

#include<stdio.h>

int power(int base, int exp)
{
    int count, result = 1;

    for(count = 1; count <= exp; count++)
    {
        result = result * base;
    }

    return(result);
}

int main()
{
    int a, b;

    printf("Enter integer values for base and exponent\n");
    scanf("%d%d", &a, &b);

    printf("%d to the power of %d is %d\n", a, b, power(a, b));

    return 0;
}

Output 1:
Enter integer values for base and exponent
2
4
2 to the power of 4 is 16

Output 2:
Enter integer values for base and exponent
3
2
3 to the power of 2 is 9

Output 3:
Enter integer values for base and exponent
5
2
5 to the power of 2 is 25

Output 4:
Enter integer values for base and exponent
5
3
5 to the power of 3 is 125

Logic

In above code, we ask the user to enter base and exponent values. We pass these 2 values to function power(). Inside power() function/method, we use for loop to multiply the value of base exponent number of times. This would give us the result and we return the result.

Example: In above code, if the user inputs 2 as base and 4 as exponent, then: for loop executes or iterates exponent number of times i.e., 4 times in our case. So 2 is multiplied 4 times.

2 x 2 x 2 x 2 = 16.

Source Code: C Program To Calculate One Number Raised To Another using Function and builtin method pow()

#include<stdio.h>
#include<math.h>

float power(int base, int exp)
{
    return( pow(base, exp) );
}

int main()
{
    int a, b;

    printf("Enter integer values for base and exponent\n");
    scanf("%d%d", &a, &b);

    printf("%d to the power of %d is %0.2f\n", a, b, power(a, b));

    return 0;
}

Output 1:
Enter integer values for base and exponent
2
4
2 to the power of 4 is 16

Output 2:
Enter integer values for base and exponent
3
2
3 to the power of 2 is 9

Output 3:
Enter integer values for base and exponent
5
2
5 to the power of 2 is 25

Output 4:
Enter integer values for base and exponent
5
3
5 to the power of 3 is 125

Logic

Here we ask the user to enter the value of base and exponent. We pass the values of base and exponent to power() method. Inside power() method we use pow() builtin method which is present in math.h header file. We pass base and exponent value to pow() function and it returns the result, which we return to calling function i.e., main method.

Note:
pow() method is present in math.h library, so we include it at the top of our c source code.

Important Note: Note that we are using float as return type in above program(second method), since built in method pow() returns floating point or double type data. If we use integer type data as return type then it would give wrong results for certain inputs.

For example, if you have below code for power() function

int power(int base, int exp)
{
    return( pow(base, exp) );
}

If would return 24 for 5 raise to 2. And 124 as result for 5 raise to 3. Both these results are wrong.

Important Note: As you might have observed already we are not using function prototype in above programs. That is because we are writing function definition at the top before main function from where we are calling power() function. So main method already knows that there is a method called power() in our program.

If we want to write more than 1 function and we need to arrange it properly, we can write function prototype at the top – so that we can know about the functions present in our program in one glance, in one place. And then we can write the function definition anywhere in our program, in any order. Function prototype and function definition orders do not matter.

Also note that, writing function prototype is optional if you write function definition before main.

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

C Program To Find Factorial of a Number using Function

Write a function to calculate the factorial value of any integer entered through the keyboard.

Related Read:
C Program To Find Factorial of a Number

Factorial of a number is the product of all the numbers preceding it. For example, Factorial of 6 is 720 (1 x 2 x 3 x 4 x 5 x 6 = 720).

In general, n objects can be arranged in n(n – 1)(n – 2) … (3)(2)(1) ways. This product is represented by the symbol n!, which is called n factorial. By convention, 0! = 1.

Video Tutorial: C Program To Find Factorial of a Number using Function


[youtube https://www.youtube.com/watch?v=OcXCoX51Xx4]

YouTube Link: https://www.youtube.com/watch?v=OcXCoX51Xx4 [Watch the Video In Full Screen.]


Source Code: C Program To Find Factorial of a Number using Function

#include<stdio.h>

void factorial(int);

int main()
{
    int num;

    printf("Enter a positive number to find Factorial\n");
    scanf("%d", &num);

    factorial(num);

    return 0;
}

void factorial(int num)
{
    int count, fact = 1;

    if(num == 0)
    {
        printf("Factorial of 0 is 1 (!0 = 1)\n");
    }
    else
    {
        for(count = 1; count <= num; count++)
        {
            fact = fact * count;
        }

        printf("\nFactorial of %d is %d (!%d = %d)\n", num, fact, num, fact);
    }
}

Output 1:
Enter a positive number to find Factorial
5

Factorial of 5 is 120 (!5 = 120)

Output 2:
Enter a positive number to find Factorial
4

Factorial of 4 is 24 (!4 = 24)

Output 3:
Enter a positive number to find Factorial
6

Factorial of 6 is 720 (!6 = 720)

Output 4:
Enter a positive number to find Factorial
7

Factorial of 7 is 5040 (!7 = 5040)

Output 5:
Enter a positive number to find Factorial
8

Factorial of 8 is 40320 (!8 = 40320)

Logic To Find Factorial of a Number

Complete for loop logic to find Factorial of a number is present at C Program To Find Factorial of a Number. Watch the video without fail to understand the logic.

Note: Function factorial doesn’t return anything so its return type is void. It accepts 1 integer type argument.

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

C Program To Generate Fibonacci Series using Function

Lets write a C program to generate Fibonacci Series using function / method.

Related Read:
Fibonacci Series using While loop: C Program
C Program To Generate Fibonacci Series using For Loop

What Is Fibonacci Series ?
Fibonacci Series is a series of numbers where the first two Fibonacci numbers are 0 and 1, and each subsequent number is the sum of the previous two. Its recurrence relation is given by Fn = Fn-1 + Fn-2.

Below are a series of Fibonacci numbers(10 numbers):
0
1
1
2
3
5
8
13
21
34

How Its Formed:
0 <– First Number (n1)
1 <– Second Number (n2)
1 <– = 1 + 0 (previous two numbers)
2 <– = 1 + 1 (previous two numbers)
3 <– = 2 + 1 (previous two numbers)
5 <– = 3 + 2 (previous two numbers)
8 <– = 5 + 3 (previous two numbers)
13 <– = 8 + 5 (previous two numbers)
21 <– = 13 + 8 (previous two numbers)
34 <– = 21 + 13 (previous two numbers)

Video Tutorial: C Program To Generate Fibonacci Series using Function


[youtube https://www.youtube.com/watch?v=R3MPbqvYooA]

YouTube Link: https://www.youtube.com/watch?v=R3MPbqvYooA [Watch the Video In Full Screen.]


Source Code: C Program To Generate Fibonacci Series using Function

#include<stdio.h>

void fibonacci(int);

int main()
{
    int limit;

    printf("Enter the number of terms to be printed\n");
    scanf("%d", &limit);

    fibonacci(limit);

    return 0;
}

void fibonacci(int num)
{
    int n1 = 0, n2 = 1, n3, count;

    printf("\nFibonacci Series ..\n");
    printf("1. %d\n2. %d\n", n1, n2);

    for(count = 3; count <= num; count++)
    {
        n3 = n1 + n2;
        printf("%d. %d\n", count, n3);

        n1 = n2;
        n2 = n3;
    }
}

Output 1:
Enter the number of terms to be printed
5

Fibonacci Series ..
1. 0
2. 1
3. 1
4. 2
5. 3

Output 2:
Enter the number of terms to be printed
14

Fibonacci Series ..
1. 0
2. 1
3. 1
4. 2
5. 3
6. 5
7. 8
8. 13
9. 21
10. 34
11. 55
12. 89
13. 144
14. 233

Logic To Generate Fibonacci Series using Function

We ask the user to enter the limit i.e., how many terms in the Fibonacci series to be printed. We pass this user entered limit to function fibonacci.

Inside fibonacci function
We initialize n1 to 0 and n2 to 1 and display it to the console. We do this because we know that in any Fibonacci series the first two numbers are 0 and 1.

Now we add n1 and n2 and assign it to n3, and display the value of n3 – which is the next number in the Fibonacci series. We use for loop to keep printing the Fibonacci series until the limit entered by the user.

Note: Function fibonacci takes 1 integer type argument and doesn’t return anything, so its return type is void.

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

C Program To Find Armstrong Numbers Between Range using Function

In today’s video tutorial lets find all the Armstrong numbers or Narcissistic numbers between user entered range, using function / method.

An Armstrong number or Narcissistic number is an n-digit base b number such that the sum of its (base b) digits raised to the power n is the number itself.

Example 1:
If number = 370
It has 3 digits: 3, 7 and 0. So n = 3.
result = 33 + 73 + 03 = 27 + 343 + 0 = 370.
So the original number 370 is equal to the result. So it’s an Armstrong Number.

Example 2:
If number = 8208
It has 4 digits: 8, 2, 0 and 8. So n = 4.
result = 84 + 24 + 04 + 84 = ‭4096‬ + 16 + 0 + ‭4096‬ = 8208.
So the original number 8208 is equal to the result. So it’s an Armstrong Number.

Related Read:
Swap 2 Numbers Using a Temporary Variable: C
Function / Methods In C Programming Language
C Program to Check Armstrong Number
C Program to print Armstrong Numbers Between Two Integers

Video Tutorial: C Program To Find Armstrong Numbers Between Range using Function


[youtube https://www.youtube.com/watch?v=nsVcZxEvIc8]

YouTube Link: https://www.youtube.com/watch?v=nsVcZxEvIc8 [Watch the Video In Full Screen.]


To count number of digits in a number

    int n = 0, temp;

    temp = num;

    while(temp)
    {
        temp = temp / 10;
        n++;
    }

To Iterate through the digits in the number

        while(num)
        {
            rem = num % 10;
            sum = sum + pow(rem, n);
            num = num / 10;
        }

To know above code logic, please visit and watch the video tutorial present at C Program to Check Armstrong Number.

Full Source Code: C Program To Find Armstrong Numbers Between Range using Function

#include<stdio.h>
#include<math.h>

float armstrong(int);

int main()
{
    int count, start, end, temp;

    printf("Enter start and end values\n");
    scanf("%d%d", &start, &end);

    if(start > end)
    {
        temp = start;
        start= end;
        end  = temp;
    }

    printf("Armstrong numbers between %d and %d are\n", start, end);

    for(count = start; count <= end; count++)
    {
        if(count == armstrong(count))
        {
            printf("%d is an Armstrong number\n", count);
        }
    }

    return 0;
}

float armstrong(int num)
{
    int rem, n = 0, temp;
    float sum = 0.0;

    temp = num;

    while(temp)
    {
        temp = temp / 10;
        n++;
    }

    while(num)
    {
        rem = num % 10;
        sum = sum + pow(rem, n);
        num = num / 10;
    }

    return(sum);
}

Output 1:
Enter start and end values
1
999
Armstrong numbers between 1 and 999 are
1 is an Armstrong number
2 is an Armstrong number
3 is an Armstrong number
4 is an Armstrong number
5 is an Armstrong number
6 is an Armstrong number
7 is an Armstrong number
8 is an Armstrong number
9 is an Armstrong number
153 is an Armstrong number
370 is an Armstrong number
371 is an Armstrong number
407 is an Armstrong number

Output 2:
Enter start and end values
500
99999
Armstrong numbers between 500 and 99999 are
1634 is an Armstrong number
8208 is an Armstrong number
9474 is an Armstrong number
54748 is an Armstrong number
92727 is an Armstrong number
93084 is an Armstrong number

Output 3:
Enter start and end values
1
99999
Armstrong numbers between 1 and 99999 are
1 is an Armstrong number
2 is an Armstrong number
3 is an Armstrong number
4 is an Armstrong number
5 is an Armstrong number
6 is an Armstrong number
7 is an Armstrong number
8 is an Armstrong number
9 is an Armstrong number
153 is an Armstrong number
370 is an Armstrong number
371 is an Armstrong number
407 is an Armstrong number
1634 is an Armstrong number
8208 is an Armstrong number
9474 is an Armstrong number
54748 is an Armstrong number
92727 is an Armstrong number
93084 is an Armstrong number

Output 4:
Enter start and end values
1
500
Armstrong numbers between 1 and 500 are
1 is an Armstrong number
2 is an Armstrong number
3 is an Armstrong number
4 is an Armstrong number
5 is an Armstrong number
6 is an Armstrong number
7 is an Armstrong number
8 is an Armstrong number
9 is an Armstrong number
153 is an Armstrong number
370 is an Armstrong number
371 is an Armstrong number
407 is an Armstrong number

Logic To Find Armstrong Numbers Between Range using Function

We ask the user to enter start and end value i.e., the range. Using for loop we iterate through all the numbers between start and end. For each iteration of for loop, variable count holds the number which we need to check if its Armstrong number or not.

Value of count is passed to function armstrong. Function armstrong counts the number of digits present in the integer number(we store it inside variable n) and then multiplies all the individual digits of the number by n and adds them. The sum is returned back to the calling function.

If value present in count and the value returned by armstrong method are equal, then the value present in count is an Armstrong number and will be displayed on the console window.

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert