1. Write a program to enter a positive integer number and count how many places
ID: 3554663 • Letter: 1
Question
1. Write a program to enter a positive integer number and count how many places the number has. This does NOT require use of an array. Use integer division and modulus in a loop.
Examples: 12323 has 5 places
263 has 3 places
12 has two places
Use integer division and a loop to figure this out.
2. Write a program that declares two arrays. Each array will hold ten integers.
Read in values for the first array from the user. Then assign the values into the second array in reverse order. Print the contents of both arrays to verify the first array was reversed in the second array.
3.
Write a program that declares an array of 15 integers. Fill the array with random integers from 1 to 20, (generate the random numbers, the user does not enter them).
Print the contents of the array.
Once you have done that, count how many of the values in the array are even and how many are odd. Print each of these counts.
Explanation / Answer
#include<iostream>
using namespace std;
int main(){
int n, x, count = 0;
cout << "Enter a positive integer: ";
cin >> n;
x = n;
while(x > 0){
count++;
x /= 10;
}
cout << n << " has " << count << " places ";
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.