Generic selectors
Exact matches only
Search in title
Search in content
Post Type Selectors

Converts a number into integer, float, and string

Write a program that takes a number as input and converts it into different data types such as integer, float, and string.

Program in C

#include <stdio.h>
#include <stdlib.h>

int main() {
    char number[50];
    int integer_number;
    float float_number;
    char string_number[50];

    printf("Enter a number: ");
    scanf("%s", number);

    // Convert to integer
    integer_number = atoi(number);

    // Convert to float
    float_number = atof(number);

    // Convert to string
    sprintf(string_number, "%s", number);

    printf("Integer value: %d\n", integer_number);
    printf("Float value: %.2f\n", float_number);
    printf("String value: %s\n", string_number);

    return 0;


  • In this C program, we declare variables to store the number in different data types: integer_number for the integer, float_number for the float, and string_number for the string.
  • The scanf function is used to read the user input as a string into the number variable.
  • Then, the atoi function is used to convert the string to an integer and store it in the integer_number variable.
  • The atof function is used to convert the string to a float and store it in the float_number variable.
  • The sprintf function is used to convert the string to a string (a copy of the input) and store it in the string_number variable.
  • Finally, the converted values are printed using printf.


Enter a number: 4
Integer value: 4
Float value: 4.00
String value: 4

Program in Java

import java.util.Scanner;

public class NumberConversion {
    public static void main(String[] args) {
        String number;
        int integerNumber;
        float floatNumber;
        String stringNumber;

        Scanner scanner = new Scanner(;

        System.out.print("Enter a number: ");
        number =;

        // Convert to integer
        integerNumber = Integer.parseInt(number);

        // Convert to float
        floatNumber = Float.parseFloat(number);

        // Convert to string
        stringNumber = number;

        System.out.println("Integer value: " + integerNumber);
        System.out.printf("Float value: %.2f\n", floatNumber);
        System.out.println("String value: " + stringNumber);


  1. import java.util.Scanner;: This line imports the Scanner class from the java.util package. It is used for reading user input.
  2. public class NumberConversion: This line declares a public class named NumberConversion.
  3. public static void main(String[] args): This is the main method that serves as the entry point of the program.
  4. String number;, int integerNumber;, float floatNumber;, and String stringNumber;: These lines declare variables to store the input number in different formats.
  5. Scanner scanner = new Scanner(;: Creates a new Scanner object named scanner to read user input from the console.
  6. System.out.print(“Enter a number: “);: Prints a prompt message to enter a number.
  7. number =;: Reads the input number as a string using
  8. integerNumber = Integer.parseInt(number);: Converts the input string number to an integer using Integer.parseInt().
  9. floatNumber = Float.parseFloat(number);: Converts the input string number to a float using Float.parseFloat().
  10. stringNumber = number;: Assigns the input string number directly to the stringNumber variable.
  11. System.out.println(“Integer value: ” + integerNumber);, System.out.printf(“Float value: %.2f\n”, floatNumber);, and System.out.println(“String value: ” + stringNumber);: These lines print the converted values of the input number in integer, float, and string formats.
Enter a number: 4
Integer value: 4
Float value: 4.00
String value: 4