Question Bank › Technical › C › Coding Questions
Write a C program that reads an integer from the user and prints its digits in reverse order. For example, if the user enters 4726, the program should print 6274.
#include <stdio.h>
int main()
{
int num, reverse = 0, digit;
printf("Enter a number to reverse: ");
scanf("%d", &num);
while (num != 0)
{
digit = num % 10;
reverse = reverse * 10 + digit;
num = num / 10;
}
printf("Reverse of entered number is = %d\n", reverse);
return 0;
}Write a C program that reads an integer and prints its digits in reverse order (for example, input 4729 should print 9274).
#include<stdio.h>
int main()
{
int n, reverse = 0, rem;
printf("Enter a number: ");
scanf("%d", &n);
while(n != 0)
{
rem = n % 10;
reverse = reverse * 10 + rem;
n /= 10;
}
printf("Reversed Number: %d", reverse);
return 0;
}Write a C program that swaps two integers entered by the user without using a third variable, and prints the values before and after swapping.
#include<stdio.h>
int main()
{
int x, y;
printf("Enter two numbers: ");
scanf("%d %d", &x, &y);
printf("Before swap x=%d y=%d", x, y);
x = x + y;
y = x - y;
x = x - y;
printf("\nAfter swap x=%d y=%d", x, y);
return 0;
}Write a C program that prints "Jai Hind" without using any semicolon in the program, using an if statement.
#include <stdio.h>
int main()
{
if(printf("Jai Hind"))
{
}
}Write a C program that reads a positive integer and prints the sum of all its digits. For example, if the number is 4729, the output should be 22.
#include <stdio.h>
int main()
{
int num, r, s = 0;
printf("Enter a positive integer: ");
scanf("%d", &num);
while(num != 0)
{
r = num % 10;
s = s + r;
num = num / 10;
}
printf("The sum of the digits is : %d\n", s);
return 0;
}Write a C program that reads n integers into an array and prints the sum of the two smallest numbers in the array. For example, if the input array is 4 8 6 2 10, the two smallest numbers are 2 and 4, so the output should be 6.
#include <stdio.h>
int main()
{
int a[50], n, i, j, temp, sum;
printf("Enter array size: ");
scanf("%d", &n);
printf("Enter %d elements: ", n);
for(i = 0; i < n; i++)
scanf("%d", &a[i]);
for(i = 0; i < n - 1; i++)
{
for(j = i + 1; j < n; j++)
{
if(a[i] > a[j])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
sum = a[0] + a[1];
printf("Sum of minimum two elements is : %d\n", sum);
return 0;
}Write a C program that reads two arrays of integers from the user and prints the union of the two arrays, i.e. all distinct elements appearing in either array, without duplicates. Print the elements of the first array first, then any elements of the second array that are not already present. For example, if the first array is 2 4 6 8 and the second array is 8 10 12, the output should be 2, 4, 6, 8, 10, 12.
#include <stdio.h>
int main()
{
int a[20], b[20], n1, n2, i, j, found;
printf("Enter size of first array: ");
scanf("%d", &n1);
printf("Enter elements of first array: ");
for (i = 0; i < n1; i++)
scanf("%d", &a[i]);
printf("Enter size of second array: ");
scanf("%d", &n2);
printf("Enter elements of second array: ");
for (i = 0; i < n2; i++)
scanf("%d", &b[i]);
printf("Union: ");
for (i = 0; i < n1; i++)
printf("%d, ", a[i]);
for (i = 0; i < n2; i++)
{
found = 0;
for (j = 0; j < n1; j++)
{
if (b[i] == a[j])
{
found = 1;
break;
}
}
if (!found)
printf("%d, ", b[i]);
}
printf("\n");
return 0;
}Write a C program that multiplies two non-negative integers WITHOUT using the multiplication operator (*). Read two integers from the user and print their product by repeated addition.
#include <stdio.h>
int main()
{
int m, n, product = 0;
printf("Enter two integers: ");
scanf("%d %d", &m, &n);
while (n > 0)
{
product = product + m;
n--;
}
printf("Product is: %d\n", product);
return 0;
}Write a C program to print all perfect cube numbers up to 100 (i.e., 1, 8, 27, 64).
#include <stdio.h>
int main()
{
int n = 1;
while (n * n * n < 100)
{
printf("%4d ", n * n * n);
n++;
}
return 0;
}Write a C program that reads the order of two matrices (up to 10 x 10), reads the elements of both matrices, and then prints their sum matrix. The addition should be computed and printed directly (a[i][j] + b[i][j]) without storing the result in a third matrix.
#include <stdio.h>
int main()
{
int i, j, rows, cols, a[10][10], b[10][10];
printf("Enter the order of matrix up to (10 x 10):\n");
scanf("%d %d", &rows, &cols);
printf("\nEnter the elements of matrix A:\n");
for(i = 0; i < rows; i++)
for(j = 0; j < cols; j++)
scanf("%d", &a[i][j]);
printf("\nEnter the elements of matrix B:\n");
for(i = 0; i < rows; i++)
for(j = 0; j < cols; j++)
scanf("%d", &b[i][j]);
printf("\nMatrix Addition:\n");
for(i = 0; i < rows; i++)
{
for(j = 0; j < cols; j++)
printf("%4d", a[i][j] + b[i][j]);
printf("\n");
}
return 0;
}Write a C program to print the following alphabet pattern using ASCII values (A = 65, B = 66, ...). The first line prints A, the second line prints B A, the third line prints C B A, and so on up to the line that prints E D C B A: A B A C B A D C B A E D C B A
#include <stdio.h>
int main()
{
int row, ch;
for(row = 65; row <= 69; row++)
{
for(ch = row; ch >= 65; ch--)
{
printf("%c ", ch);
}
printf("\n");
}
return 0;
}Write a C program that checks whether a given number is an Armstrong number. An Armstrong number (for 3-digit numbers) is one whose value equals the sum of the cubes of its digits. For example, 370 is an Armstrong number because 3³ + 7³ + 0³ = 27 + 343 + 0 = 370. Read an integer from the user and print whether it is an Armstrong number or not.
#include <stdio.h>
int main()
{
int n, original, digit, sum = 0;
printf("Enter a number : ");
scanf("%d", &n);
original = n;
while (n > 0)
{
digit = n % 10;
sum = sum + (digit * digit * digit);
n = n / 10;
}
if (sum == original)
printf("\n%d is an armstrong number", original);
else
printf("\n%d is not an armstrong number", original);
return 0;
}Write a C program that reads n integers from the user and prints them in ascending order.
#include <stdio.h>
int main()
{
int i, j, n, a[50], temp;
printf("Enter how many numbers : ");
scanf("%d", &n);
printf("Enter %d numbers : ", n);
for (i = 0; i < n; i++)
scanf("%d", &a[i]);
for (i = 0; i < n - 1; i++)
{
for (j = i + 1; j < n; j++)
{
if (a[i] > a[j])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
printf("----Ascending Order----\n");
for (i = 0; i < n; i++)
printf("%d ", a[i]);
return 0;
}Write a C program that reads a limit n from the user, then reads n integers one by one, and finally prints the biggest of those n numbers. Use a loop to read the numbers and keep track of the largest value seen so far.
#include <stdio.h>
int main()
{
int i, limit, num, biggest;
printf("Enter the limit : ");
scanf("%d", &limit);
printf("\nEnter %d numbers\n", limit);
for(i = 0; i < limit; i++)
{
printf("\nEnter number %d : ", i + 1);
scanf("%d", &num);
if(i == 0 || num > biggest)
biggest = num;
}
printf("The biggest number is %d", biggest);
return 0;
}Write a C program to check whether a number entered by the user is a palindrome or not. A palindrome number reads the same forwards and backwards. For example, 121, 1331, and 12321 are palindromes.
#include <stdio.h>
int main() {
int num, original, reversed = 0, remainder;
printf("Enter a number: ");
scanf("%d", &num);
original = num;
while (num != 0) {
remainder = num % 10;
reversed = reversed * 10 + remainder;
num /= 10;
}
if (original == reversed)
printf("%d is a palindrome number.\n", original);
else
printf("%d is not a palindrome number.\n", original);
return 0;
}Write a C program that demonstrates that once an identifier is declared with a data type, it cannot be redeclared with the same data type again. Declare an int variable named marks with value 72, then attempt to redeclare another variable named Marks (same name with different case) and also try to redeclare Marks as an int with value 88.5. Print both values using %d.
#include <stdio.h>
int main()
{
int marks = 72;
int Marks;
int Marks = 88.5;
printf("The marks are %d ", marks);
printf("\nThe Marks are %d ", Marks);
return 0;
}Write a C program that reads a text file named "source.txt" character by character and writes its entire content twice (i.e., the content repeated back-to-back) into another file named "target.txt".
#include <stdio.h>
int main(void) {
FILE *in, *out;
int ch;
in = fopen("source.txt", "r");
if (in == NULL) {
printf("Could not open source.txt\n");
return 1;
}
out = fopen("target.txt", "w");
if (out == NULL) {
printf("Could not open target.txt\n");
fclose(in);
return 1;
}
while ((ch = fgetc(in)) != EOF) {
fputc(ch, out);
}
rewind(in);
while ((ch = fgetc(in)) != EOF) {
fputc(ch, out);
}
fclose(in);
fclose(out);
printf("Content written twice to target.txt\n");
return 0;
}
Write a C program that reads an integer stored in a file named "num.txt" and replaces the file's content with double that integer. For example, if the file contains 7, after running the program the file should contain 14.
#include <stdio.h>
int main(void) {
FILE *fp;
int n;
fp = fopen("num.txt", "r");
if (fp == NULL) {
printf("Cannot open file\n");
return 1;
}
fscanf(fp, "%d", &n);
fclose(fp);
n = n * 2;
fp = fopen("num.txt", "w");
if (fp == NULL) {
printf("Cannot open file\n");
return 1;
}
fprintf(fp, "%d", n);
fclose(fp);
printf("New value: %d\n", n);
return 0;
}In C, a student opens a file using fopen with mode "r" and later opens another file with mode "rb". Explain the difference between these two modes, and write a short C program that opens a file named "marks.txt" in "r" mode and a file named "photo.dat" in "rb" mode, printing an appropriate message for each based on whether fopen succeeds or fails.
#include <stdio.h>
int main(void) {
FILE *textFile = fopen("marks.txt", "r");
if (textFile == NULL) {
printf("Could not open marks.txt in r mode\n");
} else {
printf("marks.txt opened in r mode (text read)\n");
fclose(textFile);
}
FILE *binFile = fopen("photo.dat", "rb");
if (binFile == NULL) {
printf("Could not open photo.dat in rb mode\n");
} else {
printf("photo.dat opened in rb mode (binary read)\n");
fclose(binFile);
}
return 0;
}Write a C program to implement a simple Hangman game. The program should have a secret word (e.g., "india") and allow the user to guess letters one by one. It should display the current state of the word with dashes for unguessed letters. The user gets a limited number of wrong guesses (e.g., 5). The game ends when the word is guessed or the user runs out of attempts.
#include <stdio.h>
#include <string.h>
int main() {
char word[] = "india";
int len = strlen(word);
char guessed[20];
int i, attempts = 5, correct = 0;
char ch;
for (i = 0; i < len; i++)
guessed[i] = '_';
guessed[len] = '\0';
printf("Welcome to Hangman!\n");
printf("Guess the word: %s\n", guessed);
while (attempts > 0 && correct < len) {
printf("\nEnter a letter: ");
scanf(" %c", &ch);
int found = 0;
for (i = 0; i < len; i++) {
if (word[i] == ch && guessed[i] == '_') {
guessed[i] = ch;
correct++;
found = 1;
}
}
if (found)
printf("Good guess!\n");
else {
attempts--;
printf("Wrong guess! Attempts left: %d\n", attempts);
}
printf("Current word: %s\n", guessed);
}
if (correct == len)
printf("\nCongratulations! You guessed the word: %s\n", word);
else
printf("\nSorry, you ran out of attempts. The word was: %s\n", word);
return 0;
}Write a C program to find the intersection of two arrays. The program should read the size and elements of two arrays and print the common elements (each common element printed once, in the order they appear in the first array).
#include <stdio.h>
int main() {
int a[100], b[100], n1, n2, i, j, k, found;
printf("Enter size of first array: ");
scanf("%d", &n1);
printf("Enter elements of first array: ");
for (i = 0; i < n1; i++)
scanf("%d", &a[i]);
printf("Enter size of second array: ");
scanf("%d", &n2);
printf("Enter elements of second array: ");
for (i = 0; i < n2; i++)
scanf("%d", &b[i]);
printf("Intersection: ");
for (i = 0; i < n1; i++) {
found = 0;
for (j = 0; j < n2; j++) {
if (a[i] == b[j]) {
found = 1;
break;
}
}
if (found) {
// check if already printed
int already = 0;
for (k = 0; k < i; k++) {
if (a[k] == a[i]) {
already = 1;
break;
}
}
if (!already)
printf("%d ", a[i]);
}
}
printf("\n");
return 0;
}Write a C program that reads two matrices from the user and prints their product. First read the order of matrix A (rows and columns), then its elements; then read the order of matrix B and its elements. If the number of columns of A is not equal to the number of rows of B, print 'Multiplication not possible' and stop. Otherwise print the product matrix, one row per line with elements separated by spaces.
#include <stdio.h>
int main()
{
int a[10][10], b[10][10], prod[10][10];
int r1, c1, r2, c2, i, j, k;
printf("Enter order of matrix A: ");
scanf("%d%d", &r1, &c1);
printf("Enter elements of matrix A:\n");
for(i = 0; i < r1; i++)
for(j = 0; j < c1; j++)
scanf("%d", &a[i][j]);
printf("Enter order of matrix B: ");
scanf("%d%d", &r2, &c2);
if(c1 != r2)
{
printf("Multiplication not possible\n");
return 0;
}
printf("Enter elements of matrix B:\n");
for(i = 0; i < r2; i++)
for(j = 0; j < c2; j++)
scanf("%d", &b[i][j]);
for(i = 0; i < r1; i++)
{
for(j = 0; j < c2; j++)
{
prod[i][j] = 0;
for(k = 0; k < c1; k++)
prod[i][j] += a[i][k] * b[k][j];
printf("%d ", prod[i][j]);
}
printf("\n");
}
return 0;
}Write a C program to print the following number pattern using nested for loops, where every leading blank position is shown as 0: 00001 00021 00321 04321 54321 Hint: use three for loops — the outer loop controls the lines, one inner loop prints the 0s, and another inner loop prints the descending numbers.
#include <stdio.h>
int main()
{
int a, i, j;
for(a = 1; a <= 5; a++)
{
for(i = 5; i > a; i--)
{
printf("0");
}
for(j = a; j >= 1; j--)
{
printf("%d", j);
}
printf("\n");
}
return 0;
}Write a C program to print the following number pattern using nested for loops: 55555 45555 34555 23455 12345 Hint: use three for loops — the outer loop controls the number of lines, one inner loop prints the leading numbers, and another inner loop prints the trailing 5s.
#include <stdio.h>
int main()
{
int a, i, j;
for(a = 5; a >= 1; a--)
{
for(i = a; i <= 5; i++)
{
printf("%d", i);
}
for(j = a - 1; j >= 1; j--)
{
printf("5");
}
printf("\n");
}
return 0;
}Write a C program that reads a username and a password from the user, but masks the password with asterisks (*) as it is typed. The program should then display the entered password (for demonstration). Use getch() for character input without echo. Assume the maximum length for username and password is 20 characters.
#include <stdio.h>
#include <conio.h>
int main() {
char username[21], password[21];
int ch, i = 0;
printf("Username: ");
scanf("%20s", username);
printf("Password: ");
while (i < 20 && (ch = getch()) != '\r' && ch != '\n') {
password[i++] = (char)ch;
printf("*");
}
password[i] = '\0';
printf("\nYour password is: %s\n", password);
return 0;
}Write a C program that asks the user to enter a number and prints whether it is a perfect number or not. A perfect number equals the sum of its proper divisors (for example, 6 = 1+2+3 and 28 = 1+2+4+7+14).
#include <stdio.h>
int main()
{
int num, i, sum;
printf("Enter a number: ");
scanf("%d", &num);
sum = 0;
for (i = 1; i < num; i++)
{
if (num % i == 0)
{
sum = sum + i;
}
}
if (sum == num)
{
printf("%d is a perfect number\n", num);
}
else
{
printf("%d is not a perfect number\n", num);
}
return 0;
}Write a C program to print "Namaste" without using a semicolon anywhere in the code.
#include <stdio.h>
int main() {
if (printf("Namaste")) {}
}Write a C program that adds two integers 15 and 25 using inline assembly code and prints the result.
#include<stdio.h>
int main() {
int a = 15, b = 25, c;
__asm__ __volatile__(
"addl %%ebx, %%eax"
: "=a"(c)
: "a"(a), "b"(b)
);
printf("c= %d", c);
return 0;
}Write a C program that prints "Namaste" to the screen without defining a function named main(). Use a preprocessor macro to rename the entry function.
#include<stdio.h>
#define begin main
void begin() {
printf("Namaste");
}Write a C program that reads the order of two square matrices (number of rows and columns), reads both matrices from the user, and prints their matrix product. For example, multiplying [[1,2],[3,4]] by [[5,6],[7,8]] should print 19 22 on the first row and 43 50 on the second row.
#include <stdio.h>
int main() {
int a[10][10], b[10][10], prod[10][10], r, c, i, j, k;
printf("Enter the number of rows = ");
scanf("%d", &r);
printf("Enter the number of columns = ");
scanf("%d", &c);
printf("Enter the elements of the first matrix:\n");
for (i = 0; i < r; i++)
for (j = 0; j < c; j++)
scanf("%d", &a[i][j]);
printf("Enter the elements of the second matrix:\n");
for (i = 0; i < r; i++)
for (j = 0; j < c; j++)
scanf("%d", &b[i][j]);
for (i = 0; i < r; i++) {
for (j = 0; j < c; j++) {
prod[i][j] = 0;
for (k = 0; k < c; k++)
prod[i][j] += a[i][k] * b[k][j];
}
}
printf("Product of the matrices =\n");
for (i = 0; i < r; i++) {
for (j = 0; j < c; j++)
printf("%d\t", prod[i][j]);
printf("\n");
}
return 0;
}Write a C program that reads a limit n from the user and prints a Fibonacci triangle with n rows. Row 1 starts with 1; each following row starts again from 1 and contains one more Fibonacci number than the previous row. For n = 4 the output should be: 1 1 1 1 1 2 1 1 2 3
#include <stdio.h>
int main() {
int a, b, c, i, j, n;
printf("Enter the limit: ");
scanf("%d", &n);
for (i = 1; i <= n; i++) {
a = 0;
b = 1;
printf("%d\t", b);
for (j = 1; j < i; j++) {
c = a + b;
printf("%d\t", c);
a = b;
b = c;
}
printf("\n");
}
return 0;
}Write a C program that reads a positive integer entered by the user and prints each of its digits as an English word, in the same order as the digits appear. For example, if the user enters 4072, the program should print: four zero seven two
#include <stdio.h>
int main() {
long int num;
int digits[20], n = 0, i;
const char *words[] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
printf("Enter the number: ");
scanf("%ld", &num);
if (num == 0) {
digits[n++] = 0;
}
while (num > 0) {
digits[n++] = num % 10;
num = num / 10;
}
for (i = n - 1; i >= 0; i--) {
printf("%s ", words[digits[i]]);
}
printf("\n");
return 0;
}Write a C program that reads an integer n and prints an X-shaped star pattern of size (2n-1) rows and (2n-1) columns. Stars are printed where the row index equals the column index or where the column index equals (2n - row index). For example, if n = 3 the output is a 5x5 X made of stars.
#include <stdio.h>
int main(void) {
int n, m;
printf("Enter the number: ");
scanf("%d", &n);
m = 2 * n - 1;
for (int i = 1; i <= m; i++) {
for (int j = 1; j <= m; j++) {
if (i == j || j == (m - i + 1)) {
printf("*");
} else {
printf(" ");
}
}
printf("\n");
}
return 0;
}Write a C program that reads a string from the user and sorts its characters in alphabetical order (case-insensitive). For example, if the input is "Mumbai", the output should be "abimMu"? Actually, the sorting should be case-insensitive, so 'M' and 'm' are considered the same letter, but the original case is preserved in the output. For input "Mumbai", the sorted characters alphabetically (case-insensitive) would be: a, b, i, M, m, u? Wait, let's sort: letters: M, u, m, b, a, i. Case-insensitive order: a, b, i, M, m, u. But 'M' and 'm' are the same letter, so their relative order? Typically, we sort by the letter ignoring case, and if same letter, we can keep original order or sort by case. The original program used a loop from 'A' to 'Z' and checked both cases, so it would output all 'a's and 'A's first (in the order they appear in the string), then 'b's and 'B's, etc. So for "Mumbai", the output would be: a, b, i, M, m, u? Let's trace: i=65 ('A'): check each char: M (toupper=77) no, u (85) no, m (77) no, b (66) no, a (65) yes -> store 'a'; i (105) no. So store 'a'. i=66 ('B'): check: M no, u no, m no, b (66) yes -> store 'b'; a no; i no. store 'b'. i=67 ('C'): none. ... i=73 ('I'): check: M no, u no, m no, b no, a no, i (105) toupper=73 yes -> store 'i'. i=77 ('M'): check: M (77) yes -> store 'M'; u no; m (77) yes -> store 'm'; b no; a no; i no. So store 'M' then 'm'. i=85 ('U'): check: M no; u (85) yes -> store 'u'; m no; b no; a no; i no. So final store: a, b, i, M, m, u. That is "abimMu"? Actually concatenated: a b i M m u -> "abiMmu". But the original sample output was for a different input? The original sample output was 'aelpp' for some input? Not sure. For our example, output should be "abiMmu". But the question asks to write a program, not to compute output. So we need to provide a complete C program that does this sorting. We'll write a program that reads a string, sorts its characters in alphabetical order (case-insensitive), and prints the sorted string. We'll use the same logic as the original: iterate over 'A' to 'Z', and for each letter, scan the string and collect all characters that match that letter in either case, preserving their original order. Then print the collected characters.#include <stdio.h>
#include <ctype.h>
#include <string.h>
int main()
{
char text[100], sorted[100];
int i, j, len, k = 0;
printf("Enter a string: ");
fgets(text, sizeof(text), stdin);
text[strcspn(text, "\n")] = '\0'; // remove newline
len = strlen(text);
for (i = 'A'; i <= 'Z'; i++)
{
for (j = 0; j < len; j++)
{
if (toupper(text[j]) == i)
{
sorted[k++] = text[j];
}
}
}
sorted[k] = '\0';
printf("Sorted characters alphabetically: %s\n", sorted);
return 0;
}Write a C program that prints "Namaste India" to the console without using any semicolon in the source code. Use an if statement as part of the trick.
#include <stdio.h>
int main()
{
if (printf("Namaste India"))
{
}
}Write a C program that prints "Namaste India" without using any semicolon in the program, using a while loop.
#include <stdio.h>
int main()
{
while(!printf("Namaste India"))
{
}
}Write a C program that prints "Jai Hind" without using any semicolon in the source code. Use a while loop with a negated condition to achieve this.
#include <stdio.h>
int main()
{
while(!printf("Jai Hind"))
{
}
}Write a C program that prints "Namaste India" without using any semicolon in the source code. Use a switch statement to achieve this.
#include <stdio.h>
int main()
{
switch(printf("Namaste India"))
{
}
}Write a C program that reads two integers from the user and prints their sum WITHOUT using the + operator. Use bitwise operations: the XOR of the two numbers gives the sum without carries, and the AND shifted left by one gives the carry, which is added repeatedly until the carry becomes zero. For example, if the user enters 4 and 7, the output should be: Addition of two numbers is : 11
#include <stdio.h>
int add(int, int);
int main()
{
int num1, num2;
printf("Enter any two integers : ");
scanf("%d%d", &num1, &num2);
printf("Addition of two numbers is : %d", add(num1, num2));
return 0;
}
int add(int num1, int num2)
{
if (!num2)
return num1;
else
return add((num1 & num2) << 1, num1 ^ num2);
}Write a C program that reads the order (rows and columns) of a matrix from the user, reads all its elements, and prints the transpose of that matrix. For example, if the user enters a 2 x 3 matrix with elements 1 2 3 / 4 5 6, the program should print the 3 x 2 transpose: 1 4 / 2 5 / 3 6.
#include <stdio.h>
int main()
{
int i, j, rows, cols, a[10][10], t[10][10];
printf("Enter the order of the matrix (rows cols, up to 10 x 10):\n");
scanf("%d %d", &rows, &cols);
printf("Enter the elements of the matrix:\n");
for (i = 0; i < rows; i++)
for (j = 0; j < cols; j++)
scanf("%d", &a[i][j]);
for (i = 0; i < rows; i++)
for (j = 0; j < cols; j++)
t[j][i] = a[i][j];
printf("The transpose of the matrix is:\n");
for (i = 0; i < cols; i++)
{
for (j = 0; j < rows; j++)
printf("%4d", t[i][j]);
printf("\n");
}
return 0;
}Write a C program that divides two positive integers without using the division operator (/) or the modulus operator (%). Read two integers from the user and print the integer quotient. For example, if the user enters 18 and 7, the program should print 2.
#include <stdio.h>
int divideWithoutOperator(int dividend, int divisor)
{
int quotient = 0;
int temp = 1;
while (divisor <= dividend)
{
divisor <<= 1;
temp <<= 1;
}
while (temp > 1)
{
divisor >>= 1;
temp >>= 1;
if (dividend >= divisor)
{
dividend -= divisor;
quotient += temp;
}
}
return quotient;
}
int main()
{
int a, b;
printf("Enter two positive integers: ");
scanf("%d %d", &a, &b);
printf("Quotient is: %d\n", divideWithoutOperator(a, b));
return 0;
}Write a C program that subtracts one integer from another WITHOUT using the subtraction operator (-). Read two integers from the user and print their difference (first minus second) using bitwise operations.
#include <stdio.h>
int subtract(int a, int b)
{
int borrow;
while (b != 0)
{
borrow = (~a) & b;
a = a ^ b;
b = borrow << 1;
}
return a;
}
int main()
{
int x, y;
printf("Enter two integers: ");
scanf("%d %d", &x, &y);
printf("Difference is: %d\n", subtract(x, y));
return 0;
}INTMAT EDTECH (OPC) PRIVATE LIMITED
No - 16, Javaraiahna Doddi, Mysore Road,
Bengaluru Urban, BENGALURU,
Karnataka 560098, India
contact@intmatedtech.com