PLAY WITH CODING

Home Basic Program If & Else Loops Conversion Pattern logo

Program Based on Loops

Program to print sum of even and odd digit of a number

Input - Output

Input
Enter a number : 1256

Output
Sum of even digits is : 8

Sum of odd digits is : 6

Algorithm

Step 1: START

Step 2: Enter a number 'num' as input.

Step 3: Declare two variable even_sum=0,and odd_sum=0

Step 4: Start a loop: for(int i=num;i>0;i=i/10)

   rem=i%10;

  if rem%2==0;

    even_sum=even_sum+i

else    odd_sum=odd_sum+i .

Step 5:end of loop

Step 6: Print Even_sum and Odd_sum

step 7: End

CODE


/* Print Sum of even and odd digit in a number */

#include<stdio.h>
int main()
{
      int num,even=0,odd=0,rem;
      printf(" Enter the number : ");
      scanf("%d",&num);
      for(int i=num;i>0;i/=10)
    {
       rem = i % 10;
       if(rem % 2 == 0)
        even = even + rem;
       else
        odd = odd + rem;
    }
      printf(" Sum of even digits is : %d",even);
      printf("\n Sum of odd digits is : %d",odd);
    return 0;
}


/* Print sum of even and odd digit in a number */

#include<iostream>
using namespace std;
int main()
{
      int num,even=0,odd=0,rem;
      cout<<" Enter the number : ";
      cin>>num;
      for(int i=num;i>0;i/=10)
    {
       rem = i % 10;
       if(rem % 2 == 0)
        even = even + rem;
       else
        odd = odd + rem;
    }
      cout<<" Sum of even digits is : "<<even<<endl;
      cout<<" Sum of odd digits is : "<<odd;
    return 0;
}

OutPut

Enter a number : 1256

Sum of even digits is : 8
Sum of odd digits is : 6