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

C Program To Compute Smallest Number of Notes That Will Combine To Give Rs N

Consider a currency system in which there are notes of seven denominations, namely, Re 1, Rs 2, Rs 5, Rs 10, Rs 50 and Rs 100. If a sum of Rs N is entered through the keyboard, write a C program to compute the smallest number of notes that will combine to give Rs N.

Related Read:
Basic Arithmetic Operations In C

Logic To Get Minimum Number of Notes To Represent The Amount

First we divide the user entered amount with the biggest denomination i.e., Rs 100. Next, if its divisible, then we subtract the number of Rs 100 currency notes we get by dividing the amount by 100. Next we check for the next biggest denomination Rs 50 and so on ..Rs 10, Rs 5, Rs 2 and Rs 1.

Source Code: C Program To Compute Smallest Number of Notes That Will Combine To Give Rs N

 
#include < stdio.h >

int main()
{
    int amount, temp;

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

    temp   = amount / 100;
    amount = amount - (temp*100);

    printf("%d   x 100 = %d\n", temp, (temp*100));

    temp   = amount / 50;
    amount = amount - (temp*50);

    printf("%d   x 50  = %d\n", temp, (temp*50));

    temp   = amount / 10;
    amount = amount - (temp*10);

    printf("%d   x 10  = %d\n", temp, (temp*10));

    temp   = amount / 5;
    amount = amount - (temp*5);

    printf("%d   x 5   = %d\n", temp, (temp*5));

    temp   = amount / 2;
    amount = amount - (temp*2);

    printf("%d   x 2   = %d\n", temp, (temp*2));

    temp   = amount / 1;
    amount = amount - (temp*1);

    printf("%d   x 1   = %d\n", temp, (temp*1));

    return 0;
}

Output 1:
Enter amount
123
1 x 100 = 100
0 x 50 = 0
2 x 10 = 20
0 x 5 = 0
1 x 2 = 2
1 x 1 = 1

Output 2:
Enter amount
2000
20 x 100 = 2000
0 x 50 = 0
0 x 10 = 0
0 x 5 = 0
0 x 2 = 0
0 x 1 = 0

Output 3:
Enter amount
123456
1234 x 100 = 123400
1 x 50 = 50
0 x 10 = 0
1 x 5 = 5
0 x 2 = 0
1 x 1 = 1

C Program To Compute Smallest Number of Notes That Will Combine To Give Rs N


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

YouTube Link: https://www.youtube.com/watch?v=JMDLjs0eEZE [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

Assignment Operators in C

In this video tutorial we show Compound Assignment Operators in C programming language.

Related Read:
Simple Calculator Application In C

Assignment Operators

If a and c are the operands.

c += a; is equal to writing c = c + a;
c -= a; is equal to writing c = c – a;
c *= a; is equal to writing c = c * a;
c /= a; is equal to writing c = c / a;
c %= a; is equal to writing c = c % a;

Assignment Operator +=

 
#include < stdio.h >

int main()
{
    int a = 20, c = 30;

    c += a ; // c = c + a;

    printf("%d\n\n", c);

    return 0;
}

Output:
50

Assignment Operator -=

 
#include < stdio.h >

int main()
{
    int a = 20, c = 30;

    c -= a ; // c = c - a;

    printf("%d\n\n", c);

    return 0;
}

Output:
10

Assignment Operator *=

 
#include < stdio.h >

int main()
{
    int a = 20, c = 30;

    c *= a ; // c = c * a;

    printf("%d\n\n", c);

    return 0;
}

Output:
600

Assignment Operator /=

 
#include < stdio.h >

int main()
{
    int a = 2, c = 20;

    c /= a ; // c = c / a;

    printf("%d\n\n", c);

    return 0;
}

Output:
10

Assignment Operator %=

 
#include < stdio.h >

int main()
{
    int a = 2, c = 20;

    c %= a ; // c = c % a;

    printf("%d\n\n", c);

    return 0;
}

Output:
0

Compound Assignment Operators in C Programming Language


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

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


You can also learn about Logical and Relational Operators in C:
Relational Operators In C
Logical Operators In C

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

Simple Calculator Program using Switch Case: C

In this video tutorial we shall perform Addition, Subtraction, Multiplication and Division of numbers based on user input, using switch case statement(decision control statement).

Related Read:
Basic Arithmetic Operations In C
Addition of 2 Numbers: C
Subtraction of 2 Numbers: C
Multiplication of 2 Numbers: C
Division of 2 Numbers: C
else if statement in C

We had written same calculator program using else-if clause. Same program has been modified to use Switch case in this program.
Simple Calculator Application In C

Simple Calculator using Switch Case: C Program


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

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


Working of Calculator Program
We display list of operation one can perform. That is,

1. Addition
2. Subtraction
3. Multiplication
4. Division

We ask the user to input his / her choice for arithmetic operation. If the user selects 1, then we ask the user to enter 2 integer numbers to perform addition operation. Once the user enters 2 integer numbers we add and display the result on the screen.

If the user enters wrong choice, we ask the user to enter proper choice.

Calculator Program using Switch Case: C Program

#include < stdio.h >

int main()
{
    int a, b;
    char choice;

    printf("Enter your choice\n");
    printf("a. Addition\nb. Subtraction\nc. Multiplication\nd. Division\n");
    scanf("%c", &choice);


   printf("Enter 2 integer numbers\n");
   scanf("%d %d", &a, &b);


    switch(choice)
    {
        case 'a': printf("%d + %d = %d\n", a, b, (a+b));
                break;

        case 'b': printf("%d - %d = %d\n", a, b, (a-b));
                break;

        case 'c': printf("%d x %d = %d\n", a, b, (a*b));
                break;

        case 'd': if( b != 0)
                    printf("%d / %d = %d\n", a, b, (a/b));
                else
                    printf("Number can't be divided by 0\n");
                break;

        default: printf("You entered wrong choice\n");
                 break;
    }

    return 0;
}

Output
Enter your choice
a. Addition
b. Subtraction
c. Multiplication
d. Division
c
Enter 2 integer numbers
5
10
5 x 10 = 50

In above program we are asking user to enter character a or b or c or d to perform addition, subtraction, multiplication and division operations respectively.

#include < stdio.h >

int main()
{
    int a, b, choice;

    printf("Enter your choice\n");
    printf("1. Addition\n2. Subtraction\n3. Multiplication\n4. Division\n");
    scanf("%d", &choice);

    if( choice > 4 )
    {
        printf("Select with in the range!\n");
    }
    else
    {
        printf("Enter 2 integer numbers\n");
        scanf("%d %d", &a, &b);
    }


    switch(choice)
    {
        case 1: printf("%d + %d = %d\n", a, b, (a+b));
                break;

        case 2: printf("%d - %d = %d\n", a, b, (a-b));
                break;

        case 3: printf("%d x %d = %d\n", a, b, (a*b));
                break;

        case 4: if( b != 0)
                    printf("%d / %d = %d\n", a, b, (a/b));
                else
                    printf("Number can't be divided by 0\n");
                break;

        default: printf("You entered wrong choice\n");
                 break;
    }

    return 0;
}

Output:
Enter your choice
1. Addition
2. Subtraction
3. Multiplication
4. Division
4
Enter 2 integer numbers
10
2
10 / 2 = 5

Here we are asking user to enter 1 or 2 or 3 or 4 to choose the arithmetic operation.

#include

int main()
{
    int a, b;
    char choice;

    printf("Enter your choice\n");
    printf("1. Addition\n2. Subtraction\n3. Multiplication\n4. Division\n");
    scanf("%c", &choice);

    printf("Enter 2 integer numbers. Format: a + b\n");
    scanf("%d %c %d", &a, &choice, &b);



    switch(choice)
    {
        case '+': printf("%d + %d = %d\n", a, b, (a+b));
                break;

        case '-': printf("%d - %d = %d\n", a, b, (a-b));
                break;

        case '*': printf("%d x %d = %d\n", a, b, (a*b));
                break;

        case '/': if( b != 0)
                    printf("%d / %d = %d\n", a, b, (a/b));
                else
                    printf("Number can't be divided by 0\n");
                break;

        default: printf("You entered wrong choice\n");
                 break;
    }

    return 0;
}

Output:
Enter your choice
1. Addition
2. Subtraction
3. Multiplication
4. Division
1
Enter 2 integer numbers. Format: a + b
50 + 60
50 + 60 = 110

Here we ask the user to enter values as well as the operation to be performed. Operands and operator.

Note 1: There is no need of curly braces inside case.
Note 2: If a case doesn’t end with break statement, then the execution continues and the block of code present inside next case will also get executed.
Note 3: default case is optional. And the code inside it executes only when non of the cases match.
Note 4: Here switch, case, break, default are all keywords / reserve words.

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

Simple Calculator Application In C

In this video tutorial we shall perform Addition, Subtraction, Multiplication and Division of numbers based on user input.

Related Read:
Basic Arithmetic Operations In C
Addition of 2 Numbers: C
Subtraction of 2 Numbers: C
Multiplication of 2 Numbers: C
Division of 2 Numbers: C
else if statement in C

Simple Calculator Program In C


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

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


Working of Calculator Program
We display list of operation one can perform. That is,

1. Addition
2. Subtraction
3. Multiplication
4. Division

We ask the user to input his / her choice for arithmetic operation. If the user selects 1, then we ask the user to enter 2 integer numbers to perform addition operation. Once the user enters 2 integer numbers we add and display the result on the screen.

If the user enters wrong choice, we ask the user to enter proper choice.

Student Grade Calculation using else-if clause

 
#include < stdio.h >

int main()
{
    int a, b, choice;

    printf("Enter your choice\n");
    printf("1. Addition\n2. Subtraction\n3. Multiplication\n4. Division\n\n");
    scanf("%d", &choice);

    if(choice > 4)
    {
        printf("You entered wrong choice\n");
    }
    else
    {
        printf("Enter 2 integer numbers\n");
        scanf("%d %d", &a, &b);
    }


    if(choice == 1)
    {
        printf("Addition of %d and %d is %d\n", a, b, (a+b));
    }
    else if(choice == 2)
    {
        printf("Subtraction of %d and %d is %d\n", a, b, (a-b));
    }
    else if(choice == 3)
    {
        printf("Multiplication of %d and %d is %d\n", a, b, (a*b));
    }
    else if(choice == 4)
    {
        if(b != 0)
            printf("Division of %d and %d is %d\n", a, b, (a/b));
        else
            printf("Number can not be divided by 0\n");
    }
    else
    {
        printf("Please enter any of these no 1, 2, 3, 4\n");
    }


    return 0;
}

Output 1:
Enter your choice
1. Addition
2. Subtraction
3. Multiplication
4. Division

1
Enter 2 integer numbers
3
2
Addition of 3 and 2 is 5

Output 2:
Enter your choice
1. Addition
2. Subtraction
3. Multiplication
4. Division

2
Enter 2 integer numbers
8
3
Subtraction of 8 and 3 is 5

Output 3:
Enter your choice
1. Addition
2. Subtraction
3. Multiplication
4. Division

3
Enter 2 integer numbers
5
5
Multiplication of 5 and 5 is 25

Output 4:
Enter your choice
1. Addition
2. Subtraction
3. Multiplication
4. Division

4
Enter 2 integer numbers
10
2
Division of 10 and 2 is 5

Output 5:
Enter your choice
1. Addition
2. Subtraction
3. Multiplication
4. Division

4
Enter 2 integer numbers
0
12
Division of 0 and 12 is 0

Output 6:
Enter your choice
1. Addition
2. Subtraction
3. Multiplication
4. Division

4
Enter 2 integer numbers
10
0
Number can not be divided by 0

Output 7:
Enter your choice
1. Addition
2. Subtraction
3. Multiplication
4. Division

50
You entered wrong choice
Please enter any of these no 1, 2, 3, 4

Note: In division section(that is, choice 4) you can see nesting of if else statement. You can know more about nested if-else statements in this program: Nested if else Statement In C

Note: We can write the same program within while loop and ask the user if he wants to continue working inside calculator application. This avoids the need to exit and re-run the program. We’ll publish the code and video tutorial for the same in coming days. Stay subscribed to our YouTube Channel and blog.

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