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.


No comments:

Post a Comment