C Program To Find Sum of Natural Numbers Using Recursion

Write a recursive function to obtain the running sum of first 25 natural numbers.

1 + 2 + 3 + 4 + 5 + …. + 23 + 24 + 25 = 325

Related Read:
C Program to Calculate the Sum of Natural Numbers From 1 to N
C Program To Calculate the Sum of Natural Numbers From 1 to N using For Loop
Recursive Functions In C Programming Language

Video Tutorial: C Program To Find Sum of Natural Numbers Using Recursion


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

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


Source Code: C Program To Find Sum of Natural Numbers Using Recursion

#include<stdio.h>

int sum(int num)
{
    if(num)
        return(num + sum(num-1));
    else
        return 0;
}

int main()
{
    int count = 25;

    printf("Sum of 1st 25 natural numbers is %d\n", count, sum(count));

    return 0;
}

Output:
Sum of 1st 25 natural numbers is 325

Logic To Find Sum of Natural Numbers Using Recursion

25 is passed to a function sum, from main method. Inside function sum(), if the passed number is a non-zero then we add sum(num-1) to num. We keep doing it until num value is 0. Once num is 0, code inside else block gets executed and 0 is returned.

Source Code: Find Sum of Natural Numbers Using Recursion – Take input from user

#include<stdio.h>

int sum(int num)
{
    if(num)
        return(num + sum(num-1));
    else
        return 0;
}

int main()
{
    int count;

    printf("Enter a positive no\n");
    scanf("%d", &count);

    printf("Sum of 1st %d natural numbers is %d\n", count, sum(count));

    return 0;
}

Output 1:
Enter a positive no
5
Sum of 1st 5 natural numbers is 15

Output 2:
Enter a positive no
14
Sum of 1st 14 natural numbers is 105

Example:

Lets assume user has input num value as 5.
Recursive Call sum(num – 1).

numCalling FunctionReturned Value
5sum(5)
5sum(4)10
4sum(3)6
3sum(2)3
2sum(1)1
1sum(0)0
0return 0;

Value Returning – Control Shifting back.

num+sum(num-1)Return ValueResult
1 + sum(0)01 + 0 = 1
2 + sum(1)12 + 1 = 3
3 + sum(2)33 + 3 = 6
4 + sum(3)64 + 6 = 10
5 + sum(4)105 + 10 = 15

Finally, after completing the recursive calls and once num is equal to zero, sum() will return 15 to main().

i.e., 1 + 2 + 3 + 4 + 5 = 15.

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 using Recursion

Lets write a C program to print/display natural numbers from 1 to user entered limit, using recursive function calls.

Related Read:
C Program to Print Natural Numbers from 1 to N using While loop
C Program to Print Natural Numbers from 1 to N using for loop
Recursive Functions In C Programming Language

Video Tutorial: C Program To Print Natural Numbers from 1 To N using Recursion


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

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


Source Code: C Program To Print Natural Numbers using Recursion

#include<stdio.h>

void display(int);

int main()
{
    int limit;

    printf("Enter the number of terms to be printed\n");
    scanf("%d", &limit);

    printf("\nNatural Numbers from 1 To %d are:", limit);
    display(limit);

    return 0;
}

void display(int num)
{
    if(num)
        display(num-1);
    else
        return;

    printf("\n%d\n", num);
}

Output:
Enter the number of terms to be printed
14

Natural Numbers from 1 To 14 are:
1

2

3

4

5

6

7

8

9

10

11

12

13

14

Logic To Print Natural Numbers using Recursion

We ask the user to input the limit or the number of terms of natural numbers to be printed. We store that value inside variable limit. We pass this value to a function called display().

Inside display() function
We check if number is not zero, in that case we call the same function display() recursively and pass (num-1) to it. In the else block we write the base condition, that is, return the control back to the calling function if num is 0.

This prints the natural numbers from 1 to user input limit.

Example:

If limit = 5. We copy the value of limit to num. So num = 5.

numCalling FunctionCalled Function
5main()display(5)
5display(5)display(4)
4display(4)display(3)
3display(3)display(2)
2display(2)display(1)
1display(1)display(0)
returns the control backdisplay(0)return;

Value Returning – Control Shifting back.

FunctionReturn Value
return;
display(0)1
display(1)2
display(2)3
display(3)4
display(4)5

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 Floyd’s Triangle using For Loop

Lets write C program to print Floyd’s Triangle, using nested for loop.

Floyd’s Triangle: is a right angled Triangle formed with natural numbers.

Related Read:
For Loop In C Programming Language
Nested For Loop In C Programming Language
C Program To Print Floyd’s Triangle

Video Tutorial: C Program To Print Floyd’s Triangle using For Loop


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

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

Logic To Print Floyd’s Triangle using For Loop

We ask the user to input the number of rows for Floyd’s Triangle, we store it inside variable num.

Outer For loop
In our C program, row number and the total numbers to be printed for that row is present inside variable row. So the outer for loop selects the row number and the number of elements to be printed in that row.

We initialize the row to 1 and iterate through the outer for loop until row is less than or equal to user entered number. For each iteration of the outer for loop we increment the value of row by 1. That way selecting the next row for each iteration.

Inner For loop
Inner for loop prints natural numbers in each selected row. Variable col is assigned to 1 for each iteration of outer for loop, so that the numbers gets printed from the first position in any selected row.

Inner for loop prints the natural number from the first position till the selected row number of times.

For Example, if user enters num = 5, the following Triangle will be printed:

1
2 3
4 5 6
7 8 9 10
11 12 13 14 15

Note that the Triangle printed is a right angled Triangle and has 5 rows of natural numbers.

Important Note:
Also note that first row has 1 number. Second row has 2 numbers. Third row has 3 numbers and so on. So row number and total numbers in that particular row are always equal in any Floyd’s Triangle.

Source Code: C Program To Print Floyd’s Triangle using For Loop

#include<stdio.h>

int main()
{
    int num, row, col, count = 1;

    printf("Enter number of rows for Floyd's Triangle\n");
    scanf("%d", &num);

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

    return 0;
}

Output 1:
Enter number of rows for Floyd’s Triangle
5

1
2 3
4 5 6
7 8 9 10
11 12 13 14 15

Output 2:
Enter number of rows for Floyd’s Triangle
14

1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31 32 33 34 35 36
37 38 39 40 41 42 43 44 45
46 47 48 49 50 51 52 53 54 55
56 57 58 59 60 61 62 63 64 65 66
67 68 69 70 71 72 73 74 75 76 77 78
79 80 81 82 83 84 85 86 87 88 89 90 91
92 93 94 95 96 97 98 99 100 101 102 103 104 105

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 Calculate Sum of Natural Numbers Between Range using For Loop

Lets write a C program to calculate sum of all natural numbers between the user entered range of numbers, using for loop.

We check if the user has entered smaller number first and then the larger number. If not, we swap the numbers present in the variables min and max.

Related Read:
For Loop In C Programming Language
C Program To Print Natural Numbers Between Two Numbers using for loop
Swap 2 Numbers Using a Temporary Variable: C

Video Tutorial: C Program To Calculate Sum of Natural Numbers Between Range using For Loop


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

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


Source Code: C Program To Calculate Sum of Natural Numbers Between Range using For Loop

 
#include<stdio.h>

int main()
{
    int min, max, temp, count, sum = 0;

    printf("Enter 2 positive numbers\n");
    scanf("%d%d", &min, &max);

    if(min > max)
    {
        temp = min;
        min  = max;
        max  = temp;
    }

    printf("\nNatural Numbers from %d to %d are:\n", min, max);

    for(count = min; count <= max; count++)
    {
        printf("%d\n", count);
        sum = sum + count;
    }

    printf("Sum of Natural Numbers from %d to %d is %d\n", min, max, sum);

    return 0;
}

Output 1:
Enter 2 positive numbers
5
10

Natural Numbers from 5 to 10 are:
5
6
7
8
9
10
Sum of Natural Numbers from 5 to 10 is 45

Output 2:
Enter 2 positive numbers
14
10

Natural Numbers from 10 to 14 are:
10
11
12
13
14
Sum of Natural Numbers from 10 to 14 is 60

Logic To Calculate Sum of Natural Numbers Between Range using For Loop

We ask the user to enter minimum and maximum number(i.e., the range) and we store it inside variable min and max. If value of min is greater than value of max, then we swap the values of min and max.

We initialize the variable count to min and iterate through the for loop until count value is less than or equal to value of max. We keep incrementing the value of count by 1 for each iteration of for loop.

Inside for loop we add the value of count to previous value of sum and once the control exits the for loop we display the value present in variable sum.

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 Calculate the Sum of Natural Numbers From 1 to N using For Loop

Lets write a C program to calculate sum of all natural numbers from 1 to N, using for loop.

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

Video Tutorial: C Program to Calculate the Sum of Natural Numbers From 1 to N using For Loop


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

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


Source Code: C Program to Calculate the Sum of Natural Numbers From 1 to N using For Loop

 
#include<stdio.h>

int main()
{
    int num, count, sum = 0;

    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\n", count);
        sum   = sum + count;
    }

    printf("\nSum of natural numbers from 1 to %d is: %d\n", num, sum);

    return 0;
}

Output 1:
Enter a positive number
5

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

Sum of natural numbers from 1 to 5 is: 15

Output 2:
Enter a positive number
10

Natural Numbers from 1 to 10 are:
1
2
3
4
5
6
7
8
9
10

Sum of natural numbers from 1 to 10 is: 55

Logic To Calculate the Sum of Natural Numbers From 1 to N

We ask the user to enter a positive number and store it in variable num.

For Loop
Variable count is assigned value 1. For loop executes until count value is less than or equal to the user entered number. For each iteration of for loop, count value increments by 1.

Inside for loop, we display the value of count(which is natural number between 1 and user entered number). We also keep adding the value of count to the previous value of variable sum. Once the control exits for loop, sum will have the sum of all the natural numbers between 1 to user entered number.

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