C Programming
In this exercise section of the tutorial we will write some C program.
(a + b)2
where the value of a and b will be provided by the userThe formula of (a + b)2
is given below.
(a + b)2 = a2 + b2 + 2ab
Now we will convert this into a valid C expression.
Let x = (a + b)2
= a2 + b2 + 2ab
= (a * a) + (b * b) + (2 * a * b)
Now we will write the code.
/**
* file: algebra.c
* author: yusuf shakeel
* date: 2010-12-15
* description: algebra program
*/
#include <stdio.h>
int main(void)
{
//variables
float a, b, x;
//user input
printf("Enter value of a: ");
scanf("%f", &a);
printf("Enter value of b: ");
scanf("%f", &b);
//compute
x = (a * a) + (b * b) + (2 * a * b);
//output
printf("Result = %f\n", x);
printf("End of code\n");
return 0;
}
Output
Enter value of a: 4
Enter value of b: 2
Result = 36.000000
End of code
(a + b)3
where the value of a and b will be provided by the userTry to solve this problem yourself.
Help:
Let x = (a + b)3
= a3 + b3 + 3a2b + 3ab2
To find Simple Interest we will need the Principal, Rate of Interest and Time.
Click here to learn more about Simple Interest.
To keep things simple we will assume Time is in years and an integer value whereas, Principal and Rate of Interest are float type.
Formula for Simple Interest is given below.
SI = PRT/100
= (P * R * T) / 100
where,
P = Principal
R = Rate of Interest
T = Time
Now, lets write the C code.
/**
* file: simple-interest.c
* author: yusuf shakeel
* date: 2010-12-15
* description: simple interest program
*/
#include <stdio.h>
int main(void)
{
//variables
float si, p, r;
int t;
//user input
printf("Enter P: ");
scanf("%f", &p);
printf("Enter R: ");
scanf("%f", &r);
printf("Enter T: ");
scanf("%d", &t);
//compute
si = (p * r * t) / 100;
//output
printf("SI = %f\n", si);
printf("End of code\n");
return 0;
}
Output
Enter P: 1000
Enter R: 10
Enter T: 2
SI = 200.000000
End of code
ADVERTISEMENT