Hello Readers,

In this post we are going to learn about three terms which are as follows:-

1.PALINDROME:-

A palindrome is a word, number, phrase, or other sequence of characters which reads the same backward as forward, such as madam, racecar. There are also numeric palindromes, including date/time stamps using short digits 11/11/11   11:11 and long digits 02/02/2020.

So, lets understand all the logic in the code in C++, JAVA, PYTHON.

Now, Lets understand the logic for Palindrome 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. First declare five variables as a, b, c, d, i. (we will be using these five variables only).

2. Now use variable 'a' to take the input from the user.

3. Assign variable 'b' also  the same as 'a' i.e.(b==a).

4. And declare variable 'c' as 0.

5. Now using while loop(while b!=0) i.e. the loop will be executed until the value of b is equal to 0.

6. Using modulus operator(%) divide the number by 10 and store the value of remainder to variable 'd' (% - modulus operator is used to get the remainder during division). [d= b%10]

7. Now multiply variable 'c' with 10 and add value of d to it. [ c=( c*10)+d].

8. Now divide the value of b(i.e. the input value) by 10 and store the quotient value in b only.

9. In this way in the loop the value will be stored in  variable 'c' one by one and if the value of c is equal to the input value (variable 'a') then the number is palindrome and if not then the number is not palindrome.

Eg. 
Let's say input value a= 121
then b=a
and c=0
Now in loop
While (b!=0)
d=b%10
c=(c*10) + d
b=b/10

Now conditional statement
If(c==b/a)
The number is palindrome

Else
The number is not palindrome

NOTE: The Above Example program does not follow any syntax its just for explanation purpose.

Alternative Method for Palindrome:

1. First take the string input from the user and assign it to 'a' variable 

2. Now reverse the string input and store it in the second variable i.e. 'b' 

3. Now compare both the variables and if both the variables are same then the number is palindrome else it is not a palindrome

Eg. a=12321
Now reverse the input string and store it in b
i.e. b= 12321

Now conditional statement
If(a==b)
The number is palindrome

Else:
The number is not a palindrome



2.FIBONACCI SERIES:-

In mathematics, the Fibonacci numbers form a sequence, called the Fibonacci sequence, such that each number is the sum of the two preceding ones, starting from 0 and 1.

The beginning of the sequence is thus:

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89....

So, lets understand all the logic in the code in C++, JAVA, PYTHON.

Now, Lets understand the logic for Fibonacci series 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. First declare five variables as a,b,c,d,i

2. Now take the input from the user and store it in the variable 'a'.

3. Predefine the value of the variables 'b' and 'c' as 0 and 1. i.e.(b = 0 and c= 1).

4. Now As we know that any Fibonacci series starts with 0 and 1 so print the variable 'b' before loop.

5. Now using for loop by starting the iterator (i) with 1 till the input number(a) till 'i' becomes equal to 'a' the for loop will increment 'i' by 1.

6. Now add the value of 'b' and 'c' and store the added value to variable 'd' i.e. [d = b+c]

7. Now print the value of 'd'.

8. After printing the value store the value of 'c' to 'b' and the value of 'd' to 'b'.

9. In this way by the loop a Fibonacci series will be formed.

Eg. 
Let's say input value a= 5
Then declare b=0 and c= 1

Now print the value of b 
"Fibonacci series is \n ",b

Now use the loop
For(i=1, i<=a, i++)
d=b+c
Print the value of d 
b=c
c=d

NOTE: The Above Example program does not follow any syntax its just for explanation purpose.


3.ARMSRTONG NUMBERS:-

An Armstrong number of three digits is an integer such that the sum of the cubes of its digits is equal to the number itself.

Example:-
1. 153 = 1^3 + 5^3 + 3^3

2. 370 = 3^3 + 7^3 + 0^3

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




Document

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;
 }

          
    

Output for C++


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 !");
    }
 }
    

Output for JAVA


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() 		

Output for PYTHON

//ENJOY CODING\\



Post a Comment

FOR ANY DOUBTS AND ERRORS FEEL FREE TO ASK. YOUR DOUBTS WILL BE ADDRESSED ASAP

Previous Post Next Post