Практикум по программированию. Основы. Ввод и вывод. Арифметическая прогрессия 1

From AsIsWiki
Jump to: navigation, search

Назад · Задачи · Дальше


Contents

Java

import java.util.Scanner;

public class Task09 {

    public static void main(String[] args) {
        
        Scanner in = new Scanner(System.in);
        
        System.out.println();
        System.out.println(" Арифметическая прогрессия");
        System.out.println("---------------------------");

        System.out.print(" Введите 1-ый член прогрессии: "); 
        double a1 = in.nextDouble();

        System.out.print(" Введите 5-ый член прогрессии: "); 
        double a5 = in.nextDouble();

        System.out.print(" Введите номер члена прогрессии: "); 
        int n = in.nextInt();

        System.out.println("---------------------------");
                            
        double d = (a5 - a1) / 4;
        double an = a1 + (n - 1) * d;
        double sn = n * (a1 + an) / 2;

        System.out.printf(" A%d = %.2f\n", n, an);
        System.out.printf(" S%d = %.2f\n", n, sn);
    }
}


C++

// g++ 4.2

#include <iostream>

using namespace std;

int main() {
    
    double a1, a5;
    int n;
    
    cout << "\n Арифметическая прогрессия\n";
    cout << "---------------------------\n";
    
    cout << " Введите 1-ый член прогрессии: ";
    cin >> a1;
    
    cout << " Введите 5-ый член прогрессии: ";
    cin >> a5;
    
    cout << " Введите номер члена прогрессии: ";
    cin >> n;
    
    cout << "---------------------------\n";
    
    double d = (a5 - a1) / 4;
    double an = a1 + (n - 1) * d;
    double sn = n * (a1 + an) / 2;
    
    printf(" A%d = %.2f\n", n, an);
    printf(" S%d = %.2f\n\n", n, sn);
    
    return 0;
}


Python

# Python 3

print('\n Арифметическая прогрессия')
print('---------------------------')

a1 = float(input(' Введите 1-ый член прогрессии: '))
a5 = float(input(' Введите 5-ый член прогрессии: '))
n = int(input(' Введите номер члена прогрессии: '))

print('---------------------------')

d = (a5 - a1) / 4
an = a1 + (n - 1) * d
sn = n * (a1 + an) / 2

print(' A%d = %.2f' % (n, an))
print(' S%d = %.2f' % (n, sn))


Pascal



JavaScript

<html lang="ru">
<head>
    <meta charset="UTF-8">
    <script>
        function calc() {
            var a1 = Number(document.getElementById("a1Id").value);
            var a5 = Number(document.getElementById("a5Id").value);
            var n = Number(document.getElementById("nId").value);

            var d = (a5 - a1) / 4;
            var an = a1 + (n - 1) * d;
            var sn = n * (a1 + an) / 2;

            document.getElementById("resultId").innerHTML =
                    "A" + n + " = " + an.toFixed(2) + "<br>" +
                    "S" + n + " = " + sn.toFixed(2);
        }
    </script>
</head>
<body>

<p>Арифметическая прогрессия</p>
<hr>
<p>Введите 1-ый член прогрессии: <input id="a1Id" size="5"></p>
<p>Введите 5-ый член прогрессии: <input id="a5Id" size="5"></p>
<p>Введите номер члена прогрессии: <input id="nId" size="5"></p>
<hr>
<p id="resultId"></p>

<button onclick="calc()">Рассчитать</button>

</body>
</html>



Назад · Задачи · Дальше

Personal tools
Namespaces

Variants
Actions
Navigation
Tools