Coding | Mcqs | Multiple choice questions | Informative | Computer Science | Engineering | Aptitude | Quants | Verbal

INFEED

C++ - VERY-EASY - PART004-Sum of odd digits in a number

  A number N is passed as the input. The program must print the sum of odd digits in the number N.


Input Format:

The first line denotes the value of N.


Output Format:

The sum of odd digits in number N


Boundary Conditions:

1 <= N <= 9999999


Example Input/Output 1:

Input:

4165


Output:

6


Explanation:

The odd digits are 1 ,5 and their sum is 6.


Example Input/Output 2:

Input:

224


Output:

0

CODE:

#include <bits/stdc++.h>

using namespace std;

int main()

{ int num,rem,odd=0,digit,input;

cin>>num;

  input = num;

 num = abs(num);

while(num>0){

digit = num % 10;

num = num / 10;

rem = digit % 2;

if(rem != 0)

 odd=odd+digit;

}

    cout<<odd; 

    return 0; 

}

Post a Comment

Previous Post Next Post