11.
Code Optimization Technique
1. Factorial of a number
Before Optimization:
#include<stdio.h>
int main()
int i,n;
int fact=1;
printf("Enter a number: ");
scanf("%d",&n);
for(i=n;i>=1;i--)
fact=fact*i;
printf("The factorial value is %d",fact);
return 0;
OUTPUT:
Enter a number: 7
The factorial value is 5040
Process returned 0 (0x0) execution time : 5.155 s
Press any key to continue.
After optimization:
#include<stdio.h>
int main()
int n,fact;
fact=1;
printf("Enter the number: ");
scanf("%d",&n);
do{
fact=fact*n;
n--;
}while(n>0);
printf("The factorial value is %d",fact);
return 0;
OUTPUT:
Enter the number: 7
The factorial value is 5040
Process returned 0 (0x0) execution time : 2.075 s
Press any key to continue.
2. Area of a Circle
Before Optimization:
#include<stdio.h>
#define x 3.147
void main()
float r,A;
printf("Enter the radius for the circle: ");
scanf("%f",&r);
A=x*r*r;
printf("\nArea of a circle:%f",A);
OUTPUT:
Enter the radius for the circle: 5
Area of a circle:78.675003
Process returned 27 (0x1B) execution time : 4.883 s
Press any key to continue.
After Optimization:
#include<stdio.h>
void main()
float r;
printf("Enter the radius for the circle: ");
scanf("%f",&r);
printf("\nArea of a circle:%f",3.147*r*r);
OUTPUT:
Enter the radius for the circle: 5
Area of a circle:78.675000
Process returned 27 (0x1B) execution time : 4.800 s
Press any key to continue.