In above c program, we ask the user to input 2 integer value and store it in variables start and end. If value of start is greater than the value of end, then we swap the values.
For loop counter is initialized to start, and for loop executes until value of count is less than or equal to end. For each iteration of the for loop, count value increments by 1.
Inside for loop, for every value of count, we check if its not perfectly divisible by 2. If true, itβs a Odd number and we output that number to the console window.
Source Code: C Program To Find Odd Numbers Between Range using For Loop
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
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;
}
#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:
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.
printf("\nNatural Numbers from %d to %d are:\n", min, max);
for(count = min; count <= max; count++)
{
printf("%d\n", count);
sum = sum + count;
}
printf("Sum of Natural Numbers from %d to %d is %d\n", min, max, sum);
return 0;
}
#include<stdio.h>
int main()
{
int min, max, temp, count, sum = 0;
printf("Enter 2 positive numbers\n");
scanf("%d%d", &min, &max);
if(min > max)
{
temp = min;
min = max;
max = temp;
}
printf("\nNatural Numbers from %d to %d are:\n", min, max);
for(count = min; count <= max; count++)
{
printf("%d\n", count);
sum = sum + count;
}
printf("Sum of Natural Numbers from %d to %d is %d\n", min, max, sum);
return 0;
}
Output 1: Enter 2 positive numbers 5 10
Natural Numbers from 5 to 10 are: 5 6 7 8 9 10 Sum of Natural Numbers from 5 to 10 is 45
Output 2: Enter 2 positive numbers 14 10
Natural Numbers from 10 to 14 are: 10 11 12 13 14 Sum of Natural Numbers from 10 to 14 is 60
Logic To Calculate Sum of Natural Numbers Between Range using For Loop
We ask the user to enter minimum and maximum number(i.e., the range) and we store it inside variable min and max. If value of min is greater than value of max, then we swap the values of min and max.
We initialize the variable count to min and iterate through the for loop until count value is less than or equal to value of max. We keep incrementing the value of count by 1 for each iteration of for loop.
Inside for loop we add the value of count to previous value of sum and once the control exits the for loop we display the value present in variable sum.
printf("Natural numbers from %d to %d are:\n", min, max);
for(count = min; count <= max; count++)
{
printf("%d\n", count);
}
return 0;
}
#include<stdio.h>
int main()
{
int min, max, temp, count;
printf("Enter 2 positive numbers\n");
scanf("%d%d", &min, &max);
if(min > max)
{
temp = min;
min = max;
max = temp;
}
printf("Natural numbers from %d to %d are:\n", min, max);
for(count = min; count <= max; count++)
{
printf("%d\n", count);
}
return 0;
}
Output 1: Enter 2 positive numbers 10 15 Natural numbers from 10 to 15 are: 10 11 12 13 14 15
Output 2: Enter 2 positive numbers 15 10 Natural numbers from 10 to 15 are: 10 11 12 13 14 15
Logic To Print Natural Numbers Between Two Numbers using for loop
We ask the user to enter 2 numbers, and store it inside variables min and max. For loop keeps iterating till min is less than or equal to max. We assign the value of min to count and keep incrementing the value of count by 1 for each iteration of the for loop. Once value of count is greater than value of max, control exits for loop.
Write a C program to find the range of a set of numbers entered through the keyboard. Range is the difference between the smallest and biggest number in the list.
Example: If biggest number in the list is 5 and smallest number in the list is 1. The difference between them is the range. i.e., 5 β 1 = 4. So range = 4.
User Input: Enter the limit 5 Enter 5 numbers 1 2 3 4 5
Output: Small Number = 1 Big Number = 5 Range is 4
Logic To Find Range of Set of Numbers
First we ask the user to enter the length or the size of the list, and store it inside the variable limit. If the user enters the list size as 5, then we ask the user to enter 5 numbers.
Next we ask the user to enter the first number. We assign the first number entered by the user to variables small and big. Since user already entered 1 number into the list, we decrement the value of variable limit by 1.
Next we take remaining inputs inside the while loop. For each iteration of the while loop we decrement the value of variable limit by 1, until the value of limit is 0. Once value of limit is zero, control exits while loop.
For each input(inside the while loop), we check if the value entered is bigger than the value present in variable big. If its true, we assign the bigger number to variable big. We also check if the value entered is smaller than the value present in variable small. If its true, we assign the smaller number to variable small.
Once the control exits the while loop, variable big will have the biggest number in the list and variable small will have the smallest number in the list.
Finally, we use below formula to calculate range of the list: range = big β small;
Video Tutorial: C Program To Find Range of Set of Numbers