0% found this document useful (0 votes)
16 views13 pages

Aauyusha C

The document contains 20 different C programming solutions for various problems, including finding the smallest and largest of two numbers, checking if a number is even or odd, calculating the sum of natural numbers, and performing matrix operations. Each solution includes code snippets that demonstrate the implementation of the respective algorithms. The problems cover fundamental programming concepts such as loops, conditionals, and functions.

Uploaded by

subinthapa2022
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)
16 views13 pages

Aauyusha C

The document contains 20 different C programming solutions for various problems, including finding the smallest and largest of two numbers, checking if a number is even or odd, calculating the sum of natural numbers, and performing matrix operations. Each solution includes code snippets that demonstrate the implementation of the respective algorithms. The problems cover fundamental programming concepts such as loops, conditionals, and functions.

Uploaded by

subinthapa2022
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/ 13

1. WAP to find the smallest of two numbers.

Solution:
#include <stdio.h>

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

if (a < b) {
printf("%d is the smallest number.\n", a);
} else if (b < a) {
printf("%d is the smallest number.\n", b);
} else {
printf("Both numbers are equal.\n");
}

return 0;
}
Output:

2. WAP to check if a number is even or odd.


Solution:

#include<stdio.h>
#include<conio.h>

int main()
{
int n;
printf("\n Enter a number:");
scanf("%d", &n);
if (n % 2 == 0)
printf("Even Number");
else
printf("Odd Number");
return 0;
}
Output:

3. WAP to find the largest of two numbers


Solution:

#include<stdio.h>
#include<conio.h>

int main()
{
int a, b;
printf("\n Enter two numbers:");
scanf("%d %d", &a, &b);
if (a > b)
printf("%d is largest", a);
else
printf("%d is largest", b);
return 0;
}
4. WAP to find the sum of first 50 natural numbers
Solution:
#include<stdio.h>
#include<conio.h>

int main()
{
int i, sum = 0;
for(i = 1; i <= 50; i++)
{
sum = sum + i;
}
printf("\n The sum is %d", sum);
return 0;
}
5. WAP to display multiplication table of a number
Solution:
#include<stdio.h>
#include<conio.h>

int main()
{
int n, i;
printf("\n Enter a number:");
scanf("%d", &n);
for(i = 1; i <= 10; i++)
{
printf("%d x %d = %d\n", n, i, n*i);
}
return 0;
}

6. WAP to find the factorial of a number


Solution:
#include<stdio.h>
#include<conio.h>

int main()
{
int n, i, fact = 1;
printf("\n Enter a number:");
scanf("%d", &n);
for(i = 1; i <= n; i++)
{
fact = fact * i;
}
printf("\n Factorial = %d", fact);
return 0;
}

7. WAP to swap two numbers without using third


variable
Solution:
#include<stdio.h>
#include<conio.h>

int main()
{
int a, b;
printf("\n Enter two numbers:");
scanf("%d %d", &a, &b);
a = a + b;
b = a - b;
a = a - b;
printf("\n After swapping: a = %d, b = %d", a,
b);
return 0;
}
8. WAP to display the reverse of a number
Solution:
#include<stdio.h>
#include<conio.h>

int main()
{
int n, r;
printf("\n Enter a number:");
scanf("%d", &n);
printf("\n Reverse of the number is: ");
while(n > 0)
{
r = n % 10;
printf("%d", r);
n = n / 10;
}
return 0;
}
9. WAP to find the sum of digits of a number
Solution:
#include<stdio.h>
#include<conio.h>

int main()
{
int n, sum = 0, r;
printf("\n Enter a number:");
scanf("%d", &n);
while(n > 0)
{
r = n % 10;
sum = sum + r;
n = n / 10;
}
printf("\n Sum of digits = %d", sum);
return 0;
}
10. WAP to check a number is positive, negative,
or zero
Solution:
#include<stdio.h>
#include<conio.h>

int main()
{
int n;
printf("\n Enter a number:");
scanf("%d", &n);
if(n > 0)
printf("Positive number");
else if(n < 0)
printf("Negative number");
else
printf("Zero");
return 0;
}
11. WAP to calculate the average of three
numbers
Solution:
#include<stdio.h>
#include<conio.h>

int main()
{
int a, b, c;
float avg;
printf("\n Enter three numbers:");
scanf("%d %d %d", &a, &b, &c);
avg = (a + b + c) / 3.0;
printf("\n Average = %.2f", avg);
return 0;
}
12. WAP to check whether a number is a
palindrome or not
Solution:
#include <stdio.h>

int main() {
int num, reversed = 0, remainder, original;
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.\n", original);
} else {
printf("%d is not a palindrome.\n", original);
}

return 0;
}
13. WAP to print the Fibonacci series
Solution:
#include <stdio.h>

int main() {
int n, first = 0, second = 1, next, i;
printf("Enter the number of terms: ");
scanf("%d", &n);

printf("Fibonacci Series: %d %d ", first, second);

for (i = 2; i < n; i++) {


next = first + second;
printf("%d ", next);
first = second;
second = next;
}

return 0;
}
14. WAP to print multiplication of the matrix
Solution:
#include <stdio.h>

int main() {
int a[10][10], b[10][10], mul[10][10];
int r1, c1, r2, c2, i, j, k;

printf("Enter rows and columns for first matrix:


");
scanf("%d%d", &r1, &c1);

printf("Enter rows and columns for second


matrix: ");
scanf("%d%d", &r2, &c2);

if (c1 != r2) {
printf("Matrix multiplication not possible.\n");
return 0;
}

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


for (i = 0; i < r1; i++) {
for (j = 0; j < c1; j++) {
scanf("%d", &a[i][j]);
}
}

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


for (i = 0; i < r2; i++) {
for (j = 0; j < c2; j++) {
scanf("%d", &b[i][j]);
}
}

// Initializing multiplication matrix to zero


for (i = 0; i < r1; i++) {
for (j = 0; j < c2; j++) {
mul[i][j] = 0;
}
}

// Multiplying matrices
for (i = 0; i < r1; i++) {
for (j = 0; j < c2; j++) {
for (k = 0; k < c1; k++) {
mul[i][j] += a[i][k] * b[k][j];
}
}
}

printf("Resultant Matrix:\n");
for (i = 0; i < r1; i++) {
for (j = 0; j < c2; j++) {
printf("%d ", mul[i][j]);
}
printf("\n");
}

return 0;
}
15. WAP to print the matrix addition
Solution:
#include <stdio.h>
#include <conio.h>

int main() {
int a[3][3], b[3][3], c[3][3];
int i, j;

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


for(i=0; i<3; i++) {
for(j=0; j<3; j++) {
scanf("%d", &a[i][j]);
}
}

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


for(i=0; i<3; i++) {
for(j=0; j<3; j++) {
scanf("%d", &b[i][j]);
}
}

printf("Sum of matrices:\n");
for(i=0; i<3; i++) {
for(j=0; j<3; j++) {
c[i][j] = a[i][j] + b[i][j];
printf("%d ", c[i][j]);
}
printf("\n");
}
return 0;
}
16. WAP to print the days of the week using the
switch case
Solution:
#include <stdio.h>
#include <conio.h>

void main() {
int day;
printf("Enter day number (1-7): ");
scanf("%d", &day);
switch(day) {
case 1: printf("Sunday"); break;
case 2: printf("Monday"); break;
case 3: printf("Tuesday"); break;
case 4: printf("Wednesday"); break;
case 5: printf("Thursday"); break;
case 6: printf("Friday"); break;
case 7: printf("Saturday"); break;
default: printf("Invalid day");
}
getch();
}

17. WAP to check whether a character is a vowel


or consonant
Solution:
#include <stdio.h>

int main() {
char ch;
printf("Enter an alphabet: ");
scanf("%c", &ch);

if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o'


|| ch == 'u' ||
ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O'
|| ch == 'U') {
printf("%c is a vowel.\n", ch);
} else {
printf("%c is a consonant.\n", ch);
}
return 0;
}
18. WAP to calculate the simple interest
Solution:
#include <stdio.h>

int main() {
float principal, rate, time, interest;
printf("Enter principal amount, rate of interest,
and time (in years): ");
scanf("%f %f %f", &principal, &rate, &time);

interest = (principal * rate * time) / 100;


printf("Simple Interest = %.2f\n", interest);

return 0;
}
19. WAP to calculate the power of a number (using
for loop)
Solution:
#include <stdio.h>

int main() {
int base, exponent;
long long result = 1;
printf("Enter base and exponent: ");
scanf("%d %d", &base, &exponent);

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


result *= base;
}

printf("%d^%d = %lld\n", base, exponent,


result);

return 0;
}
20. WAP to find the sum of digits of a number
Solution:
#include <stdio.h>
int main() {
int num, sum = 0, digit;

printf("Enter a number: ");


scanf("%d", &num);

while (num != 0) {
digit = num % 10;
sum += digit;
num /= 10;
}

printf("Sum of digits = %d\n", sum);

return 0;
}

You might also like