C programming: Find the subtraction of two greatest numbers out of three - c

I'm starting to learn the c programming language and I have to write a program that subtracts two greatest numbers out of the three entered numbers. Can anyone help me with this?
Edit: Sorry, I still don't know how this site functions...
I know how to find the greatest number, but I'm not sure how to find the other one.
#include<stdio.h>
int main()
{
int a, b, c, d, max;
printf("Enter three numbers: ");
scanf("%d%d%d%d", &a, &b, &c);
max=a;
if (max<b) {max=b;}
if (max<c) {max=c;}
printf("the greatest number is %d\n", max);
return 0;
}

Don't expect to get answers to questions like yours. Try this. The assumption is that you are dealing with positive integers only. If you need to consider negative integers as well, you can do it yourself. You should note that this is the not the best solution, there can be much more elegant ones.
#include <stdio.h>
int main() {
int numbers[3];
printf("Enter number 1: \n");
scanf("%d", &numbers[0]);
printf("Enter number 2; \n");
scanf("%d", &numbers[1]);
printf("Enter number 3: \n");
scanf("%d", &numbers[2]);
printf("%d %d %d\n", numbers[0], numbers[1], numbers[2]);
int maximum_0 = 0;
int maximum_1 = 0;
int i;
for (i = 0; i < 3; i++) {
if (numbers[i] > maximum_0) {
maximum_0 = numbers[i];
}
}
for (i = 0; i < 3; i++) {
if (numbers[i] > maximum_1 && numbers[i] < maximum_0) {
maximum_1 = numbers[i];
}
}
printf("Result: %d\n", (maximum_0 - maximum_1));
return 0;
}

//Here it is since you are beginner Without loops
#include<stdio.h>
int main()
{
int a, b, c, d, max,min,result;
printf("Enter three numbers: ");
scanf("%d%d%d", &a, &b, &c);
if(a>b)
{
if(a>c)
{
max=a;
if(c>b)
min=c;
else
min=b;
}
else
{
max=c;
a=min;
}
}
else
{
if(b>c)
{
max=b;
if(a>c)
min=a;
else
min=c;
}
else
{
max=c;
min=b;
}
}
result=max-min;
printf("the greatest number is %d\n", result);
return 0;
}

Use an array and then use qsort to sort your values. The code below will do this for you. The order of the array will be such that the first element will have your smallest number and the last element your largest.
#include<stdio.h>
void sort(const void* d1, const void* d2)
{
int a = *(int*)d1;
int b = *(int*)d2;
if (a > b)return 1;
if (b > a)return -1;
return 0;
}
int main()
{
int abc[3];
printf("Enter three numbers: ");
scanf("%d%d%d", &abc[0], &abc[1], &abc[2]);
qsort(abc, 3, sizeof(int), sort);
printf("%d\n", abc[2] - abc[1]);
while (1){}
return 0;
}

Related

How to put commas on a series of numbers in a proper way

Yes I have searched through the net and also here but failed to find similar cases. I have here a program which prints prime numbers between 2 given integers. But I have to print them with commas in a proper way. like 1, 2, 3, 4 without having a comma on the last integer. Here is my code.
#include<stdio.h>
void main()
{
int x, y, i, j;
puts("Enter first number: ");
scanf("%d", &x);
puts("Enter second number: ");
scanf("%d", &y);
for(i=x; i<=y; i++)
{
for(j=2; j<=i; j++)
{if(i%j==0)
{
break;
}
}
if(i==j)
{
printf("%d, ", i);
}
}
}
I wanted to identify the total number of prime numbers printed in order to set a condition that if it is the last one, a comma will not print but I don't know if it will work. That is the only thing I can think of for now and your help guys will be really appreciated. Thank you!
As #kaylum pointed out, use a flag to "skip" printing the comma when printing the first number. Set that flag to false after the very first number you print.
#include <stdio.h>
void main() {
int x, y, i, j;
puts("Enter first number: ");
scanf("%d", &x);
puts("Enter second number: ");
scanf("%d", &y);
int is_first_time = 1;
for (i = x; i <= y; i++) {
for (j = 2; j <= i; j++) {
if (i % j == 0) {
break;
}
}
if (i == j) {
if (is_first_time) {
printf("%d", i);
is_first_time = 0;
}
else {
printf(", %d", i);
}
}
}
}
You can use a flag, as others have suggested, but you can also use a string as a separator, setting it to empty for the first element and any desired separator after that.
#include <stdio.h>
void print_join(const char *sep, int n, int a[n])
{
const char *s = "";
for (int i = 0; i < n; i++)
{
printf("%s%d", s, i);
s = sep;
}
printf("\n");
}
int main(void)
{
int a[] = {1, 2, 3, 4, 5};
int n = sizeof a / sizeof *a;
print_join(", ", n, a);
print_join(";", n, a);
print_join("", n, a);
print_join("--", n, a);
return 0;
}

Calculating Arithmetic and Geometric mean by introducing numbers until 0 is pressed

I have to calculate the arithmetic and geometrical mean of numbers entered by the user in C language. The algorithm works fine, but I don't know how to do the enter numbers until 0 is pressed part. I have tried many things but nothing works. Here is what I have tried to do until now. Thanks for the help.
int main() {
int n, i, m, j, arr[50], sum = 0, prod = 1;
printf("Enter numbers until you press number 0:");
scanf("%d",&n);
while (n != 0) {
for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
sum = sum + arr[i];
prod = prod * arr[i];
}
}
int armean = sum / n;
float geomean = pow(prod, (float)1 / n);
printf("Arithmetic Mean = %d\n", armean);
printf("Geometric Mean = %f\n", geomean);
getch();
}
Your code is asking for the number of values in advance and subsequently reading that many values. That's not what you were asked to do.
You need to ask for numbers in a loop and exit the loop when the number that you read is 0. You don't even need an array:
int n = 0, i, m, j, sum=0, prod=1;
while (1) {
int value;
scanf("%d",&value);
if (value == 0) {
break;
}
sum=sum+value;
prod=prod*value;
n++;
}
int armean=sum/n;
float geomean=pow(prod,(float) 1/n);
You have to break the for loop when value 0 entered; so you should check for arr[i].
While loop is not required.
Please go through below code; this could be help full:
#include <stdio.h>
int main()
{
int n, i, m, j, arr[50], sum=0, prod=1;
printf("Enter numbers until you press number 0:");
for(i=0; i<50; i++)
{
scanf("%d",&arr[i]);
if (arr[i] == 0)
{
break;
}
sum=sum+arr[i];
prod=prod*arr[i];
}
printf ("%d %d\n",sum, prod);
n = i+1;
int armean=sum/n;
float geomean=pow(prod,(float) 1/n);
printf("Arithmetic Mean = %d\n",armean);
printf("Geometric Mean = %f\n",geomean);
getch();
return 0;
}
what dbush said is right, you don't need array and are not asking the number in advance but what he did not tell is how can you find the number of values
int main()
{
int n, sum=0, prod=1, num;
printf("Enter numbers until you press number 0:\n");
for(n=0; ; n++)
{
scanf("%d",&num);
if(num==0)
break;
sum=sum+num;
prod=prod*num;
}
printf("sum is %d \n",sum);
printf("prod is %d \n",prod);
printf("n is %d \n",n);
float armean=sum/n; //why int?
float geomean=pow(prod,(float) 1/n);
printf("Arithmetic Mean = %d\n",armean);
printf("Geometric Mean = %f\n",geomean);
//getch(); why getch(), you are not using turboc are you?
}
There is no need for an array, but you should test if the number entered in 0 after reading it from the user. It would be better also to use floating point arithmetic to avoid arithmetic overflow, which would occur quickly on the product of values.
In any case, you must include <math.h> for pow to be correctly defined, you should test the return value of scanf() and avoid dividing by 0 if no numbers were entered before 0.
#include <stdio.h>
#include <math.h>
int main() {
int n = 0;
double value, sum = 0, product = 1;
printf("Enter numbers, end with 0: ");
while (scanf("%lf", &value) == 1 && value != 0) {
sum += value;
product *= value;
n++;
}
if (n > 0) {
printf("Arithmetic mean = %g\n", sum / n);
printf("Geometric mean = %g\n", pow(product, 1.0 / n));
getch();
}
return 0;
}

Find Quantity of Biggest integers in N integers in C

I want to do this code that tells you the number of (n) integers that are bigger (or equal) than a (k) input.
So for example:
input:
4 15
12
6
15
24
output:
2
So the 4 is the number of integers the user is going to input and the 15 is the k number, now the output is the quantity of numbers that are bigger than k.
What I have of code is this:
#include<stdio.h>
int main()
{
int n, k, i;
int c, d;
scanf(" %d",&n);
scanf("%d", &k);
for(i=1;i<=n;i++)
{
scanf("%d",&c);
if (c[i]>k)
c[i]=d;
}
printf("%d", d);
return 0;
}
As you can see my code sucks, I don't know how to find the integers that are bigger than k and to print the quantity, not the numbers themselves. Any help is really appreaciated. Thanks.
Far less elegant solution, but one that keeps the value you need for some further use.. OldProgrammer did it much simpler and more pretty.
int main()
{
int num, s, i, cnt = 0;
printf("please input number of integers and int to compare with\n");
scanf("%d %d", &s, &num);
int arr[s];
for(i = 0; i < s; i++)
{
printf("Please input %d. number", i+1);
scanf("%d", &arr[i]);
}
for(i = 0; i < s; i++)
{
if(arr[i] >= num)
cnt++;
}
//at this point cnt holds the value you need
return 0;
}
Not sure why you are trying to reference c as an array. That is not needed. Try this:
int main()
{
int n, k, i, c;
int count = 0;
scanf(" %d",&n);
scanf("%d", &k);
for(i=1;i<=n;i++)
{
scanf("%d",&c);
if (c > k)
count++;
}
printf("%d", count);
return 0
}
Also, I would rename your variables to something more meaningful, such as numEntries, checkValue, etc.

Find Prime gcd between two numbers

For a student course in c,
I need to find the prime greatest common divisor (gcd) of two integers.
If there is no answer the output should be 1.
You can only use if statement, scanf, loops (no external functions).
Examples of inputs and outputs:
(20,20)--->5
(21,20)--->1
(22,20)--->2
(29,29)--->29
Can someone please help me with this?
Here is what I have so far:
#include <stdio.h>
int main()
{
int num1, num2, i, hcf;
printf("Enter two integers: ");
scanf("%d %d", &num1, &num2);
for(i=1; i<=num1 || i<=num2; ++i)
{
if(num1%i==0 && num2%i==0) /* Checking whether i is a factor of both number */
hcf=i;
}
printf("gcd of %d and %d is %d", num1, num2, hcf);
return 0;
}
There are a lot of examples on how to find the gcd but none that I have found for the prime gcd.
sample of fix
#include <stdio.h>
int main(void){
int num1, num2, i, hcf = 1;
int tmp1, tmp2;
printf("Enter two integers: ");
scanf("%d %d", &num1, &num2);
tmp1 = num1;
tmp2 = num2;
for(i=2; i<= tmp1 && i<= tmp2; ++i){
while(tmp1 % i== 0 && tmp2 % i == 0){
hcf=i;
tmp1 /= hcf;
tmp2 /= hcf;
}
}
printf("gcd of %d and %d is %d", num1, num2, hcf);
return 0;
}
You could just do it the straightforward way:
Step one: Generate a list of all prime numbers less than the maximum int value.
Step two: Use that list, and trial division, to find the prime factors of each of your given integers. Keep the list of prime factors for each.
Step three: go through one list of factors, and check each to see if it's in the other list. If only one is, that's your largest common prime divisor. If more than one is in both lists, the GCD is not prime.
import java.util.Scanner;
public class Main
{
static boolean CheckPrime(int n)
{
int flag=0;
if(n==0 || n==1)
{
flag=0;
}
for(int i=2;i<=n/2;i++)
{
if(n%i==0)
{
flag=1;
break;
}
}
if(flag==0)
{
return true;
}
else
{
return false;
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n1=sc.nextInt();
int n2=sc.nextInt();
int[] arr1 = new int[n1];
int[] arr2 = new int[n2];
for(int i=2;i<=n1;i++)
{
if(n1%i==0)
{
if(CheckPrime(i))
{
arr1[i-2]=i;
}
}
}
for(int j=2;j<=n2;j++)
{
if(n2%j==0)
{
if(CheckPrime(j))
{
arr2[j-2]=j;
}
}
}
int max=-1;
for(int i=1;i<n1;i++)
{
for(int j=1;j<n2;j++)
{
if(arr1[i]==arr2[j])
{
if(max<arr2[j])
{
max=arr2[j];
}
break;
}
}
}
if(max==0){max=1;}
System.out.print(max);
}
}

How to find pythagorean triples underneath a user entered number in c

I am trying to write a program in c where the user enters a positive integer and the program calculates all of the triples underneath that number, list them all, and then states the triple with the largest c value (using for,if, and else). This the code I have now, it will take the number I enter and use it as the triple (ie: I enter 15, it prints that there is one triple (15,15,15) etc) just wondering how I could fix this if you know? thanks
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a, b, c, max, counter;
int N;
int maxA=1, maxB=1, maxC=1;
{
printf("Enter a positive integer:");
scanf("%d", &N);
{
for (c=1; c<N; c++);
{
for (b=1; b<c; b++);
{
for (a=1; a<N; a++);
{
if (a*a+b*b==c*c)
counter++;
}
}
}
}
}
{
printf("There are %d pythagorean triples in this range\n", max);
{
for (c=1; c<N; c++);
{
for (b=1; b<c; b++);
{
for (a=1; a<N; a++);
{
if (a*a + b*b== c*c)
printf("(%d1, %d2, %d3) pythagorean triples\n", a,b,c);
if (c>max);
{
max = maxC;
max = maxB;
max = maxA;
}
}
}
}
}
}
printf("The pythagorean triple with the largest c value is (%d,%d, %d)\n", a,b,c);
}
I have corrected your code:
Your for loops had a terminating ; at the end.
counter was used uninitialized.
"%d1" in the printf prints the number and a 1 after it, which leads to wrong numbers.
You have overwritten your max variable 3 times instead of using maxA, maxB and maxC after finding a new highest triple. You also forgot to add braces there, it should only do this with triples and not every time.
I also removed the unnecessary braces.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a, b, c, counter = 0;
int N;
int maxA=1, maxB=1, maxC=1;
printf("Enter a positive integer:");
scanf("%d", &N);
for (c=1; c<N; c++)
{
for (b=1; b<c; b++)
{
for (a=1; a<N; a++)
{
if (a*a+b*b==c*c)
counter++;
}
}
}
printf("There are %d pythagorean triples in this range\n", counter);
for (c=1; c<N; c++)
{
for (b=1; b<c; b++)
{
for (a=1; a<N; a++)
{
if (a*a + b*b== c*c)
{
printf("(%d, %d, %d) pythagorean triples\n", a,b,c);
if (c>maxC);
{
maxC = c;
maxB = b;
maxA = a;
}
}
}
}
}
printf("The pythagorean triple with the largest c value is (%d,%d, %d)\n", maxA,maxB,maxC);
return 0;
}

Resources