marți, 11 decembrie 2018

c++ programming part eleven



1048. The largest common divisor of two integers a and b, read from the keyboard
1049. Cmmdc will be calculated using the relationship:
1050. cmmdc (a, b) = cmmdc (a-b, b), if a> b
1051. = cmmdc (a, b-a), if b> a
1052. = a, else
give you the things you do not need
anymore
1053. So:
1054. cmmdc (9,27) = cmmdc (9,18) = cmmdc (9,9) = 9 because a and b have become
equals
1055. #include  
1056. int cmmdc (int a, int b)
1057. {
1058. if (a == b) return a;
1059. else if (a> b) return cmmdc (ab, b);
1060. else return cmmdc (a, ba);
1061. }
1062. int main ()
1063. {
1064. int x, y;

1065. cout << "x =" cin >> x;
1066. cout << "y =" cin >> y;
1067. cout << "cmmdc is:" << cmmdc (x, y);
1068. return 0;
1069. }

1070. Calculating the sum of digits of a number
1071. #include  
1072. int n;
1073. /define the function s which will calculate the amount
1074. int s (int n)
1075. {
1076. / if the number is equal then the sum of its figures is 0
1077. if (n == 0) return 0;
1078. /the last digit of any number is the remainder of the 10th division, and
1079. /the number left after removing the last digit is the full length of the
1080. /dividing the number to 10
1081. else return n% 10 + s (n / 10);
1082. }
1083. int main ()
1084. {
1085. cout << "n =" cin >> n;
1086. cout << "sum of digits:" << s (n);
1087. return 0;
1088. }

Niciun comentariu:

Trimiteți un comentariu

Rețineți: Numai membrii acestui blog pot posta comentarii.