Skip to content

Commit

Permalink
26- Add Digits
Browse files Browse the repository at this point in the history
  • Loading branch information
IbrahimKhalid11 authored Apr 26, 2023
1 parent 854f5b9 commit e4da089
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions 04- April/26- Add Digits/26- Add Digits (Ibrahim Khalid).cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// author : Ibrahim Khalid
class Solution {
public:
int addDigits(int num) {
// str to convert num(int) to string to can count digits
string str=to_string(num);
int sum;
// if Digitalis number equal 1 break from loop
while(str.size()!=1){
sum=0;
// to summation digits
for(int i=0;i<str.size();i++){
//to convert from char to int
sum+=(str[i]-'0');
}
// to convert summation to string to check digitalis number
str=to_string(sum);

}
return sum;
}
};

0 comments on commit e4da089

Please sign in to comment.