CPCEJC5- Editorial

PROBLEM LINK:

Practice

Author: Manish Motwani
Tester: Mohd Rashid Meraj
Editorialist: Mohd Rashid Meraj

DIFFICULTY:

SIMPLE

PREREQUISITES:

Basic Mathematics, Arithmetic operators.

PROBLEM:

The Problem statement states that we have to tell whether a number is Armstrong or not.

QUICK EXPLANATION:

Check whether a number is Armstrong or not if it is Armstrong then print " FEELS GOOD" otherwise print “FEELS BAD”

EXPLANATION:

Armstrong number is a number whose sum of its all individual digit raised to the power of the number of the digit in that number is equal to that number itself eg… 153 = 1^3 + 5^3 + 3^3 (153 is an Armstrong number) 1634 = 1^4 + 6^4 + 3^4 + 4^4 (1634 is an Armstrong number)

SOLUTIONS:

Setter's Solution
#include <bits/stdc++.h>
using namespace std;

bool isArmstrong(int n)
{
    int temp=n,count=0,ans=0;
    int num=n;
    while(temp!=0)
    {
        temp=temp/10;
        count++;
    }
    while(n!=0)
    {
        ans+=pow(n%10,count);
        n=n/10;
    }
   if(ans==num)
   return true;
   else
   return false;
 }
 int main() {
    int t;
    cin>>t;
    while(t--)
    {
       int n;
       cin>>n;
        if(isArmstrong(n))
        {
            cout<<"FEELS GOOD"<<endl;
        }
        else
        {
            cout<<"FEELS BAD"<<endl;
        }
    }
 return 0;
}
Tester's Solution
#include<bits/stdc++.h> 

using namespace std;

/* Function to calculate x raised to the power y */
int power(int x, unsigned int y)
{
if( y == 0)
return 1;
if (y%2 == 0)
return power(x, y/2)power(x, y/2);
return x
power(x, y/2)*power(x, y/2);
}

/* Function to calculate order of the number */
int order(int x)
{
int n = 0;
while (x)
{
n++;
x = x/10;
}
return n;
}

// Function to check whether the given number is
// Armstrong number or not
bool isArmstrong(int x)
{
// Calling order function
int n = order(x);
int temp = x, sum = 0;
while (temp)
{
int r = temp%10;
sum += power(r, n);
temp = temp/10;
}

// If satisfies Armstrong condition 
return (sum == x); 

}

// Driver Program
int main()
{
int t;
cin>>t;
while(t>0) {
int n;
cin>>n;
if(isArmstrong(n)){
cout<<“FEELS GOOD”<<endl;
}
else{
cout<<“FEELS BAD”<<endl;
}
t–;
}
return 0;
}

Editorialist's Solution
#include<bits/stdc++.h> 

using namespace std;

/* Function to calculate x raised to the power y */
int power(int x, unsigned int y)
{
if( y == 0)
return 1;
if (y%2 == 0)
return power(x, y/2)power(x, y/2);
return x
power(x, y/2)*power(x, y/2);
}

/* Function to calculate order of the number */
int order(int x)
{
int n = 0;
while (x)
{
n++;
x = x/10;
}
return n;
}

// Function to check whether the given number is
// Armstrong number or not
bool isArmstrong(int x)
{
// Calling order function
int n = order(x);
int temp = x, sum = 0;
while (temp)
{
int r = temp%10;
sum += power(r, n);
temp = temp/10;
}

// If satisfies Armstrong condition 
return (sum == x); 

}

// Driver Program
int main()
{
int t;
cin>>t;
while(t>0) {
int n;
cin>>n;
if(isArmstrong(n)){
cout<<“FEELS GOOD”<<endl;
}
else{
cout<<“FEELS BAD”<<endl;
}
t–;
}
return 0;
}

2 Likes