Home > Net >  C coding interesting
C coding interesting

Time:11-05

#include <stdio.h>

int main (void) //starting entry of integer indicate not associated with any data types argument
{
    int value1, value2, calculate = 0; //integer values
    
    printf ("\nProgram should perform the following:"); 
    printf("\n"); 
    
    printf ("\nHave the user enter two different integer numbers to be divided by the program "); 
    {

                printf ("\nIf the division of both numbers");
                printf("\n"); 
                printf ("\nresult with the remainder of zero,"); 
        
                printf("\n"); 
    
                printf ("\nby the second number.");
    
                printf("\n"); 
    
                printf ("Example: thirty-five divided by seven will have a remainder of zero"); 
    
                            printf ("\n"); 
    }
    
    printf ("Please enter the first integer number>>"); 
    scanf  ("%i", &value1);
    
    printf ("Please enter the second integer>> ");  
    scanf  ("%i", &value2); 
       
    if (value1 % value2 == 0 ) {
      
       printf ("\n%i is evenly divisible by %i\n", value1, value2);
       calculate = value1 / value2;
       printf ("%i / %i = %i\n", value1, value2, calculate);
       
       
    else
       printf ("\n%i is not evenly divisible by %i\n", value1, value2);
    

return 0;

}

This code, that I have written is working correctly. But I can't seem to find which else or else if statement to use to make the code say Error: A zero is allowed in the program if the second number that the user enters is zero.

CodePudding user response:

If you want to introduce such message for the user, you have to implement the condition to check for the numbers after the user has provided the input. which can be done like:

#include <stdio.h>

int main(void)  //starting entry of integer indicate not associated with any data types argument
{
    int value1, value2, calculate = 0;  //integer values
    printf("\nProgram should perform the following:");
    printf("\n");
    printf("\nHave the user enter two different integer numbers to be divided by the program ");
    printf("\nIf the division of both numbers");
    printf("\n");
    printf("\nresult with the remainder of zero,");
    printf("\n");
    printf("\nby the second number.");
    printf("\n");
    printf("Example: thirty-five divided by seven will have a remainder of zero");
    printf("\n");
    printf("Please enter the first integer number>>");
    scanf("%i", &value1);
    printf("Please enter the second integer>> ");
    scanf("%i", &value2);

    if (value1 == 0 && value2 != 0)
    {
        printf("\nA zero is allowed in the program if the second number that the user enters is zero");
        return 0;
    }
    else
    {
        if (value1 % value2 == 0)
        {
            printf("\n%i is evenly divisible by %i\n", value1, value2);
            calculate = value1 / value2;
            printf("%i / %i = %i\n", value1, value2, calculate);
        }
        else
        {
            printf("\n%i is not evenly divisible by %i\n", value1, value2);
        }
    }

    return 0;
}

CodePudding user response:

Don't use parentheses after printf, it changes nothing and use %d inside scanf and printf for integers. And close if parentheses before else.

  • Related