0% found this document useful (0 votes)
8 views6 pages

C Programming Tasks Complete

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

C Programming Tasks Complete

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

Basic C Programming Task

1. Arithmetic Operations Using User Input

Description: Takes two integers from the user and performs addition, subtraction, multiplication, and
division.

Input:
Enter two integers: 8 4

Output:
Sum = 12
Difference = 4
Product = 32
Quotient = 2

#include <stdio.h>

int main() {
int a, b;
printf("Enter two integers: ");
scanf("%d %d", &a, &b);
printf("Sum = %d\n", a + b);
printf("Difference = %d\n", a - b);
printf("Product = %d\n", a * b);
if(b != 0)
printf("Quotient = %d\n", a / b);
else
printf("Division by zero not allowed.\n");
return 0;
}
Basic C Programming Task

2. Check Even or Odd Using Conditional Statement

Description: Checks if a number is even or odd using if-else.

Input:
Enter an integer: 7

Output:
7 is odd.

#include <stdio.h>

int main() {
int num;
printf("Enter an integer: ");
scanf("%d", &num);
if(num % 2 == 0)
printf("%d is even.\n", num);
else
printf("%d is odd.\n", num);
return 0;
}
Basic C Programming Task

3. Find Factorial of a Number Using For Loop

Description: Calculates the factorial of a given number using a for loop.

Input:
Enter a number: 5

Output:
Factorial of 5 = 120

#include <stdio.h>

int main() {
int n, i;
unsigned long long fact = 1;
printf("Enter a number: ");
scanf("%d", &n);
for(i = 1; i <= n; ++i) {
fact *= i;
}
printf("Factorial of %d = %llu\n", n, fact);
return 0;
}
Basic C Programming Task

4. Reverse a Number and Check for Palindrome

Description: Reverses a number and checks if it's the same as the original.

Input:
Enter a number: 121

Output:
121 is a palindrome.

#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.\n", original);
else
printf("%d is not a palindrome.\n", original);
return 0;
}
Basic C Programming Task

5. Sort an Array in Ascending and Descending Order (Bubble Sort)

Description: Sorts an array using Bubble Sort in ascending and descending order.

Input:
Enter 5 numbers: 4 2 5 1 3

Output:
Ascending: 1 2 3 4 5
Descending: 5 4 3 2 1

#include <stdio.h>

int main() {
int arr[5], i, j, temp;
printf("Enter 5 numbers: ");
for(i = 0; i < 5; i++)
scanf("%d", &arr[i]);
for(i = 0; i < 4; i++) {
for(j = 0; j < 4 - i; j++) {
if(arr[j] > arr[j + 1]) {
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
printf("Ascending: ");
for(i = 0; i < 5; i++)
printf("%d ", arr[i]);
printf("\nDescending: ");
for(i = 4; i >= 0; i--)
printf("%d ", arr[i]);
return 0;
}
Basic C Programming Task

6. Perform Matrix Addition

Description: Adds two 2x2 matrices entered by the user.

Input:
Enter elements of 2 matrices (2x2)

Output:
Sum matrix:
[2 4]
[6 8]

#include <stdio.h>

int main() {
int a[2][2], b[2][2], sum[2][2], i, j;
printf("Enter elements of first 2x2 matrix:\n");
for(i = 0; i < 2; i++)
for(j = 0; j < 2; j++)
scanf("%d", &a[i][j]);
printf("Enter elements of second 2x2 matrix:\n");
for(i = 0; i < 2; i++)
for(j = 0; j < 2; j++)
scanf("%d", &b[i][j]);
for(i = 0; i < 2; i++)
for(j = 0; j < 2; j++)
sum[i][j] = a[i][j] + b[i][j];
printf("Sum matrix:\n");
for(i = 0; i < 2; i++) {
for(j = 0; j < 2; j++)
printf("%d ", sum[i][j]);
printf("\n");
}
return 0;
}

You might also like