Find the sum of two numbers which are in the separate line? - c

So there is a problem on SPOJ as mentioned below:
Given two natural numbers (both not greater than 200), each number in the separate line, please print the sum of them.
Example Input:
2
3
Output: 5
So I wrote a program to this problem. Code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int input1, input2, sum;
printf("Enter two natural numbers\n");
scanf("%d", &input1);
scanf("\n%d", &input2);
if ((input1>0&&input1<=200) && (input2>0&&input2<=200))
{
sum = input1 + input2;
printf("%d", sum);
}
return 0;
}
But SPOJ rejected this answer as a wrong answer.
Later I checked this on idone.
But I'm unable to detect, what's wrong with this answer, as expected it gives the same output given in above question.
Please correct if I'm wrong.

The first print statement
printf("enter two natural numbers\n");
The Second
scanf("%d",&input1);//Press Enter
The Third
scanf("%d",&input2);//Press Enter
And finally
printf("\n%d",sum);

First of all, remove the printf statement as it is not needed and will mess up the expected I/O as given by SPOJ.
Next, there is no need of the newline character in scanf. You can directly write scanf("%d %d", &input1, &input2);. Another way would be to write the scanf statement twice as:
scanf("%d", &input1);
scanf("%d", &input2);
Lastly, you can also remove the if statement, if input bounds are given by SPOJ.

Related

VS Code is showing weird numbers before the output? How to fix this?

When running my code in VS Code, it is showing a random weird number in the output:
#include <stdio.h>
void main()
{
int a;
printf("Enter a no: ");
scanf("%d", &a);
if (a%2==0) {
printf("%d It is even no");
}
else{
printf("%d It is odd no");
}
}
As Gino's said, the problem is that you have a %d at the beginning of each of your printf() calls. This tells printf() to pull a number from its argument list. Since you didn't provide a number in the argument list, it pulls the next number on the stack, whatever that happens to be. It's probably a local variable.

Finding sum of 2 variables from 1 statement in C

I recently started learning C and must create program that scanf two integer values from standard input separated by a space then printf the sum of these two integers. Must be able to accept negative values. I'm using repl.it to write code 1st then pasting in .c to compile.
Attempt:
#include <stdio.h>
int main(void) {
int j = 0;
printf("Enter 2 integers separated by space and press enter:\n");
while (scanf("%d", &j) == 1) {
printf("Here is the sum:"%d", j);
}
return 0;
}
Except this prints
Here is the sum: 1Here is the sum: 2
The output is wrong so what mistake did I make? Whats the correct method to get expected values?
(eg 1+2=3)
Your code is incorrect. It does not even compile due to "Here is the sum:"%d" ill formatted string. The program will repetitively scan an integer from standard input and print it as it was result of a sum.
Parsing two integers separated by a space can be easily done with scanf() function by using "%d %d" pattern. The function scanf() returns a number of successfully parsed arguments thus value of 2 is expected on correct input. Finally, add both number and print the result.
#include <stdio.h>
int main(void) {
int i, j;
if (scanf("%d %d", &i, &j) == 2) {
printf("Here is the sum: %d\n", i + j);
return 0;
}

How to print the smallest number from a set of inputted integer?

I want to write a program where it prompts people to enter a set of integers separated by a space. The user should be able to enter any amount of integer. It will find the two smallest integer in the set and print it out. Printing the smallest number and then printing the second smallest. My question is how do I get the value of min1 to be the first integer they enter, other than a static one? When I did a test run all it printed was a space, why is that? Here is what I have so far:
Update:
I'm now trying this approach, but it just freeze after I enter an input such as 76 5 74 2.
#include <stdio.h>
int min1, min2;
int input;
int main(){
printf("Please enter some integer: ");
scanf("%d", &min1);
while(scanf("%d", &input) != 0){
min1=input;
}
printf("%d", min1);
return 0;
}
You should add \n in the end of the first printf, so it will not buffered.
Also, be care that you work with digits - not integers.
and for your question - just write min1=getchar();.
EDIT: some code that may do what that you want:
printf("Enter numbers. (other chars to end)\n");
int min,input;
scanf("%d",&min);
while (scanf("%d",&input))
if (input<min)
min=input;
printf("min: %d\n",min);
Maybe you need scanf("%d", &number); to read integers.
For your question, just call scanf to read the first number, or set up a flag to indicate if it's the first input.
Why did you got a space printed? Because %c prints characters not numbers, try %d.
But even after that you won't get the answer you are looking for. getchar() gets a character (go figure...) from the user input, and you are storing that character into a numeric value, for single digit numbers it would magically work since even as characters '9' > '8' > '7' > ... > '0', but you'll get the ascii value of the smallest number printed at the end.
You need two things:
Some way for the user to tell your program they are done entering numbers, so some kind of conditional statement
Some way to compare the numbers they have entered so another conditional statement comparing numbers
In sudocode, maybe something like:
while (user still wants to give numbers):
number = get user input
if number does not equal exit_case:
if number < current minimum number:
current minimum number = number
else:
break out of the while loop
print current minimum number
{
int a,b=1,min;
printf("Enter Number\n");
scanf("%d",&min);
while(b<10)
{
scanf("%d",&a);
if(min>a)
{
min=a;
}
b++;
}
printf("Smallest Num ::%d",min);
}

A simple program in C, need some input

I'm trying to write a simple program where it ask user to enter a bunch of positive integers, and calculate the average of all the number entered. Program will terminate when user enter a non positive number like 0 or -1.
Here is my code. For some reason I get an error right when I try to enter the first input, can someone help?
#include <stdio.h>
int main()
{
int input=0, sum=0,average=0,i=0;
printf("Please enter positive numbers, enter 0 or -1 to end:\n");
scanf("%d",input);
while (input>0)
{
sum+=input;
i++;
scanf("%d",input);
}
average=sum/i;
printf("The average is %d",average);
}
You need to pass the address of the variable to scanf.Try this:
scanf("%d", &input);
^
Also see the C FAQ: Why doesn't the call scanf("%d", i) work?

Why doesn't my output show up until the program exits?

I have a simple program from a C programming book, and it's supposed to ask for two integers and then add them together and show the sum. I'm able to enter the two numbers, but the output doesn't show up until the very end of the program.
#include <stdlib.h>
#include <stdio.h>
/* Addition Program*/
main()
{
int integer1, integer2, sum;
printf("Enter first integer\n");
scanf("%d", &integer1);
printf("Enter second integer\n");
scanf("%d", &integer2);
sum = integer1 + integer2;
printf("Sum is %d\n", sum);
return 0;
}
The output looks like this:
2
6
Enter first integer
Enter second integer
Sum is 8
Any help would be greatly appreciated, thanks!
It is possible that the output is not being flushed automatically. You can add fflush(stdout) after each printf() and see if that helps.
Which environment are you using to build and run this program?
Further to the above, printf will only automatically flush it's buffer if it reaches a newline.
If you are running on windows, a newline is \r\n instead of \n.
Alternatively you can do:
fflush(stdout);
Another alternative is to turn off buffering by calling:
setbuf(stdout, NULL);
EDIT:
Just found this similar(but not the same) question:
Why does printf not flush after the call unless a newline is in the format string?

Resources