Question

Write a program that reads from the keypad an integer and calculates the following: If the...

Write a program that reads from the keypad an integer and calculates the following: If the number given by the user is even, then divide it by 2. If it is an odd number, multiply it by 3 and add 1.The program should repeat this procedure for the calculated new result (which will be printed on the screen) until it reaches number 1. The program will also need to print out how many steps it took overall.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Note: Could you plz go through this code and let me know if u need any changes in this.Thank You
=================================

#include <fstream>
#include <iostream>
#include <iomanip>
#include <cstring>
#include <cstdlib>
#include <ctime>
#include <vector>
using namespace std;


int main() {
   //Declaring variables
int num,cnt=0;
  
   cout<<"Enter a number :";
   cin>>num;
  
   do
   {
   if(num%2==0)
   {
       num/=2;
   }
   else
   {
       num=num*3+1;
   }
   cnt++;
cout<<num<<endl;      
   }while(num!=1);
  
   cout<<"No of Steps :"<<cnt<<endl;

   return 0;
}

===============================

Output:

========================

=====================Could you plz rate me well.Thank You

Add a comment
Know the answer?
Add Answer to:
Write a program that reads from the keypad an integer and calculates the following: If the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In Python 3 - Write a program that reads a sequence of integer inputs from the...

    In Python 3 - Write a program that reads a sequence of integer inputs from the user. When the user is finished entering the integers, they will enter a 'q'. There is no need to check if the entry is a valid integer. All integers in the test data (input) will be between -255 and 255. The program should then print: The smallest and largest of the inputs. The number of even and odd inputs (0 should be considered even)...

  • Write a C program to do the following 1) request user to enter 10 integer into...

    Write a C program to do the following 1) request user to enter 10 integer into an array 2) sort the array in ascending order 3) display the sorted array 4) count the number of odd and even number in the array and print out the result 5) add the value of the odd number and even number and calculate the average of the odd and even number. display the result 6) write function addNumber() to add all the number...

  • This MUST be done in C++ Write a program with a loop to ask the user...

    This MUST be done in C++ Write a program with a loop to ask the user for an integer greater than or equal to 2 and then pass the integer to a function which will produce and display on the console the ulam sequence1. Do not accept a number less than 2. 1The ulam sequence begins with an integer. If it is even, divide by 2. If it is odd, multiply by 3 and add 1. Then apply the appropriate...

  • using C++ Write a program that: a) Inputs an integer n from the keyboard where n<=100....

    using C++ Write a program that: a) Inputs an integer n from the keyboard where n<=100. If n is out of range then print out an error message and ask for another input. This process repeats until a valid value for n is obtained. b) Inputs two 1D arrays of doubles A and B (of size n) from the keyboard. c) Inputs an integer k (from 1 to 3) from the keyboard. d) If k = 1 then it calculates...

  • Write a program that receives a positive integer n from the user and then calculates the...

    Write a program that receives a positive integer n from the user and then calculates the sum below by using a loop. Your program needs to ask the user to re-enter a different number if the user enters a non-positive one. Display the result. 1 SUM 1 2 3 ... n x x n = = = + + + +

  • IN PYTHON 1.Choose a positive integer 2. To get the next number in the sequence we...

    IN PYTHON 1.Choose a positive integer 2. To get the next number in the sequence we do the following: If the integer is odd, we multiply by 3 and add 1. If the integer is even, we divide by 2. It is hypothesized that the above sequence will always converge to the value of 1, regardless of any valid initial choice. This hypothesis is known as the Collatz Conjecture. For example, if we start at 5, the numbers generated by...

  • Design, write, and test a program in MIPS which requests and reads two integer values from...

    Design, write, and test a program in MIPS which requests and reads two integer values from the user, multiply the values together, and then outputs the results.

  • In MIPs, design, write, and test a program which requests and reads two integer values from...

    In MIPs, design, write, and test a program which requests and reads two integer values from the user, multiply the values together, and then outputs the results.

  • In HTML write a program that calculates and displays all factors of a number with a...

    In HTML write a program that calculates and displays all factors of a number with a function. A factor is any number that divides into a number evenly. For examples: Factors of 20 are 1,2,4,5,10,20 For the program: Prompt the user for a number or use an input Call a function to calculate all factors using loops and conditionals and modulus Display all factors to the page Bonus +5 If the factor is Even : Print it in Green If...

  • This Java program reads an integer from a keyboard and prints it out with other comments....

    This Java program reads an integer from a keyboard and prints it out with other comments. Modify the comments at the top of the program to reflect your personal information. Submit Assignment1.java for Assignment #1 using Gradescope->Assignemnt1 on canvas.asu.edu site. You will see that the program has a problem with our submission. Your program is tested with 4 testcases (4 sets of input and output files). In order to pass all test cases, your program needs to produce the same...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT