Implement The Following Function: Python
Implement The Following Function: Python
def differenceofSum(n. m)
The function accepts two integers n, m as arguments Find the sum of all numbers in range from 1
to m(both inclusive) that are not divisible by n. Return difference between sum of integers not
divisible by n with sum of numbers divisible by n.
Assumption:
Example
Input
n:4
m:20
Output
90
Explanation
Input
n:3
m:10
Output
19
Python
n = int(input())
m = int(input())
sum1 = 0
sum2 = 0
for i in range(1,m+1):
if i % n == 0:
sum1+=i
else:
sum2+=i
print(abs(sum2-sum1))
C
#includ<stdio.h>;
int differenceofSum(int n, int m)
{
int i, sum1 = 0, sum2 = 0;
for(i=1; i<=m; i++)
{
if(i%n==0)
{
sum1 = sum1 + i;
}
else
{
sum2 = sum2 + i;
}
}
return sum2 - sum1;
}
int main()
{
int n, m;
int result;
scanf("%d",&n);
scanf("%d",&m);
result = differenceofSum(n, m);
printf("%d",result);
return 0;
}
Input:
3
10
Output:
19
Question: 2
def LargeSmallSum(arr)
The function accepts an integers arr of size ’length’ as its arguments you are required to return the sum of
second largest largest element from the even positions and second smallest from the odd position of
given ‘arr’
Assumption:
NOTE
Example
Input
arr:3 2 1 7 5 4
Output
Explanation
Sample Input
arr:1 8 0 2 3 5 6
Sample Output
Python
length = int(input())
arr = list(map(int, input().split()))
even_arr = []
odd_arr = []
for i in range(length):
if i % 2 == 0:
even_arr.append(arr[i])
else:
odd_arr.append(arr[i])
even_arr = sorted(even_arr)
odd_arr = sorted(odd_arr)
print(even_arr[len(even_arr)-2] + odd_arr[len(odd_arr)-2])
C
#include <stdio.h>;
int main()
{
int n, result, i;
scanf("%d",&n);
int array[n];
for(i=0; i<n; i++)
{
scanf("%d",&array[i]);
}
result = largeSmallSum(array, n);
printf("%d",result);
return 0;
}
Question: 3
The function accepts an integers sum and an integer array arr of size n. Implement the function to find the
pair, (arr[j], arr[k]) where j!=k, Such that arr[j] and arr[k] are the least two elements of array (arr[j] +
arr[k] <= sum) and return the product of element of this pair
NOTE
Example
Input
sum:9
Arr:5 2 4 3 9 7 1
Output
Explanation
Pair of least two element is (2, 1) 2 + 1 = 3 < 9, Product of (2, 1) 2*1 = 2. Thus, output is 2
Sample Input
sum:4
Arr:9 8 3 -7 3 9
Sample Output
-21
Python
n = int(input())
sum1 = int(input())
arr = list(map(int, input().split()))
if n < 2:
print('-1')
arr = sorted(arr)
for i in range(n-1):
if arr[i] + arr[i+1] < sum1:
print(arr[i] * arr[i+1])
break
else:
print('0')
C
#include<stdio.h>;
int main()
{
int n, sum, result, i;
scanf("%d",&sum);
scanf("%d",&n);
int array[n];
for(i=0; i<n; i++)
{
scanf("%d",&array[i]);
}
result = productSmallestPair(array, n, sum);
printf("%d",result);
return 0;
}
Input:
6
4
9 8 3 -7 3 9
Output:
-21
Question: 4
N-base notation is a system for writing numbers which uses only n different symbols, This symbols are the first n
symbols from the given notation list(Including the symbol for o) Decimal to n base notation are (0:0, 1:1, 2:2, 3:3,
4:4, 5:5, 6:6, 7:7, 8:8, 9:9, 10:A,11:B and so on upto 35:Z)
The function accept positive integer n and num Implement the function to calculate the n-base equivalent of num
and return the same as a string
Steps:
1. Divide the decimal number by n,Treat the division as the integer division
2. Write the the remainder (in n-base notation)
3. Divide the quotient again by n, Treat the division as integer division
4. Repeat step 2 and 3 until the quotient is 0
5. The n-base value is the sequence of the remainders from last to first
Assumption:
1 < n < = 36
Example
Input
n: 12
num: 718
Output
4BA
Explanation
718 12 59 10(A)
59 12 4 11(B)
4 12 0 4(4)
Sample Input
n: 21
num: 5678
Sample Output
CI8
Python
n = int(input())
num = int(input())
reminder = []
quotient = num // n
reminder.append(num%n)
while quotient != 0:
reminder.append(quotient%n)
quotient = quotient // n
reminder = reminder[::-1]
equivalent = ''
for i in reminder:
if i > 9:
a = i - 9
a = 64 + a
equivalent+=chr(a)
else:
equivalent+=str(i)
print(equivalent)
Input:
21
5678
Output:
CI8
Question: 5
Problem Statement
A carry is a digit that is transferred to left if sum of digits exceeds 9 while adding two numbers from right-to-left one
digit at a time
The functions accepts two numbers ‗num1‘ and ‗num2‘ as its arguments. You are required to calculate and return
the total number of carries generated while adding digits of two numbers ‗num1‘ and ‗ num2‘.
Example:
Input
o Num 1: 451
o Num 2: 349
Output
o 2
Explanation:
Adding ‗num 1‘ and ‗num 2‘ right-to-left results in 2 carries since ( 1+9) is 10. 1 is carried and (5+4=1) is 10, again
1 is carried. Hence 2 is returned.
Sample Input
Num 1: 23
Num 2: 563
Sample Output
C
#include<stdio.h>
int numberOfCarries(int num1 , int num2)
{
int carry = 0, sum, p, q, count = 0;
while((num1!=0)&&(num2!=0))
{
p = num1 % 10;
q = num2 % 10;
sum = carry + p + q;
if(sum>9)
{
carry = 1;
count++;
}
else
{
carry = 0;
}
num1 = num1/10;
num2 = num2/10;
}
return count;
}
int main()
{
int x, y, a;
scanf("%d",&x);
scanf("%d",&y);
a = numberOfCarries(x, y);
printf("%d",a);
return 0;
}
Python
def NumberOfCarries(n1,n2):
count=0
carry = 0
if len(n1) <= len(n2):
l= len(n1)-1
else:
l = len(n2)-1
for i in range(l+1):
temp = int(n1[l-i])+int(n2[l-i])+carry
if len(str(temp))>1:
count+=1
carry = 1
else:
carry = 0
return count+carry
n1=input()
n2=input()
print(NumberOfCarries(n1,n2))
Question: 6
Problem Statement
The function accepts a string ‗ str‘ of length n and two characters ‗ch1‘ and ‗ch2‘ as its arguments . Implement the
function to modify and return the string ‗ str‘ in such a way that all occurrences of ‗ch1‘ in original string are
replaced by ‗ch2‘ and all occurrences of ‗ch2‘ in original string are replaced by ‗ch1‘.
Note:
Example:
Input:
o Str: apples
o ch1:a
o ch2:p
Output:
o Paales
Explanation:
‗A‘ in original string is replaced with ‗p‘ and ‗p‘ in original string is replaced with ‗a‘, thus output is paales.
C
#include <stdio.h>
#include <string.h>
void *ReplaceCharacter(char str[], int n, char ch1, char ch2)
{
int i;
for(i=0; i<n ; i++)
{
if(str[i]==ch1)
{
str[i]=ch2;
}
else if(str[i]==ch2)
{
str[i]=ch1;
}
}
printf("%s",str);
}
int main()
{
char a[100];
char b, c;
int len;
scanf("%s",a);
scanf("%s",&b);
scanf("%s",&c);
len = strlen(a);
ReplaceCharacter(a, len, b, c);
}
Python
def swap (user_input, str1, str2):
result = ''
if user_input != None:
result = user_input.replace(str1, '*').replace(str2, str1).replace('*', str2)
return result
return 'Null'
user_input = input()
str1, str2 = map(str,input().split())
print(swap(user_input, str1, str2))
Output:
apples
a p
paales
Question: 7
Problem Statement
The function accepts 3 positive integers ‗a‘ , ‗b‘ and ‗c ‗ as its arguments. Implement the function to return.
( a+ b ) , if c=1
( a + b ) , if c=2
( a * b ) , if c=3
(a / b) , if c =4
Example:
Input
o c :1
o a:12
o b:16
Output:
o Since ‗c‘=1 , (12+16) is performed which is equal to 28 , hence 28 is returned.
Sample Input
c:2
a : 16
b : 20
Sample Output
-4
C
#include<stdio.h>
int operationChoices(int c, int a , int b)
{
if(c==1)
{
return a + b;
}
else if(c==2)
{
return a - b;
}
else if(c==3)
{
return a * b;
}
else if(c==4)
{
return a / b;
}
}
int main()
{
int x, y, z;
int result;
scanf("%d",&x);
scanf("%d",&y);
scanf("%d",&z);
result = operationChoices(x, y, z);
printf("%d",result);
}
Python
def operationChoices(c,a,b):
if c == 1 :
return(a+b)
elif c == 2:
return(a-b)
elif c == 3:
return(a*b)
else:
return(a//b)
c,a,b = map(int,input().split())
print(operationChoices(c, a, b))
Output:
2 16 12 20
-4
Question: 8
Problem Statement
You have to find and return the number between ‗a‘ and ‗b‘ ( range inclusive on both ends) which has the maximum
exponent of 2.
The algorithm to find the number with maximum exponent of 2 between the given range is
1. Loop between ‗a‘ and ‗b‘. Let the looping variable be ‗i‘.
2. Find the exponent (power) of 2 for each ‗i‘ and store the number with maximum exponent of 2 so faqrd in a
variable , let say ‗max‘. Set ‗max‘ to ‗i‘ only if ‗i‘ has more exponent of 2 than ‗max‘.
3. Return ‗max‘.
Assumption: a <b
Note: If two or more numbers in the range have the same exponents of 2 , return the small number.
Example
Input:
o 7
o 12
Output:
o 8
Explanation:
Exponents of 2 in:
7-0
8-3
9-0
10-1
11-0
12-2
Python
def countExponents(i):
count = 0
while i%2 == 0 and i != 0:
count+=1
i = i//2
return count
a, b = map(int,input().split())
print(maxExponents(a, b))
Output:
7 12
8
Question: 9
The function accepts a string ―str‖ of length ‗n‘, that contains alphabets and hyphens (-). Implement the function to
move all hyphens(.) in the string to the front of the given string.
Example :-
Input:
o str.Move-Hyphens-to-Front
Output:
o -MoveHyphenstoFront
Explanation:-
The string ―Move-Hyphens -to-front‖ has 3 hyphens (.), which are moved to the front of the string, this output is ―—
MoveHyphen‖
Sample Input
Str: String-Compare
Sample Output-
-StringCompare
Python
inp = input()
count = 0
final = ""
for i in inp:
if i == '-':
count+=1
else:
final+=i
print("-"*count,final)
Output:
move-hyphens-to-front
--- movehyphenstofront
Question : 10
The function accepts 2 positive integer ‗m‘ and ‗n‘ as its arguments.You are required to calculate the sum of
numbers divisible both by 3 and 5, between ‗m‘ and ‗n‘ both inclusive and return the same.
Note
0 < m <= n
Example
Input:
m : 12
n : 50
Output
90
Explanation:
The numbers divisible by both 3 and 5, between 12 and 50 both inclusive are {15, 30, 45} and their sum is 90.
Sample Input
m : 100
n : 160
Sample Output
405
#include <stdio.h>
int main()
{
int m, n, result;
printf("Enter the value of m : ");
scanf("%d",&m);
printf("Enter the value of n : ");
scanf("%d",&n);
result = Calculate(n,m);
printf("%d",result);
return 0;
}
calculate(m,n)
Question 11
Problem Statement
You are required to input the size of the matrix then the elements of matrix, then you have to divide the main matrix
in two sub matrices (even and odd) in such a way that element at 0 index will be considered as even and element at
1st index will be considered as odd and so on. then you have sort the even and odd matrices in ascending order then
print the sum of second largest number from both the matrices
Example
10
Python
array = []
evenArr = []
oddArr = []
n = int(input("Enter the size of the array:"))
for i in range(0,n):
number = int(input("Enter Element at {} index:".format(i)))
array.append(number)
if i % 2 == 0:
evenArr.append(array[i])
else:
oddArr.append(array[i])
evenArr = sorted(evenArr)
print("Sorted Even Array:", evenArr[0:len(evenArr)])
oddArr = sorted(oddArr)
print("Sorted Odd Array:", oddArr[0:len(oddArr)])
print(evenArr[1] + oddArr[1])
C
#include <stdio.h>
int main()
{
int arr[100];
int length, i, j, oddlen, evenlen, temp, c, d;
int odd[50], even[50];
for(i=0;i<length;i++)
{
printf("Enter element at %d index : ",i);
scanf("%d",&arr[i]);
}
if(length%2==0)
{
oddlen = length/2;
evenlen = length/2;
}
else
{
oddlen = length/2;
evenlen = (length/2) + 1;
}
printf("\n");
Instructions: You are required to write the code. You can click on compile and run anytime to check
compilation/execution status. The code should be logically/syntactically correct.
Problem: Write a program in C to display the table of a number and print the sum of all the multiples in it.
Test Cases:
Test Case 1:
Input:
5
Expected Result Value:
5, 10, 15, 20, 25, 30, 35, 40, 45, 50
275
Test Case 2:
Input:
12
Expected Result Value:
12, 24, 36, 48, 60, 72, 84, 96, 108, 120
660
C
#include <stdio.h>
int main()
{
int n, i, value=0, sum=0;
printf("Enter the number for which you want to know the table: ",n);
scanf("%d",&n);
printf("sum is %d",sum);
return 0;
}
Python
table_number = int(input())
sum = 0
for i in range(1, 11):
value = table_number * i
print(value, end=" ")
sum = sum + value
print()
print(sum)
Question: 13
Instructions: You are required to write the code. You can click on compile and run anytime to check
compilation/execution status. The code should be logically/syntactically correct.
Question: Write a program in C such that it takes a lower limit and upper limit as inputs and print all the
intermediate pallindrome numbers.
Test Cases:
TestCase 1:
Input :
10 , 80
Expected Result:
11 , 22 , 33 , 44 , 55 , 66 , 77.
Test Case 2:
Input:
100,200
Expected Result:
101 , 111 , 121 , 131 , 141 , 151 , 161 , 171 , 181 , 191.
C
#include<stdio.h>
int main()
{
int i, n, reverse, d,f,l;
printf("enter the starting \n",f);
scanf("%d",&f);
printf("enter the ending\n",l);
scanf("%d",&l);
for (i = f; i <= l; i++)
{
reverse = 0;
n = num;
while (n != 0)
{
d = n % 10;
reverse = reverse * 10 + d;
n = n / 10;
}
if (i == reverse)
printf("%d ",i);
}
return 0;
}
Python
# Palindrome Number Checking
first_number = int(input())
second_number = int(input())
Instructions: You are required to write the code. You can click on compile & run anytime to check the compilation/
execution status of the program. The submitted code should be logically/syntactically correct and pass all the test
cases.
Ques: The program is supposed to calculate the distance between three points.
For
x1 = 1 y1 = 1
x2 = 2 y2 = 4
x3 = 3 y3 = 6
C
#include <stdio.h>
#include <math.h>
int main()
{
int t;
float p1[2], p2[2], p3[2];
printf("enter x1 and y1 : ");
scanf("%f%f",&p1[0],&p1[1]);
printf("enter x2 and y2 : ");
scanf("%f%f",&p2[0],&p2[1]);
printf("enter x3 and y3 : ");
scanf("%f%f",&p3[0],&p3[1]);
t = isDistance(&p1, &p2, &p3);
printf("%d",t);
return 0;
}
Python
import math
x1, y1 = 1, 1
x2, y2 = 2, 4
x3, y3 = 3, 6