Tuesday, 30 June 2015

Program 5 : Write a program to find sum of first N odd numbers. Ex. 1+3+5+7+_ _+N.

#include<stdio.h>

void main() 

{

    int i, n, sum = 0;

    i = 1;

    scanf("%d", &n);

    while (i <= n) 

    {

        if (i % 2 != 0)

            sum = sum + i;

        i++;

    }

    printf("sum : %d", sum);

}

No comments:

Post a Comment