Why does C program give wrong output for positive integer?

online c compiler
c programming exercises
write ac program to display the equation of a line in the form ax + by c
write ac program to print a big 'c
write ac program to display multiple variables
write a c program that read 5 numbers and sum of all odd values between them.
write ac program that reads two integers and checks if they are multiplied or not
write a c program to print the following characters in a reverse way

The below program gives correct result for negative and zero integer, but for positive integer, it gives wrong output:

Enter the value of a : 6
The no is positive
The no is zero

Why?

int main()
{   int a;
    printf("Enter the value of a : ");
    scanf("%d",&a);
    if(a>0)
        printf("The no is positive\n");
    if(a<0)
        printf("The no is negative\n");
    else
       printf("The no is zero\n");
}

You have to write

if(a>0)
    printf("The no is positive\n");
else if(a<0)
    printf("The no is negative\n");
else
   printf("The no is zero\n");

Otherwise the two if statements are executed independly.

if(a>0)
    printf("The no is positive\n");

and

if(a<0)
    printf("The no is negative\n");
else
   printf("The no is zero\n");

and for a positive number you will get two outputs.

C Exercises: Accepts 4 integers and check various condition , C programming, exercises, solution: Write a C program that accepts 4 integers p, q, r, s from the user where r and s are positive and p is even. if the sum of r and s is greater than the sum of p and q print 'Correct values', otherwise print 'Wrong values'. Sample Output: puts("Please give 4 integers:");. If the number is greater than 0, then the given number is positive. Else the value we entered is 0. C program to find Positive or Negative Number using Else If. This program lets the user enter any number and then checks whether the entered value is either positive or negative or zero using Else If Statement in C.

Your second if should be else if. Both the first condition and the else get run otherwise when you have a positive number.

C Exercises: Accepts a positive integer n less than 100 from the user , C programming, exercises, solution: Write a C program that accepts a positive integer n less than Test data and expected output: number less than 100: \n"​); scanf("%d", & n); if (n < 1 || n >= 100) { printf("Wrong input\n"); return 0; } Input a positive number less than 100: 68 Sum of the series is 37361622. Force user to input a positive integer and put user in a loop till they do. So I want everything including characters not allowed just over > 0 I tried the following: while (i &lt; 0) do {

It's because you are using if statements without following else blocks. Once the first if is evaluated, it still evaluated the second if and then since that will be false if the input is positive, the else block will run, printing the wrong output.

C Basics - C Programming Tutorial, Below is a simple C program that illustrates the important programming 1 + 2 * 3 // give int 7 int sum, number; sum + number // evaluated to an int value double The integers (except char ) are signed number (which can hold zero, positive and C programs use function printf() of library stdio to print output to the console. @Carol In C and C++ I always initialize my values (Java complains about it, so I always do it now), you cannot exit the function without reading a correct value so it is not necessary. Flush is to make sure that the standard input buffer is correctly displayed, I was thought that way, you can remove it if you want.

Brother you have to use else if because the code checks for the first check i.e a>0 which is true then it goes after that as no else is present it goes on to the next if block i.e a<0 which is false so it goes to else condition of that and prints the number is zero. Inorder to avoid it consider using else if so only one block is true i.e a>0 and then the program exits the conditional statement

if(a>0)
    printf("The no is positive\n");
else if(a<0)
    printf("The no is negative\n");
else
    printf("The no is zero\n");

Elementary Logic: For Software Development, S(x, y) is true iff x is a real number and y is the absolute value of x. (c) The problem of finding the sum of any nonempty finite sequence of numbers. S(N, y​) is true iff N is a positive integer and y is the Nth even prime number. A partially correct program never gives the wrong answer, but in some cases it may give no​  Example 1: C Output #include <stdio.h> int main() { // Displays the string inside quotations printf("C Programming"); return 0; } Output. C Programming. How does this program work? All valid C programs must contain the main() function. The code execution begins from the start of the main() function.

You have missed the condition, to get it satisfied you have to use if..else..if

int main()
{   
  int a;
  printf("Enter the value of a : ");
  scanf("%d",&a);
  if(a>0)
    printf("The no is positive\n");
  else 
  if(a<0)
    printf("The no is negative\n");
  else
   printf("The no is zero\n");
}

In your case second if condition was failing so it was directly printing whatever there is in else condition Please try above code you will get correct answer.

COMPUTER APPLICATIONS Class – IX, Vs (s – a)(s – b)(s – c) Write a program to accept five numbers from the user and print the largest of them. Make sure that the lucky number should be positive. if the program is giving wrong output in some cases, we will discuss that later. C programming provides a set of built-in functions to read the given input and feed it to the program as per requirement. When we say Output, it means to display some data on screen, printer, or in any file. C programming provides a set of built-in functions to output the data on the computer screen as well as to save it in text or binary files.

C Primer Plus, One of the nice features of strcmp () is that it compares strings, not arrays. The user is told that he or she is wrong. LISTING 11.18 The compback.c Program /* compback.c -- stromp returns “s the second alphabetically and that it returns a positive number if the order is the Therefore, comparing "C" to "A" gives a 1. C language has standard libraries that allow input and output in a program. The stdio.h or standard input output library in C that has methods for input and output. scanf() The scanf() method, in C, reads the value from the console as per the type specified. Syntax: scanf(“%X”, &variableOfXType);

Classroom Activities for 'A' Level Computing, If the value is not positive, the message "You must enter a positive value" is (3) brace wrong way round 5. before value * value Exercise 51: C Spot the Difference 1 Task 1 The program asks for two (floating point) numbers, checks for division by zero and outputs the result of dividing the first number by the second. Format specifiers in C The format specifier is used during input and output. It is a way to tell the compiler what type of data is in a variable during taking input using scanf() or printing using printf().

Why is the output of this code negative as -0.000000?, sign bit, is zero. This means we have a positive number. What is the output in the following C program? Why is the C program giving the wrong output? Output 1. Enter a number: 6 Enter a number: 12 Enter a number: 7 Enter a number: 0 Enter a number: -2 The sum is 25. Here, the dowhile loop continues until the user enters a negative number. When the number is negative, the loop terminates; the negative number is not added to the sum variable. Output 2. Enter a number: -6 The sum is 0.

Comments
  • when asking about a run time problem as this question is doing, always post a minimal reproducible example so we can reproduce the problem and help you debug it.
  • OT: regarding: scanf("%d",&a); always check the returned value to assure the operation was successful. (The scanf() family of functions returns the number of successful input format conversions) In this case anything other than 1 indicates an error occurred
  • In general, mutually exclusive 'if()' statements should have all those after the first prefixed with else if
  • Going through that code, what makes you think that it should behave different?
  • Please add some explanation to your code such that others can learn from it