Complete the function printNumber which takes an integer input from the user and prints it on the screen.
Use:-
Input(user gives value): 7
Output: 7
Input(user gives value): -5
Output: -5
Input(user gives value): 0
The idea is to take an integer input from the user and print it on the screen. This task can be broken down into the following steps:
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
// Function to take input and display output
void printNumber() {
int number;
// Take input
cin >> number;
// Print output
cout << number;
}
};
// Driver code
int main() {
// Creating an instance of Solution class
Solution sol;
// Function call to take input and display output
sol.printNumber();
return 0;
}import java.util.Scanner;
class Solution {
// Function to take input and display output
public void printNumber(Scanner sc) {
int number;
// Take input
number = sc.nextInt();
// Print output
System.out.print(number);
}
}
// Driver code
class Main {
public static void main(String[] args) {
// Creating an instance of Solution class
Solution sol = new Solution();
// Scanner class
Scanner sc = new Scanner(System.in);
// Function call to take input and display output
sol.printNumber(sc);
}
}class Solution:
# Function to take input and display output
def printNumber(self):
# Take input
number = int(input())
# Print output
print(number)
# Driver code
if __name__ == "__main__":
# Creating an instance of Solution class
sol = Solution()
# Function call to take input and display output
sol.printNumber()class Solution {
// Function to take input and display output
printNumber(value) {
const n = value;
console.log(n);
}
}
// Input number
let n = 6678;
/* Creating an instance of
Solution class */
let sol = new Solution();
// Function call to print the number
sol.printNumber(n);
Time Complexity: O(1), Because the operations of receiving input and printing output are executed once regardless of the size of the input.
Space Complexity: O(1), Using only a single variable to store the input and not using any additional space that grows with the input size.
Your notes are automatically saved in your browser's local storage and will persist across sessions on this device.