C++ Programs: Problem & Answer


0

Q: Write a program to read n integer/char values from the keyboard and display the result on the screen.

#include <iostream>
using namespace std;

class Person
{
    char name[30];
    int age,roll,cls;

public:
   void getdata();
   void display();
};

void Person::getdata()
{
    cout<<"\nEnter Your Name: ";
    cin.getline(name, 30);
    cout<<"\nEnter Age: ";
    cin>>age;
    cout<<"\nRoll: ";
    cin>>roll;
    cout<<"Class: ";
    cin>> cls; 
}

void Person::display(){
    cout<<"\nName: "<< name;
    cout<<"\nAge: " << age;
    cout<<"\nRoll: " << roll;
    cout <<"\nClass: " << cls;
}

int main()
{
    Person data;
    data.getdata();
    data.display();
    return 0;
}

Q2: Write a program to display the following output using a single cout statement
Maths = 90
Physics = 77
Chemistry = 69

#include <iostream>
using namespace std;

int main() {
    cout << "Maths = 90\nPhysics = 77\nChemistry = 69" << endl;
    return 0;
}

Q3: Write a program to read two numbers from the keyboard and display the larger value on the screen.

#include <iostream>
using namespace std;

int main() {
    int num1, num2;

    // Read the two numbers from the keyboard
    cout << "Enter the first number: ";
    cin >> num1;

    cout << "Enter the second number: ";
    cin >> num2;

    // Find and display the larger value
    if (num1 > num2) {
        cout << "The larger value is: " << num1 << endl;
    } else {
        cout << "The larger value is: " << num2 << endl;
    }

    return 0;
}

Q4: Write a program that inputs a character and displays its corresponding ASCII value on the screen.

#include <iostream>
using namespace std;

int main() {
    char ch;

    // Read the character from the keyboard
    cout << "Enter a character: ";
    cin >> ch;

    // Display its corresponding ASCII value
    cout << "The ASCII value of '" << ch << "' is: " << static_cast<int>(ch);

    return 0;
}

Q5: Write a program to read the value of a, b, and c and display the value of x where
x = a/b -c

#include <iostream>
using namespace std;

int main() {
    double a, b, c, x;

    // Read the values of a, b, and c from the keyboard
    cout << "Enter the value of a: ";
    cin >> a;

    cout << "Enter the value of b: ";
    cin >> b;

    cout << "Enter the value of c: ";
    cin >> c;

    // Calculate the value of x
    x = a / b - c;

    // Display the value of x
    cout << "The value of x is: " << x << endl;

    return 0;
}

Q6: Write C++ program that will ask for a temperature in Fahrenheit and display it in Celsius.

#include <iostream>
using namespace std;

int main() {
    double fahrenheit, celsius;

    // Read the temperature in Fahrenheit from the user
    cout << "Enter the temperature in Fahrenheit: ";
    cin >> fahrenheit;

    // Convert Fahrenheit to Celsius
    celsius = (fahrenheit - 32) * 5 / 9;

    // Display the temperature in Celsius
    cout << "Temperature in Celsius: " << celsius << endl;

    return 0;
}

Q7: Redo the N.6 program using a class called “temp” and member functions.

#include <iostream>
using namespace std;

class Temp {
    double fahrenheit;
    double celsius;

public:
    void getTemperature();
    void convertToCelsius();
    void displayTemperature();
};

void Temp::getTemperature() {
    cout << "Enter the temperature in Fahrenheit: ";
    cin >> fahrenheit;
}

void Temp::convertToCelsius() {
    celsius = (fahrenheit - 32) * 5 / 9;
}

void Temp::displayTemperature() {
    cout << "Temperature in Fahrenheit: " << fahrenheit << endl;
    cout << "Temperature in Celsius: " << celsius << endl;
}

int main() {
    Temp temperature;
    temperature.getTemperature();
    temperature.convertToCelsius();
    temperature.displayTemperature();

    return 0;
}

More at


Like it? Share with your friends!

0
mrwixxsid

0 Comments

Choose A Format
Personality quiz
Series of questions that intends to reveal something about the personality
Trivia quiz
Series of questions with right and wrong answers that intends to check knowledge
Poll
Voting to make decisions or determine opinions
Story
Formatted Text with Embeds and Visuals
List
The Classic Internet Listicles
Countdown
The Classic Internet Countdowns
Open List
Submit your own item and vote up for the best submission
Ranked List
Upvote or downvote to decide the best list item
Meme
Upload your own images to make custom memes
Video
Youtube and Vimeo Embeds
Audio
Soundcloud or Mixcloud Embeds
Image
Photo or GIF
Gif
GIF format