C Programs 17 To 43
C Programs 17 To 43
#include <stdio.h>
int main() {
// Declare variables for price and quantity of both items
float price1, quantity1, price2, quantity2;
float total_cost1, total_cost2, total_items, average_value;
return 0;
}
18. Write a C program to swap two variables using a third variable
and take input from the user.
#include <stdio.h>
int main() {
// Declare three variables
float a, b, temp;
return 0;
}
19. Write a program to print the power of a given number using for
loop.
#include <stdio.h>
int main() {
int base, exponent;
long long result = 1; // Initialize result to 1
return 0;
}
20. Write a program to print equilateral triangle or pyramid.(star
pattern)
*
*
***
***
#include <stdio.h>
int main() {
int i, j, rows;
return 0;
}
21. Write a program to print the following pattern
1
12
123
1234
#include <stdio.h>
int main() {
int i, j;
return 0;
}
22. Write a C program to print the LCM of two numbers.
#include <stdio.h>
int main() {
int num1, num2;
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);
return 0;
}
23. Write a C program to swap two numbers using bitwise
operators.
#include <stdio.h>
int main() {
int a, b;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
return 0;
}
24. Write a C program to check if the character is uppercase or
lowercase.
#include <stdio.h>
#include <ctype.h>
int main() {
char ch;
printf("Enter a character: ");
scanf("%c", &ch);
if (isupper(ch))
printf("%c is uppercase.\n", ch);
else if (islower(ch))
printf("%c is lowercase.\n", ch);
else
printf("%c is neither uppercase nor lowercase.\n", ch);
return 0;
}
25. Write a C program to check whether the input is an alphabet,
digit or special character.
#include <stdio.h>
#include <ctype.h>
int main() {
char ch;
printf("Enter a character: ");
scanf("%c", &ch);
if (isalpha(ch))
printf("%c is an alphabet.\n", ch);
else if (isdigit(ch))
printf("%c is a digit.\n", ch);
else
printf("%c is a special character.\n", ch);
return 0;
}
26. Write a C program to check whether a triangle is valid if angles
are given.
#include <stdio.h>
int main() {
int angle1, angle2, angle3;
return 0;
}
27. Write a C program to print a hollow pyramid.
#include <stdio.h>
int main() {
int rows;
printf("Enter the number of rows: ");
scanf("%d", &rows);
return 0;
}
28. Write a C program to calculate profit or loss.
#include <stdio.h>
int main() {
float cost_price, selling_price, profit_or_loss;
return 0;
}
29. Write a C program to print number pattern of 0 and 1 at even
and odd rows.
11111
00000
11111
00000
#include <stdio.h>
int main() {
int rows = 5; // You can change this to print more rows
return 0;
}
30. Write a C program to print number pattern of 0 and 1 at even
and odd columns.
10101
01010
10101
01010
#include <stdio.h>
int main() {
int rows = 5; // You can change this to print more rows
int cols = 5; // You can change this to print more columns
return 0;
}
31. Write a C program to find cube of any number using function.
#include <stdio.h>
int main() {
float num;
printf("Enter a number: ");
scanf("%f", &num);
return 0;
}
32. Write a C program to find maximum and minimum between two
numbers using function.
#include <stdio.h>
int main() {
int num1, num2, max, min;
return 0;
}
33. Write a C program to print reverse pyramid.
#include <stdio.h>
int main() {
int rows;
printf("Enter the number of rows: ");
scanf("%d", &rows);
return 0;
}
34. Write a C program to check if the given number is even/odd
using function.
#include <stdio.h>
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
check_even_odd(num);
return 0;
}
35.Write a C program to print all perfect numbers in a given range
using functions.
#include <stdio.h>
int main() {
int lower, upper;
return 0;
}
36.Write a C program to find the power of a number using
recursion.
#include <stdio.h>
int main() {
int base, exp;
printf("Enter base and exponent: ");
scanf("%d %d", &base, &exp);
return 0;
}
37. Write a C program to check if the given number is a
palindrome using recursion.
#include <stdio.h>
if (num != 0) {
reversed = reversed * 10 + num % 10;
reverse(num / 10);
}
return reversed;
}
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
if (original == reversed)
printf("%d is a palindrome.\n", num);
else
printf("%d is not a palindrome.\n", num);
return 0;
}
38. Write a C program to find the factor of a given number using
recursion.
#include <stdio.h>
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
return 0;
}
39. Write a C program to enter elements in an array and find the
sum of it.
#include <stdio.h>
int main() {
int n;
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[n];
int sum = 0;
return 0;
}
40.Write a C program to enter elements in an array and find the
maximum of it.
#include <stdio.h>
int main() {
int n;
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[n];
int max;
max = arr[0];
for (int i = 1; i < n; i++) {
if (arr[i] > max) {
max = arr[i];
}
}
return 0;
}
41.Write a C program to search an input in an array.
#include <stdio.h>
int main() {
int n, search, found = 0;
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[n];
printf("Enter the elements:\n");
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
if (found)
printf("Element %d found in the array.\n", search);
else
printf("Element %d not found in the array.\n", search);
return 0;
}
42.Write a C program to reverse the elements of an array.
#include <stdio.h>
int main() {
int n;
printf("Enter the number of elements: ");
scanf("%d", &n);
printf("Reversed array:\n");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
43.Write a C program to print a matrix.
#include <stdio.h>
int main() {
int rows, cols;
printf("Enter number of rows and columns: ");
scanf("%d %d", &rows, &cols);
int matrix[rows][cols];
return 0;
}