In C programming I\'m currently using printf(\"%.3fi\ \",variable1) It gives me
ID: 3796146 • Letter: I
Question
In C programming I'm currently using printf("%.3fi ",variable1) It gives me the right output for the second problem but the first problem I'm having trouble. It should print 3.2i instead it prints out 3.200i I've attached the screenshot
Enter the real component for the first number: Enter the imaginary component for the first number: Enter the real component for the second number: Enter the imaginary component for the second numbe The sum of the two complex number is -0.300 3.200 The difference of the two complex number is -6.500 7.600i Expected Enter the real component for the first number: 3.4 Enter the imaginary component for the first number:5.4 Enter the real component for the second number:3.1 Enter the imaginary component for the second number 2.2 The sum of the two complex number is -0.300 3.21 The difference of the two complex number is 50 4 7.6001 Enter the real component for the first number: Enter the imaginary component for the first number: Enter the real component for the second number Enter the imaginary component for the second number The sum of the two complex number is 14.200 2.4001 The difference of the two complex number is 4.000 8.000i Expected Enter the real component for the first number:9.1 Enter the imaginary component for the first number: Enter the real component for the second number:5.1 Enter the imaginary component for the second numbe 2.8 The sum of the two complex number is 14.200 2.4001 The difference of the two complex number is 4.000 0001Explanation / Answer
printf("%.4gi ",variable1);
above printf statement gives you the desired output. We use .4g. In number before f format specifier shows the number of decimal places and number before g is format specifier shows length of entire number. Threfore we use 4.
sample program and output are given below:
#include <stdio.h>
int main()
{
float variable1=3.20000;
printf("%.4gi",variable1);
return 0;
}
output
3.2i