C Programming BCA Practical Slips Solution
Q1. Write a C program to check whether a input number is perfect number of not.
[15 Marks]
#include<stdio.h>
int main(){
int n,i=1,sum=0;
printf("Enter a number: ");
scanf("%d",&n);
while(i<n){
if(n%i==0)
sum=sum+i;
i++;
}
if(sum==n)
printf("%d is a perfect number",i);
else
printf("%d is not a perfect number",i);
return 0;
}
Q2. Write a program having a menu with the following options and corresponding actions
[25 Marks]
Options Actions
1. Area of square Accept length ,Compute area of square and print
2. Area of Rectangle Accept length and breadth, Compute area of rectangle and
print
3. Area of triangle Accept base and height , Compute area of triangle and Print
#include <stdio.h>
void main()
{
int fig_code;
float side, base, length, breadth, height, area, radius;
printf("-------------------------\n");
printf(" 1 --> Rectangle\n");
printf(" 2 --> Triangle\n");
printf(" 3 --> Square\n");
printf("-------------------------\n");
printf("Enter the Figure code\n");
scanf("%d", &fig_code);
switch(fig_code)
{
case 1:
printf("Enter the breadth and length\n");
scanf("%f %f", &breadth, &length);
area = breadth * length;
printf("Area of a Reactangle = %f\n", area);
break;
case 2:
printf("Enter the base and height\n");
scanf("%f %f", &base, &height);
area = 0.5 * base * height;
printf("Area of a Triangle = %f\n", area);
break;
case 3:
printf("Enter the side\n");
scanf("%f", &side);
area = side * side;
printf("Area of a Square=%f\n", area);
break;
default:
printf("Error in figure code\n");
break;
}
}
No comments:
Post a Comment