Enter a float

topic

Write a program, whose function is to: a floating-point input from the keyboard (with three digits after the decimal point), and then outputs the integer and fractional part of the number.
Sample input: 123.456
Sample Output: 123 456

Code 1

#include<iostream>
#include<cstdio>
using namespace std;
int main()
{
	int a,b;
	scanf("%d.%d",&a,&b);
	printf("%d %03d\n",a,b);
	return 0;
 }

Code 2

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int main()
{
	char a[100];
	gets(a);
	int s,i;
	s=strlen(a);
	
	for(i=0;i<s;i++)
	{
		if(a[i]=='.')cout<<" ";
		else cout<<a[i];
	}
	
	return 0;
 } 

result

Here Insert Picture Description

to sum up

The first method for solving the problem is tricky, and the second a wider application conditions.

Released eight original articles · won praise 1 · views 249

Guess you like

Origin blog.csdn.net/xililixilu/article/details/104159576