0% found this document useful (0 votes)
2 views

Content for video c++

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

Content for video c++

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

Basic C++ program to print a message.

#include <iostream>
using namespace std;
int main() {
cout << "Welcome to C++ Programming!" << endl;
return 0;
}

Modify the program to print multiple lines.

#include <iostream>
using namespace std;
int main() {
cout << "Hello, World!" << endl;
cout << "This is my first C++ program." << endl;
return 0;
}

Print multiple statements on a single line using \n.


#include <iostream>
using namespace std;
int main() {
cout << "Line 1\nLine 2\nLine 3";
return 0;
}

Demonstrate std::cout without using namespace std;

#include <iostream>
int main() {
std::cout << "This program does not use 'using namespace std;'";
return 0;
}

Write a program to take user input and print it.

#include <iostream>
using namespace std;
int main() {
string name;
cout << "Enter your name: ";
cin >> name;
cout << "Hello, " << name << "!";
return 0;
}

Declare and initialize different data types.

#include <iostream>
using namespace std;
int main() {
int a = 10;
float b = 5.5;
char c = 'A';
bool d = true;
cout << a << " " << b << " " << c << " " << d;
return 0;
}
Demonstrate local and global variable scope.

#include <iostream>
using namespace std;
int x = 50; // Global variable
int main() {
int x = 10; // Local variable
cout << "Local x: " << x << endl;
cout << "Global x: " << ::x;
return 0;
}

Demonstrate const keyword for constants.

#include <iostream>
using namespace std;
int main() {
const float PI = 3.1416;
cout << "Value of PI: " << PI;
return 0;
}

Demonstrate static storage class.

#include <iostream>
using namespace std;
void counter() {
static int count = 0;
count++;
cout << "Counter: " << count << endl;
}
int main() {
counter();
counter();
counter();
return 0;
}

Arithmetic operators.

#include <iostream>
using namespace std;
int main() {
int a = 10, b = 3;
cout << "Addition: " << (a + b) << endl;
cout << "Subtraction: " << (a - b) << endl;
cout << "Multiplication: " << (a * b) << endl;
cout << "Division: " << (a / b) << endl;
return 0;
}

Relational operators.

#include <iostream>
using namespace std;
int main() {
int x = 10, y = 20;
cout << (x == y) << endl;
cout << (x > y) << endl;
return 0;
}

Take multiple user inputs.

#include <iostream>
using namespace std;
int main() {
int a, b;
cout << "Enter two numbers: ";
cin >> a >> b;
cout << "Sum: " << a + b;
return 0;
}

Check if a number is even or odd.

#include <iostream>
using namespace std;
int main() {
int num;
cout << "Enter a number: ";
cin >> num;
if (num % 2 == 0)
cout << "Even";
else
cout << "Odd";
return 0;
}

Check largest of three numbers using nested if-else.

#include <iostream>
using namespace std;
int main() {
int a, b, c;
cout << "Enter three numbers: ";
cin >> a >> b >> c;
if (a >= b && a >= c)
cout << "Largest: " << a;
else if (b >= a && b >= c)
cout << "Largest: " << b;
else
cout << "Largest: " << c;
return 0;
}

Print numbers from 1 to 10 using for loop.

#include <iostream>
using namespace std;
int main() {
for (int i = 1; i <= 10; i++)
cout << i << " ";
return 0;
}

Find the sum of numbers using while loop.


#include <iostream>
using namespace std;
int main() {
int num, sum = 0;
cout << "Enter a number: ";
cin >> num;
while (num > 0) {
sum += num;
num--;
}
cout << "Sum: " << sum;
return 0;
}

Use break to exit a loop.

#include <iostream>
using namespace std;
int main() {
for (int i = 1; i <= 10; i++) {
if (i == 5)
break;
cout << i << " ";
}
return 0;
}

Use continue to skip an iteration.

#include <iostream>
using namespace std;
int main() {
for (int i = 1; i <= 10; i++) {
if (i == 5)
continue;
cout << i << " ";
}
return 0;
}

You might also like