forked from 7oSkaaa/LeetCode_DailyChallenge_2023
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request 7oSkaaa#1002 from Hima-khalid/main
Add Digits 26th
- Loading branch information
Showing
1 changed file
with
22 additions
and
0 deletions.
There are no files selected for viewing
22 changes: 22 additions & 0 deletions
22
04- April/26- Add Digits/26- Add Digits (Ibrahim Khalid).cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
}; |