-1

what's wrong with my code that i can't get the values for x and y, assume that the denominator is not zero. i can't get constant values of x and y, most of the time its equal to zero. a1x + b1y = c1 a2x + b2y = c2

#include <stdio.h>
#include <math.h>
int main()
{
 /* Write your program code here */
 int a1,b1,c1,a2,b2,c2,x,y;
 printf("Enter the value for a1: \n");
 scanf("%d", &a1);
 printf("Enter the value for b1: \n");
 scanf("%d", &b1);
 printf("Enter the value for c1: \n");
 scanf("%d", &c1);
 printf("Enter the value for a2: \n");
 scanf("%d", &a2);
 printf("Enter the value for b2: \n");
 scanf("%d", &b2);
 printf("Enter the value for c2: \n");
 scanf("%d", &c2);
 x=((b2*c1)-(b1*c2))/((a1*b2)-(a2*b1));
 printf("x is %d\n", x);
 y=((a1*c2)-(a2*c1))/((a1*b2)-(a2*b1));
 printf("y is %d\n", y);
 return 0;
}
wty
  • 19
  • Welcome to stackoverflow.com. Please take some time to read [the help pages](http://stackoverflow.com/help), especially the sections named ["What topics can I ask about here?"](http://stackoverflow.com/help/on-topic) and ["What types of questions should I avoid asking?"](http://stackoverflow.com/help/dont-ask). Also please [take the tour](http://stackoverflow.com/tour) and [read about how to ask good questions](http://stackoverflow.com/help/how-to-ask). Lastly please learn how to create a [Minimal, Complete, and Verifiable Example](http://stackoverflow.com/help/mcve). – Some programmer dude Aug 15 '18 at 20:16
  • Also please read [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/), and all of http://idownvotedbecau.se/ to learn some reasons your question might be down-voted. Finally, please [learn how to debug your programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). – Some programmer dude Aug 15 '18 at 20:17
  • 2
    And lastly a possible hint about your problem: You're doing *integer arithmetic*. Integers don't have decimals, all operations will *truncate* the resulting values (i.e. cut of any decimals). You should probably read through your class-notes, tutorials or text books more thoroughly. – Some programmer dude Aug 15 '18 at 20:18
  • 3
    You are using integer arithmetic. If the denominator is `>` numerator the result will be `0`. **Please post the input values and expected output**. – Weather Vane Aug 15 '18 at 20:19
  • 1
    Here is working normal, can u say an example that you get 0. When the division result is 0.54... or other decimal started at 0, the result show will be 0. You can try to use float/double to the x,y. – RogerVieiraa Aug 15 '18 at 20:19
  • All of the variables should be `double`. It then follows that the scanfs and printfs need to use `%lf`. – user3386109 Aug 15 '18 at 20:50
  • possible duplicate of [Why does division result in zero instead of a decimal?](https://stackoverflow.com/q/8906722/995714) – phuclv Aug 16 '18 at 01:17

1 Answers1

3

Maybe you need to change the type for x and y variables from int to float or double if you want accurate/precise results for x and y.

Using int type will not show you same results like float/double type because it won't show you numbers after decimal point instead it's truncated towards zero, yielding largest whole number which is smaller that floating point number for numbers greater than zero, and smallest whole number which is larger than floating number for numbers below zero.

SergeyA
  • 61,605
  • 5
  • 78
  • 137
Joe Vanjik
  • 357
  • 2
  • 7
  • 1
    Please expand the answer to explain what's wrong with using `int`. – R Sahu Aug 15 '18 at 20:22
  • 2
    Operations on `int` are _not_ "rounded to the nearest whole number". They are truncated toward zero - `9/10 == 0` and `-19/10 == -1` for example. – Clifford Aug 15 '18 at 20:38