No cache version.

Caching disabled. Default setting for this page:enabled (code LNG204)
If the display is too slow, you can disable the user mode to view the cached version.

Multiplication par additions

Un premier exercice nous est proposé : écrire un programme de multiplication de deux nombres entiers qui ne fait usage que de l'addition.

Les différentes parties du code :

  1. /*
  2. Multi.c
  3. multiplication à la russe (utilisation uniquement d'additions)
  4. */
  5. #include <stdio.h>

Prototype de la fonction multi() :

  1. int multi (int,int);

Fonction principale :

  1. void main (void)
  2. {
  3. int p, q;
  4. printf("\nintroduire 2 nbres dont le second est > 0 : ");
  5. scanf("%d",&p);
  6. while (p)
  7. {
  8. scanf("%d", &q);
  9. printf("\n %d * %d = %d\n", p, q, multi(p, q));
  10. printf("\nintroduire 2 nbres dont le second est > 0 : ");
  11. scanf("%d", &p);
  12. }
  13. }

Nous pouvons remarquer ici que les signes opérateurs (*,=) sont considérés uniquement comme éléments de la chaîne de caractères, et non comme opérateurs.
Nous pouvons aussi remarquer l'appel de la fonction multi, à laquelle nous fournissons deux valeurs (arguments).

Fonction multi() :

  1. int multi (int a, int b)
  2. {
  3. int m=0, k;
  4. for (k=0;<b; k++) m+=a;
  5. return m;
  6. }

English translation

You have asked to visit this site in English. For now, only the interface is translated, but not all the content yet.

If you want to help me in translations, your contribution is welcome. All you need to do is register on the site, and send me a message asking me to add you to the group of translators, which will give you the opportunity to translate the pages you want. A link at the bottom of each translated page indicates that you are the translator, and has a link to your profile.

Thank you in advance.

Document created the 19/03/2002, last modified the 26/10/2018
Source of the printed document:https://www.gaudry.be/en/c-exemple-multiplication.html

The infobrol is a personal site whose content is my sole responsibility. The text is available under CreativeCommons license (BY-NC-SA). More info on the terms of use and the author.