0% found this document useful (0 votes)
25 views5 pages

C++ Language Programs: Program 2

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views5 pages

C++ Language Programs: Program 2

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

C++ LANGUAGE PROGRAMs

Program 2
C++

#include <iostream>

using namespace std;

int main() {

string name;

cout << "Enter your name: ";

cin >> name;

cout << "Hello, " << name << "!" << endl;

return 0;

3. Calculating Area of a Rectangle:


C++

#include <iostream>

using namespace std;

int main() {

double length, width, area;

cout << "Enter the length of the rectangle: ";

cin >> length;

cout << "Enter the width of the rectangle: ";

cin >> width;

area = length * width;

cout << "The area of the rectangle is: " << area << endl;

return 0; }

4. write a program to input speed and time to calculate distance and then
print it.
C++

#include <iostream>

using namespace std;


int main() {

double speed, time;

cout << "Enter the speed in kilometers per hour: ";

cin >> speed;

cout << "Enter the time in hours: ";

cin >> time;

// Calculate distance

double distance = speed * time;

cout << "Distance traveled: " << distance << " kilometers" << endl;

return 0;

6.Here's a simple C++ program for adding two numbers


C++

#include <iostream>

using namespace std;

int main() {

int num1, num2, sum;

cout << "Enter two numbers: ";

cin >> num1 >> num2;

sum = num1 + num2;

cout << "The sum of " << num1 << " and " << num2 << " is: " << sum << endl;

return 0;

Write a program that displays the following triangle pattern on the screen:
#include<iostream>

using namespace std;

int main(){

cout<<" *"<<endl;

cout<<" ***"<<endl;

cout<<" *****"<<endl;

cout<<" ********"<<endl;
return 0;

8. Write a program any two number division


# include <iostream>

using namespace std;

int main() {

double x = 10.0, y = 3.0;

cout << "Floating-point Division (x / y): " << (x / y) << endl; // Output: 3.3333...

return 0;

9. Write a program any two number Multiplication


# include <iostream>

using namespace std;

int main() {

double x = 10, y = 3;

cout << "Floating-point Division (x * y): " << (x *y) << endl;

return 0;

10.Write a program the result of student are pass or fail.


# include <iostream>

using namespace std;

int main() {

int number;

cout << "Enter a number: ";

cin >> number;

if (number >=50) {

cout << "PASS"<<endl;

}else{

cout << "Fail"<<endl;

return 0;
}

11. Check if a number is positive


# include <iostream>

using namespace std;

int main() {

int number;

cout << "Enter a number: ";

cin >> number;

if (number > 0) {

cout << "The number is positive." << endl;

return 0;

The Increment and Decrement Operators


12. Write a program to demonstrate the difference between postfix and prefix increment
operator
# include <iostream>

using namespace std;

int main() {

int x = 5;

cout << "Postfix: " << x++ << endl; // Outputs 5, then x becomes 6

cout << "Prefix: " << ++x << endl; // x becomes 7, then outputs 7

return 0;

13. Program
# include <iostream>

using namespace std;

int main() {

int x = 5;

cout << "Postfix: " << x-- << endl; // Outputs 5, then x becomes 6
cout << "Prefix: " << --x << endl; // x becomes 7, then outputs 7

return 0;}

14. Write a program to print numbers from 1 to 10 using a while loop.


# include <iostream>

using namespace std;

int main() {

int num = 1;

while (num <= 10) { // Continue until num is greater than 10

cout << num << " ";

num++;

return 0;

Output:

1 2 3 4 5 6 7 8 9 10

You might also like