0% found this document useful (0 votes)
6 views22 pages

C++ Basic Questions

The document contains a collection of C++ programming examples demonstrating various concepts such as input/output, arithmetic operations, control structures, functions, and data structures. Each example includes a brief code snippet that illustrates the specific functionality, such as calculating sums, checking for palindromes, and using pointers. Overall, it serves as a comprehensive guide for beginners to understand fundamental programming techniques in C++.

Uploaded by

VERCHER FF
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)
6 views22 pages

C++ Basic Questions

The document contains a collection of C++ programming examples demonstrating various concepts such as input/output, arithmetic operations, control structures, functions, and data structures. Each example includes a brief code snippet that illustrates the specific functionality, such as calculating sums, checking for palindromes, and using pointers. Overall, it serves as a comprehensive guide for beginners to understand fundamental programming techniques in C++.

Uploaded by

VERCHER FF
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

Hello World

#include <iostream>
using namespace std;

int main() {
cout << "Hello, World!";
return 0;
}

Print Your Name

#include <iostream>
using namespace std;

int main() {
cout << "My name is John.";
return 0;
}

Input and Output

#include <iostream>
using namespace std;

int main() {
int age;
cout << "Enter your age: ";
cin >> age;
cout << "You are " << age << " years old.";
return 0;
}
Add Two Numbers

#include <iostream>
using namespace std;

int main() {
int a, b;
cin >> a >> b;
cout << "Sum = " << a + b;
return 0;
}

Subtract Two Numbers

#include <iostream>
using namespace std;

int main() {
int a, b;
cin >> a >> b;
cout << "Difference = " << a - b;
return 0;
}

Multiply Two Numbers

#include <iostream>
using namespace std;

int main() {
int a, b;
cin >> a >> b;
cout << "Product = " << a * b;
return 0;
}
Divide Two Numbers

#include <iostream>
using namespace std;

int main() {
int a, b;
cin >> a >> b;
cout << "Quotient = " << a / b;
return 0;
}

Find Remainder
#include <iostream>
using namespace std;

int main() {
int a, b;
cin >> a >> b;
cout << "Remainder = " << a % b;
return 0;
}

Swap Two Numbers (with third variable)

#include <iostream>
using namespace std;

int main() {
int a = 5, b = 10, temp;
temp = a;
a = b;
b = temp;
cout << "a = " << a << ", b = " << b;
return 0;
}
Swap Two Numbers (without third variable)

#include <iostream>
using namespace std;

int main() {
int a = 5, b = 10;
a = a + b;
b = a - b;
a = a - b;
cout << "a = " << a << ", b = " << b;
return 0;
}

Check Even or Odd

#include <iostream>
using namespace std;

int main() {
int n;
cin >> n;
if(n % 2 == 0)
cout << "Even";
else
cout << "Odd";
return 0;
}

Find Largest of Two Numbers

#include <iostream>
using namespace std;

int main() {
int a, b;
cin >> a >> b;
if(a > b)
cout << a << " is larger";
else
cout << b << " is larger";
return 0;
}

Find Largest of Three Numbers

#include <iostream>
using namespace std;

int main() {
int a, b, c;
cin >> a >> b >> c;
if(a >= b && a >= c)
cout << a << " is largest";
else if(b >= a && b >= c)
cout << b << " is largest";
else
cout << c << " is largest";
return 0;
}

Simple Calculator
#include <iostream>
using namespace std;

int main() {
char op;
int a, b;
cin >> op >> a >> b;
switch(op) {
case '+': cout << a + b; break;
case '-': cout << a - b; break;
case '*': cout << a * b; break;
case '/': cout << a / b; break;
default: cout << "Invalid operator";
}
return 0;
}
Factorial of a Number (Loop)

#include <iostream>
using namespace std;

int main() {
int n, fact = 1;
cin >> n;
for(int i = 1; i <= n; i++)
fact *= i;
cout << "Factorial = " << fact;
return 0;
}

Fibonacci Series (Loop)

#include <iostream>
using namespace std;

int main() {
int n, a = 0, b = 1, next;
cin >> n;
cout << a << " " << b << " ";
for(int i = 2; i < n; i++) {
next = a + b;
cout << next << " ";
a = b;
b = next;
}
return 0;
}
Reverse a Number

#include <iostream>
using namespace std;

int main() {
int n, rev = 0;
cin >> n;
while(n != 0) {
rev = rev * 10 + n % 10;
n /= 10;
}
cout << "Reversed = " << rev;
return 0;
}

Palindrome Number

#include <iostream>
using namespace std;

int main() {
int n, rev = 0, temp;
cin >> n;
temp = n;
while(temp != 0) {
rev = rev * 10 + temp % 10;
temp /= 10;
}
if(n == rev)
cout << "Palindrome";
else
cout << "Not Palindrome";
return 0;
}
Armstrong Number

#include <iostream>
#include <cmath>
using namespace std;

int main() {
int n, sum = 0, temp, digits = 0;
cin >> n;
temp = n;
while(temp != 0) {
digits++;
temp /= 10;
}
temp = n;
while(temp != 0) {
sum += pow(temp % 10, digits);
temp /= 10;
}
if(sum == n) cout << "Armstrong";
else cout << "Not Armstrong";
return 0;
}

Sum of Digits

#include <iostream>
using namespace std;

int main() {
int n, sum = 0;
cin >> n;
while(n != 0) {
sum += n % 10;
n /= 10;
}
cout << "Sum of digits = " << sum;
return 0;
}
Count Digits

#include <iostream>
using namespace std;

int main() {
int n, count = 0;
cin >> n;
while(n != 0) {
count++;
n /= 10;
}
cout << "Number of digits = " << count;
return 0;
}

Leap Year Check

#include <iostream>
using namespace std;

int main() {
int year;
cin >> year;
if((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0))
cout << "Leap Year";
else
cout << "Not Leap Year";
return 0;
}
Print Multiplication Table

#include <iostream>
using namespace std;

int main() {
int n;
cin >> n;
for(int i = 1; i <= 10; i++)
cout << n << " * " << i << " = " << n * i << endl;
return 0;
}

Sum of First N Natural Numbers

#include <iostream>
using namespace std;

int main() {
int n, sum = 0;
cin >> n;
for(int i = 1; i <= n; i++)
sum += i;
cout << "Sum = " << sum;
return 0;
}

Sum of Even Numbers up to N

#include <iostream>
using namespace std;

int main() {
int n, sum = 0;
cin >> n;
for(int i = 2; i <= n; i += 2)
sum += i;
cout << "Sum of even numbers = " << sum;
return 0;
}
Sum of Odd Numbers up to N

#include <iostream>
using namespace std;

int main() {
int n, sum = 0;
cin >> n;
for(int i = 1; i <= n; i += 2)
sum += i;
cout << "Sum of odd numbers = " << sum;
return 0;
}

Check Vowel or Consonant

#include <iostream>
using namespace std;

int main() {
char c;
cin >> c;
c = tolower(c);
if(c=='a' || c=='e' || c=='i' || c=='o' || c=='u')
cout << "Vowel";
else
cout << "Consonant";
return 0;
}
Character is Alphabet or Not

#include <iostream>
using namespace std;

int main() {
char c;
cin >> c;
if((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))
cout << "Alphabet";
else
cout << "Not Alphabet";
return 0;
}

Find Largest Element in Array


#include <iostream>
using namespace std;

int main() {
int n;
cin >> n;
int arr[n];
for(int i = 0; i < n; i++) cin >> arr[i];
int max = arr[0];
for(int i = 1; i < n; i++)
if(arr[i] > max) max = arr[i];
cout << "Largest = " << max;
return 0;
}
Find Smallest Element in Array

#include <iostream>
using namespace std;

int main() {
int n;
cin >> n;
int arr[n];
for(int i = 0; i < n; i++) cin >> arr[i];
int min = arr[0];
for(int i = 1; i < n; i++)
if(arr[i] < min) min = arr[i];
cout << "Smallest = " << min;
return 0;
}

Reverse an Array

#include <iostream>
using namespace std;

int main() {
int n;
cin >> n;
int arr[n];
for(int i = 0; i < n; i++) cin >> arr[i];
cout << "Reversed array: ";
for(int i = n-1; i >= 0; i--)
cout << arr[i] << " ";
return 0;
}
Reverse a String

#include <iostream>
#include <algorithm>
using namespace std;

int main() {
string s;
getline(cin, s);
reverse([Link](), [Link]());
cout << "Reversed = " << s;
return 0;
}

Check Palindrome String

#include <iostream>
#include <algorithm>
using namespace std;

int main() {
string s;
getline(cin, s);
string rev = s;
reverse([Link](), [Link]());
if(s == rev) cout << "Palindrome";
else cout << "Not Palindrome";
return 0;
}
Structure Example (Student Data)

#include <iostream>
using namespace std;

struct Student {
string name;
int age;
float marks;
};

int main() {
Student s1;
cin >> [Link] >> [Link] >> [Link];
cout << "Name: " << [Link] << "\nAge: " << [Link] << "\nMarks: " << [Link];
return 0;
}

Pointer Example

#include <iostream>
using namespace std;

int main() {
int x = 10;
int *p = &x;
cout << "Value of x = " << x << endl;
cout << "Address of x = " << p << endl;
cout << "Value using pointer = " << *p;
return 0;
}
Input and Print a String

#include <iostream>
#include <string>
using namespace std;

int main() {
string str;
cout << "Enter a string: ";
getline(cin, str); // takes input with spaces
cout << "You entered: " << str << endl;
return 0;
}

Find Length of String (without length())

#include <iostream>
#include <string>
using namespace std;

int main() {
string str = "HelloWorld";
int len = 0;
for(char c : str) len++;
cout << "Length: " << len << endl;
return 0;
}
Reverse a String
#include <iostream>
#include <string>
using namespace std;

int main() {
string str = "Hello";
string rev = "";
for(int i = [Link]()-1; i >= 0; i--)
rev += str[i];
cout << "Reversed: " << rev << endl;
return 0;
}

Function without return and without parameters

#include <iostream>
using namespace std;

void greet() {
cout << "Hello! Welcome to C++ functions.\n";
}

int main() {
greet(); // function call
return 0;
}
Function with parameters but no return

#include <iostream>
using namespace std;

void add(int a, int b) {


cout << "Sum = " << a + b << endl;
}

int main() {
add(5, 7);
add(10, 20);
return 0;
}

Function with return type and parameters

#include <iostream>
using namespace std;

int multiply(int x, int y) {


return x * y;
}

int main() {
int result = multiply(4, 6);
cout << "Product = " << result << endl;
return 0;
}
Simple pointer example

#include <iostream>
using namespace std;

int main() {
int a = 10;
int *p = &a; // pointer stores address of a

cout << "Value of a: " << a << endl;


cout << "Address of a: " << &a << endl;
cout << "Pointer p stores address: " << p << endl;
cout << "Value at pointer p: " << *p << endl;

return 0;
}

Pointer to change variable value

#include <iostream>
using namespace std;

int main() {
int x = 5;
int *ptr = &x;

cout << "Before: " << x << endl;


*ptr = 20; // modifying value using pointer
cout << "After: " << x << endl;

return 0;
}
Arithmetic Operators
#include <iostream>
using namespace std;

int main() {
int a, b;
cout << "Enter two numbers: ";
cin >> a >> b;

cout << "Addition: " << a + b << endl;


cout << "Subtraction: " << a - b << endl;
cout << "Multiplication: " << a * b << endl;
cout << "Division: " << a / b << endl;
cout << "Modulus: " << a % b << endl;

return 0;
}

Relational Operators
#include <iostream>
using namespace std;

int main() {
int a, b;
cout << "Enter two numbers: ";
cin >> a >> b;

cout << "Equal to: " << (a == b) << endl;


cout << "Not equal to: " << (a != b) << endl;
cout << "Greater than: " << (a > b) << endl;
cout << "Less than: " << (a < b) << endl;
cout << "Greater or equal: " << (a >= b) << endl;
cout << "Less or equal: " << (a <= b) << endl;

return 0;
}
Logical Operators
#include <iostream>
using namespace std;

int main() {
bool x, y;
cout << "Enter two boolean values (0 or 1): ";
cin >> x >> y;

cout << "Logical AND: " << (x && y) << endl;


cout << "Logical OR: " << (x || y) << endl;
cout << "Logical NOT x: " << (!x) << endl;

return 0;
}

Assignment Operators

#include <iostream>
using namespace std;

int main() {
int a = 10, b = 5;

a += b; // a = a + b
cout << "After += : " << a << endl;

a -= b; // a = a - b
cout << "After -= : " << a << endl;

a *= b; // a = a * b
cout << "After *= : " << a << endl;

a /= b; // a = a / b
cout << "After /= : " << a << endl;

a %= b; // a = a % b
cout << "After %= : " << a << endl;

return 0;
}
Increment / Decrement Operators

#include <iostream>
using namespace std;

int main() {
int a = 5;

cout << "Original a: " << a << endl;


cout << "Post-increment a++: " << a++ << endl; // use then increment
cout << "After post-increment: " << a << endl;

cout << "Pre-increment ++a: " << ++a << endl; // increment then use
cout << "Pre-decrement --a: " << --a << endl;
cout << "Post-decrement a--: " << a-- << endl;
cout << "After post-decrement: " << a << endl;

return 0;
}

Bitwise Operators

#include <iostream>
using namespace std;

int main() {
int a = 5, b = 3; // 5=0101, 3=0011

cout << "a & b: " << (a & b) << endl; // AND
cout << "a | b: " << (a | b) << endl; // OR
cout << "a ^ b: " << (a ^ b) << endl; // XOR
cout << "~a: " << (~a) << endl; // NOT
cout << "a << 1: " << (a << 1) << endl; // left shift
cout << "a >> 1: " << (a >> 1) << endl; // right shift

return 0;
}

You might also like