PROGRAM TRICKY QUESTIONS
1. If N is divisible by 4 or ends with 4, it is "Special Multiple" return 1.
Otherwise, "Not Special" , return 0.
void checkSpecialMultiple(int N) {
if (N % 4 == 0 || N % 10 == 4) {
return 1;
} else {
return 0;
}
}
2. Write a C function that takes an ASCII value as input, determines
whether it corresponds to a vowel or consonant. Return 1 if it is
vowel and 0 if it is consonant
int vowelcheck(int value) {
char ch = (char)value;
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u')
return 1;
return 0;
3. Create a calculator program that takes three inputs: two numbers
and an operation selection (1-5). The program should use a function
to perform the specified operation and return the result. The
operations are:
. Addition
. Subtraction
. Multiplication
. Division
. Modulo
int arithmeticcalculator(int num1, int num2, int operation) {
int op;
switch(operation){
case 1:
op=num1+num2;
break;
case 2:
op=num2-num1;
break;
case 3:
op=num1*num2;
break;
case 4:
op=num2/num1;
break;
default:
return 0;
break;
}
return op;
}
4. Given a positive integer n, write a program to compute the factorial
of the number.
Note: Factorial of 0 is 1.
int factorial(int n) {
int fact=1;
for(int i=1;i<=n;i++){
fact=fact*i;
}
return fact;
}
5. Write a C program that converts temperature between Celsius,
Fahrenheit, and Kelvin based on user choice.
1 ) Celsius to Fahrenheit: F = (C * 9/5) + 32
2 ) Celsius to Kelvin: K = C + 273.15
3) Fahrenheit to Celsius: C = (F - 32) * 5/9
4 ) Fahrenheit to Kelvin: K = (F - 32) * 5/9 + 273.15
5 ) Kelvin to Celsius: C = K - 273.15
6 ) Kelvin to Fahrenheit: F = (K - 273.15) * 9/5 + 32
int converttemperature(int temp, int choice) {
switch(choice){
case 1:
return (temp*9/5)+32;
break;
case 2:
return temp+273.15;
break;
case 3:
return (temp-32)*5/9;
break;
case 4:
return (temp-32)*5/9+273.15;
break;
case 5:
return temp-273.15;
break;
case 6:
return (temp-273.15)*9/5+32;
break;
default:
return 0;
}
}
6. Write a C function that take Principal (P), Rate of Interest (R), and Time
Period (T) as inputs and compute the interest accordingly.
1. SI=(P*N*R)/100
2. CI=P×(1+R/100)^T−P
int calculateinterest(int P, int N , int R, int interestType) {
if(interestType==1){
return (P*N*R)/100;
}
else if(interestType==2){
float amount=1.0;
for(int i=0;i<N;i++){
amount=amount*(1.0+R/100.0);
}
float total=(amount*P)-P;
return total;
}
}
[Link] a positive integer n, write a program to compute the sum of the
squares of all numbers from 1 to n.
int sum_of_squares(int n) {
int result=0;
for(int i=0;i<=n;i++){
result+=i*i;
}
return result;}
8. Given a positive integer num, your task is to check whether all
digits of the number are in strictly ascending order from left to right.
If they are in ascending order, return 1, otherwise return 0.
int ascending_order(int num) {
int prev_digit = num % 10; // Start with the last digit
num /= 10;
// Loop through the digits from right to left
while (num > 0) {
int curr_digit = num % 10; // Get the current digit
if (curr_digit >= prev_digit) { // If current digit is not less
than the previous one
return 0; // Digits are not in ascending order
}
prev_digit = curr_digit; // Move to the next digit
num /= 10; // Remove the last digit
}
return 1; // All digits were in ascending order
}
9. Given a positive integer num, your task is to determine if the number is
divisible by the sum of its digits.
A number is called a Harshad Number if it is divisible by the sum of its
digits.
For example:
18 is a Harshad Number because the sum of digits 1 + 8 = 9, and 18 % 9 ==
0.
19 is not a Harshad Number because the sum of digits 1 + 9 = 10, and 19 %
10 != 0.
int harshad_number(int n) {
int digits;
int sum=0;
int num=n;
while(n>0){
digits= n%10;
sum += digits;
n /=10;
}
if(num % sum == 0){
return 1;
}
else{
return 0;
}
}
10. Given a positive integer num, your task is to compute the sum of the
factorial of each digit. For example, if num = 145, the result should be 1! +
4! + 5! = 1 + 24 + 120 = 145.
int sum_of_digit_factorials(int num) {
int factorials[10] = {1, 1, 2, 6, 24, 120, 720, 5040, 40320,
362880};//factorials from 1 to 10 .
int sum = 0;
if (num == 0) return 1; // Special case for 0
while (num > 0) {
int digit = num % 10;
sum += factorials[digit];
num /= 10;
}
return sum;
}
11. Given a positive integer num, determine if all of its digits
are unique (i.e., no digit repeats). If the number has all unique digits,
return 1. Otherwise, return 0.
int has_unique_digits(int num) {
int seen[10] = {0};
while (num > 0) {
int digit = num % 10;
if (seen[digit]) // ckecks if seen[digit]!=0
return 0; // digit already seen
seen[digit] = 1;//mark the seen digit as 1
num /= 10;
}
return 1; // all digits unique
}
12. Given n, keep reversing the digits of a number until the result is
greater than 100. Return that result
int special_reverse(int n) {
int rev=0;
int num=n;
while(n>0){
rev=rev*10+n%10;
n/=10;
}
if(rev==num || rev>100){
return rev;
}
else{
return -1;
}
13. Count Occurrences of a Digit in a Number
int count_digit_occurrence(int n, int d){
int count=0;
while(n>0){
int digits=n%10;
if(digits == d){
count++;
}
n/=10;
}
return count;
}
14. Given a non-negative integer n, check whether it is a palindrome (reads
the same backward as forward). Return 1 if it is, otherwise return 0
int is_palindrome(int n) {
int new_num=0;
int digits;
int old_num=n;
while(n>0){
digits=n%10;
new_num=new_num*10+digits;
n/=10;
}
if(new_num == old_num){
return 1;
}
else{
return 0;
}
}
15. Given a positive integer n, write a program to compute the sum of
reciprocals from 1 to n. Convert the final answer into integer value.
int sum_of_reciprocals(int n) {
float sum=0.0;
int position=1;
do{
sum+=1.0/position;
position++;
}while(position<n || position==n);
return (int)sum;
}
16. Write a C program to check whether a number is a Duck Number.
A Duck Number must meet the following conditions:
It contains at least one '0'.
The number must not start with '0
int isducknumber(int n) {
int digits;
int zcount=0;
if(n==0){
return 0;
}
while(n>0){
digits=n%10;
if(digits==0){
zcount++;
}
n/=10;
}
return zcount;
}
17. Given a positive integer num, your task is to form all possible 2-digit
numbers by taking any two consecutive digits from the number and return
the largest one.
int consecutive_2digit_number(int n) {
int num=n;
int largest= 1;
int digit=n%10;
n /=10;
while(n>0){
int digit1=n%10;
int digit2= digit1*10+digit;
if(digit2>largest){
largest= digit2;
}
digit=digit1;
n/=10;
}
return largest;
}
18. Check if a Number Has Alternating Even-Odd Digits
int hasalternatingevenodddigits(int n) {
int odcount=0,evcount=0;
int digits;
while(n>0){
digits=n%10;
if(digits%2==0){
evcount++;
}
else{
odcount++;
}
n/=10;
}
if(odcount==evcount){
return 1;
}
else{
return 0;
}
}
19. Write a C program to check whether a number is
a palindrome using recursion
int rev_check(int n){
return n == reverse(n,0);
}
int reverse(int n,int rev){
if(n==0) return rev;
else
return reverse(n/10,rev*10+(n%10));
}
20. Check if a Number is a Palindrome Using Recursion
int rev_check(int n){
return n == reverse(n,0);
}
int reverse(int n,int rev){
if(n==0) return rev;
else
return reverse(n/10,rev*10+(n%10));
}
21. Write a C program to check whether a given non-negative integer n is
a Kaprekar number.
A number is a Kaprekar number if:
Square the number.
Split the square into two parts: right and left.
Add both parts; if the sum equals the original number, it is a Kaprekar
number.
Note:
The right part must not be empty.
For single-digit Kaprekar numbers like 1 and 9, the left part can be 0.
int isKaprekar(int n){
int num=n;
int result;
int square=n*n;
if(square<1000){
result= sumnum(square);
}
else{
result= bigsum_num(square);
}
return num==result;
22. Check if Sum of Digits is a Prime Number
int isdigitsumprime(int n) {
for(int i=2;i*i<=n;i++){
if(n%i ==0){
return 0 ;
}
}
return 1;
}
int checkprime(int n){
int sum=0;
if(n<2){
return 0;
}
while(n>2){
sum+=n%10;
n/=10;
}
return isdigitsumprime(sum);
23. Given a positive integer n, return the sum of the first n Fibonacci
numbers.
The Fibonacci sequence is:
0, 1, 1, 2, 3, 5, 8, 13, ...
int sum_of_fibonacci(int n) {
int a=0,b=1;
int c;
int sum=0;
for(int i=1;i<n;i++){
c =a+b;
a=b;
b=c;
sum+=a;
}
return sum;
}
[Link] 2-Digit Number Formed by Consecutive
Digits
int consecutive_2digit_number(int n) {
int num=n;
int largest= 1;
int digit=n%10;
n /=10;
while(n>0){
int digit1=n%10;
int digit2= digit1*10+digit;
if(digit2>largest){
largest= digit2;
}
digit=digit1;
n/=10;
}
return largest;
}
25. Write a C program to compute the GCD (Greatest Common Divisor) of
two integers using Euclid’s Algorithm.
Euclid’s Algorithm is based on the principle:
GCD(a, b) = GCD(b, a % b)
and
GCD(a, 0) = a
This method is efficient and works recursively or iteratively.
int gcdeuclid(int a, int b) {
while(b !=0){
int temp=b;
b=a%b;
a=temp;
}
return a;
}
26. Write a C program to check if a given number n is a semi-prime.
A semi-prime number is a natural number that is the product of exactly two
prime numbers. These two primes may be the same (like 3 × 3 = 9) or
different (like 2 × 5 = 10)
int factors_num(int n){
for(int i=2;i*i<=n;i++){
if(n%i==0){
if(issemiprime(i) && issemiprime(n/i)){
return 1;
}
}
}
return 0;
}
int issemiprime(int num){
if(num<2) return 0;
for(int i=2;i*i<=num;i++){
if(num%i==0)
return 0;
}
return 1;
}
27. Write a C program that takes a number n and two digits d1 and d2.
Replace all occurrences of digit d1 with digit d2 in the number n, form the
new number, and check if the new number is prime.
int replaceandcheckprime(int n,int d1,int d2) {
int rev_num=0;
while(n>0){
int digit=n%10;
if(digit==d1){
digit=d2;
}
rev_num=rev_num*10+digit;
n/=10;
}
return isPrime(rev_num);
}
int isPrime(int num){
if(num<2) return 0;
for(int i=2;i*i<=num;i++){
if(num%i==0)
return 0;
}
return 1;
}
28. Given a positive integer n, check for symmetric digit pairs.
A symmetric digit pair is a pair of digits at positions i and len - i +
1 (from the left) that are equal.
Return the count of such pairs
int countsymmetricpairs(int n) {
char arr[20];
int length=0;
int count=0;
while(n>0){
arr[length++]=n%10;
n/=10;
}
for(int i=0;i<length/2;i++){
if(arr[i] ==arr[length-i-1]){
count++;
}
}
return count;
}
29. Write a C program that accepts an integer n and finds the next prime
number greater than n.
Return the gap between n and this next prime.
int primeGap(int n){
int new=n+1;
while(!isPrime(new)){
new++;
}
int result=new-n;
return result;
}
int isPrime(int num){
if(num<2) return 0;
for(int i=2;i*i<=num;i++){
if(num%i==0)
return 0;
}
return 1;
}
30. Given a number n, extract its digits and check whether every digit
(from left to right) divides the digit immediately after it.
Return 1 if it forms a factor chain, else return 0.
Ignore cases where division by zero would occur — if a digit is 0,
immediately return 0.
int check_num(int n){
int arr[20];
int count=0;
while(n>0){
arr[count++]=n%10;
n/=10;
}
for(int i=0;i<count/2;i++){
int temp=arr[i];
arr[i]=arr[count-i-1];
arr[count-i-1]=temp;
}
for(int i=0;i<count-1;i++){
if(arr[i]==0 || arr[i+1]%arr[i] !=0){
return 0;
}
}
return 1;
31. Given a positive integer n, examine its digits from left to right and
determine the length of the longest consecutive sequence (run) of the
same digit
int longestdigitrun(int n) {
int count=0;
int max_count=0;
int length=0;
int arr[20];
int rev_num=0;
while(n>0){
rev_num=rev_num*10+n%10;
n/=10;
}
while(rev_num !=0){
arr[length++]=rev_num%10;
rev_num/=10;
}
int prev=arr[0];
for(int i=0;i<length;i++){
if(arr[i]==prev){
count++;
}
else{
count=1;
prev=arr[i];
}
if(count>max_count){
max_count=count;
}
}
return max_count;
}
32. Given a number n, determine whether it is a rotational identity
number, i.e., it remains the same after rotating it 180 degrees.
Only the following digits are valid under 180-degree rotation:
0 → 0
1 → 1
6 → 9
8 → 8
9 → 6
Other digits (2, 3, 4, 5, and 7) are invalid — if any of them appear, the
number cannot be a rotational identity number.
The rotated number is formed by:
1. Flipping each digit (using the mapping above),
2. Reversing the order.
The number is a rotational identity number if it equals the rotated result
int checkNum(int n){
int origin_num=n;
int num;
int rot_num=0;
while(n>0){
int digit=n%10;
num=isrotational(digit);
if(num == -1)return 0;
if(num==0 || num==8 || num==1)
rot_num=rot_num*10+num;
if(num==6)
rot_num=rot_num*10+6;
if(num==9)
rot_num=rot_num*10+9;
n/=10;
}
return rot_num == origin_num;
}
int isrotational(int num){
switch(num){
case 0: return 0;
case 1: return 1;
case 6: return 9;
case 8: return 8;
case 9: return 6;
default: return -1;
}
}
33. Given a positive integer n, repeatedly sum its digits until the result is a
single-digit number. Count and return the number of times this summing
operation is performed. This count is known as the additive persistence of
the number.
int digitSeperate(int n){
int count=0;
while(n>=10){
n=additivepersistence(n);
count++;
}
return count;
}
int additivepersistence(int n){
int sum=0;
while(n>0){
sum+=n%10;
n/=10;
}
return sum;
}
34. Given a positive integer n, form two numbers by alternately picking
digits of a number from left to right and return their difference.
int alternatingdigitsubtractor(int n) {
int rev_num=0;
int length=0;
while(n>0){
rev_num=rev_num*10+n%10;
n/=10;
}
int arr[10];
while(rev_num>0){
arr[length++]=rev_num%10;
rev_num/=10;
}
int n1=0,n2=0;
for(int i=0;i<=length-1;i++){
if(i%2==0){
n1=n1*10+arr[i];
}
else{
n2=n2*10+arr[i];
}
}
return abs(n1-n2);
}
35. A number is called a step number if the absolute difference between
every two adjacent digits is exactly 1.
You are given a positive integer n. Write a program to determine
whether n is a step number or not. Return 1 if it is a step number,
otherwise return 0.
int isstepnumber(int n) {
int rev_num=0;
while(n>0){
rev_num=rev_num*10+n%10;
n/=10;
}
int digit=rev_num%10;
rev_num/=10;
while(rev_num>0){
int num=rev_num%10;
if(abs(num-digit)!=1)
return 0;
digit=num;
rev_num/=10;
}
return 1;}
36. Given a positive integer n, consider the 1-based position of each digit
starting from the leftmost digit.
You must sum all positions where the digit is equal to its position index.
Return this sum
int inversedigitpositionsum(int n) {
int arr[20];
int length=0;
int sum=0;
int rev_num=0;
while(n>0){
rev_num=rev_num*10+n%10;
n/=10;
}
while(rev_num>0){
arr[length++]=rev_num%10;
rev_num/=10;
}
for(int i=0;i<=length;i++){
if(arr[i]==i+1){
sum=sum+i+1;
}
}
return sum;
}