+ 1

Program to check that whether there is a equal difference between digits of a numbers. For eg- 12345 , 2468 , 13579

15th Feb 2017, 6:27 AM
Shikhar Awasthi
5 Réponses
+ 2
What is the question?
15th Feb 2017, 6:45 AM
Shahar Levy
Shahar Levy - avatar
+ 2
Try to be more clear with your question and try to illustrate using some mathematics. By the way the code for you. #include <iostream> #include <cmath> using namespace std; int main() { int n,diff; cout << "Enter a number: "; cin >> n; int digits=ceil(log10(fabs(n)+1)); int *ar=new int [digits]; for(int i=0;i<digits;i++,n/=10,ar[i]=n%10) if(i==1 && i!=0) diff=ar[1]-ar[0]; else if (diff==ar[i]-ar[i-1] && i!=0) {cout<<"\nno"; return 0;} cout<<"\nyes"; return 0; }
15th Feb 2017, 11:34 AM
Megatron
Megatron - avatar
+ 2
Without arrays and other things like cmath #include <iostream> using namespace std; int main() { int n,diff,diff2; cout << "Enter a number: "; cin >> n; for(int i=0;n>0;i++) { diff=n%10; n/=10; diff-=n%10; diff2=n%10; n/=10; diff2-=n%10; n/=10; if(diff!=diff2) { cout<<"\nno"; return 0; } } cout<<"\nyes"; return 0; }
16th Feb 2017, 5:05 AM
Megatron
Megatron - avatar
+ 1
question is that we have to check that whether there is equal difference between the digits of number or not. for eg- 2468 , or 13579 and 147
15th Feb 2017, 7:00 AM
Shikhar Awasthi
0
only use loops not array or any other thing.
15th Feb 2017, 3:16 PM
Shikhar Awasthi