-
Notifications
You must be signed in to change notification settings - Fork 64
/
60_Adding_one_to_the_numbers.cpp
54 lines (45 loc) · 1.01 KB
/
60_Adding_one_to_the_numbers.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
//
// main.cpp
//
// Created by Prince Kumar on 16/04/20.
// Copyright © 2020 Prince Kumar. All rights reserved.
//
// ---** ADD ONE TO THE NUMBER **---
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main(){
cout<<"Enter The size of Vector : "<<endl;
int n; cin>>n;
vector<int> v; v.push_back(0);
for(int i=0;i<n;i++){
int x;
cin>>x;
v.push_back(x);
}
// check conditions
int k = (int)v.size() - 1;
if(v[k]<9){
v[k]+=1; // v[k] = v[k] + 1;
}else{
while(v[k]==9){
v[k]=0;
k= k-1;
}
v[k]= v[k]+1;
}
// Print the result
// we have to omit the leading zero's
int s = 0;
for(int i=0;i<(int)v.size() ;i++){
if(v[i]!=0){
s=i; // here we store index
break;
}
}
for (int i=s;i<(int)v.size();i++){
cout<<v[i]<<" ";
}
cout<<endl;
}