Answer to Question #155732 in C++ for waleed

Question #155732

Write a program that initialize array of size 10, and prints all Armstrong and Palindrome numbers in array. Your program should ask user about their choice of printing. If user press ‘A’ or ‘a’ then program should display all Armstrong numbers in array and if user press ‘P’ or ‘p’ then it should display all Palindrome numbers in array.


1
Expert's answer
2021-01-14T16:26:00-0500
#include<iostream>
#include<math.h>
using namespace std;

int palindrome(int n){

int rv=0;
for(int i=n;i>0;i/=10)
rv=rv*10+i%10;
return(n==rv);

}

void armstrong(int num){

int dm[10],k=0;

    for(int j=1;j<=num;j++){
int original,rem,sum=0,n=0,dm[10],k=0;

original=num;
while(original!=0){

original/=10;
++n;
}
original=num;

while(original!=0){

rem=original%10;
sum+=pow(rem,n);
original/=10;
}

if((sum==num)&&(k<=10)){
cout<<num;
dm[k]=num;
k++;


}
    }


    for(int i=0;i<10;++i){
    cout<<dm[i]<<endl;
    
    }

}

void pal(int min,int max){
int dm[10],k=0;
for(int i=min;i<=max;i++)
if(palindrome(i)&& (k<=10)){
//cout<<i<<" ";
dm[k]=i;
k=k+1;
}
char c;
cout<<"Enter A or a for armstrong numbers or P o p for palidrone numbers"<<endl;
cin>>c;

for(i=0;i<10;i++){

if((c=='a') || (c=='A')){
armstrong(dm[i]);
}else

if((c=='P') || (c=='p')){
cout<<dm[i]<<endl;
}

}
}

int main(){
pal(100,200);
return 0;}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS