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

rashed

The document contains a series of C++ code snippets demonstrating various programming concepts such as input/output, loops, conditionals, and mathematical calculations. Each snippet showcases different functionalities, including displaying text, performing arithmetic operations, handling user input, and implementing algorithms. The examples range from simple print statements to more complex logic involving arrays and mathematical functions.

Uploaded by

tamralwsaby5
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)
3 views

rashed

The document contains a series of C++ code snippets demonstrating various programming concepts such as input/output, loops, conditionals, and mathematical calculations. Each snippet showcases different functionalities, including displaying text, performing arithmetic operations, handling user input, and implementing algorithms. The examples range from simple print statements to more complex logic involving arrays and mathematical functions.

Uploaded by

tamralwsaby5
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/ 15

1- #include<iostream>

using namespace std;


int main()
{
cout<<"Rashed";
}

2- #include<iostream>
using namespace std;
int main()
{
cout<<"Rashed ";
cout<<"Ameen ";
cout<<"Alowsabi";
}

3- #include <iostream>
#include <windows.h>
using namespace std;
int main() {
string name,namefa;
int x = 0;
cout<<"Enter your name:";
cin>>name;
cout<<"Enter your father name:";
cin>>namefa;
while (true) {
system("cls");
cout << string(x, ' ') <<name<< " " <<namefa;
x++;
if (x > 50) break;
Sleep(50);
}
}

4- #include <iostream>
using namespace std;
int main() { int n;
cout << "Enter your number of star:";
cin >> n;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++)
cout << "* ";
cout << endl; }
}
5- #include <iostream>
using namespace std;
int main() {
int A = 5, B = 10;
cout << "A+B: " << A + B << endl;
cout << "A-B: " << A - B << endl;
cout << "A*B: " << A * B << endl;
cout << "A/B: " << A / B << endl;

6- #include <iostream>
using namespace std;
int main() {
int A, B;
cout << "Enter value of A: ";
cin >> A;
cout << "Enter value of B: ";
cin >> B;
cout << "Value of A+B: " << A + B << endl;
cout << "Value of A-B: " << A - B << endl;
cout << "Value of A*B: " << A * B << endl;
if (B != 0)
cout << "Value of A/B: " << A / B << endl;
}

7- #include <iostream>
using namespace std;
int main() {
int a, b, c;
cout<< "Enter number 1: ";
cin>>a;
cout<< "Enter number 2: ";
cin>>b;
cout<<"Enter number 3: ";
cin>>c;
int sum = a + b + c;
double average = (double)sum / 3;
cout<<"Sum: " << sum << endl;
cout<<"Average: "<< average << endl;
cout<<"Square of number 1: "<<a*a<< endl;
cout<<"Square of number 2: "<<b*b<< endl;
cout<<"Square of number 3: "<<c*c<< endl;
}

8- #include <iostream>
using namespace std;
int main() {
const int workingHours=120;
const int hourlyWage=45;
int salary = workingHours * hourlyWage;
cout <<"Employee salary: "<<salary<< endl;
}

9- #include <iostream>
using namespace std;
int main() {
int workingHours, hourlyWage;
cout << "Enter number of working hours: ";
cin >>workingHours;
cout << "Enter hourly wage: ";
cin >>hourlyWage;
int salary = workingHours * hourlyWage;
cout << "Employee salary: " << salary << endl;
}

10- #include <iostream>


using namespace std;
int main() {
double X, A, Y;
cout << "Enter value of X: ";
cin >> X;
cout << "Enter value of A: ";
cin >> A;
Y=X*((A+5)-4)/2;
cout << "Value of Y: " << Y << endl;
}

11- #include <iostream>


using namespace std;
int main() {
const double pi=3.14159;
double r=5;
double area = pi*r*r;
double circumference = 2*pi*r;

cout << "Area of circle: " << area << endl;


cout << "Circumference of circle: " << circumference << endl;
}

12- #include <iostream>


using namespace std;
int main() {
const double pi=3.14159;
double r;

cout <<"Enter radius of circle: ";


cin >> r;
double area = pi*r*r;
double circumference = 2*pi*r;
cout << "Area of circle: " << area << endl;
cout << "Circumference of circle: " << circumference << endl;
}

13- #include <iostream>


using namespace std;
int main() {
int num;
cout << "Enter a number: ";
cin >> num;

if (num > 0) {
cout << "The Number Is Positive" << endl;
} else if (num < 0) {
cout << "The Number Is Negative" << endl;
} else {
cout << "The number is zero" << endl;
}

14- #include <iostream>


using namespace std;
int main() {
int num1, num2, choice;

cout << "Enter first number: ";


cin >> num1;
cout << "Enter second number: ";
cin >> num2;

cout << "Choose operation:" << endl;


cout << "1. Addition" << endl;
cout << "2. Subtraction" << endl;
cout << "3. Multiplication" << endl;
cout << "4. Division" << endl;
cin >> choice;

if (choice == 1) {
cout << "Result: " << num1 + num2 << endl;
} else if (choice == 2) {
cout << "Result: " << num1 - num2 << endl;
} else if (choice == 3) {
cout << "Result: " << num1 * num2 << endl;
} else if (choice == 4) {
if (num2 != 0) {
cout << "Result: " << num1 / num2 << endl;
} else {
cout << "Error: Division by zero" << endl;
}
} else {
cout << "Invalid choice" << endl;
}

switch‫ﺑﺎﺳﺘﺨﺪام‬

#include <iostream>
using namespace std;
int main() {
int num1, num2, choice;

cout << "Enter first number: ";


cin >> num1;
cout << "Enter second number: ";
cin >> num2;

cout << "Choose operation:" << endl;


cout << "1. Addition" << endl;
cout << "2. Subtraction" << endl;
cout << "3. Multiplication" << endl;
cout << "4. Division" << endl;
cin >> choice;

switch (choice) {
case 1:
cout << "Result: " << num1 + num2 << endl;
break;
case 2:
cout << "Result: " << num1 - num2 << endl;
break;
case 3:
cout << "Result: " << num1 * num2 << endl;
break;
case 4:
if (num2 != 0) {
cout << "Result: " << num1 / num2 << endl;
} else {
cout << "Error: Division by zero" << endl;
}
break;
default:
cout << "Invalid choice" << endl;
}

15- #include <iostream>


using namespace std;
int main() {
int num, count = 0, sum = 0;

cout << "Enter a number: ";


cin >> num;
while (num > 0) {
count++;
sum += num % 10;
num /= 10;
}
cout << "Number of digits: " << count << endl;
cout << "Sum of digits: " << sum << endl;

For statement ‫ ﺑﺎﺳﺘﺨﺪام‬-16


#include <iostream>
using namespace std;
int main() {
int n, sum = 0;

cout << "Enter a number: ";


cin >> n;

for (int i = 1; i <= n; i++) {


sum += i * i;
}
cout << "Sum of squares: " << sum << endl;}
while sataement‫ﺑﺎﺳﺘﺨﺪام‬
#include <iostream>
using namespace std;
int main() {
int n, sum = 0, i = 1;

cout << "Enter a number: ";


cin >> n;

while (i <= n) {
sum += i * i;
i++;
}

cout << "Sum of squares: " << sum << endl;


}

do while statement‫ﺑﺎﺳﺘﺨﺪام‬
#include <iostream>
using namespace std;
int main() {
int n, sum = 0, i = 1;
cout << "Enter a number: ";
cin >> n;

do {
sum += i * i;
i++;
} while (i <= n);

cout << "Sum of squares: " << sum << endl;


}

17- #include <iostream>


using namespace std;
int main() {
for (int i = 0; i <= 255; i++) {
cout << "ASCII code " << i << ": " << (char)i << endl;
}
}

18- #include <iostream>


#include <string>
using namespace std;
int main() {
string word;
cout << "Enter a word: ";
cin >> word;

if (word.length() > 10) {


cout << "Word is too long" << endl;
}

char lastChar = 'a';


for (char c : word) {
if (c > lastChar) {
lastChar = c;
}
}

cout << "Last character in alphabetical order: " << lastChar << endl;
}

19- #include <iostream>


#include <string>
using namespace std;
int main() {
string word;
cout << "Enter a word: ";
cin >> word;

if (word.length() > 10) {


cout << "Word is too long" << endl;
}

char firstChar = 'z';


for (char c : word) {
if (c < firstChar) {
firstChar = c;
}
}

cout << "First character in alphabetical order: " << firstChar << endl;
}
20- #include <iostream>
#include <cmath>
using namespace std;
int main() {
double angle;
cout << "Enter an angle in degrees: ";
cin >> angle;

double cosAngle = cos(angle * M_PI / 180);


cout << "Cosine of the angle: " << cosAngle << endl;
}

21- #include <iostream>


using namespace std;
int main() {
double numbers[5];
for (int i = 0; i < 5; i++) {
cout << "Enter number " << i + 1 << ": ";
cin >> numbers[i];
}

double sum = 0;
for (double num : numbers) {
sum += num;
}

double average = sum / 5;


cout << "Average: " << average << endl;
}

22- #include <iostream>


using namespace std;
int main() {
int numbers[5];
for (int i = 0; i < 5; i++) {
cout << "Enter number " << i + 1 << ": ";
cin >> numbers[i];
}
int maxNum = numbers[0];
int secondMaxNum = numbers[0];
for (int num : numbers) {
if (num > maxNum) {
secondMaxNum = maxNum;
maxNum = num;
} else if (num > secondMaxNum && num != maxNum) {
secondMaxNum = num;
}
}
cout << "Max number: " << maxNum << endl;
cout << "Second max number: " << secondMaxNum << endl;
}

23- #include <iostream>


using namespace std;
int main() {
int num;
cout << "Enter a number: ";
cin >> num;
cout << "Factors of " << num << ": ";
for (int i = 1; i <= num; i++) {
if (num % i == 0) {
cout << i << " ";
}
}
cout << endl;
}

24- #include <iostream>


using namespace std;
int main() {
int num;
cout << "Enter a number: ";
cin >> num;

int thousands = num / 1000;


int hundreds = (num % 1000) / 100;
int tens = (num % 100) / 10;
int ones = num % 10;

cout << "Thousands: " << thousands << endl;


cout << "Hundreds: " << hundreds << endl;
cout << "Tens: " << tens << endl;
cout << "Ones: " << ones << endl;
}

25- #include <iostream>


using namespace std;
int main() {
int n;

cout << "Enter the number: ";


cin >> n;

int a = 0, b = 1, next;

cout << "The anylaises: ";

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


if (i == 0) {
cout << a << ", ";
continue;
}
if (i == 1) {
cout << b << ", ";
continue;
}
next = a + b;
a = b;
b = next;
cout << next;
if (i != n - 1) {
cout << ", ";
}
}
cout << endl;
}

26- #include <iostream>


using namespace std;
int main() {
int num;
cout << "Enter a number: ";
cin >> num;

int reversedNum = 0;
while (num > 0) {
reversedNum = reversedNum * 10 + num % 10;
num /= 10;
}

cout << "Reversed number: " << reversedNum << endl;

27- #include <iostream>


using namespace std;
int main() {
int num;
cout << "Enter a number: ";
cin >> num;

int evenSum = 0;
int oddSum = 0;

while (num > 0) {


int digit = num % 10;
if (digit % 2 == 0) {
evenSum = evenSum * 10 + digit;
} else {
oddSum = oddSum * 10 + digit;
}
num /= 10;
}

cout << "Even sum: " << evenSum << endl;


cout << "Odd sum: " << oddSum << endl;
}

28- #include <iostream>


#include <cmath>
using namespace std;
int main() {
double a, b;
cout << "Enter length of side a: ";
cin >> a;
cout << "Enter length of side b: ";
cin >> b;

double c = sqrt(a * a + b * b);


cout << "Length of hypotenuse: " << c << endl;
}

29- #include <iostream>


using namespace std;
int main() {
int year;
cout << "Enter a year: ";
cin >> year;

if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {


cout << "It is a leap year" << endl;
} else {
cout << "It is not a leap year" << endl;
}
}

30- #include <iostream>


using namespace std;
int main(){
int n;
cout<<"Enter the number of terms: ";
cin>>n;
double sum=0;
for(int i=1;i<=n;i++)
sum+=(i-2)/(i-(i+1));
cout<<"Result: "<<sum<<endl;
}

31- #include<iostream>
#include<math.h>
using namespace std;
int main() {
int n;
double x;
cout << "Enter the value of x: ";
cin >> x;
cout << "Enter the value of n: ";
cin >> n;

double sum = 0;
for (int i = 2; i <= n; i += 2) {
double factorial = 1;
for (int j = 1; j <= i; j++) {
factorial *= j;
}
sum += pow(x, i) / factorial;
}

cout << "Result: " << sum << endl;


}

For statement‫ ﺑﺎﺳﺘﺨﺪام‬-32


#include <iostream>
using namespace std;
int main() {
double sum = 0;
double numbers[] = {5, 3, 1, 1.0, -3.0, -5.0};

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


sum += numbers[i];
}

cout << "Sum: " << sum << endl;


}
while statement*‫ﺑﺎﺳﺘﺨﺪام‬
#include <iostream>
using namespace std;
int main() {
double sum = 0;
double numbers[] = {5, 3, 1, 1.0, -3.0, -5.0};
int i = 0;

while (i < 6) {
sum += numbers[i];
i++;
}

cout << "Sum: " << sum << endl;


}
do-while statement*‫ﺑﺎﺳﺘﺨﺪام‬
#include <iostream>
using namespace std;

int main() {
double sum = 0;
double numbers[] = {5, 3, 1, 1.0, -3.0, -5.0};
int i = 0;

do {
sum += numbers[i];
i++;
} while (i < 6);

cout << "Sum: " << sum << endl;


}

33- #include <iostream>


using namespace std;
int main() {
long long sum = 0;
for (int i = 1; i <= 99; i++) {
sum += (long long)i * i * i;
}

cout << "Sum: " << sum << endl;


}

34- #include <iostream>


using namespace std;
int main() {
double I;
cout << "Enter the value of I: ";
cin >> I;

double sum = 0;
for (int i = 1; i <= I; i += 2) {
sum += 1.0 / i;
}

cout << "Sum: " << sum << endl;


}

35- #include <iostream>


#include <cmath>
using namespace std;
int main() {
double x, result = 1; int n;
cout<<"Enter the x and n:";
cin >> x >> n;
for (int i = 1; i <= n; i++) result *= sin(pow(x, i));
cout << "Result: " << result;
}

36- #include <iostream>


using namespace std;
int main() {
long long sum = 0;
for (int i = 1; i <= 99; i++) {
sum += (long long)i * i * i;
}
sum += 3 * 3 * 3 + 2 * 2 * 2 + 1 * 1 * 1;
cout << "Sum: " << sum << endl;
}

37- #include <iostream>


using namespace std;
long long factorial(int n) {
long long result = 1;
for (int i = 1; i <= n; i++) {
result *= i;
}
}
int main() {
int n, k;
cout << "Enter n and k: ";
cin >> n >> k;
long long result = factorial(n) / (factorial(k) * factorial(n-k));
cout << "Result: " << result << endl;
}

38- #include <iostream>


using namespace std;
int main() {
int result = 0;
for (int i = 0; i <= 10; i++) {
if (i == 0) {
result = 0;
} else if (i == 1) {
result = 1;
} else if (i == 2) {
result = 2;
} else if (i == 3) {
result = 3;
} else if (i == 4) {
result = 3 * 4;
} else if (i == 5) {
result = result - 7;
} else if (i == 6) {
result = result * 6;
} else if (i == 7) {
result = result - 23;
} else if (i == 8) {
result = result * 8;
} else if (i == 9) {
result = result - 47;
} else if (i == 10) {
result = result * 10;
}
cout << result << " ";}}
39- #include <iostream>
using namespace std;
int main() {
int a, b;
cout << "Enter two numbers: ";
cin >> a >> b;
int sum = a - (-b);
cout << "Sum: " << sum << endl;
}

40- #include <iostream>


using namespace std;
int main() {
int month;
cout << "Enter month number (1-12): ";
cin >> month;
if (month < 1 || month > 12) {
cout << "Error input, Month number should be between 1 and 12" << endl;
} else {
string months[] = {"January", "February", "March", "April", "May", "June", "July",
"August", "September", "October", "November", "December"};
cout << "Month: " << months[month-1] << endl;
}
}

41- #include <iostream>


using namespace std;
int main() {
double num;
cout << "Enter a number: ";
cin >> num;
int integerPart = (int)num;
double fractionalPart = num - integerPart;
cout << "Fractional part: " << fractionalPart << endl;
}

42- #include <iostream>


using namespace std;
int main() {
int n;
cout << "Enter number of values: ";
cin >> n;
double sum = 0;
for (int i = 0; i < n; i++) {
double num;
cout << "Enter value " << i+1 << ": ";
cin >> num;
sum += num;
}
cout << "Sum: " << sum << endl;
}

43- #include <iostream>


using namespace std;
int main() {
int num;
cout << "Enter a number: ";
cin >> num;
long long factorial = 1;
for (int i = 1; i <= num; i++) {
factorial *= i;
}
cout << "Factorial: " << factorial << endl;
}

44- #include <iostream>


using namespace std;
int main() {
int n, num, largest;
cout<<"Enter the count of number: ";
cin >> n;
cout<<"Enter the numbers: ";
cin >> largest;
for (int i = 1; i < n; i++) {
cin >> num;
if (num > largest) largest = num;
}
cout << "Largest: " << largest;
}

45- #include <iostream>


using namespace std;
int main() {
int n, num, smallest;
cout<<"Enter the count of number: ";
cin >> n;
cout<<"Enter the numbers: ";
cin >> smallest;
for (int i = 1; i < n; i++) {
cin >> num;
if (num < smallest) smallest = num;
}
cout << "Smallest: " << smallest;
}

46- #include <iostream>


using namespace std;
int main() {
char c;
cout << "Enter a character: ";
cin >> c;

if (c >= '0' && c <= '9') {


cout << "The character is a digit." << endl;
} else if (c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z') {
cout << "The character is a letter." << endl;
} else {
cout << "The character is neither a digit nor a letter." << endl;
}
}

47- #include <iostream>


using namespace std;
int main() {
for (int i = 1; i <= 10; i++) {
for (int j = 1; j <= 10; j++) {
cout << i * j << "\t";
}
cout << endl;
}
}

48- #include <iostream>


using namespace std;
int main() {
for (int i = 1; i <= 10; i++) {
for (int j = i; j <= 10; j++) {
cout << i * j << "\t";
}
cout << endl;
}
}

49- #include <iostream>


using namespace std;
int main() {
int n;
cout << "Enter the value of n: ";
cin >> n;

for (int i = 1; i <= n; i++) {


for (int j = 1; j <= i; j++) {
cout << j;
}
cout << endl;
}
}

50- #include <iostream>


using namespace std;
int main() {
string s = "012345";
cout << s.substr(0, 5) << "*" << s.substr(1, 5) << "*" << s.substr(2, 4) << endl;
cout << s.substr(1, 5) << "*" << s.substr(2, 4) << endl;
cout << s.substr(2, 4) << endl;
cout << s.substr(3, 3) << endl;
cout << s.substr(4, 2) << endl;
}

You might also like