1.### Program: Calculate Age using Function (With Return Type and With Arguments)
Aim:
To calculate the current age of a person using a function with return type and arguments.
Algorithm:
- Start the program.
- Define a function
cage(int num1, int num2)that calculates the difference between the current year and the birth year. - In the main function, initialize two integer variables:
num1as the current year (2020).num2as the birth year (1983).
- Call the function
cage(num1, num2)to compute the difference. - Subtract 1 from the result to adjust the age according to the birth date.
- Print the calculated age.
- End the program.
Source Code:
#include <stdio.h>
int cage(int num1, int num2)
{
int age = num1 - num2;
return age;
}
int main()
{
int num1 = 2020, num2 = 1983;
int age = cage(num1, num2);
printf("Present Age is : %d", age - 1);
return 0;
}Result: The program calculates and displays the current age of a person based on the given date of birth and current date.
2.### Program: Check Whether a Number is an Armstrong Number or Not
Aim:
To check whether the entered number is an Armstrong number using loops and mathematical operations.
Algorithm:
- Start the program.
- Declare variables:
num,sum,count,original, andtemp. - Read the integer number
numfrom the user. - Store the value of
numinoriginalto preserve the original number. - Count the number of digits in
numby dividing it repeatedly by 10. - Reassign
temp = num. - Extract each digit of
tempusing modulus (% 10) and compute its power raised to the total digit count usingpow(). - Add each powered digit to
sum. - After the loop, compare
sumwithoriginal:- If both are equal → it is an Armstrong number.
- Otherwise → it is not an Armstrong number.
- Display the result.
- End the program.
Source Code:
#include <stdio.h>
#include <math.h>
int main()
{
int num, sum = 0, count = 0;
int original, temp;
scanf("%d", &num);
original = num;
for (temp = num; temp != 0; ++count)
{
temp = temp / 10;
}
for (temp = num; temp != 0; temp /= 10)
{
int digit = temp % 10;
sum += pow(digit, count);
}
if ((int)sum == original)
{
printf("%d is armstrong number", original);
}
else
{
printf("%d is not a armstrong number", original);
}
return 0;
}Result: The program determines whether the entered number is an Armstrong number and displays the corresponding message.
3.### Program: Print the Elements of the Last Column of an n × n Matrix
Aim:
To read the elements of an n × n matrix and print the elements of its last column.
Algorithm:
- Start the program.
- Declare a two-dimensional array
a[n][n]. - Read the value of
n(the size of the matrix). - Use nested
forloops to input all elements of the matrix. - After reading the matrix, use a single
forloop to access the last column elements. - Print each element of the last column along with its position.
- End the program.
Source Code:
#include <stdio.h>
int main()
{
int n;
scanf("%d", &n);
int a[n][n];
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
scanf("%d", &a[i][j]);
}
}
for (int i = 0; i < n; i++)
{
printf("a[%d][%d] is %d\n", i, n - 1, a[i][n - 1]);
}
return 0;
}Result: The program successfully prints all the elements of the last column of the entered square matrix.
4.### Program: Sort Elements of an Array in Ascending Order
Aim:
To write a C program that reads elements into an array and sorts them in ascending order using the Bubble Sort algorithm.
Algorithm:
- Start the program.
- Declare an integer array
arr[n]and variables for loops and swapping. - Read the size of the array
n. - Input
nelements into the array. - Use Bubble Sort technique:
- Repeat the process
n-1times. - Compare each pair of adjacent elements.
- Swap them if they are in the wrong order.
- Repeat the process
- Print the sorted array elements.
- End the program.
Source Code:
#include <stdio.h>
int main()
{
int n;
scanf("%d", &n);
int arr[n];
int i, j, temp;
for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
for (i = 0; i < n - 1; i++) {
for (j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
for (i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
return 0;
}Result: The program sorts the entered array elements in ascending order and displays the sorted array.
5.### Program: Count Even and Odd Numbers in an Array
Aim:
To write a C program that reads an array of integers and counts the number of even and odd elements using loops.
Algorithm:
- Start the program.
- Declare variables for array size
n, counter variablescount(for even) andacount(for odd). - Input the size of the array
n. - Read
nintegers into the arrayarr. - Use a loop to traverse each element:
- If the element is divisible by 2, increment the even counter.
- Otherwise, increment the odd counter.
- Display the count of even and odd numbers.
- End the program.
Source Code:
#include <stdio.h>
int main()
{
int n, i, count = 0, acount = 0; // initialize acount = 0 to avoid garbage value
scanf("%d", &n);
int arr[n];
for (i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
}
for (i = 0; i < n; i++)
{
if (arr[i] % 2 == 0)
{
count++;
}
else
{
acount++;
}
}
printf("Even numbers: %d\n", count);
printf("Odd numbers: %d\n", acount);
return 0;
}



