🔒 Closed Pa tulong po

Status
Not open for further replies.

CeeDzii

Elite
1669085335001.webp


Nahirapan po ako kasi sa code ko reversed po yung lumalabas na digits
1669085547728.webp
 

Sorry, natagalan kumain pa at manual type codes mo haha di kasi kita gaano.
Pero eto temporary fix sa prob mo. Bale convert nalang yung user's input to string then iterate
Wait mo nalang mga master dito sa C++ for better and easy solution.


C++:
// Online C++ compiler to run C++ program online
#include <iostream>
#include <string>

using namespace std;

int main() {
    int sum = 0, number, individualNumber;
    cout << "Enter an Integer: ";
    cin >> number;
    cout << "The digits of " << number << " are: ";
  
    if (number < 0)
        number = number * -1;
  
    string n = to_string(number);
    for(int i = 0; i < n.length(); i++){
        cout << n[i] << ' ';
        individualNumber = number % 10;
        number /= 10;
        sum = sum + individualNumber;
    }
  
    cout << "\nThe sum of the digits is: " << sum << endl;
  
    return 0;
}

Code:
Enter an Integer: 1234
The digits of 1234 are: 1 2 3 4
The sum of the digits is: 10
 
Sorry, natagalan kumain pa at manual type codes mo haha di kasi kita gaano.
Pero eto temporary fix sa prob mo. Bale convert nalang yung user's input to string then iterate
Wait mo nalang mga master dito sa C++ for better and easy solution.


C++:
// Online C++ compiler to run C++ program online
#include <iostream>
#include <string>

using namespace std;

int main() {
    int sum = 0, number, individualNumber;
    cout << "Enter an Integer: ";
    cin >> number;
    cout << "The digits of " << number << " are: ";
 
    if (number < 0)
        number = number * -1;
 
    string n = to_string(number);
    for(int i = 0; i < n.length(); i++){
        cout << n[i] << ' ';
        individualNumber = number % 10;
        number /= 10;
        sum = sum + individualNumber;
    }
 
    cout << "\nThe sum of the digits is: " << sum << endl;
 
    return 0;
}

Code:
Enter an Integer: 1234
The digits of 1234 are: 1 2 3 4
The sum of the digits is: 10
Maraming salamat po, laking tulong napo nito hehe
 
Status
Not open for further replies.

About this Thread

  • 7
    Replies
  • 736
    Views
  • 2
    Participants
Last reply from:
zackmark29

Online now

Members online
601
Guests online
1,005
Total visitors
1,606

Forum statistics

Threads
2,277,844
Posts
28,979,216
Members
1,229,107
Latest member
worldteens2021
Back
Top