Arrays 211
7.16 Write a program to calculate & display total cost of 4 models of Pentium PCs. Use the
single dimension arrays for PC codes their price and quantity available.
# include <stdio.h>
# include <conio.h>
void main ()
{
int i,pccode[4]={l,2,3,4}?
long t=0,price[4]={25000,30000,35000,40000};
int stock[4]={25,20,15,20}?
clrscrO ?
printf (" Stock & Total Cost Details n");
pnnf/7"===========— ==========================m");
printf ("Model Qty.tRate (Rs.) Total Value");
p r i» ^ (" « = = = = = = = = = = = = = = = = = = = s = = = a s s a o r= = s = = = s = = = " );
for (i=0;i<=3;i++)
I
printf ("
Pentium%d %d %8ld %15ld ",pccode[i],stock[i],price[i],price[i]*stock[i]);
t=t+price[i]*stock[i];
I
pm j//r/
============================sa«*========w");
printf ("Total Value of All PCs in Rs. %ld",t);
printf f"
=======================================fi,/);
I
OUTPUT:
Stock & Total Cost Details
Model Qty. Rate(Rs.) Total Value
Pentiuml 25
25000
625000
Pentium2 20
30000
600000
Pentium3 15
35000 525000
Pentium4 20
40000 800000
Total Value of All PCs in Rs.2550000
Explanation
Here in the above program, three integer arrays
pccode
[ ],
price
[ ]
and
sto ck [
] are initialized. After this the for loop is used for finding the total value of the available stock of each
model. Result is printed by using simple multiplication of price [ ] and stock [ ]. This product
is then added to variable ' t ', which is the total value. The for loop is executed for four times. At the
end, variable ' t ' gives us the total cost of all PCs. Total cost is printed with pr i n t f () statement.
7.17 Write a program to display the given message by using putcO and stdouH) functions.
#include <stdio.h>
#include <conio.h>
main(void)
/