C Programming BCA Practical Slips Solution
Slip 24
Q1. Write a C program to find maximum elements of 1D array [15 Marks]
#include <stdio.h>
int main()
{
int i, n;
float arr[100];
printf("Enter total number of elements(1 to 100): ");
scanf("%d", &n);
printf("\n");
// Stores number entered by the user
for(i = 0; i < n; ++i)
{
printf("Enter Number %d: ", i+1);
scanf("%f", &arr[i]);
}
// Loop to store largest number to arr[0]
for(i = 1; i < n; ++i)
{
// Change < to > if you want to find the smallest element
if(arr[0] < arr[i])
arr[0] = arr[i];
}
printf("Largest element = %.2f", arr[0]);
return 0;
}
Q2. Create a structure Book (Bno, Bname, Price). Accept details of n Books and write a menu
driven program to perform the following operations options. [25 Marks]
1. Display all Books having price > 500
2. Display Book having maximum price
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct book
{
int bno,bcost,baccno;
char bname[20],bpub[20],bauthor[20];
}p[10];
int main()
{
int n,i,ch,largest ;
char pubname[20],authorname[20];
printf("/*How Many Records of Books You Want to Add*/\n\nEnter Limit : ");
scanf("%d",&n);
printf("------------------------------------------\n");
for(i=0;i<n;i++)
{
printf("\tEnter Details of Book-%d",i+1);
printf("\n------------------------------------------\n");
printf("Book Number : ");
scanf("%d",&p[i].bno);
printf("Book Name : ");
scanf("%s",p[i].bname);
printf("Author Name : ");
scanf("%s",p[i].bauthor);
printf("Publication : ");
scanf("%s",p[i].bpub);
printf("Cost : ");
scanf("%d",&p[i].bcost);
printf("Accession Number : ");
scanf("%d",&p[i].baccno);
printf("------------------------------------------\n");
}
while(1)
{
printf("\n\t\tMENU\n");
printf("------------------------------------------\n");
printf("\n1.All Books Costing Above Rs. 500");
printf("\n2. Books having maximum price");
printf("\n3.Exit");
printf("\n------------------------------------------\n");
printf("\nEnter Your Choice : ");
scanf("%d",&ch);
printf("\n");
switch(ch)
{
case 1:
for(i=0;i<n;i++)
{
if(p[i].bcost>500)
{
printf("Book Number : %d\n",p[i].bno);
printf("Book Name : %s \n",p[i].bname);
printf("Cost : %d\n",p[i].bcost);
printf("Accession Number : %d\n",p[i].baccno);
printf("\n------------------------------------------\n");
}
}
break;
case 2:
for(i=0;i<n;i++)
{ largest = p[0].bcost;
if (largest < p[i].bcost){
largest = p[i].bcost;
}
}
printf("Cost : %d\n",largest);
for(i=0;i<n;i++)
{
if(p[i].bcost==largest)
{
printf("Book Number : %d\n",p[i].bno);
printf("Book Name : %s \n",p[i].bname);
printf("Cost : %d\n",p[i].bcost);
printf("Accession Number : %d\n",p[i].baccno);
printf("\n------------------------------------------\n");
}
}
break;
case 3:
exit(0);
}
}
return 0;
}
No comments:
Post a Comment