Techiez Online: C Programming Questions & Answers For Placements Basic To Advanced
Techiez Online: C Programming Questions & Answers For Placements Basic To Advanced
BASIC TO ADVANCED
A Product from
TechieZ Online
JOIN TELEGRAM CHANNEL - @TechieZ_Online 1|P a g e
JOIN TELEGRAM CHANNEL - @TechieZ_Online
#include <stdio.h>
int main()
{
int first, second, add, subtract, multiply;
float divide;
printf("Enter two integers\n");
scanf("%d%d", &first, &second);
add = first + second;
subtract = first - second;
multiply = first * second;
divide = first / (float)second; //typecasting
return 0;
}
#include <stdio.h>
int main()
{
float length, width, perimeter;
/*
* Input length and width of rectangle from user
*/
printf("Enter length of the rectangle: ");
scanf("%f", &length);
printf("Enter width of the rectangle: ");
scanf("%f", &width);
/* Calculate perimeter of rectangle */
perimeter = 2 * (length + width);
/* Print perimeter of rectangle */
printf("Perimeter of rectangle = %f units ", perimeter);
return 0;
}
#include <stdio.h>
int main()
{
float length, width, area;
/*
* Input length and width of rectangle
*/
printf("Enter length of rectangle: ");
scanf("%f", &length);
printf("Enter width of rectangle: ");
scanf("%d", &width);
/* Calculate area of rectangle */
area = length * width;
/* Print area of rectangle */
printf("Area of rectangle = %f sq. units ", area);
return 0;
}
return 0;
}
#include <stdio.h>
int main()
{
float celsius, fahrenheit;
#include <stdio.h>
#define DAYSINWEEK 7
void main()
{
int ndays, year, week, days;
#include <stdio.h>
int main()
{
int base, exponent;
long long power = 1;
int i;
/* Input base and exponent from user */
printf("Enter base: ");
scanf("%d", &base);
printf("Enter exponent: ");
scanf("%d", &exponent);
/* Multiply base, exponent times*/
for(i=1; i<=exponent; i++)
{
power = power * base;
}
printf("%d ^ %d = %lld", base, exponent, power);
return 0;
}
10. Write a C program to enter any number and calculate its square root.
#include <stdio.h>
#include <math.h>
int main()
{
double num, root;
/* Input a number from user */
printf("Enter any number to find square root: ");
scanf("%lf", &num);
/* Calculate square root of num */
root = sqrt(num);
/* Print the resultant value */
printf("Square root of %.2lf = %.2lf", num, root);
return 0;
}
11. Write a C program to enter two angles of a triangle and find the third
angle.
#include <stdio.h>
int main()
{
int a, b, c;
12. Write a C program to enter base and height of a triangle and find its
area.
#include <stdio.h>
int main()
{
float base, height, area;
/* Input base and height of triangle */
printf("Enter base of the triangle: ");
scanf("%f", &base);
printf("Enter height of the triangle: ");
scanf("%f", &height);
/* Calculate area of triangle */
area = (base * height) / 2;
/* Print the resultant area */
printf("Area of the triangle = %.2f sq. units", area);
return 0;
}
14. Write a C program to enter marks of five subjects and calculate total,
average and percentage.
#include <stdio.h>
int main()
{
float eng, phy, chem, math, comp;
float total, average, percentage;
#include <stdio.h>
int main()
{
int num1, num2, num3, max;
return 0;
}
i=1;
while(i<=end)
{
printf("%d\n", i);
i++;
}
return 0;
}
2. Write a C program to print all natural numbers in reverse (from n to 1).
- using while loop
#include <stdio.h>
int main()
{
int n;
printf("Enter value of n: ");
scanf("%d", &n);
while(n>=1)
{
printf("%d\n", n);
n--;
}
return 0;
}
return 0;
}
#include <stdio.h>
int main()
{
int i, n, sum=0;
return 0;
} }
6. Write a C program to print sum of all odd numbers between 1 to n.
#include <stdio.h>
int main()
{
int i, n, sum=0;
return 0;
}
#include <stdio.h>
int main()
{
int i, num;
return 0;
}
#include <stdio.h>
int main()
{
int n, lastDigit;
return 0;
}
#include <stdio.h>
int main()
{
int num, sum=0;
/* Input a number from user */
printf("Enter any number to find sum of its digit: ");
scanf("%d", &num);
/* Repeat till num becomes 0 */
while(num!=0)
{
/* Find last digit of num and add to sum */
sum += num % 10;
#include <stdio.h>
int main()
{
int num;
long long product=1;
/* Input number from user */
printf("Enter any number to calculate product of digit: ");
scanf("%d", &num);
product = (num == 0 ? 0 : 1);
/* Repeat the steps till num becomes 0 */
while(num != 0)
{
/* Get the last digit from num and multiplies to product */
product = product * (num % 10);
/* Remove the last digit from n */
num = num / 10;
}
printf("Product of digits = %lld", product);
return 0;
}
return 0; }
#include <stdio.h>
int main()
{
int i, start;
/* Input start range from user */
printf("Enter starting value: ");
scanf("%d", &start);
for(i=start; i>=1; i--)
{
printf("%d\n", i);
}
return 0;
}
14.Write a C program to enter any number and check whether the number
is palindrome or not.
#include <stdio.h>
int main()
{
int n, reversedInteger = 0, remainder, originalInteger;
printf("Enter an integer: ");
scanf("%d", &n);
originalInteger = n;
// reversed integer is stored in variable
while( n!=0 )
{
remainder = n%10;
reversedInteger = reversedInteger*10 + remainder;
n /= 10;
}
// palindrome if orignalInteger and reversedInteger are equal
if (originalInteger == reversedInteger)
printf("%d is a palindrome.", originalInteger);
else
printf("%d is not a palindrome.", originalInteger);
return 0;
}
#include <stdio.h>
int main()
{
int n, num = 0;
/* Input number from user */
printf("Enter any number to print in words: ");
scanf("%d", &n);
/* Store reverse of n in num */
while(n != 0)
{
num = (num * 10) + (n % 10);
n /= 10;
}
while(num != 0)
{
switch(num % 10)
{
case 0:
printf("Zero ");
break;
case 1:
printf("One ");
break;
case 2:
printf("Two ");
break;
case 3:
printf("Three ");
break;
case 4:
printf("Four ");
break;
case 5:
printf("Five ");
break;
case 6:
printf("Six ");
break;
case 7:
printf("Seven ");
break;
case 8:
printf("Eight ");
break;
case 9:
printf("Nine ");
break;
}
num = num / 10;
}
return 0;
}
#include <stdio.h>
int main()
{
int i;
/* Print ASCII values from 0 to 255 */
for(i=0; i<=255; i++)
{
printf("ASCII value of character %c = %d\n", i, i);
}
return 0;
}
18.Write a C program to enter any number and print all factors of the
number.
#include <stdio.h>
int main()
{
int i, num;
return 0;
}
int main()
{
int i, Number;
long Factorial = 1;
printf("\n Please Enter any number to Find Factorial\n");
scanf("%d", &Number);
for (i = 1; i <= Number; i++)
{
Factorial = Factorial * i;
}
printf("\nFactorial of %d = %d\n", Number, Factorial);
return 0;
}
#include <stdio.h>
int main()
{
int i, num1, num2, min, hcf=1;
/* Input two numbers from user */
printf("Enter any two numbers to find HCF: ");
scanf("%d%d", &num1, &num2);
/* Find minimum between two numbers */
min = (num1<num2) ? num1 : num2;
for(i=1; i<=min; i++)
{
/* If i is factor of both number */
if(num1%i==0 && num2%i==0)
{
hcf = i;
}
}
#include <stdio.h>
int main()
{
int n1, n2, minMultiple;
printf("Enter two positive integers: ");
scanf("%d %d", &n1, &n2);
while(1)
{
if( minMultiple%n1==0 && minMultiple%n2==0 )
{
printf("The LCM of %d and %d is %d.", n1, n2,minMultiple);
break;
}
++minMultiple;
}
return 0;
}
#include <stdio.h>
int main()
{
int low, high, i, flag;
printf("Enter two numbers(intervals): ");
scanf("%d %d", &low, &high);
printf("Prime numbers between %d and %d are: ", low, high
while (low < high)
{
flag = 0;
for(i = 2; i <= low/2; ++i)
{
if(low % i == 0)
{
flag = 1;
break;
}
}
if (flag == 0)
printf("%d ", low);
++low;
}
return 0;
}
#include <stdio.h>
int main()
{
int i, originalNum, num, lastDigit, sum;
long fact;
/* Input a number from user */
printf("Enter any number to check Strong number: ");
scanf("%d", &num);
/* Copy the value of num to a temporary variable */
originalNum = num;
sum = 0;
/* Find sum of factorial of digits */
while(num > 0)
{
/* Get last digit of num */
lastDigit = num % 10;
/* Find factorial of last digit */
fact = 1;
for(i=1; i<=lastDigit; i++)
{
fact = fact * i;
}
/* Add factorial to sum */
sum = sum + fact;
num = num / 10;
}
/* Check Strong number condition */
if(sum == originalNum)
{
printf("%d is STRONG NUMBER", originalNum);
}
else
{
printf("%d is NOT STRONG NUMBER", originalNum);
}
return 0;
}
#include <stdio.h>
int main()
{
int a, b, c, i, terms
/* Input number from user */
printf("Enter number of terms: ");
scanf("%d", &terms);
/* Fibonacci magic initialization */
a = 0;
b = 1;
c = 0;
#include <stdio.h>
#define MAX_SIZE 100
/* Function declaration */
void printArray(int arr[], int start, int len);
int main()
{
int arr[MAX_SIZE];
int N, i;
/* Input size and elements in array */
printf("Enter size of the array: ");
scanf("%d", &N);
printf("Enter elements in the array: ");
for(i=0; i<N; i++)
{
scanf("%d ", &arr[i]);
}
/* Prints array recursively */
printf("Elements in the array: ");
printArray(arr, 0, N);
return 0;
}
2. Write a C program to find sum of all array elements. - using recursion.
#include <stdio.h>
#define MAX_SIZE 100
int main()
{
int arr[MAX_SIZE];
int N, i, sumofarray;
/**
* Recursively find the sum of elements in an array.
*/
int sum(int arr[], int start, int len)
{
/* Recursion base condition */
if(start >= len)
return 0;
return (arr[start] + sum(arr, start + 1, len));
}
#include <stdio.h>
#define MAX_SIZE 100 // Maximum size of the array
/* Function declarations */
int maximum(int array[], int index, int len);
int minimum(int array[], int index, int len);
int main()
{
int array[MAX_SIZE], N, max, min;
int i;
return 0;
}
/**
* Recursive function to find maximum element in the given array.
*/
int maximum(int array[], int index, int len)
{
int max;
/*
* Only last and second last element are left
*/
if(index >= len-2)
{
if(array[index] > array[index + 1])
return array[index];
else
return array[index + 1];
}
/**
* Recursive function to find minimum element in the array.
*/
int minimum(int array[], int index, int len)
{
int min;
if(index >= len-2)
{
if(array[index] < array[index + 1])
return array[index];
else
return array[index + 1];
}
min = minimum(array, index + 1, len);
if(array[index] < min)
return array[index];
else
return min;
}
#include <stdio.h>
#include <limits.h> // For INT_MIN
#define MAX_SIZE 1000 // Maximum array size
int main()
{
int arr[MAX_SIZE], size, i;
int max1, max2;
/* Input size of the array */
printf("Enter size of the array (1-1000): ");
scanf("%d", &size);
/* Input array elements */
printf("Enter elements in the array: ");
for(i=0; i<size; i++)
{
scanf("%d", &arr[i]);
}
max1 = max2 = INT_MIN;
/*
* Check for first largest and second
*/
for(i=0; i<size; i++)
{
if(arr[i] > max1)
{
/*
* If current element of the array is first largest
* then make current max as second max
* and then max as current array element
*/
max2 = max1;
max1 = arr[i];
}
else if(arr[i] > max2 && arr[i] < max1)
{
/*
* If current array element is less than first largest
* but is greater than second largest then make it
* second largest
*/
max2 = arr[i];
}
}
printf("First largest = %d\n", max1);
printf("Second largest = %d", max2);
return 0;
}
#include <stdio.h>
#define MAX_SIZE 100
int main()4
{
int source[MAX_SIZE], dest[MAX_SIZE];
int i, size;
{
scanf("%d", &source[i]);
}6666
/*
* Copy all elements from source array to dest array
*/
for(i=0; i<size; i++)
{
dest[i] = source[i];
}
/*
* Print all elements of source array
*/
printf("\nElements of source array are : ");
for(i=0; i<size; i++)
{
printf("%d\t", source[i]);
}
/*
* Print all elements of dest array
*/
printf("\nElements of dest array are : ");
for(i=0; i<size; i++)
{
printf("%d\t", dest[i]);
}
return 0;
}
Learn how to copy array elements using pointers.
Output
Enter the size of the array : 10
Enter elements of source array : 10 20 30 40 50 60 70 80 90 100
#include <stdio.h>
#define MAX_SIZE 100
int main()
{
int arr[MAX_SIZE];
int i, size, num, pos;
/* Input size of the array */
printf("Enter size of the array : ");
scanf("%d", &size);
/* Input elements in array */
printf("Enter elements in array : ");
for(i=0; i<size; i++)
{
scanf("%d", &arr[i]);
}
/* Input new element and position to insert */
printf("Enter element to insert : ");
scanf("%d", &num);
printf("Enter the element position : ");
scanf("%d", &pos);
/* If position of element is not valid */
if(pos > size+1 || pos <= 0)
{
printf("Invalid position! Please enter position between 1 to %d",
size);
}
else
{
/* Make room for new array element by shifting to right */
for(i=size; i>=pos; i--)
{
arr[i] = arr[i-1];
}
/* Insert new element at given position and increment size */
arr[pos-1] = num;
size++;
/* Print array after insert operation */
printf("Array elements after insertion : ");
for(i=0; i<size; i++)
{
printf("%d\t", arr[i]);
}
}
return 0;
}
7. Write a C program to delete an element from an array at specified
position.
#include <stdio.h>
#define MAX_SIZE 100
int main()
{
int arr[MAX_SIZE];
int i, size, pos;
else
{
/* Copy next element value to current element */
for(i=pos-1; i<size-1; i++)
{
arr[i] = arr[i + 1];
}
#include <stdio.h>
#define MAX_SIZE 100
int main()
{
int arr[MAX_SIZE], freq[MAX_SIZE];
int size, i, j, count;
if(freq[i] != 0)
{
freq[i] = count;
}
}
return 0;
}
Output
#include <stdio.h>
int main()
{
return 0;
} }
10.Write a C program to count total number of even and odd elements in
an array.
#include <stdio.h>
#define MAX_SIZE 100 //Maximum size of the array
int main()
{
int arr[MAX_SIZE];
int i, size, even, odd;
/* Input size of the array */
printf("Enter size of the array: ");
scanf("%d", &size);
/* Input array elements */
printf("Enter %d elements in array: ", size);
for(i=0; i<size; i++)
{
scanf("%d", &arr[i]);
}
/* Assuming that there are 0 even and odd elements */
even = 0;
odd = 0;
#include <stdio.h>
#define MAX_SIZE 100 // Maximum array size
int main()
{
int arr[MAX_SIZE]; // Declares array of size 100
int i, size, count = 0;
{
scanf("%d", &arr[i]);
}
/*
* Count total negative elements in array
*/
for(i=0; i<size; i++)
{
/* Increment count if current array element is negative */
if(arr[i] < 0)
{
count++;
}
}
printf("\nTotal negative elements in array = %d", count);
return 0;
}
Output
Enter size of the array : 10
Enter elements in array : 10 -2 5 -20 1 50 60 -50 -12 -9
#include <stdio.h>
#define MAX_SIZE 100 // Maximum array size
int main()
{
int arr[MAX_SIZE];
int i, j, size, count = 0;
/* Input size of array */
printf("Enter size of the array : ");
scanf("%d", &size);
/* Input elements in array */
printf("Enter elements in array : ");
for(i=0; i<size; i++)
{
scanf("%d", &arr[i]);
/*
* Find all duplicate elements in array
*/
for(i=0; i<size; i++)
{
for(j=i+1; j<size; j++)
{
/* If duplicate found then increment count by 1 */
if(arr[i] == arr[j])
{
count++;
break;
}
}
}
printf("\nTotal number of duplicate elements found in array = %d",
count);
return 0;
}
Output
Enter size of the array : 10
Enter elements in array : 1 10 20 1 25 1 10 30 25 1
#include <stdio.h>
#define MAX_SIZE 100 // Maximum size of the array
int main()
{
int arr[MAX_SIZE]; // Declares an array of size 100
int size; // Total number of elements in array
int i, j, k; // Loop control variables
/* Input size of the array */
printf("Enter size of the array : ");
scanf("%d", &size);
/*
* Find duplicate elements in array
*/
for(i=0; i<size; i++)
{
for(j=i+1; j<size; j++)
{
/* If any duplicate found */
if(arr[i] == arr[j])
{
/* Delete the current duplicate element */
for(k=j; k<size; k++)
{
arr[k] = arr[k + 1];
}
/*
* Print array after deleting duplicate elements
*/
printf("\nArray elements after deleting duplicates : ");
for(i=0; i<size; i++)
{
printf("%d\t", arr[i]);
return 0;
}
Output
Enter size of the array : 10
Enter elements in array : 10 20 10 1 100 10 2 1 5 10
#include <stdio.h>
int main()
{
int arr[100], freq[100];
int size, i, j, count;
/* Input size of array */
printf("Enter size of array: ");
scanf("%d", &size);
count++;
/* Make sure not to count frequency of same element again
*/
freq[j] = 0;
}
}
/*
* Print frequency of each element
*/
printf("\nFrequency of all elements of array : \n");
for(i=0; i<size; i++)
{
if(freq[i] != 0)
{
printf("%d occurs %d times\n", arr[i], freq[i]);
}
}
return 0;
}
Output
Enter size of array: 10
Enter elements in array: 5 10 2 5 50 5 10 1 2 2
#include <stdio.h>
#define MAX_SIZE 100 // Maximum size of the array
int main()
{
int arr1[MAX_SIZE], arr2[MAX_SIZE], mergeArray[MAX_SIZE
* 2];
int size1, size2, mergeSize;
int index1, index2, mergeIndex;
int i;
/* Input size of first array */
printf("Enter the size of first array : ");
scanf("%d", &size1);
/* Input elements in first array */
printf("Enter elements in first array : ");
for(i=0; i<size1; i++)
{
scanf("%d", &arr1[i]);
}
/* Input size of second array */
printf("\nEnter the size of second array : ");
scanf("%d", &size2);
/* Input elements in second array */
printf("Enter elements in second array : ");
for(i=0; i<size2; i++)
{
scanf("%d", &arr2[i]);
}
/*
* Merge two array in ascending order
*/
index1 = 0;
index2 = 0;
for(mergeIndex=0; mergeIndex < mergeSize; mergeIndex++)
{
/*
* If all elements of one array
return 0;
}
16.Write a C program to find reverse of an array.
#include <stdio.h>
#define MAX_SIZE 100 // Defines maximum size of array
int main()
{
int arr[MAX_SIZE];
int size, i;
/*
* Print array in reversed order
*/
printf("\nArray in reverse order: ");
for(i = size-1; i>=0; i--)
{
printf("%d\t", arr[i]);
}
return 0;
}
#include <stdio.h>
int main()
{
int arr[MAX_SIZE];
int size, i, toSearch, found;
/*
* If element is not found in array
*/
if(found == 1)
{
printf("\n%d is found at position %d", toSearch, i + 1);
}
else
{
printf("\n%d is not found in the array", toSearch);
}
return 0;
} }
18.Write a C program to sort array elements in ascending order.
#include <stdio.h>
#define MAX_SIZE 100 // Maximum array size
int main()
{
int arr[MAX_SIZE];
int size;
int i, j, temp;
return 0;
}
19.Write a C program to sort array elements in descending order
#include <stdio.h>
int main()
{
int num = 10;
printf("Value of num = %d\n", num);
/* &num gets the address of num. */
printf("Address of num = %d\n", &num);
printf("Address of num in hexadecimal = %x", &num);
return 0;
}
2. Program to swap two numbers using pointers.
#include <stdio.h>
// function to swap the two numbers
int main()
{
const int a=10; //declare and assign constant integer
int *p; //declare integer pointer
p=&a; //assign address into pointer p
return 0;
}
#include <stdio.h>
int main()
{
char str[100];
char *p;
int vCount=0,cCount=0;
int main()
{
int arr[10]; //declare integer array
int *pa; //declare an integer pointer
int i;
printf("%08X\t%03d\n",(pa+i),*(pa+i));
}
return 0;
}