C Program To Print Multiplication Table Using Function

Lets write a C program to print the multiplication table for a user input number, using function/method. The table should get displayed in the following format:

Example: Multiplication table of 5
5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50

Coding Challenge on Numbers!
Print the multiplication table which gives the following output:

multiplication table for number 37

Hint: Initialize count to 3. Increment the value of count by 3 for each iteration of for loop.

Answer: We’ve posted the source code to above coding challenge at the end of this blog post.

Related Read:
C Program To Print Multiplication Table Using For Loop
C Program To Print Multiplication Table Using While Loop

Video Tutorial: C Program To Print Multiplication Table Using Function


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

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


Source Code: C Program To Print Multiplication Table Using Function

#include<stdio.h>

void tables(int);

int main()
{
    int num;

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

    printf("\nMultiplication Table for %d is:\n", num);

    tables(num);

    return 0;
}

void tables(int num)
{
    int count;

    for(count = 1; count <= 10; count++)
    {
        printf("%d x %d = %d\n", num, count, num*count);
    }
}

Output 1:
Enter a positive number
5

Multiplication Table for 5 is:
5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50

Output 2:
Enter a positive number
2

Multiplication Table for 2 is:
2 x 1 = 2
2 x 2 = 4
2 x 3 = 6
2 x 4 = 8
2 x 5 = 10
2 x 6 = 12
2 x 7 = 14
2 x 8 = 16
2 x 9 = 18
2 x 10 = 20

Output 3:
Enter a positive number
7

Multiplication Table for 7 is:
7 x 1 = 7
7 x 2 = 14
7 x 3 = 21
7 x 4 = 28
7 x 5 = 35
7 x 6 = 42
7 x 7 = 49
7 x 8 = 56
7 x 9 = 63
7 x 10 = 70

Output 4:
Enter a positive number
9

Multiplication Table for 9 is:
9 x 1 = 9
9 x 2 = 18
9 x 3 = 27
9 x 4 = 36
9 x 5 = 45
9 x 6 = 54
9 x 7 = 63
9 x 8 = 72
9 x 9 = 81
9 x 10 = 90

Logic: Multiplication Table

User enters a positive number, we store it inside variable num. We pass this number entered by the user to a function called tables().

Inside tables() function we write a for loop. We initialize the for loop counter variable count to 1 and iterate through this for loop until count is less than or equal to 10 – because we want to display multiplication tables from 1 to 10. For each iteration of for loop we increment the value of count by 1.

Inside for loop we display the multiplication table.

You can also watch C Program To Print Multiplication Table Using While Loop video tutorial.

Just For Some Number Fun

Here I’m initializing 3 to count variable. For loop executes until count is less than or equal to 27. For each iteration of for loop count value increments by 3.

We assign 37 to num variable. Check the output.

Source Code: Some numerology!

#include<stdio.h>

void tables(int);

int main()
{
    int num = 37;

    printf("\nMultiplication Table for %d is:\n", num);

    tables(num);

    return 0;
}

void tables(int num)
{
    int count;

    for(count = 3; count <= 27; count += 3)
    {
        printf("%d x %d = %d\n", num, count, num*count);
    }
}

Output:
Multiplication Table for 37 is:

37 x 3 = 111
37 x 6 = 222
37 x 9 = 333
37 x 12 = 444
37 x 15 = 555
37 x 18 = 666
37 x 21 = 777
37 x 24 = 888
37 x 27 = 999

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 If Two Numbers are Co-Prime or Not using Function

Lets write a C program to check whether two positive numbers entered by the user are Co-Prime numbers / Relative Prime Numbers or not using function / method.

Co-Prime numbers / Relative Prime Numbers: Two numbers are said to be co-prime or relative prime numbers if they do not have a common factor other than 1.

OR

Two numbers whose Greatest Common Divisor(GCD) is 1 are known as Co-Prime or Relative Prime Numbers.

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

Note: User entered numbers(to check for co-prime) do not require to be prime numbers.

Logic To Find If Two Numbers are Co-Prime or Not

Complete logic is present in our previous day video tutorial, please watch it before continuing: C Program To Find Two Numbers are Co-Prime or Not

Related Read:
C Program To Find Prime Number or Not using While Loop
C Program to Find Factors of a Number using For Loop
Biggest of Two Numbers Using Ternary Operator: C
C Program to Find GCD or HCF of Two Numbers

Video Tutorial: C Program To Find If Two Numbers are Co-Prime or Not using Function


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

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

Source Code: C Program To Find If Two Numbers are Co-Prime or Not using Function

#include<stdio.h>

int coprime(int num1, int num2)
{
    int min, count, flag = 1;

    min = num1 < num2 ? num1 : num2;

    for(count = 2; count <= min; count++)
    {
        if( num1 % count == 0 && num2 % count == 0 )
        {
            flag = 0;
            break;
        }
    }

    return(flag);
}

int main()
{
    int n1, n2;

    printf("Enter 2 positive numbers\n");
    scanf("%d%d", &n1, &n2);

    if( coprime(n1, n2) )
    {
        printf("%d and %d are co-prime numbers.\n", n1, n2);
    }
    else
    {
        printf("%d and %d are not co-prime numbers.\n", n1, n2);
    }

    return 0;
}

Output 1:
Enter 2 positive numbers
8
15
8 and 15 are co-prime numbers.

Output 2:
Enter 2 positive numbers
12
15
12 and 15 are not co-prime numbers.

Note:
coprime() method returns 1 or 0 value. If it returns 1, then the code inside if block gets executed. If it returns 0, then the code inside else block gets executed.

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 Express A Number as Sum of Two Prime Numbers

Lets write a C program to check whether a user input/entered positive number can be expressed as sum of two prime numbers or not.

Related Read:
C Program To Find Prime Number or Not using While Loop

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 2 numbers, sum of which is equal to the original number, must be prime numbers.

For example, if user enters num as 14. Number 14 is not a prime number. We get following result:

3 + 11 = 14
7 + 7 = 14

Numbers 3, 11 and 7 are prime numbers.

Example:

If user enters num = 14;

First Iteration
num = 14;
count = 2; // First prime number in the series

(num-count) => (14-2) = 12;

count + (num-count) = num
2 + 12 = 14.

But 12 is not a prime number. So we discard this 2 + 12 = 14.

Second Iteration
num = 14;
count = 3; // Next Prime Number in the series

(num-count) => (14-3) = 11;

count + (num-count) = num
3 + 11 = 14.

Both 3 and 11 are prime numbers. So we display 3 + 11 = 14.

Third Iteration
num = 14;
count = 5; // Next Prime Number in the series

(num-count) => (14-5) = 9;

count + (num-count) = num
5 + 9 = 14.

But 9 is not a prime number. So we discard this 5 + 9 = 14.

Forth Iteration
num = 14;
count = 7; // Next Prime Number in the series

(num-count) => (14-7) = 7;

count + (num-count) = num
7 + 7 = 14.

Number 7 is a prime numbers. So we display 7 + 7 = 14.

Fifth Iteration
num = 14;
count = 11; // Next Prime Number in the series

(num-count) => (14-11) = 3;

count + (num-count) = num
11 + 3 = 14.

Both 11 and 3 are prime numbers. But we’ve already displayed 3 + 11 = 14 in second iteration. So we do not display it again.

Condition to exit for loop
The condition to exit for loop is: when count is less than or equal to (num – count);

In above case, in fifth iteration: count value is 11. And (num-count) is 3. So 11 <= 3 returns false. So control exits for loop.

Video Tutorial: C Program To Express A Number as Sum of Two Prime Numbers


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

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


Source Code: C Program To Express A Number as Sum of Two Prime Numbers

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

int nxtPrime(int);
int isPrime(int);

int main()
{
    int num, count, flag = 0;

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

    for(count = 2; count <=(num-count); count = nxtPrime(count))
    {
        if(isPrime(num-count))
        {
            flag = 1;
            printf("%d + %d = %d\n", count, num-count, num);
        }
    }

    if(flag == 0)
    {
        printf("%d cannot be expressed as the sum of 2 prime numbers.\n", num);
    }


    return 0;
}

int nxtPrime(int num)
{
    do
    {
        num++;
    }while(!isPrime(num));

    return(num);
}

int isPrime(int num)
{
    int count, inum, prime = 1;

    inum = sqrt(num);

    for(count = 2; count <= inum; count++)
    {
        if(num % count == 0)
        {
            prime = 0;
            break;
        }
    }

    return(prime);
}

Output 1:
Enter a positive number
14
3 + 11 = 14
7 + 7 = 14

Output 2:
Enter a positive number
5
2 + 3 = 5

Output 3:
Enter a positive number
24
5 + 19 = 24
7 + 17 = 24
11 + 13 = 24

Output 4:
Enter a positive number
34
3 + 31 = 34
5 + 29 = 34
11 + 23 = 34
17 + 17 = 34

Output 5:
Enter a positive number
41
41 cannot be expressed as the sum of 2 prime numbers.

Note: Function nxtPrime() and isPrime() returns integer type data so its return type is int. And both these methods / functions accepts 1 integer type argument.

Logic To Express A Number as Sum of Two Prime Numbers

1. We ask the user to enter a positive number and store it inside variable num. Now we write the for loop. We initialize the loop counter variable count to 2 – because 2 is the first number in prime number series. We iterate through the for loop until count is less than or equal to (num-count). For each iteration of for loop we change the value of count to next prime number in the prime number series.

We use following simple expression to find and printout the result:
count + (num – count) = num;

We already know that value of count is prime number. Next inside for loop we check the second part of above expression i.e., (num-count) for prime number or not. If both count and (num-count) are prime numbers, then we output the values, if not, then we change the value of count to next prime number and check if (num-count) is prime or not. We continue doing this until count is less than or equal to (num-count);

2. We need to get next prime number for loop count variable count. So we need to define (write logic for) nxtPrime() method.

int nxtPrime(int num)
{
    do
    {
        num++;
    }while(!isPrime(num));

    return(num);
}

value of count is passed to nxtPrime() method, which is copied to local variable num. First we increment the value of num inside do block and then check the condition in while. If the number is prime then we exit the do-while loop and return the number orelse we keep incrementing the value of num and check if the new number is prime or not, by passing it to the function isPrime().

Table of all prime numbers up to 1,000:
prime numbers from 1 to 1000

3. Next we write the logic to check if the number is prime or not. We’ve already explained the complete logic in a separate video tutorial present at C Program To Find Prime Number or Not using While Loop.

int isPrime(int num)
{
    int count, inum, prime = 1;

    inum = sqrt(num);

    for(count = 2; count <= inum; count++)
    {
        if(num % count == 0)
        {
            prime = 0;
            break;
        }
    }

    return(prime);
}

This is the beauty of function. We call the function isPrime() 2 times. Once from main() function and once from nxtPrime() function. This avoids repeating our code. We write the code once in our function and call it any number of times in the program. This is called DRY concept in programming i.e., Do Not Repeat Yourself.

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 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