Thursday, 28 October 2010

More about Input/Output of floats and Integer

More about Input/Output of floats and Integer
Variations in Output for integer an floats
Integer and floating-points can be displayed in different formats in C programming as:
#include<stdio.h>
int main(){
    printf("Case 1:%6d\n",9876);    
/*  Prints the number right justified within 6 columns  */
    printf("Case 2:%3d\n",9876);    
/* Prints the number to be right justified to 3 columns but, there are 4 digits so number is not right justified  */
    printf("Case 3:%.2f\n",987.6543);
/* Prints the number rounded to two decimal places */
    printf("Case 4:%.f\n",987.6543);
/* Prints the number rounded to 0 decimal place, i.e, rounded to integer */
    printf("Case 5:%e\n",987.6543);
/* Prints the number in exponential notation(scientific notation) */
    return 0;
}
Output
Case 1:  9876
Case 2:9876
Case 3:987.65
Case 4:988
Case 5:9.876543e+002
Variations in Input for integer and floats
#include <stdio.h>
int main(){
    int a,b;
    float c,d;
    printf("Enter two intgers: ");
/*Two integers can be taken from user at once as below*/
    scanf("%d%d",&a,&b);
    printf("Enter intger and floating point numbers: ");
/*Integer and floating point number can be taken at once from user as below*/
    scanf("%d%f",&a,&c);
    return 0;
}
Similarly, any number of input can be taken at once from user.

No comments:

Post a Comment