//1a.
PROGRAM: (with temp variable)
#include<stdio.h>
int main(){
int a=2;
int b=6;
int temp;
temp=a;
a=b;
b=temp;
printf("swapped numbers: a:%d,b:%d",a,b);
return 0;}
OUTPUT:
Swapped numbers: a:6, b:2
PROGRAM: (without temp variable)
#include<stdio.h>
int main()
{
int a=1;
int b=6;
a=a+b;
b=a-b;
a=a-b;
printf("swapped numbers:a;%d,b:%d",a,b);
return 0;
}
OUTPUT:
swapped numbers:a;6,b:1
//[Link]:
#include<stdio.h>
int main()
{
int r;
printf("Enter a value for r:");
scanf("%d",&r);
int area=3.14*r*r;
printf("area of circle:%d",area);
return 0;
}
OUTPUT:
Enter a value for r:6
area of circle:113
//[Link]:
#include<stdio.h>
int main()
{
char a;
printf("Enter a character:");
scanf("%c",&a);
printf("The ASCII value of '%c' is %d",a,a);
return 0;
}
OUTPUT:
Enter a character:a
The ASCII value of 'a' is 97
//[Link]:
#include<stdio.h>
int main()
{
char a;
printf("Enter a character in uppercase:");
scanf("%c",&a);
printf("lowercase:%c",a+32);
return 0;
}
OUTPUT:
Enter a character in uppercase:A
lowercase:a
//[Link]:
#include<stdio.h>
int main()
{
int c;
printf("Enter a farenheit value:");
scanf("%d",&c);
int C,F;
C=0.56*F-32;
printf("Celsius value:%d",C);
return 0;
}
OUTPUT:
Enter a farenheit value:46
Celsius value:17511
//[Link]:
#include <stdio.h>
#include <math.h>
int main() {
float x1, y1, x2, y2, distance;
// Input coordinates of the two points
printf("Enter x1, y1: ");
scanf("%f %f", &x1, &y1);
printf("Enter x2, y2: ");
scanf("%f %f", &x2, &y2);
// Calculate distance
distance = sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
// Output the distance
printf("Distance: %.2f\n", distance);
return 0;
}
OUTPUT:
Enter x1, y1: 4,8
Enter x2, y2:0,0 Distance: 4.00
//[Link]:
#include <stdio.h>
#include <math.h>
int main()
{
float a, b, c, s, area;
scanf("%f %f %f", &a, &b, &c);
s = (a + b + c) /
area = sqrt(s * (s - a) * (s - b) * (s - c));
printf("%.2f\n", area);
return 0;
}
OUTPUT:
345
6.00
//[Link]:
#include <stdio.h>
int main()
{
int a, b, c;
scanf("%d %d %d", &a, &b, &c);
int largest = (a > b && a > c) ? a : (b > c) ? b : c;
printf("%d\n", largest);
return 0;
}
OUTPUT:
583
8
//[Link]:
#include<stdio.h>
int main(){
float Salary, Basicpay,HRA,TA;
printf(“Enter Basic pay amount:”);
scanf(“%f ”,&Basicpay);
HRA=(0.1)*Basicpay;
TA =(0.05)*Basicpay;
Salary=Basicpay+HRA+TA;
printf(“Total Salary : %.2f”,Salary);
return 0;
}
OUTPUT:
Enter Basic pay amount: 20000
Total Salary : 23000.00
//2c..PROGRAM:
#include<stdio.h>
int main(){
int a;
printf("Enter a number: ");
scanf("%d",&a);
if (a%2==0){
printf("The number is Even");
}
else{
printf("The number is Odd");
}
return 0;
}
OUTPUT:
Enter a number: 5
The number is Odd
//[Link]:
#include<stdio.h>
int main(){
char var;
printf("Enter a variable: ");
scanf("%c",&var);
if (var =='a'||var =='e'||var =='i'||var =='o'||var =='u'){
printf("%c is a Vowel",var);
}
else{
printf("%c is Not a Vowel",var);
}
return 0;
}
OUTPUT:
Enter a variable: s
s is Not a Vowel
//[Link]:
#include<stdio.h>
int main(){
int age;
printf("Enter your age: ");
scanf("%d",&age);
if (age >=18){
printf("Eligible to vote",age);
}
else{
printf("Not eligible to vote",age);
}
return 0;
}
OUTPUT:
Enter your age: 24
Eligible to vote
//[Link]:
#include<stdio.h>
int main(){
char a,b;
printf("Enter a character in Lowercase: ");
scanf("%c",&a);
b= a-32;
printf("%c",b);
return 0;
}
OUTPUT:
Enter a character in Lowercase: b
B
//[Link]:
#include<stdio.h>
int main() {
int a =1900;
do{
if (((a%4==0)&&(a%100!=0))||(a%400==0)){
printf("%d is a leap year\n",a);
}
a++;
}
while (a<=2025);
return 0;
}
OUTPUT:
1904 is a leap year
1908 is a leap year
.
.
.
2016 is a leap year
2020 is a leap year
2024 is a leap year
//[Link]:
#include<stdio.h>
int main() {
int num=10,sum=0;
int i=1;
while(i<num){
sum=i;
i++; }
printf(“Sum of first 10 numbers:%d\n”,sum);
return 0;
}
OUTPUT:
Sum of first 10 numbers:55.
//[Link]:
#include<stdio.h>
int main() {
int i,n;
int fact=1;
printf("Enter number: ");
scanf("%d",&n);
for (i=1;i<=n;i++){
fact = fact*i;
}
printf("Factorial is :%d",fact);
return 0;
}
OUTPUT:
Enter number: 5
Factorial is :120
//[Link]:
#include<stdio.h>
int main() {
int i,j,n;
printf("Enter no. of rows: ");
scanf("%d",&n);
for (i=1;i<=n;i++){
for (j=1;j<=i;j++){
printf("%d",j);
}
printf("\n");
}
return 0;
}
OUTPUT:
Enter no. of rows: 5
1
12
123
1234
12345
//[Link]:
#include<stdio.h>
int main() {
int i,j,n;
int num =1;
printf("Enter no. of rows: ");
scanf("%d",&n);
for (i=1;i<=n;i++){
for (j=1;j<=i;j++){
printf(" %d",num++);
}
printf("\n");
}
return 0;
}
OUTPUT:
Enter no. of rows: 5
1
23
456
7 8 9 10
11 12 13 14 15
//[Link]:
#include <stdio.h>
int linearSearch(int arr[], int size, int target) {
for (int i = 0; i < size; i++) {
if (arr[i] == target) {
return i;
}
}
return -1;
}
int main() {
int size, target;
printf("Enter the number of elements in the array: ");
scanf("%d", &size);
int arr[size];
printf("Enter %d elements:\n", size);
for (int i = 0; i < size; i++) {
scanf("%d", &arr[i]);
}
printf("Enter the element to search for: ");
scanf("%d", &target);
int result = linearSearch(arr, size, target);
if (result != -1) {
printf("Element %d found at index %d.\n", target, result);
} else {
printf("Element %d not found in the array.\n", target);
}
return 0;
}
OUTPUT:
Enter the number of elements in the array: 3
Enter 3 elements:
123
Enter the element to search for: 3
Element 3 found at index 2.
//[Link]:
#include <stdio.h>
int binarySearch(int arr[], int size, int target) {
int left = 0;
int right = size - 1;
while (left <= right) {
int mid = left + (right - left) / 2;
if (arr[mid] == target) {
return mid;
}
if (arr[mid] < target) {
left = mid + 1;
} else {
right = mid - 1;
}
}
return -1;
}
int main() {
int size, target;
printf("Enter the number of elements in the sorted array: ");
scanf("%d", &size);
int arr[size];
printf("Enter %d sorted elements:\n", size);
for (int i = 0; i < size; i++) {
scanf("%d", &arr[i]);
}
printf("Enter the element to search for: ");
scanf("%d", &target);
int result = binarySearch(arr, size, target);
if (result != -1) {
printf("Element %d found at index %d.\n", target, result);
} else {
printf("Element %d not found in the array.\n", target);
}
return 0;
}
OUTPUT:
Enter the number of elements in the sorted array: 3
Enter 3 sorted elements:
356
Enter the element to search for: 5
Element 5 found at index 1
//[Link]:
#include <stdio.h>
void selectionSort(int arr[], int size) {
for (int i = 0; i < size - 1; i++) {
int minIndex = i;
for (int j = i + 1; j < size; j++) {
if (arr[j] < arr[minIndex]) {
minIndex = j;
}
}
int temp = arr[i];
arr[i] = arr[minIndex];
arr[minIndex] = temp;
}
}
int main() {
int arr[] = {64, 25, 12, 22, 11};
int size = sizeof(arr) / sizeof(arr[0]);
selectionSort(arr, size);
printf("Sorted array: ");
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
OUTPUT:
Sorted array: 11 12 22 25 64
//[Link]:
#include <stdio.h>
int main() {
int rows, cols;
printf("Enter the number of rows and columns: ");
scanf("%d %d", &rows, &cols)
int matrix[rows][cols];
int transpose[cols][rows];
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]);
}
}
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
transpose[j][i] = matrix[i][j];
}
}
printf("Transposed matrix:\n");
for (int i = 0; i < cols; i++) {
for (int j = 0; j < rows; j++) {
printf("%d ", transpose[i][j]);
}
printf("\n");
}
return 0;
}
OUTPUT:
Enter the number of rows and columns: 2 3
Enter the elements of the matrix:
123
456
Transposed matrix:
14
25
36
//[Link]:
#include <stdio.h>
int main() {
int rows, cols;
printf("Enter the number of rows and columns: ");
scanf("%d %d", &rows, &cols);
int matrix1[rows][cols];
int matrix2[rows][cols];
int sum[rows][cols];
printf("Enter the elements of the first matrix:\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
scanf("%d", &matrix1[i][j]);
}
}
printf("Enter the elements of the second matrix:\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
scanf("%d", &matrix2[i][j]);
}
}
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
sum[i][j] = matrix1[i][j] + matrix2[i][j];
}
}
printf("Resultant matrix after addition:\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("%d ", sum[i][j]);
}
printf("\n");
}
return 0;
}
OUTPUT:
Enter the number of rows and columns: 2 2
Enter the elements of the first matrix:
12
34
Enter the elements of the second matrix:
56
78
Resultant matrix after addition:
68
10 12
//[Link]:
#include <stdio.h>
int main() {
int rows1, cols1, rows2, cols2;
printf("Enter the number of rows and columns for the first matrix: ");
scanf("%d %d", &rows1, &cols1);
printf("Enter the number of rows and columns for the second matrix: ");
scanf("%d %d", &rows2, &cols2);
if (cols1 != rows2) {
printf("Matrix multiplication is not possible.\n");
return 1;
}
int matrix1[rows1][cols1];
int matrix2[rows2][cols2];
int product[rows1][cols2];
printf("Enter the elements of the first matrix:\n");
for (int i = 0; i < rows1; i++) {
for (int j = 0; j < cols1; j++) {
scanf("%d", &matrix1[i][j]);
}
}
printf("Enter the elements of the second matrix:\n");
for (int i = 0; i < rows2; i++) {
for (int j = 0; j < cols2; j++) {
scanf("%d", &matrix2[i][j]);
}
}
for (int i = 0; i < rows1; i++) {
for (int j = 0; j < cols2; j++) {
product[i][j] = 0;
}
}
for (int i = 0; i < rows1; i++) {
for (int j = 0; j < cols2; j++) {
for (int k = 0; k < cols1; k++) {
product[i][j] += matrix1[i][k] * matrix2[k][j];
}
}
}
printf("Resultant matrix after multiplication:\n");
for (int i = 0; i < rows1; i++) {
for (int j = 0; j < cols2; j++) {
printf("%d ", product[i][j]);
}
}
return 0;
}
OUTPUT:
Enter the number of rows and columns for the first matrix: 2 3
Enter the number of rows and columns for the second matrix: 3 2
Enter the elements of the first matrix:
123
456
Enter the elements of the second matrix:
78
9 10
11 12
Resultant matrix after multiplication:
58 64
139 154
//[Link]:
#include <stdio.h>
#include <string.h>
int main() {
char str1[100], str2[100], copy[100];
printf("Enter first string: ");
gets(str1);
printf("Enter second string: ");
gets(str2);
printf("\nLength of first string: %ld", strlen(str1));
printf("\nLength of second string: %ld", strlen(str2));
if (strcmp(str1, str2) == 0)
printf("\nStrings are equal.");
else
printf("\nStrings are not equal.");
strcat(str1, str2)
printf("\nAfter concatenation: %s", str1);
strcpy(copy, str1);
printf("\nCopied string: %s", copy)
return 0;
}
OUTPUT:
Enter first string: Welcome
Enter second string: LICET
Length of first string: 8
Length of second string: 5
Strings are not equal.
After concatenation: Welcome LICET
Copied string: Welcome LICET
//[Link]:
#include <stdio.h>
int main() {
char str[100];
int length = 0;
printf("Enter a string: ");
fgets(str, sizeof(str), stdin);
while (str[length] != '\0' && str[length] != '\n') {
length++;
}
printf("Length of the string: %d\n", length);
return 0;
}
OUTPUT:
Enter a string: welcome
Length of the string: 8
//[Link]:
#include <stdio.h>
int main() {
char source[100], destination[100];
int i = 0;
printf("Enter the source string: ");
fgets(source, sizeof(source), stdin);
while (source[i] != '\0') {
destination[i] = source[i];
i++;
}
destination[i] = '\0';
printf("Source string: %s", source);
printf("Copied string: %s", destination);
return 0;
}
OUTPUT:
Enter the source string: hello
Source string: hello
Copied string: hello
//[Link]:
#include <stdio.h>
int main() {
char s1[100], s2[100];
int i = 0, equal = 1;
printf("Enter first string: ");
fgets(s1, sizeof(s1), stdin);
printf("Enter second string: ");
fgets(s2, sizeof(s2), stdin);
while (s1[i] != '\0' && s2[i] != '\0') {
if (s1[i] != s2[i]) {
equal = 0;
break;
}
i++;
}
if (equal && s1[i] == s2[i])
printf("Strings are equal.\n");
else
printf("Strings are not equal.\n");
return 0;
}
OUTPUT:
Enter first string: hello
Enter second string: LICET
Strings are not equal.
//[Link]:
#include <stdio.h>
int main() {
char s1[100], s2[100], s3[200];
int i = 0, j = 0;
printf("Enter first string: ");
gets(s1);
printf("Enter second string: ");
gets(s2);
while(s1[i] != '\0') {
s3[i] = s1[i];
i++;
}
while(s2[j] != '\0') {
s3[i+j] = s2[j];
j++;
}
s3[i+j] = '\0';
printf("Concatenated string: %s\n", s3);
return 0;
}
OUTPUT:
Enter first string: Hello
Enter second string: I am Kisha
Concatenated string: Hello I am Kisha
//[Link]:
#include <stdio.h>
int add(int a, int b) {
return a + b;
}
int main() {
int num1, num2, sum;
printf("Enter two integers: ");
scanf("%d %d", &num1, &num2);
sum = add(num1, num2);
printf("The sum of %d and %d is: %d\n", num1, num2, sum);
return 0;
}
OUTPUT:
Enter two integers: 5 10
The sum of 5 and 10 is: 15
//[Link]:
#include <stdio.h>
void swapByValue(int a, int b) {
int temp;
temp = a;
a = b;
b = temp;
printf("Inside swapByValue: a = %d, b = %d\n", a, b);
}
void swapByReference(int *a, int *b) {
int temp;
temp = *a;
*a = *b;
*b = temp;
printf("Inside swapByReference: a = %d, b = %d\n", *a, *b);
}
int main() {
int num1, num2;
printf("Enter two integers: ");
scanf("%d %d", &num1, &num2);
printf("Before swapByValue: num1 = %d, num2 = %d\n", num1, num2);
swapByValue(num1, num2);
printf("After swapByValue: num1 = %d, num2 = %d\n", num1, num2);
printf("Before swapByReference: num1 = %d, num2 = %d\n", num1, num2);
swapByReference(&num1, &num2);
printf("After swapByReference: num1 = %d, num2 = %d\n", num1, num2);
return 0;
}
OUTPUT:
Enter two integers: 5 10
Before swapByValue: num1 = 5, num2 = 10
Inside swapByValue: a = 10, b = 5
After swapByValue: num1 = 5, num2 = 10
Before swapByReference: num1 = 5, num2 = 10
Inside swapByReference: a = 10, b = 5
After swapByReference: num1 = 10, num2 = 5
//[Link]:
#include <stdio.h>
int findLargest(int a, int b) {
if (a > b) {
return a;
} else {
return b;
}
}
int main() {
int num1, num2, largest;
printf("Enter two integers: ");
scanf("%d %d", &num1, &num2);
largest = findLargest(num1, num2);
printf("The largest of %d and %d is: %d\n", num1, num2, largest);
return 0;
}
OUTPUT:
Enter two integers: 15 25
The largest of 15 and 25 is: 25
//[Link]:
#include <stdio.h>
int isPrime(int num) {
if (num <= 1) {
return 0;
}
for (int i = 2; i * i <= num; i++) {
if (num % i == 0) {
return 0;
}
}
return 1;
}
int main() {
int number;
printf("Enter a number: ");
scanf("%d", &number);
if (isPrime(number)) {
printf("%d is a prime number.\n", number);
} else {
printf("%d is not a prime number.\n", number);
}
return 0;
}
OUTPUT:
Enter a number: 17
17 is a prime number.
//[Link]:
#include <stdio.h>
void mergeArrays(int arr1[], int size1, int arr2[], int size2, int merged[]) {
for (int i = 0; i < size1; i++) {
merged[i] = arr1[i];
}
for (int j = 0; j < size2; j++) {
merged[size1 + j] = arr2[j];
}
}
void displayReverse(int arr[], int size) {
for (int i = size - 1; i >= 0; i--) {
printf("%d ", arr[i]);
}
printf("\n");
}
int main() {
int size1, size2;
printf("Enter the size of the first array: ");
scanf("%d", &size1);
int arr1[size1];
printf("Enter %d elements for the first array: ", size1);
for (int i = 0; i < size1; i++) {
scanf("%d", &arr1[i]);
}
printf("Enter the size of the second array: ");
scanf("%d", &size2);
int arr2[size2];
printf("Enter %d elements for the second array: ", size2);
for (int i = 0; i < size2; i++) {
scanf("%d", &arr2[i]);
}
int merged[size1 + size2];
mergeArrays(arr1, size1, arr2, size2, merged);
printf("Merged array in reverse order: ");
displayReverse(merged, size1 + size2);
return 0;
}
OUTPUT:
Enter the size of the first array: 3
Enter 3 elements for the first array: 1 2 3
Enter the size of the second array: 4
Enter 4 elements for the second array: 4 5 6 7
Merged array in reverse order: 7 6 5 4 3 2 1
//[Link]:
#include <stdio.h>
void towerOfHanoi(int n, char source, char destination, char auxiliary) {
if (n == 1) {
printf("Move disk 1 from %c to %c\n", source, destination);
return;
}
towerOfHanoi(n - 1, source, auxiliary, destination);
printf("Move disk %d from %c to %c\n", n, source, destination);
towerOfHanoi(n - 1, auxiliary, destination, source);
}
int main() {
int n;
printf("Enter the number of disks: ");
scanf("%d", &n);
towerOfHanoi(n, 'A', 'C', 'B');
return 0;
}
OUTPUT:
Enter the number of disks: 2
Move disk 1 from A to B
Move disk 2 from A to C
Move disk 1 from B to C
//[Link]:
#include <stdio.h>
int factorial(int n) {
if (n == 0) return 1;
return n * factorial(n - 1);
}
int main() {
int number;
printf("Enter a positive integer: ");
scanf("%d", &number);
if (number < 0) {
printf("Factorial is not defined for negative numbers.\n");
} else {
printf("Factorial of %d is %d\n", number, factorial(number));
}
return 0;
}
OUTPUT:
Enter a positive integer: 3
Factorial of 3 is 6
//[Link]:
#include <stdio.h>
int fibonacci(int n) {
if (n <= 0) return 0;
if (n == 1) return 1;
return fibonacci(n - 1) + fibonacci(n - 2);
}
int main() {
int number;
printf("Enter a positive integer: ");
scanf("%d", &number);
if (number < 0) {
printf("Fibonacci is not defined for negative numbers.\n");
} else {
printf("Fibonacci of %d is %d\n", number, fibonacci(number));
}
return 0;
}
OUTPUT:
Enter a positive integer: 5
Fibonacci of 5 is 011235813
//[Link]:
#include <stdio.h>
int sumOfDigits(int n) {
if (n == 0) return 0;
return (n % 10) + sumOfDigits(n / 10);
}
int main() {
int number;
printf("Enter a positive integer: ");
scanf("%d", &number);
if (number < 0) {
printf("Please enter a positive integer.\n");
} else {
printf("Sum of digits of %d is %d\n", number, sumOfDigits(number));
}
return 0;
}
OUTPUT:
Enter a positive integer: 26
Sum of digits of 26 is 8
//[Link]:
#include <stdio.h>
#include <string.h>
void reverseString(char str[], int start, int end) {
if (start >= end) return;
char temp = str[start];
str[start] = str[end];
str[end] = temp;
reverseString(str, start + 1, end - 1);
}
int main() {
char str[100];
printf("Enter a string: ");
fgets(str, sizeof(str), stdin);
str[strcspn(str, "\n")] = 0;
reverseString(str, 0, strlen(str) - 1);
printf("Reversed string: %s\n", str);
return 0;
}
OUTPUT:
Enter a string: hello
Reversed string: olleh
//[Link]:
#include <stdio.h>
int main() {
int matrix[3][3];
int *ptr = &matrix[0][0];
printf("Enter elements of the 3x3 matrix:\n");
for (int i = 0; i < 9; i++) {
scanf("%d", ptr + i);
}
printf("\nThe 3x3 matrix is:\n");
for (int i = 0; i < 9; i++) {
printf("%d ", *(ptr + i));
if ((i + 1) % 3 == 0) {
printf("\n");
}
}
return 0;
}
OUTPUT:
Enter elements of the 3x3 matrix:
123456789
The 3x3 matrix is:
123
456
789
//[Link]:
#include <stdio.h>
int main() {
char source[] = "Hello, World!";
char destination[50];
char *src_ptr = source;
char *dest_ptr = destination;
while (*src_ptr) {
*dest_ptr++ = *src_ptr++;
}
printf("Source: %s\n", source);
printf("Destination: %s\n", destination);
return 0;
}
OUTPUT:
Source: Hello, World!
Destination: Hello, World!
//[Link]:
#include <stdio.h>
void findSmallest(int *arr, int n) {
int smallest = arr[0];
int position = 0;
for (int i = 1; i < n; i++) {
if (arr[i] < smallest) {
smallest = arr[i];
position = i;
}
}
printf("Smallest number: %d\n", smallest);
printf("Position of smallest number: %d\n", position);
}
int main() {
int n;
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[n];
printf("Enter %d numbers:\n", n);
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
findSmallest(arr, n);
return 0;
}
OUTPUT:
Enter the number of elements: 5
Enter 5 numbers:
12345
Smallest number: 1
Position of smallest number: 0
//[Link]:
#include <stdio.h>
int main() {
int n, i;
float sum = 0;
printf("Enter how many numbers: ");
scanf("%d", &n);
float nums[n];
printf("Enter %d numbers: ", n);
for(i = 0; i < n; i++) {
scanf("%f", &nums[i]);
}
float *ptr = nums;
for(i = 0; i < n; i++) {
sum += *ptr;
ptr++;
}
printf("Mean = %.2f", sum / n);
return 0;
}
OUTPUT:
Enter how many numbers: 5
Enter 5 numbers: 3 9 6 4 8
Mean = 6.00