0% found this document useful (0 votes)
45 views3 pages

C++ Programs

The document contains several C++ programs demonstrating basic functionalities such as printing a name using different methods (printf, puts, and cout), checking if a number is even or odd, printing a user-entered number, and adding two integers. Each program includes the necessary code and expected output. The examples illustrate fundamental concepts in C++ programming.

Uploaded by

damodar
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)
45 views3 pages

C++ Programs

The document contains several C++ programs demonstrating basic functionalities such as printing a name using different methods (printf, puts, and cout), checking if a number is even or odd, printing a user-entered number, and adding two integers. Each program includes the necessary code and expected output. The examples illustrate fundamental concepts in C++ programming.

Uploaded by

damodar
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

C++

Hello World Program


// C++ program to demonstrate how to print your
// name using printf() method
#include <bits/stdc++.h>
using namespace std;

int main() {

// Printing the name using printf() method


printf("Anmol");
return 0;
}
Output
Anmol
// C++ Program to demonstrate how to print your

// name using puts() method

#include <bits/stdc++.h>

using namespace std;

int main() {

// Printing the name using puts function

puts("Anmol");

return 0;

Output
Anmol
// C++ program demonstrate how to print your

// name by taking it as input

#include <bits/stdc++.h>
using namespace std;
int main() {
// Variable to store the name
string str;
// Taking the name string as input using
// cin object
cin >> str;
// Print the name string using cout object
cout << str;
return 0;
}
Input
Anmol
Output
Anmol

Check Whether Number is Even or odd using if else


#include <iostream>
using namespace std;

int main() {
int n;

cout << "Enter an integer: ";


cin >> n;

if ( n % 2 == 0)
cout << n << " is even.";
else
cout << n << " is odd.";

return 0;
}
Output
Enter an integer: 23
23 is odd.

Print Number Entered by User


#include <iostream>
using namespace std;

int main() {
int number;

cout << "Enter an integer: ";


cin >> number;

cout << "You entered " << number;


return 0;
}
Output
Enter an integer: 23
You entered 23

Program to Add Two Integers


#include <iostream>
using namespace std;

int main() {

int first_number, second_number, sum;

cout << "Enter two integers: ";


cin >> first_number >> second_number;

// sum of two numbers in stored in variable sumOfTwoNumbers


sum = first_number + second_number;

// prints sum
cout << first_number << " + " << second_number << " = " << sum;

return 0;
}
Output
Enter two integers: 4
5
4+5=9

You might also like