Question

In: Computer Science

Write a program in c++ using only while and for loops . Use of arrays and...

Write a program in c++ using only while and for loops . Use of arrays and functions is not allowed.

Given the first value, generate the next ten terms of the sequence like 1, 2, 4, 8, 16, 22, 26,
38, 62, 74, 102, 104, …

Explaination with code is required.

Solutions

Expert Solution

C++ code

#include <iostream>
using namespace std;

int main() {
  
   int start ;
   //takes the starting number of sequence
   cout<<" Enter the first term of sequence "<<endl;
   cin>>start;
   int i ;
   //for loop as prescribed to use
   for( i = 0 ; i < 10 ; i++ ){
       //storing the element
       int prev = start ;
       //printing the newly calculated element of sequence
       cout<< start << ", ";
       int add = 1;
       //while loop as prescribed to use
       //logic
       // adding the number and the product of non-zero digits
       while( start != 0 ){
           //checking if the digit is zero or not
           int r = start%10;
           if( r != 0 ){
               //so that this is the new value to add into element
               add *= r;
           }
           //parsing every digit
           start = start/10;
       }
       //adding new value to the element and getting new element of sequence
       start = prev + add ;
   }
   //end of for loop
   return 0;
}

Output

74 as first term

 Enter the first term of sequence 
74, 102, 104, 108, 116, 122, 126, 138, 162, 174, 

1 as first term

 Enter the first term of sequence 
1, 2, 4, 8, 16, 22, 26, 38, 62, 74, 
 

202 as first term

 Enter the first term of sequence 
202, 206, 218, 234, 258, 338, 410, 414, 430, 442, 

Related Solutions

In C program, Use "do...while" and "for" loops to write a program that finds all prime...
In C program, Use "do...while" and "for" loops to write a program that finds all prime numbers less than a specified value.
Code in Python. You can only use while loops NOT for loops. Program 1: cost_living In...
Code in Python. You can only use while loops NOT for loops. Program 1: cost_living In 2020 the average cost of living/month (excluding housing) for a family of 4 in Pittsburgh was $3850 per month. Write a program to print the first year in which the cost of living/month is over $4450 given that it will rise at a rate of 2.1% per year. (Note:  this program requires no input). Program 2: discount A discount store is having a sale where...
Objective: Write this program in the C programming language Loops with input, strings, arrays, and output....
Objective: Write this program in the C programming language Loops with input, strings, arrays, and output. Assignment: It’s an organization that accepts used books and then sells them a couple of times a year at book sale events. Some way was needed to keep track of the inventory. You’ll want two parallel arrays: one to keep track of book titles, and one to keep track of the prices. Assume there will be no more than 10 books. Assume no more...
I need a C++ program using while loops that counts the number of characters in a...
I need a C++ program using while loops that counts the number of characters in a sentence. The user inputs a sentence and then terminates the input with either '.' or '!'. And then it needs to count and display the number of a's, e's, i's, o's, u's, and consonants. The program should read both lower and upper case. They don't want us using switch statements or string operators, and want us to us if else if statements. I have...
C++ DO not use arrays to write this program. Write a program that repeatedly generates three...
C++ DO not use arrays to write this program. Write a program that repeatedly generates three random integers in the range [1, 100] and continues as follows: If the right-most digit of all the three integers is equal, the program displays them in ascending order on the screen and continues. If the generated integers have different right-most digits, they are not displayed and the program continues. The program terminates once the right-most digits of all the three random numbers are...
C++ . It should all be in one code and using WHILE loops 2. Write a...
C++ . It should all be in one code and using WHILE loops 2. Write a piece of code that asks the user for a number and adds up the even numbers from 1 to that entered number. 3. Write a piece of code that asks the user for 2 numbers and adds up the numbers between and including the numbers. 4. Write another piece of code that asks the user for a random file name and adds all of...
using only loops, no functions and no arrays. with the heading: #include <iostream> using namespace std;...
using only loops, no functions and no arrays. with the heading: #include <iostream> using namespace std; "Write a program that asks the user to enter an odd positive integer. The program reads a value (n) entered by the user and prints an n x n grid displaying a large letter X. The left half should be made up of pluses (+) and the right half should be made with the character "x" and the very center should be an asteric...
Using only loops, no functions and no arrays. with the heading: #include <iostream> using namespace std;...
Using only loops, no functions and no arrays. with the heading: #include <iostream> using namespace std; "Write a program that asks the user to enter an integer between 1 and 20. If the user enters an illegal number, the program repeatedly asks the user to enter the correct one. If the user has not entered a correct number after 10 attempts, the program chooses the number 10 as the user's number. The program prints the cube of the user's number.
Write a program of Binary Search in C++ by using function and arrays with the explanation.
Write a program of Binary Search in C++ by using function and arrays with the explanation.
Write a program in C# for a Cricket match using Jagged Arrays. The name of the...
Write a program in C# for a Cricket match using Jagged Arrays. The name of the project will be on your name. It has the following modules: Create two functions in class Cricket_Match for random numbers generation. The first function generates 1-6 numbers which is known as balls played by each player. The second function generates a 0-6 number which is known as the score produced against each ball by an individual player. In the main show the individual player...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT