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
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
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.
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
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>
int main()
{
int i;
for(i = 1; i <= 5; i++)
{
if(i == 3)
break;
printf("%d Apple\n", i);
}
printf("\nEnd of for loop.\n", i);
return 0;
}
Output: 1 Apple 2 Apple
End of for loop.
In above C program we introduce break keyword when i is equal to 3. so for 3rd iteration of the for loop control exits the loop and whatever instructions present after the for loop gets executed.
Source Code: Continue Statement In C Programming Language : Nested For Loop
#include<stdio.h>
int main()
{
int i, j;
for(i = 1; i <= 5; i++)
{
printf("%d Apple\n", i);
for(j = 1; j <= 3; j++)
{
if(j == 2)
break;
printf("\t%d Oracle\n", j);
}
}
printf("\nEnd of for loop.\n", i);
return 0;
}
Output:
1 Apple
1 Oracle
2 Apple
1 Oracle
3 Apple
1 Oracle
4 Apple
1 Oracle
5 Apple
1 Oracle
End of for loop.
In above C program, inside inner for loop we’ve break keyword when j value is equal to 2. So once the keyword break is encountered, control exits the inner for loop. This doesn’t affect the outer for loop, as the keyword break is present inside the inner for loop.