+ 1
Program to check that whether there is a equal difference between digits of a numbers. For eg- 12345 , 2468 , 13579
5 Answers
+ 2
What is the question?
+ 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;
}
+ 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;
}
+ 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
0
only use loops not array or any other thing.