# C Basic Part-II: Exercises, Practice, Solution

## C Basic-II [7 exercises with solution]

[** An editor is available at the bottom of the page to write and execute the scripts.** Go to the editor]

**1.** Write a C program that takes n number of positive integers. Find the integer that appears the least number of times among the said integers. If there are multiple such integers, select the smallest one.

**Sample Date:**

(1,2,3) -> 1

(10, 20, 4, 5, 11) -> 4

**2.** Write a C program that takes a string and two integers (n1, n2). Now reverse the sequence of characters in the string between n1 and n2.

**Sample Date:**

("abcdxyabcd", 5, 6) -> "abcdyxabcd"

("Exercises", 1, 3) -> "exercises"

**3.** Write a C program that accepts three integers from the user and finds the second largest number among them.

Constraints:

- 1≤ x ≤100
- 1≤ y ≤100
- 1≤ z ≤100

**Sample Date:**

(1 , 2, 3) -> 2

(10, 12, 24) -> 12

(34, 21, 30) -> 30

**4.** Write a C program that accepts two sequences ((a1, a2, .. an), (b1, b2, .. bn)) of integers as input. Find all integers that appear in both sequences, one by one, in ascending order.

Constraints:

- 1≤N1≤100, N1 -> number of integers in first sequence.
- 1≤N2≤100, N2 -> number of integers in second sequence.
- 1 ≤ ai≤ 100 ( 1 ≤ i ≤ N1 )
- 1 ≤ bj≤ 100 ( 1 ≤ j ≤ N2 )

**Sample Date:**

( 1 2 3 1 2 3 4) -> 1 2 3

( 1 2 3 1 2 3) -> 1 2 3

(1 2 3 4 5 6) ->

**5.** Write a C program that accepts a sequence of different values and calculates the sum of the values before and after the maximum value.

The sum of the values before the maximum value is 0, if there are no values before the maximum. Similarly, the sum of the values after the maximum value is 0, if there are no values after the maximum.

**Sample Date:**

1 2 3 -> 3 0

1 2 9 4 5 -> 3 9

2 2 2 2 -> 0 6

**6.** Write a C program that accepts a sequence of positive integers from the user and finds the longest continuous subsequence.

**Sample Date:**

Length of the sequence: 5

Sequence: 5 2 3 4 1

Length of longest ascending contiguous subsequence: 5 [2 3 4]

Length of the sequence: 6

Sequence: 10 20 30 40 50 60

Length of longest ascending contiguous subsequence: 6 [10 20 30 40 50 60]

Length of the sequence: 3

Sequence: 5 1 3

Length of longest ascending contiguous subsequence: 2 [1 3]

**7.** Write a C program that accepts three integers: A, B, and X. Find the smallest absolute value of the difference between X and the integers between A and B.

**Sample Date:**

Input A, B: 7, 11

C: 20

Smallest absolute value of difference between X and integers between A and B (inclusive): 11

Input A, B: 1, 5

C: 4

Smallest absolute value of difference between X and integers between A and B (inclusive): 4

## C programming Code Editor:

**More to Come !**

**Do not submit any solution of the above exercises at here, if you want to contribute go to the appropriate exercise page.**

## C Programming: Tips of the Day

**Setting variable to NULL after free:**

Setting unused pointers to NULL is a defensive style, protecting against dangling pointer bugs. If a dangling pointer is accessed after it is freed, you may read or overwrite random memory. If a null pointer is accessed, you get an immediate crash on most systems, telling you right away what the error is.

For local variables, it may be a little bit pointless if it is "obvious" that the pointer isn't accessed anymore after being freed, so this style is more appropriate for member data and global variables. Even for local variables, it may be a good approach if the function continues after the memory is released.

To complete the style, you should also initialize pointers to NULL before they get assigned a true pointer value.

Ref : https://bit.ly/3Awk2aU

**Weekly Trends**- Python Interview Questions and Answers: Comprehensive Guide
- Scala Exercises, Practice, Solution
- Kotlin Exercises practice with solution
- MongoDB Exercises, Practice, Solution
- SQL Exercises, Practice, Solution - JOINS
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- JavaScript String Exercises
- JavaScript HTML Form Validation
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join

We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook