I have a problem, that I need to scan two integers in one line divided by a space, so the code looks something like this:
scanf("%d %d",&integer1,&integer2);
And in this code I need to check whether there were scanned two integers. Can someone help me?
Thank you in advance
By default scanf() reads the space there is no meaning in giving space. If you want to read two integers scanf("%d %d",&integer1,&integer2); and scanf("%d%d",&integer1,&integer2); both will help you.
It will accept the following inputs:
1 2
12 22
3 2 5 //EOF
your program will pass only after reading two integers. You don't need to check anything.
To eliminate EOF
By default scanf returns number of values read so make use of it.
if(scanf("%d%d",&integer1,&integer2) != 2)
{
//if more than two values are entered
//perform some error handling
}
Related
I have to take two inputs from the user with %
input example: 20% 30%
I tried this
scanf("%d%d ", &x,&y);
how can I input two values with %? I can only take two integer values.
%% matches literal % character, so this should do it:
int r = scanf("%d%%%d%%", &x,&y);
if(r != 2) {
puts("scanf error");
exit(1);
}
General note: it's more robust to read entire lines with fgets or POSIX getline, then parse them, for example with sscanf.
Also, please read scanf documentation on what %d and %% do, and what the return value actually means.
Firstly, are you really obligated to also take the %? Couldn't you instead ask the user to input integers?
If you still want to take inputs with %, I think you could treat your inputs as char*, iterate through them until you encounter the '%' char, and then delete it.
You could then use the atoi function to transform your char* into integers.
I want to solve the problem. The problem is to check whether the number is palindrome or not.
There has been a lot of solutions that exist online. But I am trying to solve this problem through my approach without seeing any solution from the internet. I am trying this way->
#include <stdio.h>
int main(){
//Declaring variables for further proceed
int number,reminder,quotient=1;
//Just take the input from the user
printf("Input : ");
scanf("%d",&number);
while(quotient!=0){
quotient=number/10;
reminder=number%10;
printf("%d",reminder);
number=quotient;
}
return 0;
}
The problem is : My code is work for displaying the reverse order of any given number. But I could not check with this reverse order with the given number. If you can then you are most welcome. Thank you in advance.
//Write a program to check the number whether it is palindrome or not
#include <stdio.h>
int
main(void){
//Put variables for the further proceed
int number, quotient=1, remainder,i=0;
//To declare a character array
char text[100];
//To show the message to the user
printf("Enter an integer number :");
//Taking input from the user
scanf("%d",&number);
//For finding escape the integer number in the reverse order specifically
int number_update=number;
//To find out the integer number in the reverse order
while(quotient!=0){
quotient=number_update/10;
remainder=number_update%10;
number_update=quotient;
text[i] = remainder + '0';//Converts integer to character and store to the array
i++;
}
//Converts the string to a whole integer
int result_of_reverse=atoi(text);
//Check the result of reverse order with the given integer number
if(result_of_reverse==number){
//To display the result
printf("This is a palindrome number");
}
else{
//To display the result
printf("This is not a palindrome number");
}
}
Eventually, I have solve my problem. Thank you all for your suggestions.
I applaud your decision to follow your approach through.
Allow me to start a step-by-step answer according to the compromise described here (applicable to homework, challenges and very disciplined self-learners like you):
How do I ask and answer homework questions?
Step 1:
You program is able to look at digit by digit of the number in the right order (you can output them). But it does not get an overview of them. You do not store them. Neither separatly nor as a whole (string or number). Consider how to change that.
Do you know a way to store several seperate digits?
Do you know a way to store a string of characters?
Aleternatively, if you do not want to store the reordered digits, i.e. if you want to continue looking at single digits, then you need to always look at two single digits, one pair after the other. Each pair needs to consist of one digit from the high end and one digit from the low end. Maybe you can think of a way to start the number from both ends while looping. More variables to store intermediate results might help with this.
Step 2:
You "know a little bit of the way how to store a string of characters", so do that. Store the characters which you output. If you do not know how to get from digit to character read up on sprintf(). This is a little complex, because the goal is to have a single string, not several strings with one digit each. So ...
Alternatively, to store single digits as their own integers, read up on "arrays". You specifically need an array of int.
If both seems to complicated do not forget the alternative from first step, to look at pairs of digits, from both ends of the number. For that try to print for example for the input "654321" the output "6:1, 5:2, 4:3". If you can do that, things get much easier.
Task:
t denotes the number of inputs, followed by t lines, each containing a single integer n.
For each integer n given at input, display a line with the value of n.
Sample input:
4
1
2
5
3
Sample output:
1
2
5
6
The output should appear after all lines of integer n are taken as input, i.e, it should not display output after each line of input.
How can this be done using a while loop without the use of array to store the input numbers?
while(i<t)
{
scanf("%d",&num);
printf("%d",&num);
i++;
}
This code is working fine if the input numbers n are separated by a space and appears on the same line. But when the input numbers are provided after a newline, it displays the corresponding output after each input value.
Generally such type of input output are used in coding competition where a user is expected to match the expected output to the actual output.
Try pasting the input using a command prompt or online compilers and check it. It is totally fine as the output is as expected.
How does it take if the total input is given at once.
First it reads the t and then it reads the num and prints the number but your printing is actually after the input. This is how the output is checked in a coding competition.
PS: If you want everything after input, use arrays.
If you want to this without using array, then the best way i could think of is recursion. But note, that internally, your values will be stored in the stack frames, and your values will be printed in reverse(because stack is LIFO). Here is how you can do,
void foo(i, t)
{
if(t==i)
return;
int num;
scanf("%d",&num);
foo(++i, t);
printf("%d\n",num);
}
Note, however, that the values will be printed in reverse, as stack is LIFO
Currently i am enrolled in NPTEL course. There i need to make c program as assignment.
Qusetion is in this format :-
Write a program that reads numbers which are in the range 0 to 100, till it encounters -1. Print the sum of all the integers that you have read before you encountered -1
INPUT:
A sequence of integers separated by whitespace. There may be other integers following -1.
How do i read input from test case file? plz help
I have used following code :-
while((n=scanf("%d",&n1))!=EOF)
{
printf("%d",n);
}
Loop is iterating properly ie if test case 1 has 5 input its running for 5 times. If test case 2 has 2 input hen iterating 2 time. But it is unable to read input . Please Help.
You're printing n which is the number of items read. You need to print n1.
EDIT:
Your check for the while loop is incorrect. You need to check the value that is read i.e. n1 but once again, you're using the value of n to check for EOF. You should be checking for -1 as well since that's what you want right?
I want to read some numbers from the console. The numbers will appear in this way -
5 1 2 3 4 5
4 5 6 7 8
6 2 3 4 5 6 7
..............
EOF
The starting number represents how many number will appear in that line, i.e., the first number of the first line is 5, so there will be 5 more numbers on this line. The end of input will be denoted by the EOF (end-of-file).
I thought of reading the whole line as a string and then convert them to numbers but I want to know is there any other way to do this.
The 'standard' answer is scanf(). The trouble with the standard answer is that it won't allow you to check that there are the correct number of numbers on a line. So, your idea of reading a line and then converting it piecemeal is much better for error detection.
Take a look at How to use sscanf() in loops; the answer there shows you the basics of what you should do. It isn't an exact duplicate. You will read a first value identifying how many entries are on the line, followed by code to read that many entries into an array, with appropriate diagnostics if the data doesn't match the format claimed for it.
If you are in charge of the data format, you should consider dropping the count field - let the computer count how many values are on the line. Computers are good at counting, and it removes a source of errors to be detected and handled (so it makes the programming easier). (If you do change the input format, your question becomes a duplicate of the linked question.)
Have a look at the documentation of scanf:
int count, current, i;
while(scanf("%d", &count) > 0)
{
for(i = 0; i < count; i++)
{
scanf("%d", ¤t);
// store current
}
}