logo

C'de Hesap Makinesi Programı

Bu konu başlığımızda C programlama dilinde hesap makinesi programını nasıl yazdığımızı ele alacağız. Hesap Makinesi, toplama, çıkarma, çarpma, bölme, yüzde vb. gibi çeşitli aritmetik işlemleri gerçekleştirmek için kullanılan küçük bir elektronik cihazdır. Hesaplamalarımızı daha kolay ve daha hızlı hale getirir. Basit matematik işlemlerini gerçekleştirmek için her yerde kullanılabilen taşınabilir bir cihazdır. Trigonometri fonksiyonları, üstel operatörler, dereceler, radyanlar, log fonksiyonları, hiperbolik fonksiyonlar vb. gibi karmaşık hesaplamaları çözmemiz gereken bazı durumlarda bilimsel veya karmaşık bir hesap makinesi kullanırız. C dilinde bir hesap makinesi programı oluşturmanın çeşitli yollarını tartışalım. .

C'de Hesap Makinesi Programı

Hesap Makinesi Programı Algoritması

Aşama 1: Yerel değişkenleri n1, n2, res, opt olarak bildirin. Örneğin, n1 ve n2 iki sayısal değer alırken, res sonuçları saklayacak ve opt değişkeni operatör sembollerini tanımlayacaktır.

Adım 2: Seçimi Yazdırın (Toplama, Çıkarma, çarpma, bölme vb.)

Aşama 3: Seçime Girin

Adım 4: n1 ve n2 olmak üzere iki sayı alır

sonraki Java'sı var

Adım 5: Kullanıcı tarafından seçilen bir operatöre büyük/küçük harf atlaması

Adım 6: Sonucu res değişkenine kaydedin.

Adım 7: İşlem sonucunu görüntüle

Adım 8: Programdan çıkın.

C'de Hesap Makinesi Programı oluşturmanın farklı yolları

Aşağıda C dilinde bir Hesap Makinesi Programı yazmanın farklı yolları verilmiştir.

java dizeyi int'ye aktardı
  1. Switch ifadesini kullanan C'deki Hesap Makinesi Programı
  2. C'de if else if ifadesini kullanan Hesap Makinesi Programı
  3. Do-while döngüsü ve switch deyimini kullanan C'deki Hesap Makinesi Programı
  4. C'deki Hesap Makinesi Programı, fonksiyon ve anahtar ifadesini kullanarak

Örnek 1: Switch ifadesini kullanan C'deki Hesap Makinesi Programı

Switch ifadesini kullanarak Hesap Makinesi programı oluşturacak bir program yazalım

program.c

 #include int main() { // declare local variables char opt; int n1, n2; float res; printf (' Choose an operator(+, -, *, /) to perform the operation in C Calculator 
 '); scanf ('%c', &opt); // take an operator if (opt == '/' ) { printf (' You have selected: Division'); } else if (opt == '*') { printf (' You have selected: Multiplication'); } else if (opt == '-') { printf (' You have selected: Subtraction'); } else if (opt == '+') { printf (' You have selected: Addition'); } printf (' 
 Enter the first number: '); scanf(' %d', &n1); // take fist number printf (' Enter the second number: '); scanf (' %d', &n2); // take second number switch(opt) { case '+': res = n1 + n2; // add two numbers printf (' Addition of %d and %d is: %.2f', n1, n2, res); break; case '-': res = n1 - n2; // subtract two numbers printf (' Subtraction of %d and %d is: %.2f', n1, n2, res); break; case '*': res = n1 * n2; // multiply two numbers printf (' Multiplication of %d and %d is: %.2f', n1, n2, res); break; case '/': if (n2 == 0) // if n2 == 0, take another number { printf (' 
 Divisor cannot be zero. Please enter another value '); scanf ('%d', &n2); } res = n1 / n2; // divide two numbers printf (' Division of %d and %d is: %.2f', n1, n2, res); break; default: /* use default to print default message if any condition is not satisfied */ printf (' Something is wrong!! Please check the options '); } return 0; } 

Çıktı:

C'de Hesap Makinesi Programı

Örnek 2: C'de if else if ifadesini kullanan Hesap Makinesi Programı

If else if ifadesini kullanarak C dilinde basit bir Hesap Makinesi programı yazmaya yönelik bir örnek düşünelim.

program2.c

 #include int main() { // declare local variables char opt; int n1, n2; float res; printf (' Select an operator (+, -, *, /) to perform an operation in C calculator 
 '); scanf ('%c', &opt); // take an operator printf (' Enter the first number: '); scanf(' %d', &n1); // take fist number printf (' Enter the second number: '); scanf (' %d', &n2); // take second number if (opt == '+') { res = n1 + n2; // add two numbers printf (' Addition of %d and %d is: %f', n1, n2, res); } else if (opt == '-') { res = n1 - n2; // subtract two numbers printf (' Subtraction of %d and %d is: %f', n1, n2, res); } else if (opt == '*') { res = n1 * n2; // multiply two numbers printf (' Multiplication of %d and %d is: %f', n1, n2, res); } else if (opt == '/') { if (n2 == 0) // if n2 == 0, take another number { printf (' 
 Divisor cannot be zero. Please enter another value '); scanf ('%d', &n2); } res = n1 / n2; // divide two numbers printf (' Division of %d and %d is: %.2f', n1, n2, res); } else { printf(' 
 You have entered wrong inputs '); } return 0; } 

Çıktı:

C'de Hesap Makinesi Programı

Örnek 3: C'de do while döngüsü ve switch ifadesini kullanan Hesap Makinesi Programı

C'de do while döngüsünü ve switch case ifadesini kullanarak bir Hesap Makinesi programı oluşturalım.

program3.c

huffman kodlama kodu
 #include #include #include int main() { // declaration of local variable op; int op, n1, n2; float res; char ch; do { // displays the multiple operations of the C Calculator printf (' Select an operation to perform the calculation in C Calculator: '); printf (' 
 1 Addition 	 	 2 Subtraction 
 3 Multiplication 	 4 Division 
 5 Square 	 	 6 Square Root 
 7 Exit 
 
 Please, Make a choice '); scanf ('%d', &op); // accepts a numeric input to choose the operation // use switch statement to call an operation switch (op) { case 1: // Add two numbers printf (' You chose: Addition'); printf ('
 Enter First Number: '); scanf (' %d', &n1); printf (' Enter Second Number: '); scanf (' %d', &n2); res = n1 + n2; // Add two numbers printf (' Addition of two numbers is: %.2f', res); break; // break the function case 2: // Subtract two numbers printf (' You chose: Subtraction'); printf ('
 Enter First Number: '); scanf (' %d', &n1); printf (' Enter Second Number: '); scanf (' %d', &n2); res = n1 - n2; // subtract two numbers printf (' Subtraction of two numbers is: %.2f', res); break; // break the function case 3: // Multiplication of the numbers printf (' You chose: Multiplication'); printf ('
 Enter First Number: '); scanf (' %d', &n1); printf (' Enter Second Number: '); scanf (' %d', &n2); res = n1 * n2; // multiply two numbers printf (' Multiplication of two numbers is: %.2f', res); break; // break the function case 4: // Division of the numbers printf (' You chose: Division'); printf ('
 Enter First Number: '); scanf (' %d', &n1); printf (' Enter Second Number: '); scanf (' %d', &n2); if (n2 == 0) { printf (' 
 Divisor cannot be zero. Please enter another value '); scanf ('%d', &n2); } res = n1 / n2; // divide two numbers printf (' Division of two numbers is: %.2f', res); break; // break the function case 5: // getting square of a number printf (' You chose: Square'); printf ('
 Enter First Number: '); scanf (' %d', &n1); res = n1 * n1; // get square of a number printf (' Square of %d number is: %.2f', n1, res); break; // break the function case 6: // getting the square root of the number printf (' You chose: Square Root'); printf ('
 Enter First Number: '); scanf (' %d', &n1); res = sqrt(n1); // use sqrt() function to find the Square Root printf (' Square Root of %d numbers is: %.2f', n1, res); break; // break the function case 7: printf (' You chose: Exit'); exit(0); break; // break the function default: printf(' Something is wrong!! '); break; } printf (' 
 
 ********************************************** 
 '); } while (op != 7); return 0; } 

Çıktı:

C'de Hesap Makinesi Programı

Örnek 4: C'deki Hesap Makinesi Programının fonksiyon ve switch ifadesini kullanması

C'de fonksiyon ve switch case deyimini kullanarak bir Hesap Makinesi programı oluşturalım

program4.c

 #include #include #include #include // function declarations int addition(); int subtract(); int multiply(); int divide(); int sq(); int sqrt1(); void exit(); int main() { // declaration a local variable op; int op; do { // displays the multiple operations of the C Calculator printf (' Select an operation to perform the calculation in C Calculator: '); printf (' 
 1 Addition 	 	 2 Subtraction 
 3 Multiplication 	 4 Division 
 5 Square 	 	 6 Square Root 
 7 Exit 
 
 Please, Make a choice '); scanf ('%d', &op); // accepts a numeric input to choose the operation // use switch statement to call an operation switch (op) { case 1: addition(); /* It call the addition() function to add the given numbers */ break; // break the function case 2: subtract(); /* It call the subtract() function to subtract the given numbers */ break; // break the function case 3: multiply(); /* It call the multiply() function to multiply the given numbers */ break; // break the function case 4: divide(); // It call the divide() function to divide the given numbers break; // break the function case 5: sq(); // It call the sq() function to get the square of given numbers break; // break the function case 6: sqrt1(); /* It call the sqrt1() function to get the square root of given numbers */ break; // break the function case 7: exit(0); // It call the exit() function to exit from the program break; // break the function default: printf(' Something is wrong!! '); break; } printf (' 
 
 ********************************************** 
 '); } while (op != 7); return 0; } // function definition int addition() { int i, sum = 0, num, f_num; // declare a local variable printf (' How many numbers you want to add: '); scanf ('%d', &num); printf (' Enter the numbers: 
 &apos;); for (i = 1; i <= num; i++) { scanf(' %d', &f_num); sum="sum" + f_num; } printf (' total of the numbers="%d&apos;," sum); return 0; use subtract() function to subtract two int n1, n2, res; first number is: '); scanf &n1); second &n2); res="n1" - n2; subtraction %d res); multiply() multiply * divide() divide if (n2="=" 0) 
 divisor cannot be zero. please enter another value ('%d', division sq() get square given a square: n1; sqrt1() root float root: %f', < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/c-tutorial/61/calculator-program-c-5.webp" alt="Calculator Program in C"> <hr></=>