Library function for finding the larger of two numbers [duplicate] - c

This question already has answers here:
MIN and MAX in C
(16 answers)
Closed 9 years ago.
Is there any library function in c for finding out the largest number between two numbers?

You can do
#define new_max(x,y) (((x) >= (y)) ? (x) : (y))
#define new_min(x,y) (((x) <= (y)) ? (x) : (y))
valter

You can easily write your own function using comparison operators such as >, <, >=, <=, and ==.
Here is an example from this site:
#include <stdio.h>
int main()
{
int a, b;
scanf("%d %d", &a, &b);//get two int values from user
if (a < b)//is a less than b?
printf("%d is less than %d\n", a, b);
if (a == b)//is a equal to b?
printf("%d is equal to %d\n", a, b);
if (a > b)//is a greater than b?
printf("%d is greater than %d\n", a, b);
return 0;
}
You can use this knowledge to do a simple function that follows this pseudo code:
//get a and b int variable
//is a greater than or equal to b?
//return a
//else
//return b
Some other useful resources:
http://c.comsci.us/tutorial/icomp.html
http://www.cyberciti.biz/faq/if-else-statement-in-c-program/

Here is a example of using a IF statement to determine the largest of 3 numbers. Like Ahmed said.
/* C program to find largest number using if statement only */
#include <stdio.h>
int main(){
float a, b, c;
printf("Enter three numbers: ");
scanf("%f %f %f", &a, &b, &c);
if(a>=b && a>=c)
printf("Largest number = %.2f", a);
if(b>=a && b>=c)
printf("Largest number = %.2f", b);
if(c>=a && c>=b)
printf("Largest number = %.2f", c);
return 0;
}

Related

I want to know the cause of the Segmentation Fault error

#include <stdio.h>
#pragma warning(disable:4996)
int math(int a, int b) {
if (a > b) {
printf("%d %d %d", a + b, a - b, a * b);
}
else if (b > a) {
printf("%d %d %d", b + a, b - a, b * a);
}
}
int main(void) {
int n1, n2;
scanf("%d %d", &n1, &n2);
printf(math(n1, n2));
return 0;
}
A program that takes two integers and produces and outputs a function that converts the results of addition, subtraction, and multiplication of two integers. However, a Segmentation Fault error occurs when the compilation is executed. I want to know the cause of the code.
An error appears even if you create and output addition, subtraction, and multiplication functions respectively.
As mentioned in the comments, the printf function is expecting a format string prior to the call to the "math" function. With that in mind here is a snippet of code with some tweaks to avoid the segmentation fault the original code runs into.
#include <stdio.h>
int math(int a, int b) {
int result = 0;
if (a > b) {
printf("%d %d %d\n", a + b, a - b, a * b);
result = (a + b) + (a - b) + (a * b);
}
else if (b > a) {
printf("%d %d %d\n", b + a, b - a, b * a);
result = (a + b) + (b - a) + (a * b);
}
return result; /* Since this function is defined to return an integer */}
int main(void) {
int n1, n2;
printf("Enter in two integers: "); /* Aids the user to know what they are supposed to enter */
scanf("%d %d", &n1, &n2);
printf("The result of calling the math function is %d\n", math(n1, n2)); /* Revised the usage of the printf function */
return 0;
}
A couple of things to point out.
Since the "math" function is defined to return an integer, an integer variable was added to the function to accept a calculated value and then be returned to the statement that called the "math" function.
The printf statement is corrected so that it contains a formatting string to define what and how data is to be printed out.
Following is a sample of a test run from the terminal.
#Dev:~/C_Programs/Console/Formulas/bin/Release$ ./Formulas
Enter in two integers: 33 45
78 12 1485
The result of calling the math function is 1575
Give those tweaks a try to see if it meets the spirit of your project.

calculation program in C [duplicate]

This question already has answers here:
(Why) is using an uninitialized variable undefined behavior?
(7 answers)
Closed 2 years ago.
#include <stdio.h>
int main()
{
int a, b, c;
c = a * b;
printf("a는?: ");
scanf("%d", &a);
printf("b는?: ");
scanf("%d", &b);
printf("%d * %d = %d ", a, b, c);
return 0;
}
i made a calculation program . but the result is wrong.
if i put 5,10 in a,b
c should be 50(510)
but the result is 0
i used ab instead of c . then it was solved. but i want to use c variation.
what is the problem?
should i use double instead of int? please tell me the reason why the problem evoked
You're assigning c = a * b at a point when a and b doesn't have proper values. You should do this calculation after assigning values to a and b, otherwise result will be some garbage value.
Right way to do it:
#include <stdio.h>
int main()
{
int a, b, c;
printf("a는?: ");
scanf("%d", &a);
printf("b는?: ");
scanf("%d", &b);
c = a * b;
printf("%d * %d = %d ", a, b, c);
return 0;
}

Using a recursive function ,find a program that computes the multiplication of two numbers using addition operator

Using a recurrence function ,find a program that computes the multiplication of two numbers using addition operator.
What I found is as follows:
/*C program to multiply two numbers using plus operator.*/
#include <stdio.h>
int main()
{
int a,b;
int mul,loop;
printf("Enter first number: ");
scanf("%d",&a);
printf("Enter second number: ");
scanf("%d",&b);
mul=0;
for(loop=1;loop<=b;loop++){
mul += a;
}
printf("Multiplication of %d and %d is: %d\n",a,b,mul);
return 0;
}
However I'm not sure if the it uses a recurrence function,can someone check that and if it does use a recursive function then show me how to do that?
This simple logic should work for you:
int multiply(int a, int b)
{
if(a < b)
return multiply(b, a); // swap
else if(b != 0)
return (a + multiply(a, b - 1)); // recursion
else
return 0;
}

Error while executing code MSB6006 "CL.exeexecuted with code 2

I am getting the following error while executing code on Visual Studio 2019
MSB6006"CL.exe" exited with code 2
#include<stdio.h>
#include<conio.h>
int main()
{
int a, b, c,x;
x = a / (b - c);
printf("\n Enter values of a,b and c");
scanf_s("%d%d%d", &a, &b, &c);
printf("\n The value of x is %d", x);
return 0;
}
Your order of statements is off.
First assign values to a, b, and c.
Only after use those values in calculations.
#include <stdio.h>
int main(void) {
int a, b, c, x;
// x = a / (b - c); // NOPE! a, b, and c have no valid values
printf("Enter values of a, b and c\n");
scanf("%d%d%d", &a, &b, &c);
x = a / (b - c); // calculation moved here; a, b, and c (hopefully) have valid values now
printf("The value of x is %d\n", x);
return 0;
}
Note: the return value of scanf() should be checked to be sure all of a, b, and c have valid values.
if (scanf("%d%d%d", &a, &b, &c) != 3) /* error */;
Note 2: I changed your code a little bit: removed non-standard <conio.h>, changed the placing of most '\n' to be more line-oriented, replaced the optional scanf_s (this function may not exist in all C11/C18 implementations).

Finding LCM of two nos in C

I have created a code for finding LCM of two nos. I think that the code is correct but I have an undesired output. What ks the problem in this code?
#include<stdio.h>
#include<conio.h>
main()
{
int i, j, a, b, lcm;
printf("Enter two nos : ");
scanf("%d %d", &a, &b);
for(i=1; i<=b; i++)
{
for(j=1; j<=a; j++)
{
if(a*i==b*j)
{
lcm=a*i;
break;
}
}
}
printf("LCM=%d", lcm);
getch();
}
The LCM of two numbers a,b is at least max(a,b) and at most a*b, so your first idea about the boundaries is correct. But if you take a closer look to one of the definitions of LCM (of two positive integers) a and b you'll find that the LCM is the smallest number such that LCM % a = 0 and LCM % b = 0 where "%" means "remainder of integer division, truncating" and that is exactly what you can use here.
Example:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int a, b, lcm;
printf("Enter two nos : ");
scanf("%d %d", &a, &b);
/* TODO: checks and balances! */
/* Set lcm to the larger of the two numbers */
lcm = (a < b) ? b : a;
/* check if both "a" and "b" divide "lcm" without a remainder
* otherwise increment "lcm" */
for (;;) {
if ((lcm % a == 0) && (lcm % b == 0)) {
/* we got the LCM, break out of loop */
break;
}
/* Otherwise increment "lcm" by one */
lcm++;
}
printf("LCM = %d\n", lcm);
exit(EXIT_SUCCESS);
}
There are more elegant and more general methods but I think the example above is quite easy to follow.

Resources