C Program To Find Sum of All Even Numbers From 1 To N, using For loop

Lets write a C program to find sum of all the even numbers from 1 to N, using for loop.

Even Number: An even number is an integer that is exactly divisible by 2.

For Example: 8 % 2 == 0. When we divide 8 by 2, it give a reminder of 0. So number 8 is an even number.

If user enters num = 5. Even numbers between 1 to 5 are 2, 4. So we add 2 and 4. i.e., 2 + 4 = 6. We display 6 to the console window as result.

Related Read:
Decision Control Instruction In C: IF
For Loop In C Programming Language
Even or Odd Number: C Program
C Program To Find Even Numbers Between Range using For Loop

You can also watch the video for C Program To Find Sum of All Even Numbers From 1 To N, using While loop

Video Tutorial: C Program To Find Sum of All Even Numbers From 1 To N, using For loop


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

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

Logic To Find Sum of All Even Numbers From 1 To N, using For loop

Since we are checking for even numbers from 1 to user entered number, we assign value of variable count to 1. For loop keeps iterating until value of count is less than or equal to value of user input number. For each iteration of for loop we increment the value of count by 1.

Inside for loop we check for the condition, count%2 == 0. If it’s true, then we add the value present inside variable count to previous value present in variable sum.

After the control exits for loop we display the value present in variable sum – which has the sum of all the even numbers from 1 to user entered number.

Source Code: C Program To Find Sum of All Even Numbers From 1 To N, using For loop

#include<stdio.h>

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

    printf("Enter the limit\n");
    scanf("%d", &num);

    printf("Even numbers from 1 To %d are:\n", num);
    for(count = 1; count <= num; count++)
    {
        if(count % 2 == 0)
        {
            printf("%d\n", count);
            sum = sum + count;
        }

    }

    printf("Sum of even numbers from 1 To %d is %d\n", num, sum);

    return 0;
}

Output 1:
Enter the limit
5
Even numbers from 1 To 5 are:
2
4
Sum of even numbers from 1 To 5 is 6

Output 2:
Enter the limit
10
Even numbers from 1 To 10 are:
2
4
6
8
10
Sum of even numbers from 1 To 10 is 30

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 Amount In Compound Interest

When interest compounds q times per year at an annual rate of r % for n years, the principal p compounds to an amount a as per the following formula:

a = p (1 + r / q) nq

Write a C Program to read 10 sets of p, r, n & q and calculate the corresponding a‘s.

Related Read:
Basic Arithmetic Operations In C
For Loop In C Programming Language
C Program to Calculate the Compound Interest

Video Tutorial: C Program To Calculate Amount In Compound Interest


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

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


Logic To Find Compounded Amount

We ask the user to enter values for floating point variables p, n, r and q. We use the formula:

a = p (1 + r / q) nq

And display the result to the console window.

where,
p – principal amount;
n – number of years.
r – rate of interest;
q – number of times interest compounds per year.

Note: We need to convert the user entered interest into percentage by dividing the integer number by 100.

Source Code: C Program To Calculate Amount In Compound Interest

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

int main()
{
    float p, n, r, q, a;
    int count;

    for(count = 1; count <= 10; count++)
    {
        printf("Enter principal amount\n");
        scanf("%f", &p);

        printf("Enter number of years\n");
        scanf("%f", &n);

        printf("Enter rate of interest\n");
        scanf("%f", &r);

        r = r / 100;

        printf("Enter no of times you compound per year\n");
        scanf("%f", &q);

        a = p * pow( (1 + (r/q)), n * q );

        printf("Compounded amount is %f\n\n", a);
    }

    return 0;
}

Output
Enter principal amount
5000
Enter number of years
2
Enter rate of interest
8
Enter no of times you compound per year
4
Compounded amount is 5858.296875

Enter principal amount
1000
Enter number of years
7
Enter rate of interest
5
Enter no of times you compound per year
1
Compounded amount is 1407.100464

Enter principal amount
2000
Enter number of years
5
Enter rate of interest
12
Enter no of times you compound per year
12
Compounded amount is 3633.393311

Enter principal amount
5000
Enter number of years
5
Enter rate of interest
5
Enter no of times you compound per year
5
Compounded amount is 6412.160156

Enter principal amount

Note: Above program keeps on iterating 10 times and asks the input 10 times.

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 Odd Numbers Between Range using For Loop

Lets write a C program to generate odd numbers between 2 integer values input by the user using For loop.

Related Read:
Even or Odd Number: C Program
C Program to Generate Odd Numbers Between Two Integers

Note 1: An odd number is an integer that is not exactly divisible by 2.

Note 2: Odd numbers are of the form (2 * number + 1);

Note 3: Modular division( % ) returns remainder of division. For example, 10 / 2 = 5. But 10 % 2 = 0.

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


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

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


In above c program, we ask the user to input 2 integer value and store it in variables start and end. If value of start is greater than the value of end, then we swap the values.

For loop counter is initialized to start, and for loop executes until value of count is less than or equal to end. For each iteration of the for loop, count value increments by 1.

Inside for loop, for every value of count, we check if its not perfectly divisible by 2. If true, it’s a Odd number and we output that number to the console window.

Source Code: C Program To Find Odd Numbers Between Range using For Loop

 
#include<stdio.h>

int main()
{
    int start, end, temp, count;

    printf("Enter start and end value, to find odd numbers\n");
    scanf("%d%d", &start, &end);

    if(start > end)
    {
        temp  = start;
        start = end;
        end   = temp;
    }

    printf("Odd numbers between %d and %d are\n", start, end);

    for(count = start; count <= end; count++)
    {
        if(count % 2 != 0)
            printf("%d\n", count);
    }

    return 0;
}

Output 1
Enter start and end value, to find odd numbers
40
60
Odd numbers between 40 and 60 are
41
43
45
47
49
51
53
55
57
59

Output 2
Enter start and end value, to find odd numbers
60
40
Odd numbers between 40 and 60 are
41
43
45
47
49
51
53
55
57
59

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 Even Numbers Between Range using For Loop

Lets write a C program to find all the even numbers between 2 integer values input by the user, using for loop.

Related Read:
Even or Odd Number: C Program
Modulus or Modulo Division In C Programming Language
C Program to Generate Even Numbers Between Two Integers

Note 1: An even number is an integer that is exactly divisible by 2. That is reminder of division should be zero.

Note 2: Even numbers are of the form 2 * number;

Note 3: Modular division( % ) returns remainder of division. For example, 10 / 2 = 5. But 10 % 2 = 0.

Video Tutorial: C Program to Find Even Numbers Between Range using For Loop


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

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


In above c program, we ask the user to input 2 integer value and store it in variables start and end. For loop counter is initialized to start and for loop executes until loop counter is less than or equal to value of end. For each iteration of the for loop, loop counter value increments by 1.

Inside for loop, for every value of count, we check if its perfectly divisible by 2. If true, it’s a Even number and we output that number to the console window.

Source Code: C Program to Find Even Numbers Between Range using For Loop

 
#include<stdio.h>

int main()
{
    int start, end, count, temp;

    printf("Enter start value and end value to generate Even no's\n");
    scanf("%d%d", &start, &end);

    if(start > end)
    {
        temp  = start;
        start = end;
        end   = temp;
    }

    printf("Even numbers between %d and %d are:\n", start, end);

    for(count = start; count <= end; count++)
    {
        if(count % 2 == 0)
        {
            printf("%d\n", count);
        }
    }

    return 0;
}

Output 1
Enter start value and end value to generate Even no’s
10
20
Even numbers between 10 and 20 are:
10
12
14
16
18
20

Output 2
Enter start value and end value to generate Even no’s
50
40
Even numbers between 40 and 50 are:
40
42
44
46
48
50

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 Approximate Level of Intelligence of a Person

According to a study, the approximate level of intelligence of a person can be calculated using the following formula:

i = 2 + ( y + 0.5x )

Write a C program that will produce a table of values of i, y and x, where y varies from 1 to 6, and, for each value of y, x varies from 5.5 to 12.5 in steps 0.5.

Related Read:
Basic Arithmetic Operations In C
Compound Assignment Operators in C
Nested For Loop In C Programming Language

Logic To understanding the problem statement

1. Problem statement snippet: “..y varies from 1 to 6”. That means, we can accomplish it using a looping statement. So we can take a for loop and initialize variable y to 1 and iterate the for loop until y is less than or equal to 6.

2. Problem statement snippet: “..for each value of y, x varies from”. That means we should write a nested for loop here.

3. Problem statement snippet: “..for each value of y, x varies from 5.5 to 12.5 in steps 0.5”. That means, we need to write a nested for loop and initialize x value to 5.5 and iterate through the for loop until x value is less than or equal to 12.5 And for each iteration of the inner for loop we need to increment the value of x by 0.5

Video Tutorial: C Program To Calculate Approximate Level of Intelligence of a Person


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

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


Source Code: C Program To Calculate Approximate Level of Intelligence of a Person

#include<stdio.h>

int main()
{
    int y, count = 1;
    float i, x;

    for(y = 1; y <= 6; y++)
    {
        for(x = 5.5; x <= 12.5; x += 0.5)
        {
          i = 2 + (y + 0.5 * x);

          printf("%d. i = %0.2f,\t x = %0.2f,\t y = %d\n", count++,i, x, y);
        }
    }

    return 0;
}

Output:

1. i = 5.75,     x = 5.50,       y = 1
2. i = 6.00,     x = 6.00,       y = 1
3. i = 6.25,     x = 6.50,       y = 1
4. i = 6.50,     x = 7.00,       y = 1
5. i = 6.75,     x = 7.50,       y = 1
6. i = 7.00,     x = 8.00,       y = 1
7. i = 7.25,     x = 8.50,       y = 1
8. i = 7.50,     x = 9.00,       y = 1
9. i = 7.75,     x = 9.50,       y = 1
10. i = 8.00,    x = 10.00,      y = 1
11. i = 8.25,    x = 10.50,      y = 1
12. i = 8.50,    x = 11.00,      y = 1
13. i = 8.75,    x = 11.50,      y = 1
14. i = 9.00,    x = 12.00,      y = 1
15. i = 9.25,    x = 12.50,      y = 1
16. i = 6.75,    x = 5.50,       y = 2
17. i = 7.00,    x = 6.00,       y = 2
18. i = 7.25,    x = 6.50,       y = 2
19. i = 7.50,    x = 7.00,       y = 2
20. i = 7.75,    x = 7.50,       y = 2
21. i = 8.00,    x = 8.00,       y = 2
22. i = 8.25,    x = 8.50,       y = 2
23. i = 8.50,    x = 9.00,       y = 2
24. i = 8.75,    x = 9.50,       y = 2
25. i = 9.00,    x = 10.00,      y = 2
26. i = 9.25,    x = 10.50,      y = 2
27. i = 9.50,    x = 11.00,      y = 2
28. i = 9.75,    x = 11.50,      y = 2
29. i = 10.00,   x = 12.00,      y = 2
30. i = 10.25,   x = 12.50,      y = 2
31. i = 7.75,    x = 5.50,       y = 3
32. i = 8.00,    x = 6.00,       y = 3
33. i = 8.25,    x = 6.50,       y = 3
34. i = 8.50,    x = 7.00,       y = 3
35. i = 8.75,    x = 7.50,       y = 3
36. i = 9.00,    x = 8.00,       y = 3
37. i = 9.25,    x = 8.50,       y = 3
38. i = 9.50,    x = 9.00,       y = 3
39. i = 9.75,    x = 9.50,       y = 3
40. i = 10.00,   x = 10.00,      y = 3
41. i = 10.25,   x = 10.50,      y = 3
42. i = 10.50,   x = 11.00,      y = 3
43. i = 10.75,   x = 11.50,      y = 3
44. i = 11.00,   x = 12.00,      y = 3
45. i = 11.25,   x = 12.50,      y = 3
46. i = 8.75,    x = 5.50,       y = 4
47. i = 9.00,    x = 6.00,       y = 4
48. i = 9.25,    x = 6.50,       y = 4
49. i = 9.50,    x = 7.00,       y = 4
50. i = 9.75,    x = 7.50,       y = 4
51. i = 10.00,   x = 8.00,       y = 4
52. i = 10.25,   x = 8.50,       y = 4
53. i = 10.50,   x = 9.00,       y = 4
54. i = 10.75,   x = 9.50,       y = 4
55. i = 11.00,   x = 10.00,      y = 4
56. i = 11.25,   x = 10.50,      y = 4
57. i = 11.50,   x = 11.00,      y = 4
58. i = 11.75,   x = 11.50,      y = 4
59. i = 12.00,   x = 12.00,      y = 4
60. i = 12.25,   x = 12.50,      y = 4
61. i = 9.75,    x = 5.50,       y = 5
62. i = 10.00,   x = 6.00,       y = 5
63. i = 10.25,   x = 6.50,       y = 5
64. i = 10.50,   x = 7.00,       y = 5
65. i = 10.75,   x = 7.50,       y = 5
66. i = 11.00,   x = 8.00,       y = 5
67. i = 11.25,   x = 8.50,       y = 5
68. i = 11.50,   x = 9.00,       y = 5
69. i = 11.75,   x = 9.50,       y = 5
70. i = 12.00,   x = 10.00,      y = 5
71. i = 12.25,   x = 10.50,      y = 5
72. i = 12.50,   x = 11.00,      y = 5
73. i = 12.75,   x = 11.50,      y = 5
74. i = 13.00,   x = 12.00,      y = 5
75. i = 13.25,   x = 12.50,      y = 5
76. i = 10.75,   x = 5.50,       y = 6
77. i = 11.00,   x = 6.00,       y = 6
78. i = 11.25,   x = 6.50,       y = 6
79. i = 11.50,   x = 7.00,       y = 6
80. i = 11.75,   x = 7.50,       y = 6
81. i = 12.00,   x = 8.00,       y = 6
82. i = 12.25,   x = 8.50,       y = 6
83. i = 12.50,   x = 9.00,       y = 6
84. i = 12.75,   x = 9.50,       y = 6
85. i = 13.00,   x = 10.00,      y = 6
86. i = 13.25,   x = 10.50,      y = 6
87. i = 13.50,   x = 11.00,      y = 6
88. i = 13.75,   x = 11.50,      y = 6
89. i = 14.00,   x = 12.00,      y = 6
90. i = 14.25,   x = 12.50,      y = 6

Note: Inner for loop executes from 5.5 to 12.5 with a 0.5 step increment for each loop. So its 15 iterations. Outer for loop executes 6 times. So total iterations equals 15 x 6 = 90 times. So 90 values will be printed to the console window.

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