Digital जीवन

Free Online Education for India...

Full width home advertisement

Computer Basic

C Programming

Engineering Graphics

Post Page Advertisement [Top]

Dr. A.P.J. Abdul Kalam Technical University, Lucknow

KCS101 / KCS201 Programming for Problem Solving - Using C Language


Lab Exercises


14. Write a program to print the sum of even and odd numbers from 1 to N numbers.


/*
 File: Prgrm14.c
 Author: Aditya Saini
 Date: Sept 30, 2019
 Description: Program to print the sum of even and odd numbers up to given number.
*/

#include <stdio.h>

int main (void)
{
               int number;
               int even_sum, odd_sum;
               int i;

               //Input number.
               printf ("Input number: ");
               scanf ("%d", &number);

               //Finding the sum of even numbers and odd numbers.
               for (even_sum = odd_sum = 0, i = 1; i <= number; i++)
               {
                              if (i % 2 == 0)
                                             even_sum = even_sum + i;
                              else
                                             odd_sum = odd_sum + i;
               }

               //Output the result
               printf ("Sum of all odd numbers up to %d is %d.\n", number, odd_sum);
               printf ("Sum of all even numbers up to %d is %d.", number, even_sum);
               
               return 0;
};

Output

Input number: 10
Sum of all odd numbers up to 10 is 25.
Sum of all even numbers up to 10 is 30.



No comments:

Post a Comment

Please do not post spam links.

Bottom Ad [Post Page]

| Designed by Colorlib