Smiling face will be printed 5000 times. There is no logic to printing it 5000 times. It’s just a random number we selected which prints a lot of smiling faces on to the console window.
Source Code: C Program To Fill Screen With Smiling Face: Nested For Loop
#include<stdio.h>
int main()
{
int r, c;
for(r = 0; r <= 43; r++)
{
for(c = 0; c <= 79; c++)
{
printf("%c", 1);
}
}
return 0;
}
Above code will print smiling faces 43 x 79 times. I just tested some random number of rows and columns and came up with these numbers. Your console window might have different number of rows and columns.
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
#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.
Outer for loop selects number one by one for each iteration. We initialize num to 2 and for each iteration num value increments by 1. Outer for loop executes until num is less than or equal to user entered limit times.
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 From 2 To N, using For Loop
#include<stdio.h>
#include<math.h>
int main()
{
int num, count, limit, prime, inum;
printf("Enter the limit\n");
scanf("%d", &limit);
printf("Prime Numbers from 2 To %d are\n", limit);
for(num = 2; num <= limit; num++)
{
prime = 1;
inum = sqrt(num);
for(count = 2; count <= inum; count++)
{
if(num % count == 0)
{
prime = 0;
break;
}
}
if(prime)
printf("%d\n", num);
}
return 0;
}
Output 1: Enter the limit 25 Prime Numbers from 2 To 25 are 2 3 5 7 11 13 17 19 23
Output 2: Enter the limit 41 Prime Numbers from 2 To 41 are 2 3 5 7 11 13 17 19 23 29 31 37 41
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.
Outer for loop selects number one by one for each iteration. We initialize num to 1 and for each iteration num value increments by 1. Outer for loop executes until num is less than or equal to 300.
Inner For loop Logic
All the numbers are perfectly divisible by number 1, so we initialize the variable i 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 From 1 To 300 using For Loop
#include<stdio.h >
#include<math.h>
int main()
{
int num, count, i, prime;
printf("Prime Numbers from 1 To 300 are\n");
for(num = 1; num <= 300; num++)
{
if(num == 1)
{
printf("Number 1 is neither prime nor composite\n");
continue;
}
count = sqrt(num);
prime = 1;
for(i = 2; i <= count; i++)
{
if(num % i == 0)
{
prime = 0;
break;
}
}
if(prime)
{
printf("%d\t", num);
}
}
return 0;
}
Output: Prime Numbers from 1 To 300 are Number 1 is neither prime nor composite 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
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
#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:
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.