Rumah  >  Artikel  >  pembangunan bahagian belakang  >  Program C: Tambah dua pecahan

Program C: Tambah dua pecahan

WBOY
WBOYke hadapan
2023-08-31 10:21:041201semak imbas

Program C: Tambah dua pecahan

Diberikan input sebagai pecahan iaitu a/b dan c/d di mana a, b, c dan d boleh menjadi sebarang nilai integer kecuali 0, tugasnya ialah menambah dua pecahan ini untuk menjana akhir dan.

Pecahan diwakili oleh −

  • a / b, di mana a dipanggil pengangka dan b dipanggil penyebut.
  • a dan b boleh mempunyai sebarang nilai, tetapi b tidak boleh 0.
  • Jumlah dua pecahan dinyatakan sebagai a/b + c/d, peraturan untuk menambah dua sebutan ini ialah penyebutnya mestilah sama, jika tidak sama, hendaklah dibuat sama sebelum penambahan boleh dilakukan. Terjemahan bahasa Cina bagi

Contoh

Input-: 1/4 + 2/12
Output-: 5/12
Since both the fractions denominators are unequal so to make them equal either GCD or LCM can be calculated. So in this case by multiplying the denominator which is 4 by 3 we can make them equal
(1 * 3) / (4 * 3) = 3 / 12
Add both the terms: 3 / 12 + 2 / 12 = 5 / 12
Input-: 1/4 + 2/4
Output-: 3/4
Since both the terms have same denominator they can be directly added

Algoritma

In function int gcd(int a, int b)
Step 1-> If a == 0 then,
   return b
Step 2-> Return gcd(b%a, a)
In function void smallest(int &den3, int &n3)
   Step 1-> Declare and initialize common_factor as gcd(n3,den3)
   Step 2-> Set den3 = den3/common_factor
   Step 3-> Set n3 = n3/common_factor
In Function void add_frac(int n1, int den1, int n2, int den2, int &n3, int &den3)
   Step 1-> Set den3 = gcd(den1,den2)
   Step 2-> Set den3 = (den1*den2) / den3
   Step 3-> Set n3 = (n1)*(den3/den1) + (n2)*(den3/den2)
   Step 4-> Call function smallest(den3,n3)
In Function int main()
   Step 1-> Declare and initialize n1=1, den1=4, n2=2, den2=12, den3, n3
   Step 2-> Call add_frac(n1, den1, n2, den2, n3, den3)
   Step 3-> Print the values of n1, den1, n2, den2, n3, den3

Contoh

ialah:

Contoh

#include <stdio.h>
int gcd(int a, int b) {
   if (a == 0)
      return b;
   return gcd(b%a, a);
}
void smallest(int &den3, int &n3) {
   // Finding gcd of both terms
   int common_factor = gcd(n3,den3);
   den3 = den3/common_factor;
   n3 = n3/common_factor;
}
void add_frac(int n1, int den1, int n2, int den2, int &n3, int &den3) {
   // to find the gcd of den1 and den2
   den3 = gcd(den1,den2);
    // LCM * GCD = a * b
   den3 = (den1*den2) / den3;
   // Changing the inputs to have same denominator
   // Numerator of the final fraction obtained
   n3 = (n1)*(den3/den1) + (n2)*(den3/den2);
   smallest(den3,n3);
}
// Driver program
int main() {
   int n1=1, den1=4, n2=2, den2=12, den3, n3;
   add_frac(n1, den1, n2, den2, n3, den3);
   printf("%d/%d + %d/%d = %d/%d</p><p>", n1, den1, n2, den2, n3, den3);
   return 0;
}

Output

1/4 + 2/12 = 5/12

Atas ialah kandungan terperinci Program C: Tambah dua pecahan. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Kenyataan:
Artikel ini dikembalikan pada:tutorialspoint.com. Jika ada pelanggaran, sila hubungi admin@php.cn Padam