in C++ WAP to enter a number and print the sum of odd digits using while loop
Answers
Answered by
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
5
SOLUTION.
This code 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
History,
28 days ago
Social Sciences,
28 days ago
English,
28 days ago
Social Sciences,
1 month ago
English,
1 month ago
English,
9 months ago
Physics,
9 months ago