Source Code: Even or Odd Number using Macros: C Program
#include<stdio.h>
#define ODD_EVEN(num) ( (num % 2 == 0) ? printf("Even\n") : printf("ODD\n") )
int main()
{
int num;
printf("Enter a positive number\n");
scanf("%d", &num);
ODD_EVEN(num);
return 0;
}
Output 1: Enter a positive number 5 ODD
Output 2: Enter a positive number 6 Even
In above program the macro template ODD_EVEN(num) will be replaced by it’s macro expansion ( (num % 2 == 0) ? printf(“Even\n”) : printf(“ODD\n”) ) and hence if the user input number is perfectly divisible by 2, then EVEN will be printed else ODD will be printed.
Source Code: C Program To Count Prime Numbers Between Range
#include<stdio.h>
#include<math.h>
int isPrime(int num)
{
int inum = sqrt(num), prime = 1, count;
for(count = 2; count <= inum; count++)
{
if(num % count == 0)
{
prime = 0;
break;
}
}
return(prime);
}
int main()
{
int start, end, temp, num, slno = 0, on_off;
printf("Enter start and end value\n");
scanf("%d%d", &start, &end);
printf("Do you want to print prime numbers? (yes = 1, no = 0)\n");
scanf("%d", &on_off);
if(start > end)
{
temp = start;
start = end;
end = temp;
}
if(on_off)
printf("\nPrime numbers between %d and %d are:\n\n", start, end);
for(num = start; num <= end; num++)
{
if(num == 1)
{
continue;
}
if( isPrime(num) )
{
slno++;
if(on_off)
{
printf("%d. %d is prime number.\n", slno, num);
}
}
}
printf("\nThere are %d prime numbers between %d and %d.\n",
slno, start, end);
return 0;
}
Output 1: Enter start and end value 10 30 Do you want to print prime numbers? (yes = 1, no = 0) 1
Prime numbers between 10 and 30 are:
1. 11 is prime number. 2. 13 is prime number. 3. 17 is prime number. 4. 19 is prime number. 5. 23 is prime number. 6. 29 is prime number.
There are 6 prime numbers between 10 and 30.
Output 2: Enter start and end value 10 30 Do you want to print prime numbers? (yes = 1, no = 0) 0
There are 6 prime numbers between 10 and 30.
Output 3: Enter start and end value 1 10 Do you want to print prime numbers? (yes = 1, no = 0) 1
Prime numbers between 1 and 10 are:
1. 2 is prime number. 2. 3 is prime number. 3. 5 is prime number. 4. 7 is prime number.
There are 4 prime numbers between 1 and 10.
Output 4: Enter start and end value 1 300 Do you want to print prime numbers? (yes = 1, no = 0) 0
There are 62 prime numbers between 1 and 300.
Output 5: Enter start and end value 1 1000 Do you want to print prime numbers? (yes = 1, no = 0) 0
There are 168 prime numbers between 1 and 1000.
Logic To Count Prime Numbers Between Range
First we ask the user to enter the range and store it inside address of variables start and end. We make sure that start value is less than end value. If start value is greater than end value, we use a temporary variable to swap the values of start and end.
Next we ask the user if he / she wants to display the prime numbers between the range or just want to know the count of prime numbers between the entered range. We store the user answer in a variable called on_off.
We start the for loop: We initialize the loop counter variable num to start and iterate through the loop until num is less than or equal to end. For each iteration of the for loop we increment the value of num by 1. This for loop selects number one by one from start to end. And this selected number, which is present inside variable num is checked for prime or not. If its prime we display it to the console window and keep track of the count of prime numbers, if not we simply ignore that non-prime number.
We use a function to check if the selected number present in variable num is a prime number or not. We call the function/method isPrime() inside if condition and pass the value of num to it. isPrime() returns 1 or 0 value. 1 means true and 0 means false. So if isPrime() returns 1, then the if condition becomes true and we increment the value of slno by 1 and optionally printout the prime number. If isPrime() returns 0, then we simply ignore it and go to the next number.
isPrime() function has the logic to determine if the given number is prime or not. We have explained the complete logic of this in a separate video tutorial, link to which is present below.
Note: 1. We are including math.h header file or library file since we are using sqrt() builtin method. sqrt() method is present inside math.h file.
2. We are also using continue and break keywords in our program and we’ve explained about it in detail in separate videos. Please watch them for more clarity about the topic.
Perfect Number: A number is called perfect number if sum of its divisors(except the number itself) is equal to the number.
For Example: If the user entered number is 28. The numbers which perfectly divide 28 are 1, 2, 4, 7, 14, 28. Leave 28 and add all other numbers. i.e., 1 + 2 + 4 + 7 + 14 = 28. So the entered number and the sum are equal. So 28 is a perfect number.
Video Tutorial: C Program To Check Perfect Number or Not, using For loop
Source Code: C Program To Find Perfect Number using For loop
#include<stdio.h>
int main()
{
int num, count, sum = 0;
printf("Enter a number\n");
scanf("%d", &num);
printf("Factors of %d are(except the number itself):\n", num);
for(count = 1; count < num; count++)
{
if(num % count == 0)
{
printf("%d\n", count);
sum = sum + count;
}
}
if(sum == num)
printf("\n%d is a perfect number\n", num);
else
printf("\n%d is not a perfect number\n", num);
return 0;
}
Output 1 Enter a number 6 Factors of 6 are(except the number itself): 1 2 3
6 is a perfect number
Output 2 Enter a number 8 Factors of 8 are(except the number itself): 1 2 4
8 is not a perfect number
Output 3 Enter a number 14 Factors of 14 are(except the number itself): 1 2 7
14 is not a perfect number
Output 4 Enter a number 28 Factors of 28 are(except the number itself): 1 2 4 7 14
28 is a perfect number
Logic To Check Perfect Number or Not using For loop
We ask the user to enter a number and store it inside address of integer variable num. Next we find factors of that user entered number i.e., all the numbers which perfectly divide the user entered number. In that factors list, we exclude the user entered number itself.
Now we add all the factors(except the user entered number itself). If the sum and the user entered number are equal then its a perfect number.
We initialize for loop counter variable count to 1 and iterate the for loop until count is less than num. For each iteration of for loop we increment the value of count by 1.
Inside for loop we check if num % count == 0. If true, we add value of count to previous value of variable sum.
After control exits for loop we check if the value of sum and number entered by the user are same. If its same, then the user entered number is perfect number. If not, the number is not a perfect number.
Write a C program to display Factors of user entered number, using for loop. All the numbers which perfectly divide a given number are called as Factors of that number.
For Example, if user enters integer number 50. All the numbers which perfectly divide the number 50 are called Factors of number 50.
We ask the user to enter a integer number. Next we iterate through the for loop until count is less than or equal to the user entered number. Example, if user entered number is 50, then we iterate through the loop for 50 times. Each time we check if the user entered number is perfectly divisible by the value of count. Initial value of count is 1 and after each iteration of the loop count value increments by 1. Whenever a number perfectly divides the user entered number, we display it as factor of the user entered number.
Source Code: C Program to Find Factors of a Number using For Loop
#include<stdio.h>
int main()
{
int num, count;
printf("Enter a number to find factors\n");
scanf("%d", &num);
printf("Factors of %d are\n", num);
for(count = 1; count <= num; count++)
{
if(num % count == 0)
printf("%d\n", count);
}
return 0;
}
Output 1: Enter a number to find factors 60 Factors of 60 are 1 2 3 4 5 6 10 12 15 20 30 60
Output 2: Enter a number to find factors 25 Factors of 25 are 1 5 25
Output 3: Enter a number to find factors 40 Factors of 40 are 1 2 4 5 8 10 20 40
Output 4: Enter a number to find factors 75 Factors of 75 are 1 3 5 15 25 75
Lets write a C program to find sum of all the even numbers between range or between 2 integers input by the user, using for loop.
Even Number: An even number is an integer that is exactly divisible by 2.
For Example: 14 % 2 == 0. When we divide 14 by 2, it gives a reminder of 0. So number 14 is an even number.
Note: In this C program we ask the user to input start and end value. We assume that the user enters bigger value for variable end and smaller value for variable start. i.e., start < end If start value is greater than value present in end, we swap the values of variable start and end.
If user enters start = 5 and end = 14. C program finds all the even numbers between 5 and 14, including 5 and 14. So the even numbers are 6, 8, 10, 12, 14. We add all these even numbers and output the sum to the console window. i.e., 6 + 8 + 10 + 12 + 14 = 50. We out put the value 50 as result.
Logic To Find Sum of All Even Numbers Between Range, using For loop
Step 1: We ask the user to enter start and end value.
Step 2: We initialize count to start and iterate through the for loop until value of count is less than or equal to value of variable end. For each iteration of for loop count value increments by 1.
Step 3: For every iteration we check if value present in variable count is a even number. i.e., count % 2 == 0. If this condition is true, then we add the value present in variable count to the previous value of variable sum.
Step 4: Once the control exits for loop, we print the value present in variable sum – which has the sum of all the even numbers between the range entered by the user.
Source Code: C Program To Find Sum of All Even Numbers Between Range, using For loop
#include<stdio.h>
int main()
{
int start, end, temp, count, sum = 0;
printf("Enter start and end values\n");
scanf("%d%d", &start, &end);
if(start > end)
{
temp = start;
start = end;
end = temp;
}
printf("Even numbers between %d and %d are:\n", start, end);
for(count = start; count <= end; count++)
{
if(count % 2 == 0)
{
printf("%d\n", count);
sum = sum + count;
}
}
printf("Sum of all the even numbers from %d to %d is %d\n", start, end, sum);
return 0;
}
Output 1: Enter start and end values 5 14 Even numbers between 5 and 14 are: 6 8 10 12 14 Sum of all the even numbers from 5 to 14 is 50
Output 2: Enter start and end values 23 14 Even numbers between 14 and 23 are: 14 16 18 20 22 Sum of all the even numbers from 14 to 23 is 90