مثال هایی از برنامه های C++

مثال : تابعی بنویسید که یک عبارت از پیش تعریف شده را در خروجی نمایش دهد

#include <iostream>

int main() {
std::cout << “Hello World!”;
return 0;
}

&nbsp

مثال : برنامه ای بنویسید که عددی را از کاربر (ورودی بگیرد) و در خروجی نمایش دهد 

#include <iostream>
using namespace std;

int main() {
int number;

cout << “Enter an integer: “;
cin >> number;

cout << “You entered ” << number;
return 0;
}

&nbsp

مثال : برنامه ای بنویسید که دو عدد را از ورودی بگیرد و مجموع آنها را در خروجی نمایش دهد 

#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;
}

 

مثال : تابعی بنویسید که دو عدد را از ورودی بگیرد و خارج قسمت و باقیمانده تقسیم آنها را در خروجی نمایش دهد 

#include <iostream>
using namespace std;

int main()
{
int divisor, dividend, quotient, remainder;

cout << “Enter dividend: “;
cin >> dividend;

cout << “Enter divisor: “;
cin >> divisor;

quotient = dividend / divisor;
remainder = dividend % divisor;

cout << “Quotient = ” << quotient << endl;
cout << “Remainder = ” << remainder;

return 0;
}

 

 

مثال : برنامه ای بنویسید که سایز انواع داده char.int,double,float را در خروجی نمایش دهد

#include <iostream>
using namespace std;

int main()
{
cout << “Size of char: ” << sizeof(char) << ” byte” << endl;
cout << “Size of int: ” << sizeof(int) << ” bytes” << endl;
cout << “Size of float: ” << sizeof(float) << ” bytes” << endl;
cout << “Size of double: ” << sizeof(double) << ” bytes” << endl;

return 0;
}

 

مثال : جابجایی دو عدد با استفاده ار متغیر واسط

#include <iostream>
using namespace std;

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

cout << “Before swapping.” << endl;
cout << “a = ” << a << “, b = ” << b << endl;

temp = a;
a = b;
b = temp;

cout << “\nAfter swapping.” << endl;
cout << “a = ” << a << “, b = ” << b << endl;

return 0;
}

 

توضیحات 

مثال : جابجایی متغیر بدون متغیر واسط

#include <iostream>
using namespace std;

int main()
{

int a = 5, b = 10;

cout << “Before swapping.” << endl;
cout << “a = ” << a << “, b = ” << b << endl;

a = a + b;
b = a – b;
a = a – b;

cout << “\nAfter swapping.” << endl;
cout << “a = ” << a << “, b = ” << b << endl;

return 0;
}

 

توضیحات 

مثال : بررسی زوج یا فرد بودن عدد

#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;
}

 

توضیحات 

مثال : یافتن یک عدد بین 3 عدد ورودی 

#include <iostream>
using namespace std;

int main() {

double n1, n2, n3;

cout << “Enter three numbers: “;
cin >> n1 >> n2 >> n3;

// check if n1 is the largest number
if(n1 >= n2 && n1 >= n3)
cout << “Largest number: ” << n1;

// check if n2 is the largest number
else if(n2 >= n1 && n2 >= n3)
cout << “Largest number: ” << n2;

// if neither n1 nor n2 are the largest, n3 is the largest
else
cout << “Largest number: ” << n3;

return 0;
}

 

توضیحات 

مثال :  پیدا کردن ریشه های معادله درجه 2

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

int main() {

float a, b, c, x1, x2, discriminant, realPart, imaginaryPart;
cout << “Enter coefficients a, b and c: “;
cin >> a >> b >> c;
discriminant = b*b – 4*a*c;

if (discriminant > 0) {
x1 = (-b + sqrt(discriminant)) / (2*a);
x2 = (-b – sqrt(discriminant)) / (2*a);
cout << “Roots are real and different.” << endl;
cout << “x1 = ” << x1 << endl;
cout << “x2 = ” << x2 << endl;
}

else if (discriminant == 0) {
cout << “Roots are real and same.” << endl;
x1 = -b/(2*a);
cout << “x1 = x2 =” << x1 << endl;
}

else {
realPart = -b/(2*a);
imaginaryPart =sqrt(-discriminant)/(2*a);
cout << “Roots are complex and different.” << endl;
cout << “x1 = ” << realPart << “+” << imaginaryPart << “i” << endl;
cout << “x2 = ” << realPart << “-” << imaginaryPart << “i” << endl;
}

return 0;
}

   

توضیحات 

مثال :  برنامه ای بنویسید یک عدد را از ورودی بگیرد و مجموع اعداد یک تا آن عدد را در خروجی نمایش دهد 

#include <iostream>
using namespace std;

int main() {
int n, sum = 0;

cout << “Enter a positive integer: “;
cin >> n;

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

cout << “Sum = ” << sum;
return 0;
}

 

توضیحات 

مثال :  یک سال میلادی را بگیرد و بگوید در تقویم میلادی 366 روزه (کبیسه) است یا خیر؟

#include <iostream>
using namespace std;

int main() {

int year;

cout << “Enter a year: “;
cin >> year;

if (year % 4 == 0) {
if (year % 100 == 0) {
if (year % 400 == 0) {
cout << year << ” is a leap year.”;
}
else {
cout << year << ” is not a leap year.”;
}
}
else {
cout << year << ” is a leap year.”;
}
}
else {
cout << year << ” is not a leap year.”;
}

return 0;
}

 

توضیحات 

مثال :  محاسبه فاکتوریل یک عدد با گرفتن آن عدد

#include <iostream>
using namespace std;

int main() {
int n;
long factorial = 1.0;

cout << “Enter a positive integer: “;
cin >> n;

if (n < 0)
cout << “Error! Factorial of a negative number doesn’t exist.”;
else {
for(int i = 1; i <= n; ++i) {
factorial *= i;
}
cout << “Factorial of ” << n << ” = ” << factorial;
}

return 0;
}

 

توضیحات 

مثال :  یک عدد را بگیرد و مضارب 1 تا 10 آن را نمایش دهد 

#include <iostream>
using namespace std;

int main() {

int n;

cout << “Enter a positive integer: “;
cin >> n;

// run a loop from 1 to 10
// print the multiplication table
for (int i = 1; i <= 10; ++i) {
cout << n << ” * ” << i << ” = ” << n * i << endl;
}

return 0;
}

 

توضیحات 

مثال :  محاسبه بزرگترین مقسوم علیه مشترک (ب.م.م) دو عدد

#include <iostream>
using namespace std;

int main() {
int n1, n2, hcf;
cout << “Enter two numbers: “;
cin >> n1 >> n2;

// swapping variables n1 and n2 if n2 is greater than n1.
if ( n2 > n1) {
int temp = n2;
n2 = n1;
n1 = temp;
}

for (int i = 1; i <= n2; ++i) {
if (n1 % i == 0 && n2 % i ==0) {
hcf = i;
}
}

cout << “HCF = ” << hcf;

return 0;
}

 

توضیحات 

مثال :  محاسبه کوچکترین مضرب مشترک (ک.م.م)

#include <iostream>
using namespace std;

int main()
{
int n1, n2, hcf, temp, lcm;

cout << “Enter two numbers: “;
cin >> n1 >> n2;

hcf = n1;
temp = n2;

while(hcf != temp)
{
if(hcf > temp)
hcf -= temp;
else
temp -= hcf;
}

lcm = (n1 * n2) / hcf;

cout << “LCM = ” << lcm;
return 0;
}

 

توضیحات 

مثال :  نمایش جدول ضرب یک رقمی

#include <iostream>

using namespace std;

int main(){

int a , b;

for(a=1;  a<=10;  a++){
for(b=1;  b<=10;  b++)

cout<<b<<” * “<<a<<” = “<<a * b<<‘\t’;
cout<<endl;

return 0;
}
}

 

توضیحات 

مثال :  برنامه ای بنویسید اعداد از یک تا صد نمایش بده 

#include <iostream>
using namespace std;

int main() {
for (int i = 1; i <= 100; i++) {
cout << i << ” “;
}
return 0;
}

 

مباحث مربوطه : حلقه For

 

مثال :    برنامه ی ماشین حسابی را بنویسید که فقط عملیات جمع ، تفریق ، ضرب و تقسیم را انجام دهد و اگه متغیر دیگری را به او دادیم چیزی را در خروجی نمایش ندهد

#include <iostream>

using namespace std;

Int main() {
char ch; cin >> ch;
int a; cin >>a;
int b; cin >>b;
switch (ch) {
case ‘+’: cout << a+b;break;
case ‘-‘: cout << a-b;break;
case ‘*’: cout << a*b;break;
case ‘/’: cout << (float)a/b;break;
default : cout << “incorrect ch”;break;
}
return 0;
}

مباحث مربوطه : 

توضیحات :

مثال :    برنامه ای بنویسید که عدد را از ورودی دریافت کرده و ارقام آن را به ترتیب از یکان جدا کند 

#include <iostream>
#include <vector>

int main() {
int number;
std::cout << “لطفا یک عدد وارد کنید: “;
std::cin >> number;

std::vector<int> digits;
while (number > 0) {
digits.push_back(number % 10);
number /= 10;
}

std::cout << “رقم‌های عدد به ترتیب از یکان: “;
for (auto it = digits.rbegin(); it != digits.rend(); ++it) {
std::cout << *it << ” “;
}
std::cout << std::endl;

return 0;
}

مباحث مربوطه : 

توضیحات :

مثال :    برنامه ای بنویسید که عددی را وارد کنیم و سپس آن عدد را بصورت معکوس شده در خروجی به ما نمایش میدهد.

#include <iostream>
using namespace std;

int main() {
    int number, reversedNumber = 0;
    cout << “Enter a number: “;
    cin >> number;

    while (number != 0) {
        int remainder = number % 10;
        reversedNumber = reversedNumber * 10 + remainder;
        number /= 10;
    }

    cout << “Reversed number: ” << reversedNumber << endl;

    return 0;
}

مباحث مربوطه : 

توضیحات :

مثال :  برنامه ای داریم که از ما عدد مورد نظر را میپرسد که آیا عدد ارسال شده به عدد 5 بخش پذیر است یا خیر ؟ اگر بخش پذیر بود پیام Yes نمایش داده میشود و اگر بخش پذیر نبود پیام No نمایش داده میشود. 

#include <iostream>
using namespace std;

int main() {
int num;
cout << “Enter a number : “;
cin >> num;

if (num % 5 == 0) {
cout << “Yes” << endl;
} else {
cout << “No” << endl;
}

return 0;
}

مباحث مربوطه : 

توضیحات :

مثال :    برنامه ای بنویسید که فرمول x+10 بر روی 3y را بعد از گرفتن اعداد (x ,y ) به دست آورد . 

#include <iostream>
using namespace std ;

int main ()
{
double x ;
cout << “please enter X :” ;
cin >> x ;

double y = 10 ;
cout << “please enter y :” ;
cin >> y ;

double z =(x + 10) / (3 * y) ;
cout << z ;

return 0 ;

{

مباحث مربوطه : 

توضیحات :

مثال :  برنامه ای داریم که از ما سال تولدمان را به تاریخ هجری شمسی میپرسد و ما باید سال تولدمان را بنویسم و سپس سن ما را بر اساس تاریخ هجری شمسی به ما میگوید. 

#include <iostream>
#include <ctime>

using namespace std;

int main() {
int birthYear;
cout << “Enter your birth year: “;
cin >> birthYear;

time_t now = time(0);
tm *ltm = localtime(&now);
int currentYear = 1402 + ltm->tm_year;

int age = currentYear=1402 – birthYear;

cout << “Your age in Shamsi calendar is: ” << age << endl;

return 0;
}

مباحث مربوطه : 

توضیحات :

مثال : یک دستگاه خودپرداز دارای پول های 1000،500و100و50و20و10و5و2و1 ریالی است برنامه ای بنویسید که مشخص کند این دستگاه برای هر مبلغ ورودی باید چند اسکناس از هر نوعی را تحویل دهد

#include <iostream>

int main() {
    int amount;

    // وارد کردن مقدار پول توسط کاربر
    std::cout << “لطفاً مقدار پول را وارد کنید: “;
    std::cin >> amount;

    // محاسبه تعداد اسکناس‌ها و سکه‌ها بر اساس ارزش‌های استاندارد
    int thousandNotes = amount / 1000;
    amount %= 1000;

    int fiveHundredNotes = amount / 500;
    amount %= 500;

    int hundredNotes = amount / 100;
    amount %= 100;

    int fiftyNotes = amount / 50;
    amount %= 50;

    int twentyNotes = amount / 20;
    amount %= 20;

    int tenNotes = amount / 10;
    amount %= 10;

    int fiveCoins = amount / 5;
    amount %= 5;

    int twoCoins = amount / 2;
    amount %= 2;

    int oneCoins = ;

    // نمایش نتایج
    std::cout << “تعداد اسکناس‌ها و سکه‌ها:” << std::endl;
    std::cout << “1000 تومان: ” << thousandNotes << std::endl;
    std::cout << “500 تومان: ” << fiveHundredNotes << std::endl;
    std::cout << “100 تومان: ” << hundredNotes << std::endl;
    std::cout << “50 تومان: ” << fiftyNotes << std::endl;
    std::cout << “20 تومان: ” << twentyNotes << std::endl;
    std::cout << “10 تومان: ” << tenNotes << std::endl;
    std::cout << “5 تومان: ” << fiveCoins << std::endl;
    std::cout << “2 تومان: ” << twoCoins << std::endl;
    std::cout << “1 تومان: ” << oneCoins << std::endl;

    return 0;
}

مباحث مربوطه : 

توضیحات :

مثال :    برنامه ای داریم که از ما میخواهد تا کلمه مورد نظرمان را بنویسیم سپس در پاسخ به ما تعداد حروفی که کلمه ارسال شده تشکیل داده را میگوید.

#include <iostream>
#include <string>

int main() {
std::string word;
std::cout << “Please send your word : “;
std::cin >> word;

int length = word.length();
std::cout << “Number of letters \”” << word << “\” is : ” << length << std::endl;

return 0;
}

مباحث مربوطه : کاربا رشته string

توضیحات :

مثال :  برنامه ای بنویسید که دو عدد را از ورودی گرفته و عدد بزرگتر را نمایش دهد  

#include <iostream>

using namespace std;

int main() {
      int a, b;
      a=10;
      b=15;
      if (a>b) {
          cout << “max:” <<a;
      }
      if (a<b) {
          cout << “max:” <<b;
      }
      return 0;
}

مباحث مربوطه : 

توضیحات :

مثال : برنامه ای بنویسید که مقدار اینچ از کاربر بگیرد و به سانتی متر تبدیل کند   

#include<iostream>
#include<conio.h>
using namespace std;

main()
{
float cm,inch;
cout<<” Plases input your inch number :”;
cin>>inch;
cm=(inch*2.54);
cout<<“\n\n”<<inch<<” inch=”<<cm<<” cm”;
cout<<“\n\n\n”<<“press any key to exit…”;
return 0;
}

مباحث مربوطه : 

توضیحات :

مثال :  برنامه ای بنویسید که شعاع یک دایره را گرفته و مساحت و محیط آن را نمایش دهد .  

#include <iostream>
using namespace std;

int main() {
double radius;
const double Pl = 3.14159;

cout <<“لطفا شعاع دایره را وارد کنید: “;
cin >> radius;

double circumference = 2 * PI * radius;
double area = PI * radius * radius;

cout <<“محیط دایره: ” << circumference
<<endl;
cout <<“مساحت دایره: ” << area << endl;

return 0;
}

مباحث مربوطه : تابع

توضیحات :

#include <iostream>
using namespace std;

class Circle {
private:
double radius;

public:
void setRadius(double r) {
radius = r;
}

double getArea() {
return 3.14 * radius * radius;
}

double getPerimeter() {
return 2 * 3.14 * radius;
}
};

int main() {
Circle c;
double r;

cout << “Enter the radius of the circle: “;
cin >> r;

c.setRadius(r);

cout << “Area of the circle: ” << c.getArea() << endl;
cout << “Perimeter of the circle: ” << c.getPerimeter() << endl;

return 0;
}

مباحث مربوطه : کلاس ، تابع

توضیحات :

مثال : با یک کلاس و توابع مورد نیازوضعیت بدنی اشخاص را محاسبه کنید    

#include<iostream>
using namespace std;

int this_year = 1402;

class BodyStatus{
public:
string BS_name;
float BS_height;
float BS_weight;
int BS_birth_year;

BodyStatus(float height, float weight, int birth_year=0, string name=”Unknown”){
BS_name = name;
BS_height = height;
BS_weight = weight;
BS_birth_year = birth_year;
}

int age(int a=0){
if(a != 0){
BS_birth_year = a;
}
else if(BS_birth_year == 0){
cout << “set or enter the birth_year.\n”;
return 0;
}

return this_year – BS_birth_year;
}

float BMI(){
return BS_weight / (BS_height*BS_height);
}

void weightStatus(){
float w = BMI();
if(w < 18.5){
cout << “Underweight\n”;
}
else if(w < 25){
cout << “Normal weight\n”;
}
else if(w < 30){
cout << “Overweight\n”;
}
else if(w < 35){
cout << “Obesity calss1\n”;
}
else if(w < 40){
cout << “Obesity calss2\n”;
}else if(w >= 40){
cout << “Obesity calss3\n”;
}
}
};

int main()
{
BodyStatus B1(1.72, 68, 1380, “ali”);
cout << B1.BS_name << “‘s age: ” << B1.age() << “\n”;
cout << B1.BS_name << “‘s BMI: ” << B1.BMI() << “\n”;
B1.weightStatus();
return 0;
}

مباحث مربوطه : 

توضیحات :

   

 

مباحث مربوطه : 

توضیحات :

مثال :    

 

مباحث مربوطه : 

توضیحات :

دیدگاهتان را بنویسید

نشانی ایمیل شما منتشر نخواهد شد. بخش‌های موردنیاز علامت‌گذاری شده‌اند *

این فیلد را پر کنید
این فیلد را پر کنید
لطفاً یک نشانی ایمیل معتبر بنویسید.
برای ادامه، شما باید با قوانین موافقت کنید

keyboard_arrow_up