vendredi 13 novembre 2020

Iterate statements without using any loops in c++

I am very new to programming & learning C++.

I am making a program that asks for two positive numbers from the user. The first number is a single-digit positive integer, say 'len' number of digits in the number, whereas the second number is a positive integer, say 'num', of len-digits. The number of digits, 'len', shall be greater than 5. The program, then, calculates the reverse, rev, of the number, 'num', such that the third and fifth digits of the number, 'num', are increased by 1 in the reversal process. Finally, the program displays the original and reversed numbers. (If after an increase by 1 the number becomes 10, then it should be treated as zero. In other words, the addition is modulo 10.)

Output:

Original Number = 112294078

Reverse Number = 871402211

This is what I have made:

#include <iostream>
#include<conio.h>
using namespace std;
int main()
{
    int number, reverse = 0;
    int len, lenchng;
    cout << "Input a Number to Reverse and press Enter: ";
    cin >> number;   
    cout << "Input a Number to change and press Enter: ";
    cin >> len;

    if (len>=5) {

        while (number != 0)
        {
            reverse = reverse * 10;

            if (len == number % 10)
            {
                if (len == 9) {
                    lenchng = 0;
                    reverse = reverse + lenchng;
                    number = number / 10;
                }
                else
                {
                    lenchng = 1 + number % 10;
                    reverse = reverse + lenchng;
                    number = number / 10;

                }

            }
            else
            {
                reverse = reverse + number % 10;
                number = number / 10;
            }
            
        }
        cout << "New Reversed Number is:  " << reverse;

    }
    else
    {
        cout << "less then 5";
    }

    _getch();
    
    return 0;
    
}

I am getting the work done but, my requirement is I have to use only if & else conditions.

I can't use loops of any kind or anything else like functions etc, like I used while loop in the above code.

Is there a way to perform this without any loop.

(Note:) I have to use only if, else and if else statements nothing else

Any help will be really appreciated. Thanks

Aucun commentaire:

Enregistrer un commentaire