Reading lines of a float and converting to integers [closed] - c

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Write a C program that reads lines containing floating point values of type double one per line from the standard input ( e.g. using scanf ), converts those values to integers and then prints those values as right justified integers in a 20-character wide field one per line on the standard output.
#include <stdio.h>
My biggest problem is I don't know where to start. Any tips and help would be appreciated.

The concept is to TYPECAST the float to an integer.
The loop here is for multiple values if you want.
This is the program. I hope this helps; it runs as you want.
#include <stdio.h>
int main()
{
float n;
int t;
//loop here
scanf("%f", &n);
t = (int)n;
printf("%20d", t);
// end loop here
return 0;
}

Related

How to break Printf() in multiple Printf() [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
Hi can anyone help me to correct this code, the result should be
/c=4.000000/
/d=4.0000 /
I know by putting the logic in single printf() i will get my result but i am not understanding that how to use two printf() and the varibles will be given by the second printf().
Here is my code:-
#include<stdio.h>
int main()
{
int a=19,b=4;
float c,d;
c=a/b;
d=a%b;
printf("/c=%12f/\nd=%");
printf("-12.4f/",c,d);
putchar(10);
return 0;
}
If I change my code to this, I will get the result,
#include<stdio.h>
int main()
{
int a=19,b=4;
float c,d;
c=a/b;
d=a%b;
/*
printf("/c=%12f/\nd=%");
printf("-12.4f/",c,d);
*/
printf("/c=%12f/\n/d=%-12.4f/",c,d);
putchar(10);
return 0;
}
but I want to use two printf() statements.
Thanks in advance.
You can not do this:
printf("/c=%12f/\nd=%");
printf("-12.4f/",c,d);
because you are lying to both printfs, in the first one you don't use the specifiers and in the second one you use specifiers that are not expected.
You can do this:
printf("/c=%12f/\nd=%"
"-12.4f/",c,d);

How pointer variable works, without initialising it in the code in TurboC++? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I am new to C. I was just interested in making a program which takes two integer input from the user and add it but the condition is that i have to use only one variable. I came up with this code:
#include <stdio.h>
int main()
{
int *a;
scanf("%d%d",a,(a+1));
printf("\nSum=%d",*a+*(a+1));
return 0;
}
scanf() function takes an valid address as an argument and i am passing the value in a(which is not initialised yet). So, how this code worked in Turbo C++?
You are trying to access an area that is not within the scope of the program. Luckily TCC gave it, but I believe if you go on experimenting, results will be undefined.
You can do something like this to solve your problem of adding using 1 variable.
int main()
{
int a;
scanf("%d",&a); //scan the first number
getchar();
a += getchar()-'0'; // get the second number (restricted to 0-9)
printf("%d",a);
return 0;
}

Code output explanation [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
Question 1.
#include <stdio.h>
int main(void)
{
int c;
while((c=getchar())!='\0')
{
putchar(c);
}
}
Input
Hello C.
Tell me about you.
Output
Hello C.
Tell me about you.
ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ
and it continues with status-time limit exceeded.
Question 2.
#include <stdio.h>
int main(void)
{
float a;
a=46.43253;
printf("\n%d",a);
printf("\n%f",a);
return 0;
}
Output
536870912
46.432529
Output- 536870912
46.432529
In general using incorrect format specifier triggers undefined behavior - which is what you have when you use %d in printf for printing float. In this case, you can expect any output usually.
However, it may also be the case that since you have specified to read the float number as integer (e.g. by using %d specifier), it simply interpreted the result as integer - hence the strange number (since floats and integers are stored differently).
If you are interested why the second printf prints a number slightly different from yours, this may help you.
This block is fine:
float a;
a=46.43253;
This block is also fine:
printf("\n%f",a);
The problem is with this block:
printf("\n%d",a);
Particularly this part:
"\n%d"
Please keep in mind you declared a float and using the integer syntax to output it. That's why you are getting the negative output
If it is a case where you don't want to change the "%d," then simply cast it as a float before output

Is there any chance to store many variables without array? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I wanna get some numbers from keyboard. But how to store that number without array[] ? Have i chance to do that ? I don't know exact how many numbers come from keyboard. If i had permission of array, its simple. But array is not allowed.
In your situation, I'd still go with arrays, but if you insist on using pointers, this code below will help you. Regardless of whether you need arrays or pointers, you still need to define an upper limit on how many elements can be stored in memory.
It is now up to you to modify the code to make it efficient and pretty to your assignment needs.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(){
int numelements=10;
int curelement=0;
int* data=calloc(1,numelements*sizeof(int));
int* p=data;
int* res=data;
while (curelement < numelements){
scanf("%d",p);
if (*p==0){break;} //exit if number entered is zero.
p++;
curelement++;
}
//print results
while(*res != 0){
printf("%d ",*res);
res++;
}
free(data);
return 0;
}

Differences between scanf and getchar in C [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I was trying to explain to my friend something about C coding and he asked me why his code (with "scanf") didn't work.
#include
int main() {
char x=scanf("%c",&x);
printf("%c\n",x);
return 0;
}
and this one yes
#include <stdio.h>
int main()
{
int k;
char x=getchar
printf("%c\n",x);
return 0;
}
When scanf completes, x contains the character that was read. However, that value is immediately overwritten when x is assigned the return value of scanf, which is the number of items successfully matched or EOF in the event of an error.
If you call scanf without assigning the return value to x you should get the expected result.
For example, this should work.
char x;
scanf("%c",&x);

Resources