-
Notifications
You must be signed in to change notification settings - Fork 1
/
skipCounting.c
55 lines (42 loc) · 975 Bytes
/
skipCounting.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
#include <stdio.h>
int skipCount(int, int, int);
int countOddDigits(int);
int main(void){
int startPos, endPos, lower, upper;
// Insert appropriate statements for reading the inputs.
printf("Enter starting position: ");
scanf("%d",&startPos);
printf("Enter lower bound and upper bound: ");
scanf("%d %d",&lower, &upper);
endPos = skipCount(startPos, lower, upper);
printf("The ending position is %d.\n", endPos);
return 0;
}
int skipCount(int startPos, int lower, int upper){
int currentPos,i, num;
currentPos = startPos;
for(i = lower; i <=upper; i++){
num = countOddDigits(i);
currentPos = currentPos + num;
if(currentPos == 5){
continue;
}
currentPos = currentPos % 5;
}
return currentPos;
}
int countOddDigits(int num){
int lastDigit, counter;
counter = 1;
while(num >0){
lastDigit = num % 10; //get last digit
num = num/10;
if(lastDigit == 0){
continue;
}
if(lastDigit % 2 != 0){
counter++;
}
}
return counter;
}