Tuesday, 1 September 2026

Print an Integer Entered by User

 

Option 1: C Program to Print an Integer Entered by User

// Including the standard input-output header

#include <stdio.h> 

 

int main() {

   

    // Declaring an integer variable

    int number;

   

    // Asking user to input an integer

    printf("Enter an integer: ");

   

    // Reading the integer entered by

    // the user and storing it in 'number'

    scanf("%d", &number);

   

    // Printing the entered integer

    printf("You entered: %d", number);

    return 0; 

}

Run Code

 

Output:

Enter an integer: 5

You entered: 5

 

Explanation

#include <stdio.h>: This line includes the Standard Input-Output library in C, which is necessary to use functions like scanf() for input and printf() for output.

 

int main(): The main() function is where the execution of the program begins.

 

int number;: This line declares an integer variable named number that will hold the user input.

 

printf("Enter an integer: ");: This line prompts the user to enter an integer.

 

scanf("%d", &number);: This is the key function for reading input. The scanf() function reads the integer entered by the user and stores it in the variable number. The %d is a format specifier used to read integers, and the &number refers to the memory address where the input value will be stored.

 

printf("You entered: %d", number);: This line displays the message "You entered:" followed by the integer value that the user inputted. The %d format specifier is used to print the integer stored in the variable number.

 

return 0;: This indicates the successful completion of the program. Returning 0 from the main() function is a way of telling the operating system that the program executed successfully.

 

 

Time Complexity Analysis

Time Complexity : O(1)

 

 

Space Complexity Analysis

Space Complexity : O(1)

 

Option 2: C Program to Print Integer Using getchar()

#include <stdio.h>

 

int main() {

    char ch;

    int number = 0;

    printf("Enter an integer: ");

 

    // Take input using getchar()

    while ((ch = getchar()) != '\n') {

       

        // Convert character to integer

        number = number * 10 + (ch - '0'); 

    }

 

    printf("You entered: %d", number);

    return 0;

}

Run Code

 

Output:

Enter an integer: 123

You entered: 123

 

Explanation:

getchar() reads the input character by character until it encounters a newline (\n). Each character is converted to its integer value using ch - '0' and added to the number.

 

 

Time Complexity Analysis

Time Complexity : O(1)

 

 

Space Complexity Analysis

Space Complexity : O(1)

 

Option 3: C Program to Print Integer Using fgets() and atoi()

#include <stdio.h>

 

// Include lib.h library for atio() function

#include <stdlib.h>

 

int main() {

   

    // Array to store input as a string

    char input[10]; 

    printf("Enter an integer: ");

   

    // Reading input as a string

    fgets(input, 10, stdin);

   

    // Converting string to integer

    int number = atoi(input); 

   

    printf("You entered: %d", number);

    return 0;

}

Run Code

 

Output:

Enter an integer: 45

You entered: 45

 

 

Time Complexity Analysis

Time Complexity : O(1)

 

 

Space Complexity Analysis

Space Complexity : O(1)

 

Option 4: Print Integer in C Using sscanf() Instead of scanf()

#include <stdio.h>

 

int main() {

   

    // Declare character array

    char input[10];

    int number;

   

    printf("Enter an integer: ");

   

    // Reading input as a string using

    // fgets() function

    fgets(input, 10, stdin);

   

    // Extracting integer from string

    sscanf(input, "%d", &number); 

   

    printf("You entered: %d", number);

    return 0;

}

Run Code

 

Output:

Enter an integer: 89

You entered: 89

 

Explanation:

fgets() reads the input as a string.

 

sscanf() extracts the integer from the string and stores it in the number variable.

 

 

Time Complexity Analysis

Time Complexity : O(1)

 

 

Space Complexity Analysis

Space Complexity : O(1)

No comments:

Post a Comment