Â
So, lets understand all the logic in the code in C++, JAVA, PYTHON.
Now, Lets understand the logic for Armstrong numbers in Programming languages which we are going to use below:-
The following logic is same for all languages but name of the variables may differ-
1. Make three variables num, temp1, temp2 (we will use each variable in our logic) assign value zero to temp 2
2.take a 3 dig number from user store in a variable (Eg. num = 153)
3. assign same number in another temp variable to perform operations on it (eg. temp1 = num, so temp1 = 153)
4. Find the remainder by % (modulus operator)
(eg. temp1 % 10)
( i.e., 153 % 10)
5.the answer would be the number in the units placeÂ
store it in a variable (a = temp1 % 10, a = 3)
6.now divide temp by 10 to get rid of the unit place number and now you have the quotient which is a two digit number and store it in the same variable... in our case it's temp
( temp1 = temp1 /10)
(153/10Â
Now temp = 153)
(#IMPORTANT POINT
to get quotient as the answer in division make sure you are using int variable and and you do not have a floating number or decimal number as the answer)Â
7. Perform this operation
 temp2 = temp2 + (a*a*a)
to cube the number obtained in step 4and save it in temp2 itself
temp2 = 0 + 3*3*3 = 27
temp2 = 27
8. put the step 4, 5, 6, 7 in while loop with the condition that temp > 0
this will let you process the above steps repeatedly and store the cube of each digit Â
9. Once the for loop ends check whether temp2 is equal to num
(i.e. the input number is same as the number formed as the sum of cubes of each digit)
If the numbers are equal then its a palindrome number otherwise its not
Lets apply our logic in 153 and 125 and see which one is an Armstrong number
num = 153
a = 153 % 10 = 3
temp1 = 153 / 10 = 15
temp2 = 0 + (3*3*3) = 27
a = 15 % 10 = 5
temp1 = 15 / 10 = 1
temp2 = 27 + (5*5*5) = 27 + 125 = 152
a = 1 % 10 = 1
temp1 = 1 / 10 = 0
temp2 = 152 + (1*1*1) = 153
Now num = temp2, So 153 is an Armstrong number
num = 125
a = 125 % 10 = 5
temp1 = 125 / 10 = 12
temp2 = 0 + (5*5*5) = 125
a = 12 % 10 = 2
temp1 = 12 / 10 = 1
temp2 = 125 + (2*2*2) = 125 + 8 = 133
a = 1 % 10 = 1
temp1 = 1 / 10 = 0
temp2 = 133 + (1*1*1) = 134
Now num ≠temp2, So 125 is not an Armstrong number
C++
// HELP FOR CODERS
#include <iostream>
using namespace std;
//This is a function called Cases.
int Cases()
{
cout << "\n Which operation do you want to operate";
cout << "\n 1. Armstrong Number"
<< "\n 2. Fibonacci Series"
<< "\n 3. Pallindrome Number\n";
int n;
//Talikng user input for switch case
cin >> n;
//Applying Swtich case to the user input to change the flow of program
//according the user's input
switch (n)
{
//First case is for Armstrong number
case 1:
//Declaring variables
int num, dn, temp, d;
cout << "Enter the number to check for Armstrong Number\n";
//Taking user input
cin >> num;
dn = num;
//assigning zero to temporary variable
temp = 0;
//applying while loop till the number is not equal to zero
while (dn != 0)
{
d = dn % 10;
temp = temp + (d * d * d);
dn = dn / 10;
}
if (num == temp)
{
cout << num << " Is an Armstrong Number\n";
}
else
{
cout << num << " Is not an Armstrong Number\n";
}
break;
//This is second case for Fibonacci series
case 2:
//Declaring variables
int fibo_0, fibo_1, fibo, number, i;
cout << "Enter the number till where you want the Fibonacci series\n";
//Taking user input
cin >> number;
//assigning zero and one to variables
fibo_0 = 0;
fibo_1 = 1;
cout << "The Fibonacci Series is given below\n";
cout << endl
<< fibo_0 << endl
<< fibo_1;
//Applying for loop to print the series
for (i = 1; i <= number - 2; i++)
{
fibo = fibo_0 + fibo_1;
cout << endl
<< fibo;
fibo_0 = fibo_1;
fibo_1 = fibo;
}
break;
//This is the third case for Palindrome numbers
case 3:
//Declaring variables
int numb, pal, temporary, def;
cout << "Enter the number to check whether it is Palindrome or not\n";
//Taking user input
cin >> numb;
pal = numb;
//assigning zero to temporary variable
temporary=0;
//applying while loop till the number is not equal to zero
while (pal != 0)
{
def = pal % 10;
temporary = (temporary * 10) + def;
pal = pal / 10;
}
if (numb == temporary)
{
cout << numb << " Is a Palindrome number\n";
}
else
{
cout << numb << " Is not a Palindrome number\n";
}
break;
default:
cout << "**Invalid Choice**";
break;
}
return 0;
}
//This is the function named loop the run the program according to users choice
int loop()
{
char cont;
do
{
cout << "\n\n---------------------------------\n";
cout << "\nDo you want to continue ??\n"
<< "For YES 'y'"
<< " For NO 'n'" << endl;
cin >> cont;
if (cont == 'y')
{
Cases();
}
else
{
cout << "\n***Thank You***\n";
}
} while (cont == 'y');
return 0;
}
//This is the main function of program
int main()
{
cout << "\n\n*** Welcome User ***\n";
//Calling the cases function for fisrt time
Cases();
//Calling the loop function to control the flow of program according user
loop();
return 0;
}
Java
package com.company;
import java.util.*;
// Compiler version JDK 11.0.2
class Dcoder {
// making armstrong() function
public static void armstrong()
{
//defining and initializing some variables
int a = 0, temp1, temp2 = 0, armInt = 0;
//statement to display output
System.out.println("Enter number to check whether its armstrong or not");
//syntax to accept input from user
Scanner input = new Scanner(System.in);
armInt = input.nextInt();
temp1 = armInt;
//while loop runs till the condition becomes false
while (temp1 > 0) {
a = temp1 % 10;
temp1 = temp1 / 10;
temp2 = temp2 + (a * a * a);
}
//if else ladder checks the condition statement once
if (armInt == temp2) {
System.out.println(armInt + " is an armstrong number");
} else {
System.out.println(armInt + " is an not armstrong number");
}
}
// making palindrome() function
public static void palindrome()
{
String palStr = "", palRev = "";
System.out.println("Enter the number to check whether its a Palindrome or not");
Scanner input = new Scanner(System.in);
palStr = input.nextLine();
for (int i = (palStr.length() - 1); i >= 0; i--) {
palRev = palRev + palStr.charAt(i); // reverse the string
}
if (palRev.equals(palStr)) {
System.out.println(palStr + " is a Palindrome number");
} else {
System.out.println(palStr + " is an not a Palindrome number");
}
}
// making fibonacci() function
public static void fibonacci()
{
System.out.println("How many Fibonacci numbers do you want ?");
Scanner input = new Scanner(System.in);
int n = input.nextInt();
n = n - 2;
int a = 0, b = 1, c = 0;
System.out.println(a);
System.out.println(b);
for (int i = 1; i <= n; i++) {
c = a + b;
System.out.println(c);
a = b;
b = c;
}
}
//the main function
//main gate of program
//execution starts from here
public static void main(String[] args)
{
String name;
char repeat = 'y';
int choice;
Scanner input = new Scanner(System.in);
System.out.println("Please enter your name:");
name = input.nextLine();
System.out.println("Hello " + name + "\n");
do {
// choice for user
System.out.println("Enter number from the choices given below");
System.out.println("1 : Check Armstrong number");
System.out.println("2 : Check Palindrome number");
System.out.println("3 : Print Fibonacci numbers");
choice = input.nextInt();
switch (choice) {
// calling the respective function
// according to us3rs choice
// using switch case
case 1 -> armstrong();
case 2 -> palindrome();
case 3 -> fibonacci();
default -> System.out.println("Invalid input please run program again");
}
System.out.println("\n-------------------------------------\n");
// asking if user wants to continue or exit
System.out.println("Do you want to continue or exit");
System.out.println("Enter 'y' to continue OR Enter any other character to exit");
repeat = input.next().charAt(0);
}
while (repeat == 'y' || repeat == 'Y');
System.out.println("Thank You !");
}
}
Python
print("*** Welcome User ****")
#this is the function for fibonacci series
def fibonacci():
n=int(input("Enter the input for fibonacci \n"))
b=0
c=1
print("Fibonacci series is\n",b)
for i in range(1,n):
d=b+c
print(d)
b=c
c=d
#this is the function for Armstrong Number
def Armstrongnumber():
n=int(input("Enter the number to check for Armstrong Number\n"))
b=n
c=0
while b!=0:
d=b%10
c=c+(d*d*d)
b=b//10
if n==c:
print("It is an Armstrong Number\n")
else:
print("It is not an Armstrong number\n")
#this is the function for Pallindrome
def Pallindrome():
n=int(input("Enter the number to check for pallindrome\n"))
b=n
c=0
while b!=0:
d=b%10
c=(c*10) + d
b=b//10
if n==c:
print("The number is Pallindrome\n")
else:
print("The number is not a pallindrome\n")
#this is the main function
def userinput():
a=int(input("select the operation you want to operate \n 1. Armstrong Number \n 2. Fibonacci Series \n 3. Pallindrome number\n"))
if a==1:
#Calling the Armstrongnumber Function
Armstrongnumber()
global repeat
#this is to ask the user to repeat the operation.
print("Do you want to continue ?")
print("To continue press 'y' otherwise 'n'\n")
repeat=input("")
if repeat == "y":
userinput()
else:
print("** THANK YOU **")
elif a==2:
#Calling the fibonacci Function
fibonacci()
print("Do you want to continue ?")
print("To continue press 'y' otherwise 'n'\n")
repeat=input("")
if repeat == "y":
userinput()
else:
print("** THANK YOU **")
elif a==3:
#calling the Pallindrome Function
Pallindrome()
print("Do you want to continue ?")
print("To continue press 'y' otherwise 'n'\n")
repeat=input("")
if repeat =="y":
userinput()
else:
print("** THANK YOU **")
else:
print("you have given the wrong input\n")
print("\n–––—––––––––––––––\n")
print("Do you want to continue ?")
print("To continue press 'y' otherwise 'n'\n")
repeat=input("")
if repeat == "y":
userinput()
else:
print("** THANK YOU **")
#calling the main function
userinput()
Post a Comment
FOR ANY DOUBTS AND ERRORS FEEL FREE TO ASK. YOUR DOUBTS WILL BE ADDRESSED ASAP