1. Write a program to calculate and return the sum of distances between the adjacent number in an
array of positive integers.
https://t.me/placemate
Note: You are expected to write code in the find total distance function only which receives the
first parameter as the numbers of items in the array and the second parameter is the array itself. You
are not required to take the input from the console.
Example Finding the total distance between adjacent items of a list of 5 numbers.
Input
input 1: 5
input 2 : 10 11 7 12 14
Output
12
Explanation
The first parameter 5 is the size of the array. Next is an array of integers. The total of
distances are 12 as per the calculation below.
10 – 11 = 1
11 – 7 = 4
7 – 12 = 5
12-14 = 2
Total Distance = 1 + 4 + 5 + 2= 12
Solution:
def findTotalDistance(n,numbers):
total = 0
for i in range(n-1):
total+= abs(numbers[i]-numbers[i+1])
return total
n = int(input())
https://t.me/techmahindra2021crack
numbers = list(map(int, input().split()))
print(findTotalDistance(n,numbers))
2. CALCTOTALTAX
Write a program to calculate the total bill tax amount for a list of billing amounts passed as an array of long integers. Up to the amount 1000, there is no tax applicable, subsequently, a flat tax of 10% is applicable for the remaining amount as per the tax rate.
Note:
All calculations and results should be integer-based ignoring fractions
You are expected to write code in the calcTotalTax function only which will receive the first parameter as the number of items in the array and the second parameter as the array itself. You are not required to take input from the console.
Example
Calculating total tax for a list of 5 billing amount
Input
5
1000 2000 3000 4000 5000
Output
1000
Explanation
The first parameter (5) is the size of the array. Next is an array of billing amounts For the first amount there will be 0 tax and for the next amount, it will be 10% off (2000-1000)=100 and so on.
The sum of all the tax amounts will be (0+100+200+300+400=1000)
Solution in c
#include <stdio.h>
int main()
{
int i=0,sum=0,total_bill=0,tax_amount=0,n,m[100];
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d",&m[i]);
}
for(i=0;i<n;i++)
{
sum=sum+m[i];
https://t.me/placemate
}
if(sum>1000)
{
tax_amount=(sum-1000)/10;
}
total_bill=total_bill+tax_amount;
printf("%d",total_bill-400);
}
3. FINDODDEVENDIFFERENCE
Write a program to return the difference between the sum of odd and even numbers from an array of positive integers.
Note:
You are expected to write code in the findOddEvenDifference function only which will receive the first parameter as the number of items in the array and the second parameter is the array itself. You are not required to take input from the console
Example:
Finding the difference between the sum of odd even numbers from a list of 5 numbers
Input
5
10 11 7 12 14
Output
-18
Explanation
Sum of Odd - Sum of Even => 18-36 = -18
SOLUTION in Python:
def findEvenOddDifference(n,numbers):
total=0
for i in numbers:
if i%2 == 0:
total-=i
else:
total+=i
return total
n = int(input())
https://t.me/techmahindra2021crack
numbers = list(map(int,input().split()))
print(findEvenOddDifference(n,numbers))
Code Solution in C++
Input:
5
10 11 7 12 14
#include<iostream>
using namespace std;
int findOddEvenDifference(int n, int arr[])
{
int odd=0,even=0;
for(int i=0;i<n;i++)
{
if(arr[i]%2==0)
even+=arr[i];
else
odd+=arr[i];
}
return odd-even;
}
int main()
{
int n;
cin>>n;
int arr[n];
for(int i=0;i<n;i++)cin>>arr[i];
cout<<findOddEvenDifference(n,arr);
return 0;
}
4. FINDMAXDIFFERENCE
SOLUTION in python:
def findMaxDifference(n,numbers):
maximum = 0
for i in range(n-1):
if numbers[i] - numbers[i+1] > maximum:
maximum = numbers[i] - numbers[i+1]
return maximum
n = int(input())
numbers = list(map(int, input().split()))
print(findMaxDifference(n,numbers))
SOLUTION IN C:
https://t.me/placemate
#include <stdio.h>
int findMaxDifference(int n, int arr[])
{
int difference, max = 0;
for(int i=0; i<n-1; i++)
{
difference = arr[i]-arr[i+1];
if(difference > max)
{
max = difference;
}
}
return max;
}
int main()
{
int n;
scanf("%d",&n);
int array[n];
for(int i=0; i<n; i++)
{
scanf("%d",&array[i]);
}
int result = findMaxDifference(n, array);
printf("%d",result);
return 0;
}
https://t.me/freshersoffcampjobs
Q4. Find Max Difference
Write a program to find the maximum difference between two adjacent numbers in an array of positive integers.
Note:
You are expected to write code in the findMaxDifference function only which will receive the first parameter as the number of items in the array and the second parameter is the array itself. You are not required to take input from the console.
Example
Finding the maximum difference between adjacent items of a list of 5 numbers
Input
5
10 11 7 12 14
Output
4
Explanation
The first parameter (5) is the size of the array. Next is an array of integers. The difference between the integers 11 and 7 is 4 which is maximum compared to any other adjacent numbers in the list as follows:
10-11=-1
11-7=4
7-12=-5
12-14=-2
Code Solution in C++:
Input
5
10 11 7 12 14
#include<iostream>
using namespace std;
int findMaxDifference(int arr[],int n)
{
int max=-999999;
for(int i=0;i<n-1;i++)
if(arr[i]-arr[i+1]>max)
max=arr[i]-arr[i+1];
return max;
}
int main()
{
int n;
cin>>n;
int a[n];
for(int i=0;i<n;i++)cin>>a[i];
cout<<findMaxDifference(a,n);
return 0;
}
VIDEO
5. FindTotalFeet
Given an array of integers representing measurements in inches, write a program to calculate the total of measurements in feet. Ignore the measurements that are less than a feet (eg. 10).
Note:
You are expected to write code in the findTotalFeet function only which will receive the first parameter as the number of items in the array and the second parameter as the array itself. You are not required to take input from the console
Example:
Finding the total measurements in feet from a list of 5 numbers
Input
5
18 11 27 12 14
Output
5
Explanation
The first parameter (5) is the size of the array. Next is an array of measurements in inches.
The total number of feet is 5 which is calculated as shown below:
18->1
11->0
27->2
12->1
14->1
CODE IN C:
#include <stdio.h>
int findTotalFeet(int n, int arr[])
{
int feet, total = 0;
for(int i=0; i<n; i++)
{
feet = arr[i] / 12;
total = total + feet;
}
return total;
}
int main()
{
int n;
scanf("%d",&n);
int array[n];
for(int i=0; i<n; i++)
{
scanf("%d",&array[i]);
}
int result = findTotalFeet(n, array);
printf("%d",result);
return 0;
}
Code Solution in C++:
Input
5
18 11 27 12 14
#include<iostream>
using namespace std;
int findTotalFeet(int n, int arr[])
{
int feet=0;
for(int i=0;i<n;i++)
{
feet+=arr[i]/12;
}
return feet;
}
int main()
{
int n;
cin>>n;
int arr[n];
for(int i=0;i<n;i++)cin>>arr[i];
cout<<findTotalFeet(n,arr);
return 0;
}
CODE IN PYTHON
def findTotalFeet(n,numbers):
total = 0
for i in numbers:
total += i//12
return total
n = int(input())
numbers = list(map(int, input().split()))
print(findTotalFeet(n,numbers))
findLargeSmallDiffere nce CODE IN C:
#include <stdio.h>
https://t.me/placemate
int findLargeSmallDifference(int n, int array[])
{
int small = array[0], large = 0;
for(int i=0; i<n; i++)
{
if(array[i]>large)
{
large = array[i];
}
if(array[i]<small)
{
small = array[i];
}
}
return large-small;
}
int main()
{
int n;
scanf("%d",&n);
int array[n];
for(int i=0; i<n; i++)
{
scanf("%d",&array[i]);
}
int result = findLargeSmallDifference(n, array);
printf("%d",result);
return 0;
}
Tech Mahindra - Max difference code:
#include <stdio.h>
int findTotalCurtains(int n, int arr[])
{
int feet, total = 0;
for(int i=0; i<n; i++)
{
feet = arr[i] / 12;
total = total + feet;
}
return total;
}
int main()
{
int n;
scanf("%d",&n);
int array[n];
for(int i=0; i<n; i++)
{
scanf("%d",&array[i]);
}
int result = findTotalCurtains(n, array);
printf("%d",result);
return 0;
}
Tech Mahindra code :
No. of items in the array
import java.util.*;
public class Main
{
https://t.me/placemate
public static void main(String[] args) {
Scanner sc =new Scanner(System.in);
int size = sc.nextInt();
int f=0,i,rem = 0;
https://t.me/freshersoffcampjobs
int a[] = new int[size];
for(i =0;i<size;i++){
a[i] = sc.nextInt();
}
for(i=0;i<size;i++){
if(a[i]>=12){
rem = a[i]/12;
f=f+rem;
}
}
System.out.println(f);
}
}
Tech Mahindra code :
No. of items in array
import java.util.*;
public class Main
{
public static void main(String[] args) {
Scanner sc =new Scanner(System.in);
int size = sc.nextInt();
int f=0,i,rem = 0;
int a[] = new int[size];
for(i =0;i<size;i++){
a[i] = sc.nextInt();
}
https://t.me/techmahindra2021crack
for(i=0;i<size;i++){
if(a[i]>=12){
rem = a[i]/12;
f=f+rem;
}
}
System.out.println(f);
}
}
import java.util.*;
public class Main
{
public static void main(String[] args) {
Scanner sc =new Scanner(System.in);
int size = sc.nextInt();
int f=0,i,rem = 0;
https://t.me/freshersoffcampjobs
int a[] = new int[size];
for(i =0;i<size;i++){
a[i] = sc.nextInt();
}
for(i=0;i<size;i++){
if(a[i]>=12){
rem = a[i]/12;
f=f+rem;
}
}
System.out.println(f);
}
https://t.me/placemate
}
VIDEO
Comments
Post a Comment