Algoritma sorularının cevaplarına sizlerle paylaşmaya devam ediyoruz. Bu yazımızda hem döngüleri hemde karar yapılarını kullanarak çözümlenebilecek bir algoritma sorusu var.
Soru: Bir komisyoncu sattığı mallardan fiyatı 50 YTL kadar olanlardan %3, daha fazla olanlardan ise %2 komisyon almaktadır. Klavyeden girilen 5 malın komisyonlarını bularak, toplam komisyonu hesaplayınız.
Sorunun Akış Diyagramı:
Algoritması:
Fonksiyon Ana
Yaratmak Tamsayı komisyon, ucret, toplam, sayac
Atama toplam = 0
Atama komisyon = 0
For sayac = 1 .. 5
Çıktı sayac &". Ürünün Ücretini Giriniz:"
Giriş ucret
Eğer ucret < 50
Atama toplam = toplam + ucret * 0.3
Yanlış:
Atama toplam = toplam + ucret * 0.2
Son
Son
Çıktı "Toplam Komisyon Değeri =" & toplam
Son
Python Programlama Dili İle Çözümü
toplam = 0
komisyon = 0
for sayac in range(1, 5 + 1, 1):
print(str(sayac) + ". Ürünün Ücretini Giriniz:")
ucret = int(input())
if ucret < 50:
toplam = toplam + ucret * 0.3
else:
toplam = toplam + ucret * 0.2
print("Toplam Komisyon Değeri =" + str(toplam))
C# Programlama Dili İle Çözümü
using System;
public class MyProgram
{
public static void Main(string[] args)
{
int komisyon, ucret, toplam, sayac;
toplam = 0;
komisyon = 0;
for (sayac = 1; sayac <= 5; sayac++)
{
Console.WriteLine(sayac.toString() + ". Ürünün Ücretini Giriniz:");
ucret = (int) readValue();
if (ucret < 50)
{
toplam = toplam + ucret * 0.3;
}
else
{
toplam = toplam + ucret * 0.2;
}
}
Console.WriteLine("Toplam Komisyon Değeri =" + toplam);
}
// .NET can only read single characters or entire lines from the console.
// The following function safely reads a double value.
private static double readValue()
{
double result;
while (!double.TryParse(Console.ReadLine(), out result));
return result;
}
}
C++ Programlama Dili İle Çözümü
#include <iostream>
#include <sstream>
#include <string>
#include <cstdlib>
#include <cmath>
using namespace std;
// Headers
string toString (double);
int toInt (string);
double toDouble (string);
int main() {
int komisyon, ucret, toplam, sayac;
toplam = 0;
komisyon = 0;
for (sayac = 1; sayac <= 5; sayac++) {
cout << sayac << ". Ürünün Ücretini Giriniz:" << endl;
cin >> ucret;
if (ucret < 50) {
toplam = toplam + ucret * 0.3;
} else {
toplam = toplam + ucret * 0.2;
}
}
cout << "Toplam Komisyon Değeri =" << toplam << endl;
return 0;
}
// The following implements type conversion functions.
string toString (double value) { //int also
stringstream temp;
temp << value;
return temp.str();
}
int toInt (string text) {
return atoi(text.c_str());
}
double toDouble (string text) {
return atof(text.c_str());
}
Java Programlama Dili İle Çözümü:
import java.util.*;
import java.lang.Math;
public class JavaApplication {
private static Scanner input = new Scanner(System.in);
public static void main(String[] args) {
int komisyon, ucret, toplam, sayac;
toplam = 0;
komisyon = 0;
for (sayac = 1; sayac <= 5; sayac++) {
System.out.println(Integer.toString(sayac) + ". Ürünün Ücretini Giriniz:");
ucret = input.nextInt();
if (ucret < 50) {
toplam = toplam + ucret * 0.3;
} else {
toplam = toplam + ucret * 0.2;
}
}
System.out.println("Toplam Komisyon Değeri =" + toplam);
}
}