Practical 2A- Formatting output and selection

Posted: January 20, 2012 in Lab 3

Problem statement-
Write a program that reads in three positive, nonzero numbers and outputs the number, the inverse of the number, the square of the number and the square root of the number. The output must be displayed as below. (Hint: you need to use the methods in the Formatter class to display your output).
Enter the first number
12
Enter the second number
34
Enter the third number
56
Tabulated values
————————
x 1/x sqr x sqrt x
—- —- —— ——-
12.0 0.80 144.00 3.46
34.0 0.30 1156.00 5.83
56.0 0.20 3136.00 7.48

#include<stdio.h>
#include<math.h>
int tabulated(float *);
int main()
{
	float a[3];
	printf("Enter the first number\n");
	scanf("%f",&a[0]);
	printf("Enter the second number\n");
	scanf("%f",&a[1]);
	printf("Enter the third number\n");
	scanf("%f",&a[2]);
	tabulated(&a[0]);
return 0;
}
int tabulated(float *a)
{
	float x,y,z;
	int i;
	printf("Tabulated values\n");
	printf("-----------------------------------------------\n");
	printf("x          1/x           sqr x         sqrt x\n");
	printf("------- -----------   ----------   ------------\n");
	for(i=0;i<3;i++)
	{
		x=1/(*(a+i));
		y=*(a+i)**(a+i);
		z=sqrt(*(a+i));
		printf("%.2f      %.2f          %.2f        %.2f\n",*(a+i),x,y,z);
	}
	return 0;
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s