DATA TYPES IN C++

SAIROSHNI
data types in c++

Data Types

1.Integer - Stores integer(whole number) 
Example: int myNum = 5;
2.Float - Stores floating number with decimal number.
Example: 99.0
3.Char - Stores single character within the single quotes.
Example: 'a','g'.
4.Boolean - Stores boolean values.
Example:True,False
5.String - Stores texts with the double quotes. 
Example: "gocourse".

Integer:

program:

#include <iostream>
int main() {
    int user_input;
    std::cout << "Enter a number: ";
    std::cin >> user_input;
    std::cout << "You entered: " << user_input << std::endl;
    return 0;
}

Output:

Enter a number: [user enters a number, e.g. 42]
You entered: 42

Float:

Program:

#include <iostream>
int main() {
    float user_input;
    std::cout << "Enter a number: ";
    std::cin >> user_input;
    std::cout << "You entered: " << user_input << std::endl;
    return 0;
}

Output:

Enter a number: 3.14159
You entered: 3.14159
Char:

Program:

#include <iostream>
int main() {
    char user_input;
    std::cout << "Enter a character: ";
    std::cin >> user_input;
    std::cout << "You entered: " << user_input << std::endl;
    return 0;
}

Output:

Enter a character: G
You entered: G

Boolean:

Program:

#include <iostream>
int main() {
    bool is_valid = true;
    std::cout << "Is the input valid? " << is_valid << std::endl;
    return 0;
}

Output:

Is the input valid? 10

String:

Program:

#include <iostream>
#include <string>
int main() {
    std::string user_input;
    std::cout << "Enter a string: ";
    std::getline(std::cin, user_input);
    std::cout << "You entered: " << user_input << std::endl;
    return 0;
}

Output:

Enter a string: Gocourse
You entered: Gocourse



Tags
Our website uses cookies to enhance your experience. Learn More
Accept !

GocourseAI

close
send