This blog is under construction

Saturday, 22 June 2013

C program to find the sum of even numbers between 1 and n

Write a C program to find the sum of all even integers between 1 and n.


  #include <stdio.h>
  int main() {
        int n, i, sum = 0;
        printf("Enter the value for n:");
        scanf("%d", &n);

        /* calculate the sum of even nos between 1 and n */
        for (i = 1; i < n; i++) {

                /* skip the odd numbers */
                if (i % 2 != 0)
                        continue;
                sum = sum + i;
        }
        printf("Output: %d\n", sum);
        return 0;
  }



  Output:
  jp@jp-VirtualBox:~/$ ./a.out
  Enter the value for n:100
  Output: 2450



No comments:

Post a Comment