Monday, 31 July 2017

Write a C program to find sum of all odd numbers between 1 to N using for loop

Write a C program to find SUM of all ODD numbers between 1 to N using for loop.

Code:

#include <stdio.h> 
int main() { 
    int counter, N, sum = 0; 
    printf("Enter a Positive Number\n");
    scanf("%d", &N); 
    for(counter = 1; counter <= N; counter++) { 
        if(counter%2 == 1) {
            sum = sum + counter; 
        } 
    }
    printf("Sum of all Odd numbers between 1 to %d is %d", N, sum);
    return 0; 
Output: 




Try it yourself.
If you have any problem you can tell me on comment box i will help you.
thanks.

Viral Programs

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation.

0 comments:

Post a Comment