Skip to main content

TOTAL Total One day John bought a bag full of different variety of apples. He wanted to sell them, but he doesn't know the total number of apples in the bag. He asks for Jack's help to find the total number of apples in the bag. Write a program to find the total number of apples using recursion. Note : Formulate a RECURSIVE Algorithm. Note: 1) Use the function, int sumOfArray(int *a,int n), with 2 parameters. First parameter is an integer pointer(representing the array), and the second paramenter is an integer(representing the size of the array). 2) Use malloc method to create the array. Input Format: The first line of input consists of integer 'N' (number of a variety of apples). The second line of input consists of 'N' values (number of apples of each variety). Output Format: The output consists of integer (total sum). Sample Input: 4 2 3 2 1 Sample Output: 8 #include #include #include int sumOfArray(int *a,int n) { if(n<=0) return 0; else return (*(a+n-1)+sumOfArray(a,n-1)); } int main() { int i,N,*p,ans; scanf("%d",&N); p=(int*)malloc(N*sizeof(int)); for(i=0;i

Comments

Popular posts from this blog

C Program To Calculate The Revenue The demand for organic food increases every year by 8.9%. Write a program to find the increase in revenue after 3 years. Given the revenue in the first year is ‘x’ crores. Input Format: The first line of the input is an integer that corresponds to the ‘x’ value. Output Format: Output is a float value that corresponds to the revenue after 3 years, rounded off to 2 decimal places. Sample Input: 4 Sample Output: 5.17 //PROGRAM #include<stdio.h> int main() {     int x,i;     float rev;     scanf("%d",&x);     rev=x;     for(i=0;i<3;i++)     {         rev=rev*(1.089);     } printf("%.2f",rev); return 0; }
CROP DETAILS Crop Details A small farmer, Mr.John, use to grow different crops every year in his field. All the crops are being grown using goat manure, farmyard manure, groundnut and neem cake. All the crops are grown organically. He got high yield by using sufficient water and fertilizers at in this year. He will sell his crops in the market. In the market, he wants to maintain the details of each crop that are purchased by customers. Details like crop name, the weight of the crop, cost of the crop Display the details of crops in ascending order of crop names. Create a structure called crop. struct crop{ char cropName[30]; float weight; int cost; }; Input and Output Format: Refer sample input and output for formatting specification. All float values are displayed correctly to 2 decimal places. All text in bold corresponds to the input and the rest corresponds to output. Sample Input and Output: Enter the number of Crops 2 Enter the details of Crop 1 Enter name...
Fencing the ground Two non-overlapping fields need to be fenced together to guard from cattle. The fence is always a single rectangle, which cover the two fields exactly. Given the left bottom coordinate, length and width of the two fields, write a program to find dimension of the fence. Print “Invalid Input” if the fields overlap.   Input Format: The 1 st  line of the input consists of 4 integers separated by a space that correspond to x, y, l and w of the first rectangle. The 2 nd  line of the input consists of 4 integers separated by a space that correspond to x, y, l and w of the second rectangle. Output Format: Output consists of 4 integers that correspond to x, y, l and w of the Union rectangle. Sample Input 1: 0 2 4 3 4 0 2 8 Sample Output 1: 0 0 6 8   Sample Input 2: 0 2 4 3 3 0 2 8 Sample Output 2: Invalid Input //PROGRAM #include<stdio.h> int main() { int x1,x2,y1,y2,l1,l2,w1,w2,xmax,xmin,ymax,ymin; scanf("%d %d %d %d",&x1,...