Q 1) A
i. Write a ‘C’ program to accept three dimensions length (l), breadth(b) and height(h) of a cuboid and print surface area (surface area=2(lb+lh+bh)
#include <stdio.h>int main()
{
float length,breadth,height;
float surfacearea;
printf("Enter value of length, breadth & height of the cuboids:\n");
scanf("%f%f%f", &length,&breadth,&height);
surfacearea = 2 *(breadth * length + length * height + height * breadth);
printf("Surface area of cuboids is: %.3f", surfacearea);
return 0;
}
Program Output:
Enter value of length, breadth & height of the cuboids:
22
23
24
Surface area of cuboids is: 3172.000
ii Write a ‘C’ program to accept an array of n float values and display them in the reverse order.
#include <stdio.h>
int main()
{
int i,n;
float a[50];
printf("\n\nRead n number of values in an array and display it in reverse order:\n");
printf("------------------------------------------------------------------------\n");
printf("Enter the number of elements to store in the array :");
scanf("%d",&n);
printf("Enter the data :\n",n);
for(i=0;i<n;i++)
{
printf("array[%d] : ",i);
scanf("%f",&a[i]);
}
printf("\nThe values store into the array are : \n");
for(i=0;i<n;i++)
{
printf("%0.1f\t",a[i]);
}
printf("\n\nThe values store into the array in reverse are :\n");
for(i=n-1;i>=0;i--)
{
printf("%0.1f\t",a[i]);
}
return 0;
}
Program Output:
Read n number of values in an array and display it in reverse order:------------------------------------------------------------------------
Enter the number of elements to store in the array :6
Enter the data :
array[0] : 3.2
array[1] : 4.3
array[2] : 5.4
array[3] : 6.5
array[4] : 7.6
array[5] : 8.7
The values store into the array are :
3.2 4.3 5.4 6.5 7.6 8.7
The values store into the array in reverse are :
8.7 7.6 6.5 5.4 4.3 3.2
OR
Q 1) A Write a menu driven program to perform the following operations on an integer. Write separate functions.
1. Check if is even or odd
2. Check if it is prime
3. Exit
#include<stdio.h>
#include<conio.h>
int isEven(int num);
int isPrime(int num);
int main()
{
int c=0, num, res;
while(c!=3)
{
printf("\n1. Check if even or odd\n2. Check if Prime or not\n3. Exit\n");
printf("\nEnter your choice:");
scanf("%d", &c);
switch(c)
{
case 1:
printf("Enter an integer: ");
scanf("%d", &num);
res=isEven(num);
if(res==1)
printf("\n%d is Even Number.\n\n",num);
else
printf("\n%d is Odd Number.\n\n",num);
break;
case 2:
printf("Enter an integer other than 1: ");
scanf("%d", &num);
res=isPrime(num);
if(res==0)
printf("\n%d is Prime Number.\n\n",num);
else
printf("\n%d is not a Prime Number.\n\n",num);
break;
case 3:
printf("\nExit");
break;
default:
printf("\nWrong Choice...!");
}
}
}
int isEven(int num)
{
if(num%2==0)
return 1;
else
return 0;
}
int isPrime(int num)
{
int i,flag=0;
for(i=2;i<=num/2;i++)
{
if(num%i==0)
{
flag=1;
break;
}
}
return flag;
}
Program Output:
1. Check if even or odd
2. Check if Prime or not
3. Exit
Enter your choice:1
Enter an integer: 12
12 is Even Number.
1. Check if even or odd
2. Check if Prime or not
3. Exit
Enter your choice:2
Enter an integer other than 1: 7
7 is Prime Number.
1. Check if even or odd
2. Check if Prime or not
3. Exit
Enter your choice:3
Exit
0 Comments:
Post a Comment