Professional Documents
Culture Documents
Ex.1. Usage of Nested Loops Computing The Harvest: Stdio.h
Ex.1. Usage of Nested Loops Computing The Harvest: Stdio.h
Ex.1. Usage of Nested Loops Computing The Harvest: Stdio.h
#include <stdio.h>
int main(void) {
int np, nveg, totveg=0;
int i,j;
printf("Enter the number of plants : ");
scanf("%d", &np);
for (i=1;i<=7;i++)
{
for (j=1;j<=np;j++)
{
printf("Enter the number of vegetables plucked : ");
scanf("%d",&nveg);
totveg=totveg+nveg;
}
}
printf("Total number of vegetables plucked : %d",totveg);
}
Sample Input/Output:
Result: The above program is executed and verified for sample set of inputs.
Ex. 2. Usage of Functions
Calculation of Sapling Requirement using Plant Spacing
Calculate the maximum number of saplings that can be planted in a field of length l meters and width w
meters using a given plant spacing using functions
Aim: To calculate the maximum number of saplings that can be planted in a field of length l meters and
width w meters using a given plant spacing using functions
Algorithm:
1. Start.
2. Declare the required variables.
3. Read the length and width of the field in meters.
4. Read the plant spacing in cms as input.
5. Call the function calcSaplings()
6. Print the number of saplings
7. Stop
Define a function for calculating the number of saplings and return it : calcSaplings()
1. Initialize count=0.
2. Convert the input length and width into centimeters
3. Calculate the area of the field.
4. Find the area required for a sapling.
8. Using a loop that starts from 1 till the entire area is covered, count = count + 1
9. Return the number of saplings.
Source Code:
#include <stdio.h>
int calcSaplings(int l, int w, int ps)
{
int area, ap, count, i;
l=l*100;
w=w*100;
area=l*w;
ap=ps*ps;
for (i=1;i<=area;i=i+ap)
{
count=count+1;
}
return count;
}
int main(void) {
int l,w,ps,count;
printf("Enter the length of the field in mts : ");
scanf("%d",&l);
printf("Enter the width of the field in mts : ");
scanf("%d",&w);
printf("Enter plant spacing : ");
scanf("%d",&ps);
count=calcSaplings(l,w,ps);
printf("Number of plants = %d",count);
}
Sample Input/Output:
Result: The above program is executed and verified for sample set of inputs.
Store the amount of paddy cultivated (in kgs) in ‘n’ number of fields in an array and calculate the total
amount of paddy cultivated in kgs.
Aim: To store the amount of paddy cultivated (in kgs) in ‘n’ number of fields in an array and calculate
the total amount of paddy cultivated in kgs.
Algorithm:
1. Start.
2. Read the number of fields as input (n)
3. Initialize total = 0
4. Repeat steps 4 to using a loop that runs from 1 to n
5. Read the amount of paddy cultivated in each field i.e. a[i]
6. total = total + a[i]
7. Print total
8. Stop
Source Code:
#include <stdio.h>
int main()
{
float a[10], total=0.0;
int i,n;
printf("Enter the number of fields");
scanf("%d",&n);
for (i=0;i<n;i++)
{
printf("Enter the amount of field cultivated in field %d : ",i+1);
scanf("%f",&a[i]);
total=total+a[i];
}
printf("Total amount of paddy cultivated: %f kgs",total);
}
Sample Input/Output:
Result:
The above program is executed and verified for sample set of inputs.
There is a collection of fruits and vegetables. Write a program to count the number of
fruits/vegetables in each variety of a given fruit/vegetable. Also count the total number of
vegetables/fruits in the collection.
Given fruit/vegetable :
Algorithm:
1. Start
2. Declare the required variables and initialize them.
3. Read the variety name of the given vegetable/fruit
4. Check the variety name and increment the corresponding count.
5. Increment the total count also
6. Repeat steps 3 to 5 while the collection is not empty.
7. Print the number of fruits/vegetables in each variety
8. Print the total number of fruits/vegetables.
9. Stop
#include <stdio.h>
#include <string.h>
int main(void) {
int c1=0,c2=0,c3=0,c4=0,c5=0,c6=0,c7=0,tot=0;
char crop[20];
do
{
printf("Enter crop variety name : ");
scanf("%s",crop);
tot++;
if (strcmp(crop,"Kamini")==0)
c1++;
else if (strcmp(crop,"Pusa_Mukhamali")==0)
c2++;
else if (strcmp(crop,"Parbhani_Kranti")==0)
c3++;
else if (strcmp(crop,"Pusa_Sawani")==0)
c4++;
else if (strcmp(crop,"Vaishali")==0)
c5++;
else if (strcmp(crop,"Vagmi")==0)
c6++;
else if (strcmp(crop,"Padmini")==0)
c7++;
}while (strcmp(crop,"No")!=0);
printf("\nKamini = %d nos",c1);
printf("\nPusa_Mukhamali = %d nos",c2);
printf("\nParbhani_Kranti = %d nos",c3);
printf("\nPusa_Sawani = %d nos", c4);
printf("\nVaishali = %d nos",c5);
printf("\nVagmi = %d",c6);
printf("\nPadmini = %d nos",c7);
printf("\nTotal number of crops = %d",tot);
printf("\nTotal number of ladies finger %d",c1+c2+c3+c4+c5+c6+c7);
}