After execution, result was very strange:
#include <stdio.h>
int main(){
int a,b;
printf("enter two numbers :");
scanf("%d%d",&a,&b);
if(a>b){
printf("maximum number is %d",&a);
}
else{
printf("maximum number is %d",&b);
}
return 0;
}
After enter two numbers in console result was:
maximum number is 2686696
2686696 very strange for me if I enter two numbers such as 5, 3 must shows me 5 but shows 2686696 !!!
Can anyone guide me?
You are trying to print the address of int not its value. Do this:
if(a>b){
printf("maximum number is %d",a);
}
else{
printf("maximum number is %d",b);
}
& operator returns the address of a or b.
Remove & from printf to print the value, now you're printing the address. Should be:
printf("maximum number is %d",a);
The & in printf prints the address of the variables instead of the value.
To print a value use :
if(a>b){
printf("maximum number is %d",a);
}
else{
printf("maximum number is %d",b);
}
Remove & operator from the argument of printf if you are interested in printing the numbers a and b else change the format specifier %d to %p if you are trying to print the address of a and b.
printf("maximum number is %p", (void *)&a);
Related
So I'm having a problem about showing what my original variable value is after changing it in the code.
#include <stdio.h>
int main(){
int n, count =0;
printf("enter an integer = ");
scanf("%d", &n);
while (n!=0){
n/=10;
count++;
}
printf("your number %d has %d digits", n, count);
return 0;
}
Example input:123
Output of this code "your number 0 has 3 digits"
I want to know how to be able to refer the variable "n" in the printf to the original value of '123' so the output will be "your number 123 has 3 digits"
I would recommend that you use a separate variable to save your value or count with a different variable.
This code would look something like this:
#include <stdio.h>
int main()
{
int n, count =0;
printf("enter an integer = ");
scanf("%d", &n);
int buffer = n
while (buffer!=0)
{
buffer/=10;
count++;
}
printf("your number %d has %d digits", n, count);
return 0;
}
This way you save your variable in your code and you only used a buffer and not the actual value n.
You can keep a copy of the original variable and use that copy of the variable while printing.
You can do this:
int main(){
int n, count =0;
printf("enter an integer = ");
scanf("%d", &n);
printf("your number %d has ", n);
while (n!=0){
n/=10;
count++;
}
printf("%d digits", count);
return 0;
}
Of course, you may have to do some error checks..
I'm a newbie!
I'm supposed to get 2 integers from the user, and print the result(sum of all numbers between those two integers).
I also need to make sure that the user typed the right number.
The second number should be bigger than the first one.
And if the condition isn't fulfilled, I have to print "The second number should be bigger than the first one." and get the numbers from the user again until the user types right numbers that meet the condition.
So if I programmed it right, an example of the program would be like this.
Type the first number(integer) : 10
Type the second number(integer) : 1
The second number should be bigger than the first one.
Type the first number(integer) : 1
Type the second number(integer) : 10
Result : 55
End
I think that I have to make two loops, but I can't seem to figure out how.
My English is limited, to help your understanding of this quiz, I'll add my flowchart below.
I tried many different ways I can think of, but nothing seems to work.
This is the code that I ended up with now.
But this doesn't work either.
#include <stdio.h>
void main(void)
{
int a = 0;
int b = 0;
int total_sum = 0;
printf("Type the first number : \n");
scanf("%d", &a);
printf("Type the second number : \n");
scanf("%d", &b);
while (a > b) {
printf("The second number should be bigger than the first one.\n");
printf("Type the first number : \n");
scanf("%d", &a);
printf("Type the second number : \n");
scanf("%d", &b);
}
while (a <= b) {
total_sum += a;
a++;
}
printf("Result : \n", total_sum);
}
Instead of using loop to sum the numbers, we can use mathematical formula.
Sum of first N integers= N*(N+1)/2
#include <stdio.h>
int main(void)
{
int a = 0;
int b = 0;
int sum;
//Run infinite loop untill a>b
while(1)
{
printf("Type the first number : ");
scanf("%d", &a);
printf("Type the second number : ");
scanf("%d", &b);
if(a>b)
{
printf("The second number should be bigger than the first one.\n");
}
else
{
break;
}
}
//Reduce comlexity of looping
sum=((b*(b+1))-(a*(a-1)))/2;
printf("Result : %d " , sum);
return 0;
}
After corrections your code should run. The community has pointed out many mistakes in your code. Here's an amalgamated solution:
#include <stdio.h>
int main(void)
{
int a = 0;
int b = 0;
int correctInput=0;
int total_sum = 0;
do
{
printf("Type the first number : \n");
scanf("%d", &a);
printf("Type the second number : \n");
scanf("%d", &b);
if(a<b)
correctInput=1;
else
printf("The second number should be bigger than the first one.\n");
}
while (correctInput ==0) ;
while (a <= b) {
total_sum += a;
a++;
}
printf("Result : %d \n" , total_sum);
return 0;
}
Factorials are used frequently in probability problems. The factorial of a positive integer n (written n! and pronounced "n factorial") is equal to the product of the positive integers from 1 to n: n! = 1 x 2 x 3 x x n Write a program that takes as input an integer n and computes n!.
I want to find the maximum value of two numbers, and print it. I want to print all three numbers. I am using the following code.
#include<stdio.h>
#include<conio.h>
main()
{
//clrscr();
int a,b,c;
printf("insert two numbers:");
scanf("%d%d", &a, &b);
c = (a>b) ? a : b;
printf("\nmaximum of %d",a," and %d",b," is = %d" c);
getch();
}
However, I receive two syntax errors (Please find the attached figure).
Could anybody help me out with it?
Change the line where you print the output to:
printf("\nmaximum of %d and %d is = %d",a,b,c);
See the docs here
printf("\nmaximum of %d and %d is = %d",a,b,c);
I would like to avoid 'floating point error in this code.
A purpose of this code is to gain 'The average of whole numbers' but the number of 'whole numbers' is limited by the input of users. Please help me.
#include <stdio.h>
int main(void)
{
int num=0;
int limit;
int result=0;
printf("number of integer: ");
scanf("&d", &limit);
while(num<limit)
{
int output;
printf("Input integer : ");
scanf("%d", &output);
result += output;
num++;
}
printf("average of total integer: %d \n", result/limit);
return 0;
}
Thank you for reading.
When you divide 2 integers, the result is also an integer.
To return a float, you need to cast one of the arguments as a float.
So your last line becomes
printf("average of total integer: %f \n", result/(float)limit);
As the result of two integer dividing is also an integer,so it as
printf("average of total integer: %f \n", result/(float)limit);
when you type cast the variable limit to float what happens is that result will be implicitly converted to float and so the result is a float.
#include <stdio.h>
int main(void)
{
int a,b,c;
printf("Enter values of a,b,c:");
scanf("%d %d %d",a,b,c);
printf("\nDescending order of the numbers entered:");
/*Test for Biggest Number*/
if((a>b)&&(a>c))
printf("%d",a);
else if((b>a)&&(b>c))
printf("%d",b);
else if((c>a)&&(c>b))
printf("%d",c);
/*Test for Second Biggest Number*/
if((a>b&&a<c)||(a<b&&a>c))
printf("%d",a);
else if((b>a&&b<c)||(b<a&&b>c))
printf("%d",b);
else if((c>a&&c<b)||(c<a&&c>b))
printf("%d",c);
/*Test for Smallest Number*/
if((a<b)&&(a<c))
printf("%d",a);
else if((b<a)&&(b<c))
printf("%d",b);
else if((c<a)&&(c<b))
printf("%d",c);
return 0;
}
this is a c program in which 3 numbers are entered and the program prints the in descending order. i compiled the program and the ran the program.after entering the three numbers the program would just crash. is there something wrong with my code or do i have to add something?
That's because you are not passing the address of your variables to scanf. Change
scanf("%d %d %d",a,b,c)
to
scanf("%d %d %d",&a,&b,&c)
Try to use:-
scanf("%d %d %d",&a,&b,&c)
instead of
scanf("%d %d %d",a,b,c)
as & refers to the address of your variables.
In C the parameters are passed by value so you need to pass the address (or pointer). When you pass the address (or pointer) then scanf knows where it has to put the value.