C Program To Fill Screen With Smiling Face

Lets write a C program to fill the entire screen with a smiling face. The smiling face has an ASCII value 1.

Related Read:
For Loop In C Programming Language
Nested For Loop In C Programming Language

Video Tutorial: C Program To Fill Screen With Smiling Face


[youtube https://www.youtube.com/watch?v=azFIGQa1GqQ]

YouTube Link: https://www.youtube.com/watch?v=azFIGQa1GqQ [Watch the Video In Full Screen.]

Source Code: C Program To Fill Screen With Smiling Face: For Loop

#include<stdio.h>
int main()
{
    int i;

    for(i = 0; i <= 5000; i++)
    {
           printf("%c", 1);
    }
    return 0;
}

Output:

☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺
☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺
☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺
☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺
☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺
☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺
☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺

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;
}

Output:

☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺
☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺
☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺
☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺
☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺
☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺
☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺

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.

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

C Program To Find Prime Numbers Between Range, using For Loop

Lets write a C program to find and print/display all the prime numbers between 2 integer values input by the user, using nested for loop.

Prime Number: Any natural number which is greater than 1 and has only two factors i.e., 1 and the number itself is called a prime number.

Related Read:
Decision Control Instruction In C: IF
Nested For Loop In C Programming Language
break Statement In C Programming Language
C Program To Find Prime Number or Not using For Loop
C Program To Find Prime Numbers From 2 To N, using For Loop

Video Tutorial: C Program To Find Prime Numbers Between Range, using For Loop


[youtube https://www.youtube.com/watch?v=r1kVO45iaLw]

YouTube Link: https://www.youtube.com/watch?v=r1kVO45iaLw [Watch the Video In Full Screen.]

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:

prime number or not

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.

You can also watch C Program To Find Prime Numbers Between Two Intervals, using While Loop video tutorial.

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

C Program To Find Prime Numbers From 2 To N, using For Loop

Lets write a C program to find and print / display all the prime numbers from 2 to N. Here N is the user entered number / limit.

Prime Number: Any natural number which is greater than 1 and has only two factors i.e., 1 and the number itself is called a prime number.

Related Read:
Decision Control Instruction In C: IF
Nested For Loop In C Programming Language
break Statement In C Programming Language
C Program To Find Prime Number or Not using For Loop

Video Tutorial: C Program To Find Prime Numbers From 2 To N, using For Loop


[youtube https://www.youtube.com/watch?v=dz8lyJL_T6U]

YouTube Link: https://www.youtube.com/watch?v=dz8lyJL_T6U [Watch the Video In Full Screen.]

Outer For loop Logic

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

You can also watch C Program To Find Prime Numbers From 2 To N, using While Loop video tutorial.

Table of all prime numbers up to 1,000:

prime number or not

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.

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

C Program To Find Prime Numbers From 1 To 300 using For Loop

Lets write a C program to print all the prime numbers from 1 to 300. (Hint: Use nested loops, break and continue).

Prime Number: is a natural number greater than 1, which has no positive divisors other than 1 and itself.

Note: Number 1 is neither prime nor composite number.

Related Read:
Nested For Loop In C Programming Language
C Program To Find Prime Number or Not using For Loop

Continue Statement In C Programming Language
break Statement In C Programming Language

Video Tutorial: C Program To Find Prime Numbers From 1 To 300 using For Loop


[youtube https://www.youtube.com/watch?v=a9GtETARgPk]

YouTube Link: https://www.youtube.com/watch?v=a9GtETARgPk [Watch the Video In Full Screen.]

Outer For loop Logic

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

Table of all prime numbers up to 1,000:

prime number or not

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

break Statement In C Programming Language

In this video tutorial lets learn more about the working of break statement or break keyword in C programming language.

Related Read:
Nested For Loop In C Programming Language

Video Tutorial: break Statement In C Programming Language


[youtube https://www.youtube.com/watch?v=7WsVGKKs2bg]

YouTube Link: https://www.youtube.com/watch?v=7WsVGKKs2bg [Watch the Video In Full Screen.]


Source Code: break Statement In C Programming Language : 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.

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert