Suppose integer takes 1 byte of memory space. Write a C++ program which takes 2 integer inputs from user (value must be between 1-100 and first integer must be last 2 digit of your arid number). Convert both input number in binary format and Store each bit in an array. Add both binary numbers and store in separate memory. Finally convert the binary sum into decimal format and display on the screen as shown in the following output. You can use pow() function of math.h header file, for calculation of power.
Answers
Answered by
0
Simple Iterative Solution
The integer entered by the user is stored in variable n. Then the while loop is iterated until the test expression n != 0 is evaluated to 0 (false).
- After first iteration, the value of n will be 345 and the count is incremented to 1.
- After second iteration, the value of n will be 34 and the count is incremented to 2.
- After third iteration, the value of n will be 3 and the count is incremented to 3.
- At the start of fourth iteration, the value of n will be 0 and the loop is terminated.
- Then the test expression isevaluated to false and the loop terminates.
// Iterative C++ program to count
// number of digits in a number
#include <bits/stdc++.h>
using namespace std;
int countDigit(long long n)
{
int count = 0;
while (n != 0)
{
n = n / 10;
++count;
}
return count;
}
// Driver code
int main(void)
{
long long n = 345289467;
cout << "Number of digits : " << countDigit(n);
return 0;
}
// This code is contributed
// by Akanksha Rai
Similar questions
Social Sciences,
2 months ago
English,
4 months ago
Physics,
4 months ago
Hindi,
10 months ago
Science,
10 months ago