EXERCISE-3
1. Write a C program to convert a string to a long integer.
SOURCE CODE:
2. Write a program in C which is a Menu-Driven Program to compute the area of the various geometrical shape.
SOURCE CODE:
3. Write a C program to calculate the factorial of a given number.
SOURCE CODE:
SOURCE CODE:
#include<stdio.h>
#include<stdlib.h>
int main(){
long int l;
char str[30];
printf("Enter a string:\n");
scanf("%s",str);
l=atol(str);
printf("String to long integer is : %ld",l);
return 0;
}
OUTPUT:#include<stdlib.h>
int main(){
long int l;
char str[30];
printf("Enter a string:\n");
scanf("%s",str);
l=atol(str);
printf("String to long integer is : %ld",l);
return 0;
}
Enter a string:
123456789
String to long integer is : 123456789
123456789
String to long integer is : 123456789
2. Write a program in C which is a Menu-Driven Program to compute the area of the various geometrical shape.
SOURCE CODE:
#include<stdio.h>
#include<stdlib.h>
int main(){
float radius, length, width, base, height, area;
int choice;
do{
printf("1. Area of circle\n2. Area of rectangle\n3. Area of triangle\n4. Exit\n");
printf("Enter your choice : ");
scanf("%d",&choice);
switch(choice){
case 1:
printf("Enter radius : ");
scanf("%f",&radius);
area = 3.14 * radius * radius;
printf("Area of circle : %f\n",area);
break;
case 2:
printf("Enter length and width : ");
scanf("%f%f",&length,&width);
area = length * width;
printf("Area of rectangle : %f\n",area);
break;
case 3:
printf("Enter base and height : ");
scanf("%f%f",&base,&height);
area = (base*height)/2;
printf("Area of triangle : %f\n",area);
break;
case 4:
exit(0);
}
} while(1);
return 0;
}
OUTPUT:#include<stdlib.h>
int main(){
float radius, length, width, base, height, area;
int choice;
do{
printf("1. Area of circle\n2. Area of rectangle\n3. Area of triangle\n4. Exit\n");
printf("Enter your choice : ");
scanf("%d",&choice);
switch(choice){
case 1:
printf("Enter radius : ");
scanf("%f",&radius);
area = 3.14 * radius * radius;
printf("Area of circle : %f\n",area);
break;
case 2:
printf("Enter length and width : ");
scanf("%f%f",&length,&width);
area = length * width;
printf("Area of rectangle : %f\n",area);
break;
case 3:
printf("Enter base and height : ");
scanf("%f%f",&base,&height);
area = (base*height)/2;
printf("Area of triangle : %f\n",area);
break;
case 4:
exit(0);
}
} while(1);
return 0;
}
1. Area of circle
2. Area of rectangle
3. Area of triangle
4. Exit
Enter your choice : 1
Enter radius : 4
Area of circle : 50.240002
1. Area of circle
2. Area of rectangle
3. Area of triangle
4. Exit
Enter your choice : 2
Enter length and width : 4 8
Area of rectangle : 32.000000
1. Area of circle
2. Area of rectangle
3. Area of triangle
4. Exit
Enter your choice : 3
Enter base and height : 2 3
Area of triangle : 3.000000
1. Area of circle
2. Area of rectangle
3. Area of triangle
4. Exit
Enter your choice : 4
2. Area of rectangle
3. Area of triangle
4. Exit
Enter your choice : 1
Enter radius : 4
Area of circle : 50.240002
1. Area of circle
2. Area of rectangle
3. Area of triangle
4. Exit
Enter your choice : 2
Enter length and width : 4 8
Area of rectangle : 32.000000
1. Area of circle
2. Area of rectangle
3. Area of triangle
4. Exit
Enter your choice : 3
Enter base and height : 2 3
Area of triangle : 3.000000
1. Area of circle
2. Area of rectangle
3. Area of triangle
4. Exit
Enter your choice : 4
3. Write a C program to calculate the factorial of a given number.
SOURCE CODE:
#include<stdio.h>
int main(){
int num,fact=1,i;
printf("Enter a number: ");
scanf("%d",&num);
for(i=1;i<=num;i++)
fact = fact*i;
printf("Factorial of %d is %d",num,fact);
return 0;
}
OUTPUT:int main(){
int num,fact=1,i;
printf("Enter a number: ");
scanf("%d",&num);
for(i=1;i<=num;i++)
fact = fact*i;
printf("Factorial of %d is %d",num,fact);
return 0;
}
Enter a number: 5
Factorial of 5 is 120
Factorial of 5 is 120
No comments