نمونه سوالات سی پلاس پلاس

فهرست مطالب

برنامه‌ای بنویسید که طول و عرض مستطیلی دریافت کند و محیط و مساحت آن را چاپ کند.

                                            
                                        
                        #include <iostream>

int main() {
    double length, width;

    // Ask the user for the length of the rectangle
    std::cout << "Enter the length of the rectangle: ";
    std::cin >> length;

    // Ask the user for the width of the rectangle
    std::cout << "Enter the width of the rectangle: ";
    std::cin >> width;

    // Calculate the perimeter of the rectangle
    double perimeter = 2 * (length + width);

    // Calculate the area of the rectangle
    double area = length * width;

    // Display the results
    std::cout << "The perimeter of the rectangle is: " << perimeter << std::endl;
    std::cout << "The area of the rectangle is: " << area << std::endl;

    return 0;
}                    
                

برنامه‌ای بنویسید که یک عدد دریافت کند و زوج و فرد بودن آن را گزارش کند.

                                            
                                        
                        #include <iostream>

int main() {
    int number;

    // Ask the user for a number
    std::cout << "Enter a number: ";
    std::cin >> number;

    // Check if the number is even or odd
    if (number % 2 == 0) {
        std::cout << "The number " << number << " is even." << std::endl;
    } else {
        std::cout << "The number " << number << " is odd." << std::endl;
    }

    return 0;
}                    
                

برنامه ای بنویسید که یک زمان مشخص بر اساس ساعت، دقیقه و ثانیه دریافت کند و مشخص کند چند ثانیه از شروع روز گذشته است.

                                            
                                        
                        #include <iostream>

int main() {
    int hours, minutes, seconds;

    // Prompt the user to enter hours
    std::cout << "Enter the hours (0-23): ";
    std::cin >> hours;

    // Prompt the user to enter minutes
    std::cout << "Enter the minutes (0-59): ";
    std::cin >> minutes;

    // Prompt the user to enter seconds
    std::cout << "Enter the seconds (0-59): ";
    std::cin >> seconds;

    // Calculate the total seconds since the start of the day
    int totalSeconds = (hours * 3600) + (minutes * 60) + seconds;

    // Display the result
    std::cout << "Total seconds since the start of the day: " << totalSeconds << std::endl;

    return 0;
}                    
                

برنامه‌ای بنویسید که یک عدد دریافت کند و قدرمطلق آن را چاپ نماید.

                                            
                                        
                        #include <iostream>
#include <cmath> // Include cmath library for abs function

int main() {
    double number;

    // Ask the user for a number
    std::cout << "Enter a number: ";
    std::cin >> number;

    // Calculate the absolute value
    double absoluteValue = std::abs(number);

    // Display the result
    std::cout << "The absolute value of " << number << " is " << absoluteValue << std::endl;

    return 0;
}                    
                

برنامه‌ای بنویسید که ضرایب یک معادله درجه دوم را دریافت کند و ریشه های معادله راچاپ کند.

                                            
                                        
                        #include <iostream>
#include <cmath> // Include cmath library for sqrt function

int main() {
    double a, b, c;

    // Ask the user for the coefficients
    std::cout << "Enter the coefficient a: ";
    std::cin >> a;
    std::cout << "Enter the coefficient b: ";
    std::cin >> b;
    std::cout << "Enter the coefficient c: ";
    std::cin >> c;

    // Calculate the discriminant
    double discriminant = b * b - 4 * a * c;

    // Check the nature of the roots based on the discriminant
    if (discriminant > 0) {
        // Two distinct real roots
        double root1 = (-b + std::sqrt(discriminant)) / (2 * a);
        double root2 = (-b - std::sqrt(discriminant)) / (2 * a);
        std::cout << "The roots are real and different." << std::endl;
        std::cout << "Root 1: " << root1 << std::endl;
        std::cout << "Root 2: " << root2 << std::endl;
    } else if (discriminant == 0) {
        // One real root
        double root = -b / (2 * a);
        std::cout << "The roots are real and the same." << std::endl;
        std::cout << "Root: " << root << std::endl;
    } else {
        // Complex roots
        double realPart = -b / (2 * a);
        double imaginaryPart = std::sqrt(-discriminant) / (2 * a);
        std::cout << "The roots are complex and different." << std::endl;
        std::cout << "Root 1: " << realPart << " + " << imaginaryPart << "i" << std::endl;
        std::cout << "Root 2: " << realPart << " - " << imaginaryPart << "i" << std::endl;
    }

    return 0;
}                    
                

برنامه‌ای بنویسید که سه ضلع یک مثلث را دریافت کند و مشخص کند مثلث متساوی الاضلاع، متساوی الساقین یا مختلف الاضلاع است

                                            
                                        
                        #include <iostream>

int main() {
    double side1, side2, side3;

    // Prompt the user to enter the lengths of the three sides of the triangle
    std::cout << "Enter the length of the first side: ";
    std::cin >> side1;
    std::cout << "Enter the length of the second side: ";
    std::cin >> side2;
    std::cout << "Enter the length of the third side: ";
    std::cin >> side3;

    // Check if the sides form a valid triangle
    if (side1 + side2 > side3 && side1 + side3 > side2 && side2 + side3 > side1) {
        // Determine the type of triangle
        if (side1 == side2 && side2 == side3) {
            std::cout << "The triangle is equilateral." << std::endl;
        } else if (side1 == side2 || side1 == side3 || side2 == side3) {
            std::cout << "The triangle is isosceles." << std::endl;
        } else {
            std::cout << "The triangle is scalene." << std::endl;
        }
    } else {
        std::cout << "The lengths entered do not form a valid triangle." << std::endl;
    }

    return 0;
}                    
                

برنامه‌ای بنویسید که از ورودی عدد n را دریافت کند و مجموع ۱ تا n را محاسبه کند.

                                            
                                        
                        #include <iostream>

int main() {
    int n;

    // Ask the user for a number
    std::cout << "Enter a positive integer: ";
    std::cin >> n;

    // Ensure the input is a positive integer
    if (n <= 0) {
        std::cout << "Please enter a positive integer." << std::endl;
        return 1; // Exit the program with an error code
    }

    // Calculate the sum from 1 to n
    int sum = 0;
    for (int i = 1; i <= n; ++i) {
        sum += i;
    }

    // Display the result
    std::cout << "The sum of integers from 1 to " << n << " is " << sum << std::endl;

    return 0;
}                    
                

برنامه‌ای بنویسید که از ورودی n را دریافت کند و فاکتوریل آن را محاسبه کند.

                                            
                                        
                        #include <iostream>

int main() {
    int n;
    unsigned long long factorial = 1; // Initialize factorial to 1 to handle larger values of n

    // Ask the user for a non-negative integer
    std::cout << "Enter a non-negative integer: ";
    std::cin >> n;

    // Check if the input is valid (non-negative)
    if (n < 0) {
        std::cout << "Please enter a non-negative integer." << std::endl;
        return 1; // Exit the program with an error code
    }

    // Calculate the factorial of n
    for (int i = 1; i <= n; ++i) {
        factorial *= i;
    }

    // Display the result
    std::cout << "The factorial of " << n << " is " << factorial << std::endl;

    return 0;
}                    
                

برنامه‌ای بنویسید که نمره ۱۰۰ دانشجو از ورودی دریافت کند و میانگین نمرات بالای ۱۵ را محاسبه کند.

                                            
                                        
                        #include <iostream>

int main() {
    const int numStudents = 100;
    int scores[numStudents];
    int sum = 0;
    int countAbove15 = 0;

    // Ask the user to enter the scores of 100 students
    std::cout << "Enter the scores of 100 students:\n";

    // Input scores
    for (int i = 0; i < numStudents; ++i) {
        std::cout << "Enter score for student " << i + 1 << ": ";
        std::cin >> scores[i];

        // Check if the score is above 15
        if (scores[i] > 15) {
            sum += scores[i];
            countAbove15++;
        }
    }

    // Calculate the average score above 15
    double averageAbove15 = 0.0;
    if (countAbove15 > 0) {
        averageAbove15 = static_cast<double>(sum) / countAbove15;
    }

    // Display the result
    std::cout << "The average score of students with scores above 15 is: " << averageAbove15 << std::endl;

    return 0;
}                    
                

مجید حسینی

نویسنده در زیرال ایده

طراح وب سایت و نویسنده ماهر در شرکت زیرال ایده

لطفا نظر خود را درباره این مطلب بگذارید. باتشکر!