C Program To Find Prime Numbers From 2 To N, using While 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 While Loop: C Program
C Program To Find Prime Number or Not using While Loop

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


[youtube https://www.youtube.com/watch?v=iYHo-9rClAs]

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

While loop Logic

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

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

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

int main()
{
    int start = 2, num, count, flag, inum;

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

    printf("\n\nPrime numbers from 2 to %d are:\n", num);

    while(start <= num)
    {
        inum = sqrt(start);
        count= 2;
        flag = 1;

        while(count <= inum)
        {
            if(start%count == 0)
            {
                flag = 0;
                break;
            }
            count++;
        }

        if(flag) printf("%d, ", start);

        start++;
    }

    printf("\n\n");

    return 0;
}

Output 1:
Enter the limit
50

Prime numbers from 2 to 50 are:
2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47,

Output 2:
Enter the limit
75

Prime numbers from 2 to 75 are:
2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73,

Output 3:
Enter the limit
100

Prime numbers from 2 to 100 are:
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,

Output 4:
Enter the limit
500

Prime numbers from 2 to 500 are:
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, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499,

Output 5:
Enter the limit
1000

Prime numbers from 2 to 1000 are:
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, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997,

Logic To Find Prime Numbers From 2 To N, using While Loop

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 inner while loop.

For example,
If num = 100;
inum = sqrt(num);
inum = sqrt(100);
inum = 10;

User entered number 100 is perfectly divisible by 5 and 10, so number 100 is not a prime number.

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

We start checking for prime number from number 2 till the user entered number. Variable start is assigned an initial value of 2. For each iteration of outer while loop value of start increments by 1. Inside the inner while loop we check if the value present in variable start is prime number or not. If its prime number then we print that number on to the console window.

Outer while loop selects the number and stores it in variable start on each iteration. Inner while loop checks if the selected number(present in variable start) is prime number or not. You can know the complete logic to check whether a selected number is prime or not: C Program To Find Prime Number or Not using While Loop

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

Lets write a C program to check whether user input number is 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.

Related Read:
while loop in C programming
if else statement in C

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

While 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 Check Whether a Given Number is Prime Number or Not, using While Loop


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

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

Method 1 Source Code: Prime Number or Not

#include<stdio.h>

int main()
{
    int num, count = 2, flag = 1;

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

    while(count < num)
    {
        if(num%count == 0)
        {
            flag = 0;
            break;
        }
        count++;
    }

    if(flag) printf("%d is prime number\n", num);
    else     printf("%d is 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 accept the number from the user. In while loop condition we check if the user entered number is greater than the number present in variable count. We are not checking for greater than or equal to, because all the positive natural numbers are divisible by itself, so we skip checking divisibility of user entered number by itself.

Inside while loop we keep incrementing the value of count by one for each iteration and we check if num%count == 0. If that condition is true, then we store 0 in variable flag and break out of the loop. That indicates there is yet another number(other than 1 and the number itself) which perfectly divides the user entered number.

Outside the while loop we check if the value of flag is 1 or 0. If its 1, then the user entered number is prime number orelse its not a prime number.

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

#include<stdio.h>

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

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

    inum = num / 2;

    while(count <= inum)
    {
        if(num%count == 0)
        {
            flag = 0;
            break;
        }
        count++;
    }

    if(flag) printf("%d is prime number\n", num);
    else     printf("%d is 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 while loop.

If num = 50;
inum = num / 2;
inum = 50 / 2;
inum = 25;

So its enough if we iterate through the while loop 25(num/2) times to check if number 50 is divisible by any number from 2 to 25.

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

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

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

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

    inum = sqrt(num);

    while(count <= inum)
    {
        if(num%count == 0)
        {
            flag = 0;
            break;
        }
        count++;
    }

    if(flag) printf("%d is prime number\n", num);
    else     printf("%d is 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 while loop.

If num = 50;
inum = sqrt(num);
inum = sqrt(50);
inum = 7;

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

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

C Program To Convert Polar To Cartesian Co-ordinates

Write a C program to receive Polar co-ordinates(r, θ) and convert them into Cartesian co-ordinates(x, y). Ask the user to enter theta(θ) value in degrees.

Related Read:
C Program To Convert Cartesian To Polar Co-ordinates

Formula To Convert Polar To Cartesian Co-ordinates

x = r * cos(θ);

y = r * sin(θ);

where x and y are Cartesian co-ordinates. r and θ are polar co-ordinates.

Formula To Convert Degree To Radian

radian = degree * (PI/180.0);
where PI is 3.141592

Source Code: Convert Polar To Cartesian Co-ordinates: C Program

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

int main()
{
    float x, y, r, theta;
    const float PI = 3.141592;

    printf("Enter Polar Co-ordinates(r, theta)\n");
    scanf("%f%f", &r, &theta);

    /* Convert angle from Degree To Radian */    theta = theta * (PI / 180.0); 

    x = r * cos(theta);
    y = r * sin(theta);

    printf("Cartesian Co-ordinates (x, y) = (%f, %f)\n", x, y);

    return 0;
}

Output:
Enter Polar Co-ordinates(r, theta)
5.01
53.131
Cartesian Co-ordinates (x, y) = (3.005938, 4.008047)

Convert Polar To Cartesian Co-ordinates: C Program


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

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


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 Convert Cartesian To Polar Co-ordinates

Write a C program to receive Cartesian co-ordinates(x, y) of a point and convert them into Polar co-ordinates(r, θ).

Related Read:
C Program To Convert Polar To Cartesian Co-ordinates

Formula To Convert Cartesian To Polar Co-ordinates

r = sqrt(x*x + y*y);
OR
r = sqrt( pow(x, 2) + pow(y, 2) );

theta(θ) = atan(y/x);

where x and y are Cartesian co-ordinates.
atan() is the method in math.h library for calculating tan inverse.

Formula To Convert Radian To Degree

degree = radian * (180.0 / PI);
where PI is 3.141592

Source Code: Convert Cartesian To Polar Co-ordinates: C Program

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

int main()
{
    float x, y, r, theta;
    const float PI = 3.141592;

    printf("Enter Cartesian Co-ordinates(x, y)\n");
    scanf("%f%f", &x, &y);

    r     = sqrt(x*x + y*y);
    theta = atan(y/x);       // Radian

    theta = theta * (180.0 / PI); //Radian To Degree Conversion

    printf("Polar Co-ordinates: (r, theta) = (%f, %f)\n", r, theta);

    return 0;
}

Output:
Enter Cartesian Co-ordinates(x, y)
3
4
Polar Co-ordinates: (r, theta) = (5.000000, 53.130112)

Convert Cartesian To Polar Co-ordinates: C Program


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

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


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

Find Area of a Triangle Using Its Sides: C Program

If lengths of three sides of a Triangle are input through the keyboard, write a program to find the area of the Triangle.

Related Read:
Basic Arithmetic Operations In C
Triangle Valid or Not based On Sides: C Program

Note:A Triangle is valid if the sum of 2 sides of the Triangle is greater than the largest of the 3 sides.

Important: In this program we assume that the user has entered valid lengths of the Triangle.

Formula To Calculate Area and Semi-perimeter of a Triangle

sp = (a + b + c) / 2.0;
area = sqrt( sp * (sp-a) * (sp-b) * (sp-c) );

where a, b and c are lengths of sides of the Triangle.
sp – Semi-perimeter.

Note: In geometry, above formula to calculate area of Triangle is called Heron’s formula (sometimes called Hero’s formula).

Find Area of a Triangle Using Its Sides: C Program


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

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


Find Area of a Triangle Using Its Sides: C Program

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

int main()
{
    float a, b, c, sp, area;

    printf("Enter values of a, b and c\n");
    scanf("%f%f%f", &a, &b, &c);

    sp = (a+b+c)/2.0;

    area = sqrt(sp*(sp-a)*(sp-b)*(sp-c));

    printf("Area of triangle is %f\n", area);

    return 0;
}

Output:
Enter values of a, b and c
4
5
6
Area of triangle is 9.921567

Note: While calculating Semi-perimeter, make sure to divide by 2.0 and not by 2. Because division by integer number returns only the integer part of the result. So for certain inputs the result might be wrong. So always use the floating/double value while dividing in C Programs.

Note: Since we are using sqrt() method, we need to include math.h library file (header file) without fail.

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