C Program to Print Natural Numbers from 1 to N In Reverse Order using for loop

Lets write a simple C program to print natural numbers from 1 to N in reverse order, using for loop.

Related Read:
For Loop In C Programming Language
C Program to Print Natural Numbers from 1 to N using for loop

C Program to Print Natural Numbers from 1 to N In Reverse Order using for loop


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

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


Source Code: C Program to Print Natural Numbers from 1 to N In Reverse Order using for loop

 
#include<stdio.h>

int main()
{
    int num, count;

    printf("Enter a positive number\n");
    scanf("%d", &num);

    printf("\nNatural numbers from %d to 1 are:\n", num);

    for(count = num; count >= 1; count--)
    {
        printf("%d\n", count);
    }

    return 0;
}

Output:
Enter a positive number
14

Natural numbers from 14 to 1 are:
14
13
12
11
10
9
8
7
6
5
4
3
2
1

Natural numbers from 10 to 1 are:

Logic To Print Natural Numbers from 1 to N In Reverse Order using for loop

We ask the user to enter a positive number, and store it inside a variable num. We assign value of num to variable count. We iterate through the loop until the count is equal to zero. For example, if user enters num = 5, we iterate the loop 5 times. In for loops modification section we keep decrementing the value of variable count. Once the value of count becomes 0, we exit the for loop. For each iteration we print the value of count.

This way we printout the natural numbers from 1 to N, in reverse order.

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 Print Natural Numbers from 1 to N using for loop

Lets write a simple C program to print natural numbers from 1 to N, using for loop.

Related Read:
For Loop In C Programming Language

Source Code: C Program to Print Natural Numbers from 1 to N using for loop

 
#include<stdio.h>

int main()
{
    int num, count;

    printf("Enter a positive number\n");
    scanf("%d", &num);

    printf("\nNatural numbers from 1 to %d are:\n", num);

    for(count = 1; count <= num; count++)
    {
        printf("%d\t", count);
    }

    printf("\n");

    return 0;
}

Output:
Enter a positive number
5

Natural numbers from 1 to 5 are:
1 2 3 4 5

C Program to Print Natural Numbers from 1 to N using for loop


[youtube https://www.youtube.com/watch?v=sbIwx-_A5D0]

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


Logic To Print Natural Numbers from 1 to N using for loop

We start by assigning 1 to variable count. Now we ask the user to enter a positive number. Now for loop keeps executing until value of count is less than or equal to user entered value. Inside for loop we printout the value of count and then increment the value of count by one for each iteration of loop.

This way we printout all the natural numbers from 1 to N.

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

For Loop In C Programming Language

Today lets learn about another loop control statement i.e., for loop. For loop is used to repeatedly execute certain set of instructions.

For loop allows us to specify 3 things in a single line:
1. Loop count initialization.
2. Condition Checking.
3. Modification Statement(increment/decrement statements).

Related Read
while loop in C programming
Relational Operators In C
Logical Operators In C
Video Tutorial: For Loop In C Programming Language


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

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

Source Code: For Loop In C Programming Language

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

    for(i = 0; i < 10; i++)
    {
        printf("%d IBM\n", i );
    }

    return 0;
}

Output:
0 IBM
1 IBM
2 IBM
3 IBM
4 IBM
5 IBM
6 IBM
7 IBM
8 IBM
9 IBM

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

    for(i = 0; i < 10; i++)
    {
        printf("%d IBM\n", i + 1);
    }

    return 0;
}

Output:
1 IBM
2 IBM
3 IBM
4 IBM
5 IBM
6 IBM
7 IBM
8 IBM
9 IBM
10 IBM

i = 1 and i<= 10

#include<stdio.h>

int main()
{
    int i;

    for(i = 1; i <= 10; i++)
    {
        printf("%d IBM\n", i);
    }

    return 0;
}

Output:
1 IBM
2 IBM
3 IBM
4 IBM
5 IBM
6 IBM
7 IBM
8 IBM
9 IBM
10 IBM

No initialization statement and No Modification Statement

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

    for(; i < 10;)
    {
        printf("%d IBM\n", i + 1);
        i = i + 1;
    }

    return 0;
}

Output:
1 IBM
2 IBM
3 IBM
4 IBM
5 IBM
6 IBM
7 IBM
8 IBM
9 IBM
10 IBM

No Curly braces and No initialization statement

#include<stdio.h>

int main()
{
    int i = 0;

    for(; (i < 10); i = i + 1)
        printf("%d IBM\n", i + 1);

    return 0;
}

Output:
1 IBM
2 IBM
3 IBM
4 IBM
5 IBM
6 IBM
7 IBM
8 IBM
9 IBM
10 IBM

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 Convert Radian To Degree

Lets write a C program to convert angle from Radian to Degree.

Formula To Convert Radian To Degree

degree = radian x (180.0 / M_PI);

where M_PI is a constant present in header file / library file math.h and its approximately equal to 3.14;

Related Read:
Keywords, Constants, Variables: C
Basic Arithmetic Operations In C
Print All Trigonometric Ratios: C Program
C Program To Convert Degree To Radian

Video Tutorial: C Program To Convert Radian To Degree


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

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

Source Code: C Program To Convert Radian To Degree

#include<stdio.h>
#include<math.h>

int main()
{
    float radian, degree;

    printf("Enter angle in Radian\n");
    scanf("%f", &radian);

    degree = radian * ( 180.0 / M_PI );

    printf("Angle in Degree is %f\n", degree);

    return 0;
}

Output 1:
Enter angle in Radian
1
Angle in Degree is 57.295780

Output 2:
Enter angle in Radian
5
Angle in Degree is 286.478912

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 Convert Degree To Radian

Lets write a C program to convert angle from Degree to Radian.

Formula To Convert Degree To Radian

radian = degree x (M_PI / 180.0);

where M_PI is a constant present in header file / library file math.h and its approximately equal to 3.14;

Note: Trigonometric Ratios like Sin, Cos, Tan etc take radian value as input and not degrees. So its important to learn how to convert angles from degree to radian.

Related Read:
Keywords, Constants, Variables: C
Basic Arithmetic Operations In C
Print All Trigonometric Ratios: C Program
C Program To Convert Radian To Degree

Video Tutorial: C Program To Convert Degree To Radian


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

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

Source Code: C Program To Convert Degree To Radian

#include<stdio.h>
#include<math.h>

int main()
{
    float degree, radian;

    printf("Enter angle in Degrees\n");
    scanf("%f", &degree);

    radian = degree * ( M_PI / 180.0 );

    printf("Angle in Radian is %f\n", radian);

    return 0;
}

Output 1:
Enter angle in Degrees
90
Angle in Radian is 1.570796

Output 2:
Enter angle in Degrees
360
Angle in Radian is 6.283185

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