Computer Science, asked by shreeparnagupta3, 26 days ago

in C++ WAP to enter a number and print the sum of odd digits using while loop​

Answers

Answered by charan555
3

Explanation:

#include<iostream.h>

using namespace std ;

int main()

{

int a[i] , n , i ;

cin>>" enter a number: ";

for ( i = 0 ; i < n ; i ++)

cin>> a[i] ;

while ( a[i] %2 != 0 )

{

sum += a[i] ;

i++;

}

return sum ;

}

Answered by anindyaadhikari13
5

SOLUTION.

This c‎‎ode is written in CPP.

#include <iostream>

using namespace std;

int main() {

   int n,d;

   cout<<"Enter a number: ";

   cin>>n;

   int sum=0;

   while(n!=0){

       d=n%10;

       if(d%2==1)

           sum+=d;

       n/=10;

   }

   cout<<"The sum of odd digits of the number is: "<<sum;

   return 0;

}

LOGIC.

  • Accept the number.
  • Initialise sum = 0.
  • Loop while number is greater than 0.
  •    Find the last digit of the number. If the digit is odd, add the digit to sum.
  •    Divide the number by 10.
  • Display the sum.

See attachment for output.

Attachments:
Similar questions