My objective in this exercise is to create a function names MultiTwo() that will multiply two inserted integers by the user and then print the quotient. MultiTwo has to be called inside main().
Here's my try:
#include <stdio.h>
int MultiTwo(int x, int y, int result);
int main() {
int x, y, result;
printf("Insert an integer: \n");
x = scanf("%d", &x);
printf("Insert a second integer: \n");
y = scanf("%d", &y);
result = x * y;
printf("The quotient of the two inserted integers is: %d", result);
return 0;
}
I insert two integers and the result I always get, despite the integers inserted, is 1.
x = scanf("%d", &x);
This first reads the value from input and assigns it to the variable x. Then you clobber that value by setting x to the return value of scanf(), which is the number of variables successfully read, in this case it is 1.
Related
int main()
{ unsigned int x, y;
printf_s("Enter value for x: ");
scanf_s("%u", &x);
printf_s("Enter value for y: ");
scanf_s("%u", &y);
unsigned int count = 1;
while (count < y);
{
x *= x;
count++;
}
printf_s("x raised to the power of y is: %u", x);
}
Hi, I'm not sure where I went wrong.
When I run it, I enter two values as prompted and then nothing else happens.
It just stops after I enter they value for y.
Enter value for x: 3
Enter value for y: 2
Like this. Could someone point me in the right direction?
I understand that this way will not work if y <= 1.
But shouldn't it work for when y > 1?
I've searched for it. There is another question using for loop.
I can see that it could be done with for loop but I think while loop is more appropriate since it gives more freedom.
Please and thank you!
Use functions
you have ; past the while and your code in braces is not executed in the loop
use a larger integer type as the result as a "normal" unsigned int will wraparound quickly.
x *= x is definitely wrong.
unsigned long long mypow(unsigned x, unsigned y)
{
unsigned long long result = 1;
while(y--) result *= x;
return result;
}
int main(void)
{
unsigned x, y;
scanf("%u,%u", &x, &y);
printf("%u ^ %u = %llu\n", x, y, mypow(x, y));
}
https://godbolt.org/z/xzx8Mo5aW
#include <iostream>
int main()
{
unsigned int long x; // assign x
printf_s("Enter value for x: "); // prompt
scanf_s("%u", &x); // read input
unsigned int y; // assign y
printf_s("Enter value for y: "); // prompt
scanf_s("%u", &y); // read input
unsigned int count = 0; // initialize count
unsigned int power = 1; // initialize power
while (count < y) // loop while y is less than count
{
power *= x; // multiply power by x
count++; // increment count
} // loop ends
printf_s("x raised to the power of y is: %u", power); // display result
}
I've deleted ;.
I've added long to unsigned int.
I've set up a new variable, power, in order to contain the value of x raised to a power of y.
I've checked if this works when y = 0 or 1 and it does.
Thank you everyone for your help!
P.S. Textbook answer has pritnf( "%s", "Enter first integer: "); for prompting for the first integer, x. I'm not sure why they add %s as it works perfectly fine without it. Does anyone know why one would add %s?
I am new to c ,while i am writing a basic program in c ,it is showing two answers ..1)when i declare and intialize variables separately 2)when I declare and initialize variable in a same line.can any one tell me my mistake please?
#include <stdio.h>
#include <stdlib.h>
void sum()
{
printf("enter the numbers to be added\n");
int x=scanf("%d",&x);
int y=scanf("%d",&y);
int sum=(x+y);
printf("the sum of two numbers is %d\n",sum);
}
int main()
{
printf("welcome to addition calculator\n");
sum();
return 0;
}
I am getting 2 as answer when i gave 3 and 4 as inputs
scanf("%d", &x) will store the read number into x. It will return the number of successfully read fields (1 in your case). If you assign that return value to x afterwards, you overwrite whatever the user entered with that 1. And 1 + 1 produce 2.
Solution:
int x;
int y;
scanf("%d",&x);
scanf("%d",&y);
As David reminds in comments, you might want to check that all fields were read successfully. For example, in your case, if you enter a non-digit, scanf will not resolve the %d field as successful, and will return 0. You can test this result to make sure the user did what they were supposed to do:
int x;
int y;
while (scanf("%d", &x) != 1) {
printf("Enter a NUMBER, you illiterate buffoon!\n");
}
while (scanf("%d", &y) != 1) {
printf("Enter a NUMBER! You managed with %d, how is this suddenly hard now?!\n", x);
}
scanf() function returns 1 if it scan successfully otherwise it return 0. That's why when you put an integer to x, scanf() return 1 and assign it to x(x=1). Same for y(y=1).
As x=1 and y=1.
sum = 2
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;
}
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.
No matter what value I enter for x, the output for the value of y is always 1. Any ideas why?
#include <stdio.h>
int main() {
int x, y;
y = scanf("%d", &x);
printf("y = %d\n", y);
return 0;
}
From scanf(3) - Linux man page:
These functions return the number of input items successfully matched and assigned, which can be fewer than provided for, or even zero in the event of an early matching failure.
Since scanf return value is the number of items written (in your case, 1, since only 1 int was scanned), not the integer value of the scanned character
int main() {
int x, y, z, n;
n = scanf("%d", &x);
printf("n = %d\n", n); // prints 1
n = scanf("%d%d", &x, &y);
printf("n = %d\n", n); // prints 2
n = scanf("%d%d%d", &x, &y,&z);
printf("n = %d\n", n); // prints 3
return 0;
}
One of my assignments was to create a c program that uses the Simpson's 1/3 rule to find the sum. I am running into issues that I am having trouble fixing. Can some one with more experience point me in the right direction?
In theory my code integrates y=ax^2+bx+c where the user selects values for a,b,c and then the user selects the upper and lower bounds [d,e]. Then the user selects the n value which splits up the area into more rectangles (the value that we will use in my class is 100, so the area is split into 100 rectangles). After which it runs through the Simpson's rule and prints out the sum.
//n is required number of iterations.
#include<stdio.h>
#include<conio.h>
#include<math.h>
double integral (int a,int b,int c,int d,int e,int n)
int main()
{
double a, b, c, d, e, n;
printf("Please select values for y=ax^2+bx+c");
printf("Please select value for a");
scanf("%d", &a);
printf("Please select value for b");
scanf("%d", &b);
printf("Please select value for c");
scanf("%d", &c);
printf("Please select value for the upper limit");
scanf("%d", &d);
printf("Please select value for the lower limit");
scanf("%d", &e);
printf("Please select the number of rectangles for the Simpson's Rule (Input 100)");
scanf("%n", &n);
int i;
double sum=0,length=(double)(d-e)/(n),ad,bd,cd,dd;
ad=(double)a;
bd=(double)b;
cd=(double)c;
dd=(double)d;
for (i=0;i<n;i++)
{
sum+=(ad*(dd*dd+2*dd*length*i+length*length*i*i)+bd*(dd+length*i)+cd)*length;
printf("the value is = %d",sum);
}
return sum;
}
Why do you think this
scanf("%e", &e);
should be that way?
The scanf() function takes a format specifier to match the scanned input with, in your case you want to store the values in a double variable, for which you need the "%lf" specifier, so all your scanf()'s should change to
scanf("%lf", &whateverDoubleVariableYouWantToStoreTheResultIn);
You don't need to cast from a variable of a given type to the same type, like here
dd=(double)d;
And also, you must know, that scanf() returns a value, you should not ignore it because your program will misbehave in case of bad input, you should check scanf() in a library manual or the C standard to understand better how to use it.
In addition to #iharob fine advice:
Change n type
// double a, b, c, d, e, n;
double a, b, c, d, e;
int n;
Adjust input code
// and previous lines
if (1 != scanf("%lf", &e)) // %d --> %lf
Handle_InputError();
printf("Please select the number of rectangles for the Simpson's ...
if (1 != scanf("%d", &n) // %n --> %d
Handle_InputError();
Adjust output
// printf("the value is = %d",sum);
printf("the value is = %e",sum); // or %f
Minor bits
// int main()
int main(void) // or int main(int argc, char *argv[])
// return sum; returning a double here is odd
return 0;