-5

Input and Output Format:

Input consists of a number which corresponds to the bill number. bill number is a 3-digit number and all the 3 digits in the number are even.

Output consists of a string that is either 'yes' or 'no'. Output is yes when the customer receives the prize and is no otherwise.

Samples:

Input     Output
565       no
620       yes
66        no         # Not a 3-digit number (implicit leading zeros not allowed)
002       yes        # 3-digit number

I have solved the problem by getting single digit with "number" mod 10 and then checking if "digit" mod 2 is 0 or not......

But in case if I give input "002", it prints "no" instead I want it should be "yes".

Code — copied and formatted from comment:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
int main()
{
    int num, t, flag, count = 0;
    while (num)
    {
        t = num % 10;
        num = num / 10;
        if (t % 2 == 0)
        {
            flag = 1;
        }
        else
        {
            flag = 0;
        }
        count++;
    }
    if (flag == 1 && count == 3)
    {
        printf("yes");
    }
    else
    {
        printf("no");
    }
    return 0;
}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • @SharptechCompany: How sure are you that this code is working? You are not even considering the input from the user for `num`. – Santosh A Jul 08 '15 at 05:23
  • 1
    Please edit your code into your question in future. I've done it for you this time (formatted the way I prefer, more or less — if you prefer a different format, please reformat it to suit yourself). Where do you initialize `num`? Short answer: you don't. You can't run the code and supply different values. Don't miss out crucial code. You probably need to read a string and analyze the string; otherwise, you won't be able to spot leading zeros. – Jonathan Leffler Jul 08 '15 at 05:30

3 Answers3

1

You have to work with strings instead of numbers, otherwise you cannot represent the 002 value.

Recognize even digits:

int even(char c) {
    switch (c) {
        case '0':
        case '2':
        case '4':
        case '6':
        case '8':
            return 1;
        default:
            return 0;
    }
}

Recognize strings with only even digits:

int all_even(char* s) {
    while (*s != '\0') {
        if (!even(*s)) {
            return 0;
        }
        s++;
    }
    return 1;
}

Return "yes" only for strings of 3 even digits, and return "no" for all other strings:

char* answer(char* s) {
    return (strlen(s) == 3 && all_even(s)) ? "yes" : "no";
}
dlask
  • 8,776
  • 1
  • 26
  • 30
0

How can you read 002 in integer? When you enter 002, value of num will be 2 only not 002. In this case you while loop will run for one time only and value of count will be 1. So you if condition is false in this case.

Try this solution.

 #include<stdio.h>
 #include<string.h>
 void main(){
    char bill[4];
    int i, flag=0;
    int digit, len;

    scanf("%s",bill);

    len = strlen(bill);
    if(len<3){
            printf("no\n");
            return;
    }

    for(i=0;i<len;i++){

            digit = bill[i] - '0';

            if(digit%2 == 0) flag = 1;
            else{
                    flag = 0;
                    break;
            }
    }

    if(i==3 && flag == 1) printf("Yes\n");
    else printf("No\n");

    return;
}
NightWatcher
  • 148
  • 10
  • In case of your suggestion if i'll give input of any one digit like "1","2"..etc it will give "yes"...but i want "no" for a single digit and same for two digit number. Output should "yes" only if input is 3 digits and all of three should even. – Sharptech Company Jul 08 '15 at 05:27
  • Then try to read this `bill number` as `char array` and then you can do it. Here is sample code for it. ` char bill[4]; int len; int digit; int i, flag=0; scanf(“%s”,bill); len = strlen(bill); if(len < 3){ printf(“No\n”); return; } for(i=0;i – NightWatcher Jul 08 '15 at 05:31
  • it is giving output "yes" for input as "002" but for "032" its giving "yes" too but 3 is odd digit in the number – Sharptech Company Jul 08 '15 at 05:51
  • Note that this will report 'yes' (it is an even number) if you type 2467. That's because the `scanf()` stops after three non-blank characters. – Jonathan Leffler Jul 08 '15 at 06:54
  • Yes Right, But @SharptechCompany has mentioned that bill number contains 3 digit. User can enter more then 3 digit. Developer needs to handle all this senior. – NightWatcher Jul 08 '15 at 07:58
0

Check if this works

 #include <stdio.h>
    #include <string.h>
    char * checknum(char a)
    {
        switch (a){
            case '0':
            case '2':
            case '4':
            case '8':
            return "YES";
            default: return "NO";
        }
    }

    int main()
    {
        char input [3];int i;
        char * output;
        printf("Enter the number");
        scanf("%s",&input);
        if(strlen(input)==3)
        {
            for(i=0;i<strlen(input);i++)
            {
                output=checknum(input[i]);
                if(output=="NO")
                {
                    printf("NO\n");
                    break;
                }
            }
            if(output=="YES")
                {
                    printf("YES\n");
                }
        }
        return 0;
    }
Shravan Yadav
  • 1,297
  • 1
  • 14
  • 26
  • 1
    i tried the above code but it is giving Compilation Errors - warning: comparison with string literal results in unspecified behavior [-Waddress] Main.c:115:14: warning: comparison with string literal results in unspecified behavior [-Waddress] – Sharptech Company Jul 08 '15 at 06:19
  • I am testing this code on below link http://www.tutorialspoint.com/compile_c_online.php – Shravan Yadav Jul 08 '15 at 06:25
  • You compare strings with `strcmp()`. The use of `"YES"` and `"NO"` in the check function is unexpected. It would be better to call the function `is_even_digit()` and have it return 0 (false) if not and 1 (true) if so. I note that if the user types `fluffy` instead of a number, the code would treat that as a sequence of odd digits — the letters of the alphabet are very odd digits indeed, of course. – Jonathan Leffler Jul 08 '15 at 06:52