-
Notifications
You must be signed in to change notification settings - Fork 0
/
Balance.c
59 lines (48 loc) · 1001 Bytes
/
Balance.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <stdio.h>
/* כתוב תוכנית שמקבלת מספר
ומחזירה האם המספר הוא BAךANCED
או UNBALANCED
*/
int getFirstNum(long num) {
if (num >10) {
countOfDigits(num);
return (int)getFirstNum(num / 10);
}
else
return (int)num;
}
int countOfDigits(long num) {
static int counter = 0;
if (num > 0)
counter++;
return counter;
}
void mainBalance() {
long num;
int counter = 0;
int balance = 1,firstnum;
printf("enter number to check: ");
scanf_s("%ld" ,&num);
firstnum = getFirstNum(num);
if ((countOfDigits(0)) != firstnum)//האם כמות המספרים לבדוק גדולה מהמספר?
{
balance = 0;
}
else
{
long copy = num;
for (int i = 0; i < countOfDigits(0) - 1; i++)
if (copy % 10 != firstnum) {
balance = 0;
break;
}
else
copy /= 10;
}
if (balance == 1)
printf("the number %ld is Balanced!!!", num);
else
printf("the number %ld is UNBalanced", num);
printf("\n");
system("pause");
}