Simple question | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Simple question

PROBLEM Write a C++ program to compute the powers of 2. The program must take a single integer number 'n' as input and print the value of 2 raised to the power 'n'. INPUT FORMAT A single integer ranging from 0 to 30 (Both Inclusive) You need to ensure that you handle all values within this range. You are not required to verify that the input integer in within range or not. CONSTRAINTS 0 <= n <= 30 OUTPUT FORMAT  Output a single integer denoting the value of 2 raised to the power n SAMPLE EXAMPLE:       Input - 4      Output - 16 (computed as 2^4 = 2*2*2*2)

16th Aug 2018, 7:33 PM
Satyam Kesari
Satyam Kesari - avatar
2 Answers
+ 3
#include <cmath> #include <iostream> int main(){ int value; std::cin >> value; std::cout <<pow(2,value); return 0; }
16th Aug 2018, 7:40 PM
Dlite
Dlite - avatar
+ 3
#include <iostream> int main() { unsigned v; std::cin >> v; std::cout << (1<<v); return 0; } *Hope this is not your Homework, lol.*
16th Aug 2018, 9:24 PM
Maz
Maz - avatar