MCPcopy Create free account
hub / github.com/Vishruth-S/CompetitiveCode / main

Function main

Codeforces_problems/sum of digits/solution.cpp:8–24  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

6#include<bits/stdc++.h>
7using namespace std;
8int main()
9{
10 ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); //this line is to make the input output operation faster
11 string s; //string define
12 int n,sum=0; //variables define we need in our problem sum is counter for the steps and n is the resulting number of adding operation
13 cin>>s; //take the input number as string
14 if(s.length()==1){cout<<sum<<endl; return 0;} //check if the length is already one digit so we prit it easily
15 else{
16 while(1){ //infinite loop until we get the wanted condition
17 int n=0; //making the resulting zero every iteration to don't be affected with the last number
18 for(int i=0;i<s.length();i++) n+=(int)(s[i]-'0'); //calculating the resulting number of adding all the didgits
19 s=to_string(n); //trasform the resulting number to string
20 sum++; //add one more step to the sum
21 if(s.length()==1){cout<<sum; break;} //break the code if the condition is happened and print the result sum
22 }
23 }
24}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected