0% found this document useful (0 votes)
2 views17 pages

First Assign

The document contains a series of C++ programming examples, including basic programs for input/output, a simple calculator, temperature conversion, factorial calculation, palindrome checking, a number guessing game, file I/O, array manipulation, and error handling. Each example demonstrates fundamental programming concepts and techniques in C++. Additionally, it includes explanations of forward declarations and storage classes in C++.

Uploaded by

savliyatanish
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)
2 views17 pages

First Assign

The document contains a series of C++ programming examples, including basic programs for input/output, a simple calculator, temperature conversion, factorial calculation, palindrome checking, a number guessing game, file I/O, array manipulation, and error handling. Each example demonstrates fundamental programming concepts and techniques in C++. Additionally, it includes explanations of forward declarations and storage classes in C++.

Uploaded by

savliyatanish
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/ 17

1. "Hello, World!

" Program
#include <iostream>

using namespace std;

int main() {

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

return 0;

}
------------------------------------------------
2. Basic Input/Output Program
#include <iostream>

#include <string>

using namespace std;

int main() {

string name;

int age;

cout << "Enter your name: ";

getline(cin, name);
cout << "Enter your age: ";

cin >> age;

cout << "Hello, " << name << "! You are " <<
age << " years old." << endl;

return 0;

}
---------------------------------------------------
--------------
3. Simple Calculator Program
#include <iostream>

using namespace std;

int main() {

double num1, num2;

char operation;

cout << "Enter first number: ";

cin >> num1;

cout << "Enter second number: ";

cin >> num2;


cout << "Enter operation (+, -, *, /): ";

cin >> operation;

double result;

switch (operation) {

case '+':

result = num1 + num2;

break;

case '-':

result = num1 - num2;

break;

case '*':

result = num1 * num2;

break;

case '/':

if (num2 != 0) {
result = num1 / num2;

} else {

cout << "Error: Division by zero!"


<< endl;

return 1;

break;

default:

cout << "Error: Invalid operation!" <<


endl;

return 1;

cout << num1 << " " << operation << " " << num2
<< " = " << result << endl;

return 0;

}
---------------------------------------------------
----------------------
4. Temperature Conversion Program
#include <iostream>

using namespace std;

double celsiusToFahrenheit(double celsius) {

return (celsius * 9 / 5) + 32;

int main() {

double celsius;

cout << "Enter temperature in Celsius: ";

cin >> celsius;

double fahrenheit =
celsiusToFahrenheit(celsius);

cout << celsius << " degrees Celsius = " <<


fahrenheit << " degrees Fahrenheit" << endl;

return 0;

}
---------------------------------------------------
---------------
5. Factorial Calculation Program.
#include <iostream>

using namespace std;

int calculateFactorial(int n) {

if (n == 0 || n == 1) {

return 1;

} else {

return n * calculateFactorial(n - 1);

int main() {

int number;

cout << "Enter a non-negative integer: ";

cin >> number;

if (number < 0) {
cout << "Error: Factorial is not defined
for negative numbers!" << endl;

return 1;

int factorial = calculateFactorial(number);

cout << "Factorial of " << number << " is " <<
factorial << endl;

return 0;

}
---------------------------------------------------
---------------------
6. Palindrome Checker Program
#include <iostream>

#include <string>

#include <algorithm>

using namespace std;

bool isPalindrome(string str) {

// Remove non-alphanumeric characters and


convert to lowercase

str.erase(remove_if(str.begin(), str.end(),
[](char c) { return !isalnum(c); }), str.end());

transform(str.begin(), str.end(), str.begin(),


::tolower);

// Check if the string is equal to its reverse

return equal(str.begin(), str.begin() +


str.size() / 2, str.rbegin());

int main() {

string str;

cout << "Enter a string: ";

getline(cin, str);

if (isPalindrome(str)) {

cout << "The string is a palindrome." <<


endl;

} else {
cout << "The string is not a palindrome."
<< endl;

return 0;

}
---------------------------------------------------
-------------------
7. Number Guessing Game
#include <iostream>

#include <cstdlib>

#include <ctime>

using namespace std;

int main() {

srand(time(0)); // Seed the random number


generator

int secretNumber = rand() % 100 + 1; //


Generate a random number between 1 and 100

int guess;

int numGuesses = 0;
cout << "Welcome to the Number Guessing Game!"
<< endl;

cout << "I'm thinking of a number between 1 and


100." << endl;

do {

cout << "Enter your guess: ";

cin >> guess;

numGuesses++;

if (guess < secretNumber) {

cout << "Too low! Try again." << endl;

} else if (guess > secretNumber) {

cout << "Too high! Try again." << endl;

} else {

cout << "Congratulations! You guessed


the number in " << numGuesses << " guesses!" <<
endl;

}
} while (guess != secretNumber);

return 0;

}
---------------------------------------------------
--------------------
8. Basic File I/O Program
#include <iostream>

#include <fstream>

#include <string>

using namespace std;

int main() {

string filename = "example.txt";

string data;

// Writing to a file

ofstream outputFile(filename);

if (outputFile.is_open()) {

cout << "Enter data to write to the file


(press Enter without input to finish):" << endl;

while (getline(cin, data) && !data.empty())


{

outputFile << data << endl;

outputFile.close();

cout << "Data written to the file


successfully." << endl;

} else {

cout << "Unable to open the file for


writing." << endl;

// Reading from a file

ifstream inputFile(filename);

if (inputFile.is_open()) {

cout << "Contents of the file:" << endl;

while (getline(inputFile, data)) {


cout << data << endl;

inputFile.close();

} else {

cout << "Unable to open the file for


reading." << endl;

return 0;

}
---------------------------------------------------
-----------------
9. Array Manipulation Program
#include <iostream>

using namespace std;

const int SIZE = 5;

void printArray(const int arr[], int size) {

for (int i = 0; i < size; i++) {


cout << arr[i] << " ";

cout << endl;

int main() {

int numbers[SIZE];

// Initialize the array with user input

cout << "Enter " << SIZE << " integers:" <<
endl;

for (int i = 0; i < SIZE; i++) {

cin >> numbers[i];

cout << "Original array: ";

printArray(numbers, SIZE);

// Multiply each element by 2

for (int i = 0; i < SIZE; i++) {


numbers[i] *= 2;

cout << "Array after multiplying each element


by 2: ";

printArray(numbers, SIZE);

// Find the sum and average of the elements

int sum = 0;

for (int i = 0; i < SIZE; i++) {

sum += numbers[i];

double average = static_cast<double>(sum) /


SIZE;

cout << "Sum of the elements: " << sum << endl;

cout << "Average of the elements: " << average


<< endl;

return 0;
}
---------------------------------------------------
---------------------
10. Errors in C++
#include <iostream>
using namespace std;

int main() {
int x = 10;
cout << x / 0; // Runtime error: division by
zero
return 0;
}
11. Forward Declarations in C++
#include <iostream>
using namespace std;

void displayMessage(); // Forward declaration

int main() {
displayMessage();
return 0;
}

void displayMessage() { // Function definition


cout << "Hello, world!" << endl;
}
12. Storage Classes in C++
#include <iostream>
using namespace std;
void counterFunction() {
static int count = 0; // Static variable
retains its value
count++;
cout << "Count: " << count << endl;
}

int main() {
counterFunction();
counterFunction();
counterFunction();
return 0;
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy