VISHVESHWARAYA TECHNOLOGICAL
UNIVERSITY
JANANA SANGAMA, BELAGAVI - 590018
BLDEA'S VP Dr PG. HALAKATTI COLLEGE OF
ENGINEERING AND TECHNOLOGY VIJAYAPUR-586103
Department Of Information Science and Engineering
ADA Assignment – 02
Solved 10 problems in Hackerrank.com
Submitted by veeresh panchikatti
USN 2BL23IS059
Courses code :-BCS401
Prof. R.N.Patil
P.H.Unki
1
Course coordinator HOD
Q.1 functions in c
Objective
n this challenge, you will learn simple usage of functions in C. Functions are a
bunch of statements grouped together. A function is provided with zero or more
arguments, and it executes the statements on it. Based on the return type, it
either returns nothing (void) or something.
A sample syntax for a function is
return_type function_name(arg_type_1 arg_1, arg_type_2 arg_2, ...) {
...
...
...
[if return_type is non void]
return something of type `return_type`;
}
For example, a function to read four variables and return the sum of them can be
written as
int sum_of_four(int a, int b, int c, int d) {
int sum = 0;
sum += a;
sum += b;
sum += c;
sum += d;
return sum;
}
+= : Add and assignment operator. It adds the right operand to the left operand
and assigns the result to the left operand.
a += b is equivalent to a = a + b;
2
Task
Write a function int max_of_four(int a, int b, int c, int d) which reads four
arguments and returns the greatest of them.
Note
There is not built in max function in C. Code that will be reused is often put in a
separate function, e.g. int max(x, y) that returns the greater of the two values.
Input Format
Input will contain four integers - , one on each line.
Output Format
Print the greatest of the four integers.
Note: I/O will be automatically handled.
Sample Input
3
4
6
5
Sample Output
6
Salutation Code
#include <stdio.h>
int max(int x, int y) {
return (x > y) ? x : y;
}
int max_of_four(int a, int b, int c, int d) {
return max(max(a, b), max(c, d));
}
int main() {
int a, b, c, d;
3
scanf("%d\n%d\n%d\n%d", &a, &b, &c, &d);
int result = max_of_four(a, b, c, d);
printf("%d\n", result);
return 0;
}
Output:
Input (stdin)
3
4
6
5
Your Output (stdout)
6
Expected Output
6
Q.2 Calculate the Nth term
Objective
This challenge will help you learn the concept of recursion.
A function that calls itself is known as a recursive function. The C
programming language supports recursion. But while using recursion,
if...else one needs to be careful to define an exit condition from the
function, otherwise it will go into an infinite loop.
To prevent infinite recursion, statement (or similar approach) can be
used where one branch makes the recursive call and other doesn't.
void recurse() {
.....
recurse() //recursive call
.....
4
}
int main() {
.....
recurse(); //function call
.....
}
Task
There is a series, S, where the next term is the sum of pervious three
terms,. Given the first three terms of the series,a ,b , and c
respectively, you have to output the nth term of the series using
recursion.
Recursive method for calculating nth term is given below.
S(n)={a n=1, b n=2,c n=3,S(n-1)+S(n-2)+S(n-3) otherwise
Input Format
The first line contains a single integer, .
The next line contains 3 space-separated integers,a ,b , and c .
Constraints
1<=n<=20,1<=a,b,c<=100
Output Format
Print the nth term of the series, S(n).
Sample Input 0
5
123
Sample Output 0
11
salutation code
#include <stdio.h>
int find_nth_term(int n, int a, int b, int c) {
5
if (n == 1) return a;
if (n == 2) return b;
if (n == 3) return c;
return find_nth_term(n - 1, a, b, c)
+ find_nth_term(n - 2, a, b, c)
+ find_nth_term(n - 3, a, b, c);
}int main() {
int n, a, b, c;
scanf("%d", &n);
scanf("%d %d %d", &a, &b, &c);
printf("%d\n", find_nth_term(n, a, b, c));
return 0;
}
Output:
input (stdin)
5
123
Your Output (stdout)
11
Expected Output
11
6
Q.3 1D Arrays in C
Objective
An array is a container object that holds a fixed number of values of a single
type. To create an array in C, we can do int arr[n];. Here, arr, is a variable array
which holds up to 10 integers. The above array is a static array that has memory
allocated at compile time. A dynamic array can be created in C, using the malloc
function and the memory is allocated on the heap at runtime. To create an
integer array, arr of size n , int arr = (int)malloc(n * sizeof(int)), where arr
points to the base address of the array. When you have finished with the array,
use free(arr) to deallocate the memory.
In this challenge, create an array of size n dynamically, and read the values from
stdin. Iterate the array calculating the sum of all elements. Print the sum and
free the memory where the array is stored.
While it is true that you can sum the elements as they are read, without first
storing them to an array, but you will not get the experience working with an
array. Efficiency will be required later.
Input Format
The first line contains an integer, n.
The next line contains n space-separated integers.
Constraints
1<=n<=1000
1<=a[i]<=1000
Output Format
Print the sum of the integers in the array.
Sample Input 0
6
16 13 7 2 1 12
Sample Output 0
51
Salutation Code
#include <stdio.h>
#include <stdlib.h>
7
int main() {
int n;
int *arr;
int sum = 0;
scanf("%d", &n);
arr = (int*)malloc(n * sizeof(int));
if (arr == NULL) {
printf("Memory allocation failed.\n");
return 1;
}for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
sum += arr[i];
}
printf("%d\n", sum);
free(arr);
return 0;
}
Output:
Input (stdin)
6
16 13 7 2 1 12
Your Output (stdout)
51
Expected Output
51
Q4. Pointers in C
8
Objective
In this challenge, you will learn to implement the basic functionalities of pointers in C. A
pointer in C is a way to share a memory address among different contexts (primarily
functions). They are primarily used whenever a function needs to modify the content of a
variable that it does not own.
In order to access the memory address of a variable, , prepend it with sign. For example, &val
returns the memory address of .
This memory address is assigned to a pointer and can be shared among various functions. For
example, will assign the memory address of to pointer . To access the content of the memory
to which the pointer points, prepend it with a * . For example, *p will return the value
reflected by and any modification to it will be reflected at the source (val ).
Task
Complete the function void update(int *a,int *b) . It receives two integer pointers, int* a and
int* b. Set the value of to their sum, and to their absolute difference. There is no return value,
and no return statement is needed.
Input Format
The input will contain two integers, and , separated by a newline.
Sample Input
Sample Output
Salutation Code
#include <stdio.h>
#include <stdlib.h> // for abs()
void update(int *a,int *b) {
9
int sum = *a + *b;
int diff = abs(*a - *b);
*a = sum;
*b = diff;
}
int main() {
int a, b;
int *pa = &a, *pb = &b;
scanf("%d", &a);
scanf("%d", &b);
update(pa, pb);
printf("%d\n%d", a, b);
return 0;
}
Output
9
1
Q.5 Printing Pattern Using Loops
Print a pattern of numbers from to as shown below. Each of the numbers is separated by a
single space.
Input Format
The input will contain a single integer n.
Constraints
Sample Input 0
2
10
Sample Input 0
Sample Input 1
5
Sample Output 1
Salutation Code
#include <stdio.h>
int main() {
int n;
scanf("%d", &n);
int size = 2 * n - 1;
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
int min = i < j ? i : j;
min = min < size - i - 1 ? min : size - i - 1;
min = min < size - j - 1 ? min : size - j - 1;
printf("%d ", n - min);
}
printf("\n");
}
return 0;
Output
4444444
4333334
4322234
11
4321234
4322234
4333334
4444444
Q.6 Array Reversal
Input Format
The first line contains an integer, n denoting the size of the array. The next line contains n
spaceseparated integers denoting the elements of the array.
Constraints
Output Format
The output is handled by the code given in the editor, which would print the array
Salutation Code
#include <stdio.h>
12
int main() {
int n;
scanf("%d", &n);
int arr[n];
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
for (int i = 0; i < n / 2; i++) {
int temp = arr[i];
arr[i] = arr[n - i - 1];
arr[n - i - 1] = temp;
}
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
return 0;
}
Output
2 13 12 20 15 13 1
Q.7 Digit Frequency
Given a string, , consisting of alphabets and digits, find the frequency of each digit in the
given string.
Input Format
The first line contains a string, num which is the given number.
Constraints
Output Format
Print ten space-separated integers in a single line denoting the frequency of each digit from 0
to 9.
Sample Input 1
13
lw4n88j12n1
Sample Output 1
0210100020
Sample Input 2
1v88886l256338ar0ekk
Sample Output 2
2/2 1 1 1 2 0 1 2 0 5 0
Salutation Code
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {]
char str[1000];
scanf("%s", str);
int digit_count[10] = {0};
for (int i = 0; i < strlen(str); i++) {
if (str[i] >= '0' && str[i] <= '9') {
digit_count[str[i] - '0']++;
}
}
for (int i = 0; i < 10; i++) {
printf("%d ", digit_count[i]);
}
printf("\n");
return 0;
}
Output
0210100020
Q.8 Printing Tokens
14
Given a sentence, s, print each word of the sentence in a new line.
Input Format
The first and only line contains a sentence,s .
Constraints
1<=len(s)<=1000
Output Format
Print each word of the sentence in a new line.
Sample Input 0
This is C
Sample Output 0
This
is
C
Salutation Code
#include <stdio.h>
#include <string.h>
int main() {
char s[1001];
fgets(s, sizeof(s), stdin);
size_t len = strlen(s);
if (len > 0 && s[len - 1] == '\n') {
s[len - 1] = '\0';
}
char *token = strtok(s, " ");
while (token != NULL) {
printf("%s\n", token);
15
token = strtok(NULL, " ");
}
return 0;
}
Output:
Input (stdin)
This is C
Your Output (stdout)
This
is
C
Expected Output
This
is
C
Q.9 Sum of Digits of a Five Digit Number
Objective
The modulo operator, % , returns the remainder of a division. For example, 4 % 3 = 1 and 12
% 10 = 2 . The ordinary division operator, / , returns a truncated integer value when
performed on integers. For example, 5 / 3 = 1 . To get the last digit of a number in base 10,
use as the modulo divisor.
Task
Given a five digit integer, print the sum of its digits.
Input Format
The input contains a single five digit number,n
16
Salutation Code
#include <stdio.h>
int main() {
int num, sum = 0;
scanf("%d", &num);
sum += num % 10;
num /= 10;
sum += num % 10;
num /= 10;
sum += num % 10;
num /= 10;
sum += num % 10;
num /= 10;
sum += num % 10;
printf("%d\n", sum);
return 0;
}
Input: 10543
Output: 13
17
Q.10 Conditional Statements in C
Task
Given a positive integer denoting n , do the following:
If 1<=n<=9, print the lowercase English word corresponding to the number (e.g., one for 1, two for 2 ,
etc.).
If , print Greater than 9.
Input Format
The first line contains a single integer, n.
Constraints
1<=n<=10^9
Output Format
If 1<=n<=9, then print the lowercase English word corresponding to the number (e.g., one for 1, two
for 2 , etc.); otherwise, print Greater than 9 instead.
Sample Input
5
Sample Output
five
Salutation Code
#include <stdio.h>
int main() {
int n;
scanf("%d", &n);
if (n >= 1 && n <= 9) {
switch (n) {
case 1: printf("one\n"); break;
case 2: printf("two\n"); break;
case 3: printf("three\n"); break;
case 4: printf("four\n"); break;
case 5: printf("five\n"); break;
case 6: printf("six\n"); break;
case 7: printf("seven\n"); break;
case 8: printf("eight\n"); break;
18
case 9: printf("nine\n"); break;
}
} else {
printf("Greater than 9\n");
}
return 0;
}
Output:
Input (stdin)
5
Expected
five
19