0% found this document useful (0 votes)
18 views27 pages

C Programs 17 To 43

The document contains a series of C programming exercises that cover various topics such as calculating averages, swapping variables, printing patterns, checking character types, and finding factors. Each exercise includes a brief description followed by the corresponding C code implementation. The exercises range from basic input/output operations to more complex functions and recursion.

Uploaded by

yixapa5425
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views27 pages

C Programs 17 To 43

The document contains a series of C programming exercises that cover various topics such as calculating averages, swapping variables, printing patterns, checking character types, and finding factors. Each exercise includes a brief description followed by the corresponding C code implementation. The exercises range from basic input/output operations to more complex functions and recursion.

Uploaded by

yixapa5425
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 27

17.

Write a program that accepts 2 items' price and number of


purchase (both floating points' values) and calculates the average
value of the items.

#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;

// Accept price and quantity for item 1


printf("Enter the price of item 1: ");
scanf("%f", &price1);
printf("Enter the number of item 1 purchased: ");
scanf("%f", &quantity1);

// Accept price and quantity for item 2


printf("Enter the price of item 2: ");
scanf("%f", &price2);
printf("Enter the number of item 2 purchased: ");
scanf("%f", &quantity2);

// Calculate total cost for both items


total_cost1 = price1 * quantity1;
total_cost2 = price2 * quantity2;

// Calculate the total number of items purchased


total_items = quantity1 + quantity2;

// Calculate the average value


average_value = (total_cost1 + total_cost2) / total_items;

// Display the result


printf("The average value of the items is: %.2f\n", 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;

// Taking input from the user for the two variables


printf("Enter the value of variable a: ");
scanf("%f", &a);

printf("Enter the value of variable b: ");


scanf("%f", &b);

// Display values before swapping


printf("\nBefore swapping: \na = %.2f, b = %.2f\n", a, b);

// Swapping using a third variable


temp = a;
a = b;
b = temp;

// Display values after swapping


printf("\nAfter swapping: \na = %.2f, b = %.2f\n", a, b);

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

// Taking input from the user


printf("Enter the base number: ");
scanf("%d", &base);

printf("Enter the exponent: ");


scanf("%d", &exponent);

// Calculating power using for loop


for (int i = 1; i <= exponent; i++) {
result *= base; // Multiply result by base each time
}

// Displaying the result


printf("%d raised to the power of %d is %lld\n", base, exponent, result);

return 0;
}
20. Write a program to print equilateral triangle or pyramid.(star
pattern)
*
*
***
***

#include <stdio.h>

int main() {
int i, j, rows;

// Taking input from the user for the number of rows


printf("Enter the number of rows: ");
scanf("%d", &rows);

// Loop to handle the number of rows


for(i = 1; i <= rows; i++) {
// Print spaces before stars on each row
for(j = i; j < rows; j++) {
printf(" ");
}
// Print stars on each row
for(j = 1; j <= (2*i - 1); j++) {
printf("*");
}
// Move to the next line after each row
printf("\n");
}

return 0;
}
21. Write a program to print the following pattern
1
12
123
1234

#include <stdio.h>

int main() {
int i, j;

// Loop through each row


for(i = 1; i <= 4; i++) {
// Loop to print numbers in each row
for(j = 1; j <= i; j++) {
printf("%d", j); // Print the number
}
// Move to the next line after printing each row
printf("\n");
}

return 0;
}
22. Write a C program to print the LCM of two numbers.

#include <stdio.h>

// Function to find GCD (Greatest Common Divisor)


int gcd(int a, int b) {
while (b != 0) {
int temp = b;
b = a % b;
a = temp;
}
return a;
}

// Function to find LCM using GCD


int lcm(int a, int b) {
return (a * b) / gcd(a, b);
}

int main() {
int num1, num2;
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);

printf("LCM of %d and %d is %d\n", num1, num2, lcm(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);

// Swapping using bitwise XOR


a = a ^ b;
b = a ^ b;
a = a ^ b;

printf("After swapping: a = %d, b = %d\n", 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;

printf("Enter the three angles of the triangle: ");


scanf("%d %d %d", &angle1, &angle2, &angle3);

if (angle1 + angle2 + angle3 == 180)


printf("The triangle is valid.\n");
else
printf("The triangle is not valid.\n");

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);

for (int i = 1; i <= rows; i++) {


for (int j = 1; j <= rows - i; j++) {
printf(" ");
}

for (int j = 1; j <= (2 * i - 1); j++) {


if (j == 1 || j == (2 * i - 1) || i == rows) {
printf("*");
} else {
printf(" ");
}
}
printf("\n");
}

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;

printf("Enter the cost price: ");


scanf("%f", &cost_price);

printf("Enter the selling price: ");


scanf("%f", &selling_price);

if (selling_price > cost_price) {


profit_or_loss = selling_price - cost_price;
printf("Profit = %.2f\n", profit_or_loss);
} else if (selling_price < cost_price) {
profit_or_loss = cost_price - selling_price;
printf("Loss = %.2f\n", profit_or_loss);
} else {
printf("No profit, no loss.\n");
}

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

for (int i = 1; i <= rows; i++) {


if (i % 2 == 0) {
for (int j = 1; j <= rows; j++) {
printf("0");
}
} else {
for (int j = 1; j <= rows; j++) {
printf("1");
}
}
printf("\n");
}

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

for (int i = 1; i <= rows; i++) {


for (int j = 1; j <= cols; j++) {
if ((i + j) % 2 == 0)
printf("1");
else
printf("0");
}
printf("\n");
}

return 0;
}
31. Write a C program to find cube of any number using function.

#include <stdio.h>

float cube(float num) {


return num * num * num;
}

int main() {
float num;
printf("Enter a number: ");
scanf("%f", &num);

printf("The cube of %.2f is %.2f\n", num, cube(num));

return 0;
}
32. Write a C program to find maximum and minimum between two
numbers using function.

#include <stdio.h>

void find_max_min(int a, int b, int *max, int *min) {


if (a > b) {
*max = a;
*min = b;
} else {
*max = b;
*min = a;
}
}

int main() {
int num1, num2, max, min;

printf("Enter two numbers: ");


scanf("%d %d", &num1, &num2);

find_max_min(num1, num2, &max, &min);

printf("Maximum: %d\nMinimum: %d\n", 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);

for (int i = rows; i >= 1; i--) {


for (int j = 1; j <= rows - i; j++) {
printf(" ");
}

for (int j = 1; j <= (2 * i - 1); j++) {


printf("*");
}
printf("\n");
}

return 0;
}
34. Write a C program to check if the given number is even/odd
using function.

#include <stdio.h>

void check_even_odd(int num) {


if (num % 2 == 0)
printf("%d is even.\n", num);
else
printf("%d is odd.\n", num);
}

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 is_perfect(int num) {


int sum = 0;

for (int i = 1; i <= num / 2; i++) {


if (num % i == 0) {
sum += i;
}
}

return sum == num;


}

int main() {
int lower, upper;

printf("Enter the range (lower and upper): ");


scanf("%d %d", &lower, &upper);

printf("Perfect numbers in the range %d to %d are:\n", lower, upper);

for (int i = lower; i <= upper; i++) {


if (is_perfect(i)) {
printf("%d\n", i);
}
}

return 0;
}
36.Write a C program to find the power of a number using
recursion.

#include <stdio.h>

int power(int base, int exp) {


if (exp == 0)
return 1;
return base * power(base, exp - 1);
}

int main() {
int base, exp;
printf("Enter base and exponent: ");
scanf("%d %d", &base, &exp);

printf("%d raised to the power of %d is %d\n", base, exp, power(base, exp));

return 0;
}
37. Write a C program to check if the given number is a
palindrome using recursion.

#include <stdio.h>

int reverse(int num) {


static int reversed = 0;

if (num != 0) {
reversed = reversed * 10 + num % 10;
reverse(num / 10);
}

return reversed;
}

int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);

int original = num;


int reversed = reverse(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>

void find_factors(int num, int i) {


if (i <= num) {
if (num % i == 0) {
printf("%d ", i);
}
find_factors(num, i + 1);
}
}

int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);

printf("Factors of %d are: ", num);


find_factors(num, 1);

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;

printf("Enter the elements:\n");


for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
sum += arr[i];
}

printf("The sum of the elements is: %d\n", sum);

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;

printf("Enter the elements:\n");


for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}

max = arr[0];
for (int i = 1; i < n; i++) {
if (arr[i] > max) {
max = arr[i];
}
}

printf("The maximum element is: %d\n", max);

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]);
}

printf("Enter the element to search: ");


scanf("%d", &search);

for (int i = 0; i < n; i++) {


if (arr[i] == search) {
found = 1;
break;
}
}

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);

int arr[n], temp;


printf("Enter the elements:\n");
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}

for (int i = 0; i < n / 2; i++) {


temp = arr[i];
arr[i] = arr[n - i - 1];
arr[n - i - 1] = temp;
}

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];

printf("Enter the elements of the matrix:\n");


for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
scanf("%d", &matrix[i][j]);
}
}

printf("The matrix is:\n");


for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("%d ", matrix[i][j]);
}
printf("\n");
}

return 0;
}

You might also like