Logic To Find Sum of All Even Numbers From 1 To N, using While loop
Since we are checking for even numbers from 1 to user entered number, we assign value of variable count to 1. While loop keeps iterating until value of count is less than or equal to value of user input number. For each iteration of while loop we increment the value of count by 1.
Inside while loop we check for the condition, count%2 == 0. If it’s true, then we add the value present inside variable count to previous value present in variable sum.
After the control exits while loop we display the value present in variable sum – which has the sum of all the even numbers from 1 to user entered number.
Source Code: C Program To Find Sum of All Even Numbers From 1 To N, using While loop
#include<stdio.h>
int main()
{
int num, count = 1, sum = 0;
printf("Enter the limit\n");
scanf("%d", &num);
while(count <= num)
{
if(count%2 == 0)
{
sum = sum + count;
}
count++;
}
printf("Sum of Even numbers from 1 to %d is %d\n", num, sum);
return 0;
}
Output 1: Enter the limit 5 Sum of Even numbers from 1 to 5 is 6
Output 2: Enter the limit 20 Sum of Even numbers from 1 to 20 is 110
Logic To Find Sum of All Odd Numbers From 1 To N, using While loop
Since we are checking for odd numbers from 1 to user entered number, we assign value of variable count to 1. While loop keeps iterating until value of count is less than or equal to value of user input number. For each iteration of while loop we increment the value of count by 1.
Inside while loop we check for the condition, count%2 != 0. If it’s true, then we add the value present inside variable count to previous value present in variable sum.
After the control exits while loop we display the value present in variable sum – which has the sum of all the odd numbers from 1 to user entered number.
Source Code: C Program To Find Sum of All Odd Numbers From 1 To N, using While loop
#include<stdio.h>
int main()
{
int num, count = 1, sum = 0;
printf("Enter a integer number\n");
scanf("%d", &num);
while(count <= num)
{
if(count%2 != 0)
{
sum = sum + count;
}
count++;
}
printf("Sum of ODD integer number is %d\n", sum);
return 0;
}
Output 1: Enter a integer number 5 Sum of ODD integer number is 9
Output 2: Enter a integer number 20 Sum of ODD integer number is 100
All the numbers are perfectly divisible by number 1, so we initialize the variable count to 2, instead of 1. So our c program starts checking for divisibility from number 2.
While loop Logic
Outer while loop selects a number for each iteration and stores inside variable start. Inner while loop checks if the selected value(present in variable start) is prime or not. If its a prime number then the variable prime will have value of 1 or else it’ll have value 0 inside it(after completion of inner while loop iteration). If the value present in variable prime is 1, then we print the value present in variable start on to the console window.
Source Code: C Program To Find Prime Numbers Between Two Intervals, using While Loop
#include < stdio.h >
#include < math.h >
int main()
{
int start, end, count, prime, inum;
printf("Enter start and end value\n");
scanf("%d%d", &start, &end);
printf("\n\nPrime Numbers from %d to %d are:\n", start, end);
while(start <= end)
{
inum = sqrt(start);
count = 2;
prime = 1;
while(count <= inum)
{
if(start%count == 0)
{
prime = 0;
break;
}
count++;
}
if(prime) printf("%d, ", start);
start++;
}
printf("\n\n");
return 0;
}
Output 1: Enter start and end value 10 50
Prime Numbers from 10 to 50 are: 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47,
Output 2: Enter start and end value 50 100
Prime Numbers from 50 to 100 are: 53, 59, 61, 67, 71, 73, 79, 83, 89, 97,
Output 3: Enter start and end value 100 200
Prime Numbers from 100 to 200 are: 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199,
Output 4: Enter start and end value 25 50
Prime Numbers from 25 to 50 are: 29, 31, 37, 41, 43, 47,
Output 5: Enter start and end value 75 150
Prime Numbers from 75 to 150 are: 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149,
Logic To Find Prime Number, 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 selected number is divisible by any number other than 1 and itself.
Table of all prime numbers up to 1,000:
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.
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,
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:
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.
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
#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:
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.