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 Numbers Between Range, using For Loop

Lets write a C program to find and print/display all the prime numbers between 2 integer values input by the user, using nested for 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.

Related Read:
Decision Control Instruction In C: IF
Nested For Loop In C Programming Language
break Statement In C Programming Language
C Program To Find Prime Number or Not using For Loop
C Program To Find Prime Numbers From 2 To N, using For Loop

Video Tutorial: C Program To Find Prime Numbers Between Range, using For Loop


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

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

Logic To Find Prime Number Between Range, using For Loop

We ask the user to enter start and end value. We check if the value of variable start is greater than variable end. If true, we swap the values of variable start and end.

Outer For Loop Logic

We assign value of start to num and keep iterating the for loop until num is less than or equal to value of variable end. For each iteration of outer for loop num will increment by 1, from start to end value.

Inner For loop Logic

All the numbers are perfectly divisible by number 1, so we initialize the variable count to 2, instead of 1. So our inner for loop starts checking for divisibility from number 2.

The selected number(selected by outer for loop and stored in variable num), is divided by numbers 2 to num-1 times. If num is perfectly divisible by any number between 2 to num-1, then the number is not a prime number, else its a prime number.

Source Code: C Program To Find Prime Numbers Between Range, using For Loop

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

int main()
{
    int start, end, num, count, prime, temp, inum;

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

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

    printf("Prime Numbers between %d and %d are\n", start, end);

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

        if(prime) printf("%d,\t", num);
    }

    return 0;
}

Output 1:
Enter start and end value
10
20
Prime Numbers between 10 and 20 are
11, 13, 17, 19,

Output 2:
Enter start and end value
20
10
Prime Numbers between 10 and 20 are
11, 13, 17, 19,

Output 3:
Enter start and end value
25
60
Prime Numbers between 25 and 60 are
29, 31, 37, 41, 43, 47, 53, 59,

Output 4:
Enter start and end value
50
150
Prime Numbers between 50 and 150 are
53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149,

Output 5:
Enter start and end value
5
41
Prime Numbers between 5 and 41 are
5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41,

Logic To Find Prime Number, using For Loop

In this method, we apply square root to the selected number and store it inside variable inum. This reduces the number of iterations of inner while loop.

For example,
If num = 41;
inum = sqrt(num);
inum = sqrt(41);
inum = 6;

User entered number 41 is not perfectly divisible by any number between 2 to 6, so number 41 is a prime number.

So its enough if we iterate through the while loop sqrt(num) times to check if the selected number is divisible by any number other than 1 and itself.

Table of all prime numbers up to 1,000:

prime number or not

Note: We are not using curly braces around if statement because we only have 1 line of code after if – so curly braces are optional. If we have multiple lines of code, then we must use curly braces to wrap around the block of code.

You can also watch C Program To Find Prime Numbers Between Two Intervals, using While Loop video tutorial.

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 Numbers From 2 To N, using For Loop

Lets write a C program to find and print / display all the prime numbers from 2 to N. Here N is the user entered number / limit.

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.

Related Read:
Decision Control Instruction In C: IF
Nested For Loop In C Programming Language
break Statement In C Programming Language
C Program To Find Prime Number or Not using For Loop

Video Tutorial: C Program To Find Prime Numbers From 2 To N, using For Loop


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

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

Outer For loop Logic

Outer for loop selects number one by one for each iteration. We initialize num to 2 and for each iteration num value increments by 1. Outer for loop executes until num is less than or equal to user entered limit times.

Inner For loop Logic

All the numbers are perfectly divisible by number 1, so we initialize the variable count to 2, instead of 1. So our inner for loop starts checking for divisibility from number 2.

The selected number(selected by outer for loop and stored in variable num), is divided by numbers 2 to num-1 times. If num is perfectly divisible by any number between 2 to num-1, then the number is not a prime number, else its a prime number.

Source Code: C Program To Find Prime Numbers From 2 To N, using For Loop

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

int main()
{
    int num, count, limit, prime, inum;

    printf("Enter the limit\n");
    scanf("%d", &limit);

    printf("Prime Numbers from 2 To %d are\n", limit);

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

        if(prime)
            printf("%d\n", num);
    }

    return 0;
}

Output 1:
Enter the limit
25
Prime Numbers from 2 To 25 are
2
3
5
7
11
13
17
19
23

Output 2:
Enter the limit
41
Prime Numbers from 2 To 41 are
2
3
5
7
11
13
17
19
23
29
31
37
41

You can also watch C Program To Find Prime Numbers From 2 To N, using While Loop video tutorial.

Table of all prime numbers up to 1,000:

prime number or not

Note: We are not using curly braces around if statement because we only have 1 line of code after if – so curly braces are optional. If we have multiple lines of code, then we must use curly braces to wrap around the block of code.

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 Numbers From 1 To 300 using For Loop

Lets write a C program to print all the prime numbers from 1 to 300. (Hint: Use nested loops, break and continue).

Prime Number: is a natural number greater than 1, which has no positive divisors other than 1 and itself.

Note: Number 1 is neither prime nor composite number.

Related Read:
Nested For Loop In C Programming Language
C Program To Find Prime Number or Not using For Loop

Continue Statement In C Programming Language
break Statement In C Programming Language

Video Tutorial: C Program To Find Prime Numbers From 1 To 300 using For Loop


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

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

Outer For loop Logic

Outer for loop selects number one by one for each iteration. We initialize num to 1 and for each iteration num value increments by 1. Outer for loop executes until num is less than or equal to 300.

Inner For loop Logic

All the numbers are perfectly divisible by number 1, so we initialize the variable i to 2, instead of 1. So our inner for loop starts checking for divisibility from number 2.

The selected number(selected by outer for loop and stored in variable num), is divided by numbers 2 to num-1 times. If num is perfectly divisible by any number between 2 to num-1, then the number is not a prime number, else its a prime number.

Source Code: C Program To Find Prime Numbers From 1 To 300 using For Loop

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

int main()
{
    int num, count, i, prime;

    printf("Prime Numbers from 1 To 300 are\n");

    for(num = 1; num <= 300; num++)
    {
        if(num == 1)
        {
            printf("Number 1 is neither prime nor composite\n");
            continue;
        }

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

        if(prime)
        {
            printf("%d\t", num);
        }
    }

    return 0;
}

Output:
Prime Numbers from 1 To 300 are
Number 1 is neither prime nor composite
2
3
5
7
11
13
17
19
23
29
31
37
41
43
47
53
59
61
67
71
73
79
83
89
97
101
103
107
109
113
127
131
137
139
149
151
157
163
167
173
179
181
191
193
197
199
211
223
227
229
233
239
241
251
257
263
269
271
277
281
283
293

Table of all prime numbers up to 1,000:

prime number or not

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 Number or Not using For Loop

Lets write a C program to check whether user input number is prime number or not, using for loop.

Prime Number: is a natural number greater than 1, which has no positive divisors other than 1 and itself.

Related Read:
For Loop In C Programming Language
if else statement in C
break Statement In C Programming Language

In this video tutorial we’re illustrating 3 methods to find if the user entered number is prime number or not.

For loop Logic

All the numbers are perfectly divisible by number 1, so we initialize the variable count to 2. So our c program starts checking for divisibility from number 2.

Video Tutorial: C Program To Find Prime Number or Not using For Loop


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

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

Method 1 Source Code: Prime Number or Not

#include<stdio.h>

int main()
{
    int num, count, prime = 1;

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

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

    if(prime)
        printf("%d is a Prime Number\n", num);
    else
        printf("%d is a not Prime Number\n", num);

    return 0;
}

Output 1:
Enter a number
7
7 is prime number

Output 2:
Enter a number
10
10 is not prime number

Logic: Method 1

We ask the user to enter a positive number and store it in variable num. Using for loop we start dividing the user entered number from 2 to num-1 times. If any number from 2 to num-1 perfectly divide the user entered number, then it’s not a prime number. We assign value 0 to variable prime and break out of the loop and print the message to the user.

Method 2 Source Code: Prime Number or Not: Divide By 2

#include<stdio.h>

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

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

    inum = num / 2;

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

    if(prime)
        printf("%d is a Prime Number\n", num);
    else
        printf("%d is a not Prime Number\n", num);

    return 0;
}

Output 1:
Enter a number
41
41 is prime number

Output 2:
Enter a number
15
15 is not prime number

Logic: Method 2

Please read the logic for method 1 above before proceeding.
In this method, we divide the user entered number by 2. This reduces the number of iterations of for loop.

If num = 41;
inum = num / 2;
inum = 41 / 2;
inum = 20;

So its enough if we iterate through the for loop 19(num/2) times to check if number 41 is perfectly divisible by any number from 2 to 20.

Method 3 Source Code: Prime Number or Not: square root Method

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

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

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

    inum = sqrt(num);

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

    if(prime)
        printf("%d is a Prime Number\n", num);
    else
        printf("%d is a not Prime Number\n", num);

    return 0;
}

Output 1:
Enter a number
50
50 is not prime number

Output 2:
Enter a number
53
53 is prime number

Logic: Method 3

Please read the logic for method 1 above before proceeding.
In this method, we apply square root to the user entered number and store it inside variable inum. This reduces the number of iterations of for loop even further.

If num = 41;
inum = sqrt(num);
inum = sqrt(41);
inum = 6;

So its enough if we iterate through the while loop 5( sqrt(num) ) times to check if number 41 is perfectly divisible by any number from 2 to 6.

Table of all prime numbers up to 1,000:

prime number or not

Note: We are not using curly braces around if and else because we only have 1 line of code after if and else – so curly braces are optional. If we have multiple lines of code, then we must use curly braces to wrap around the block of code.

You can also watch video for C Program To Find Prime Number or Not using While Loop

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