Welcome to our website! We are adding new data every day, so please check back often for updates.
Posts

How to Write a C Program to Check the Values of Four Integers

write a C program that accepts four integers p, q, r and s from the user where q, r and s are positive and p is even.

If you are learning C programming, you might have encountered some problems that require you to check the values of four integers and print a message based on some conditions. For example, you might want to check if the integers satisfy some inequalities or some arithmetic relations. In this program, I will show you how to write a C program that accepts four integers p, q, r and s from the user where q, r and s are positive and p is even. If q is greater than r and s is greater than p and if the sum of r and s is greater than the sum of p and q, the program prints "Correct values", otherwise it prints "Wrong values".

What are the Conditions for the Four Integers?

The conditions for the four integers p, q, r and s are as follows:

  •  q, r and s must be positive. This means that they must be greater than zero.
  •  p must be even. This means that it must be divisible by two without any remainder.
  • -q must be greater than r. This means that q - r must be positive.
  •  s must be greater than p. This means that s - p must be positive.
  • The sum of r and s must be greater than the sum of p and q. This means that (r + s) - (p + q) must be positive.

If all these conditions are met, the program prints "Correct values". If any of these conditions are not met, the program prints "Wrong values".

C Program

To write a C program that can check the values of four integers and print the result, we need to do three things:

  • Declare a function that takes four parameters (the four integers) and prints the result.
  • Define the function using an if-else statement to check the conditions and set the result accordingly.
  • Write a main function that prompts the user to enter four integers and calls the check values function with the input as arguments.

Here is how our program looks like:

Sure, I can try to write a C program for that. Here is one possible solution:

#include <stdio.h>

// A function to check the values of p, q, r and s and print the result

void check_values(int p, int q, int r, int s) {

  // Declare a variable to store the result

  char *result;

  // Check if q is greater than r and s is greater than p and if the sum of r and s is greater than the sum of p and q

  if (q > r && s > p && (r + s) > (p + q)) {

    // If yes, set the result to "Correct values"

    result = "Correct values";

  }

  else {

    // If no, set the result to "Wrong values"

    result = "Wrong values";

  }

  // Print the result to the standard output

  printf("%s\n", result);

}

// A main function to test the check_values function

int main() {

  // Declare some variables to store the input

  int p, q, r, s;

  // Prompt the user to enter four integers

  printf("Enter four integers (p, q, r, s): ");

  scanf("%d %d %d %d", &p, &q, &r, &s);

  // Check if q, r and s are positive and p is even

  if (q > 0 && r > 0 && s > 0 && p % 2 == 0) {

    // If yes, call the check_values function with the input as arguments

    check_values(p, q, r, s);

  }

  else {

    // If no, print an error message to the standard output

    printf("Invalid input. Please enter positive values for q, r and s and an even value for p.\n");

  }

}

Output 

The output of the program depends on the input that the user enters. For example, if the user enters 2 4 6 8, the output will be:

Wrong values

Conclusion

In this program, I have shown you how to write a C program that can check the values of four integers and print a message based on some conditions. I hope you have learned something useful and enjoyed reading this post. If you have any questions or feedback, please leave a comment below. Thank you for reading!



We love your feedback and invite you to comment on our articles, exercises, examples, quizzes and others. Your feedback helps us make our content awesome and serve you better. Please leave a comment and tell us what you think. How did our content help you learn something new? Thank you for being a part of our community!

Post a Comment

For more Tech content follow us on Social media
© Ruturaj Khansole. All rights reserved. Distributed by ASThemesWorld