To create a program that calculates a person's age in years, months, and days based on their birthdate and the current date.
- Begin the program.
- Define a function
calculateAge()
that takes six parameters:birth_day
,birth_month
,birth_year
,current_day
,current_month
, andcurrent_year
. - Inside the function:
- Calculate the difference in years, months, and days between the birthdate and the current date.
- Adjust the age if the current day or month is less than the birth day or month respectively.
- Print the calculated age in years, months, and days.
- In the
main()
function:- Define variables
birth_day
,birth_month
,birth_year
,current_day
,current_month
, andcurrent_year
with predefined values representing the birthdate and the current date. - Call the
calculateAge()
function with these variables as arguments.
- Define variables
- End the program.
#include <stdio.h>
void calculateAge(int birth_day, int birth_month, int birth_year,
int current_day, int current_month, int current_year) {
int age_years, age_months, age_days;
age_years = current_year - birth_year;
age_months = current_month - birth_month;
age_days = current_day - birth_day;
if (age_days < 0) {
age_months--;
age_days += 31;
}
if (age_months < 0) {
age_years--;
age_months += 12;
}
printf("Present Age Years: %d Months: %d Days: %d\n",
age_years, age_months, age_days);
}
int main() {
int birth_day = 12, birth_month = 12, birth_year = 1990;
int current_day = 8, current_month = 10, current_year = 2020;
calculateAge(birth_day, birth_month, birth_year, current_day,
current_month, current_year);
return 0;
}
Thus the required program is written and executed successfully.
To develop a program that checks the validity of a given date.
- Begin the program.
- Define a function
checkDateValidity(int d, int m, int y)
that takes three parameters representing the day, month, and year of the date to be checked. - Inside the function:
- Declare an array
daysInMonth[]
to store the number of days in each month. - Check if the year is valid (between 1900 and 9999).
- Check if the month is valid (between 1 and 12).
- Adjust the number of days in February for leap years.
- Check if the day is valid based on the number of days in the given month.
- Print whether the day is valid or invalid.
- Declare an array
- In the
main()
function:- Declare variables
d
,m
, andy
to store the day, month, and year respectively. - Prompt the user to input the date in the format "dd/mm/yyyy".
- Read the input date using
scanf()
function. - Call the
checkDateValidity()
function with the input date as arguments.
- Declare variables
- End the program.
#include <stdio.h>
void checkDateValidity(int d, int m, int y) {
int daysInMonth[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
if (y < 1900 || y > 9999) {
printf("Year is not valid.\n");
return;
}
if (m < 1 || m > 12) {
printf("Month is not valid.\n");
return;
}
if (y % 400 == 0 || (y % 4 == 0 && y % 100 != 0)) {
daysInMonth[1] = 29;
}
if (d < 1 || d > daysInMonth[m - 1]) {
printf("Day is invalid.\n");
return;
}
printf("Day is valid.\n");
}
int main() {
int d, m, y;
scanf("%d/%d/%d", &d, &m, &y);
checkDateValidity(d, m, y);
return 0;
}
Thus the required program is written and executed successfully.
To create a program that prints a specific pattern of numbers.
- Begin the program.
- Use nested for loops to iterate over rows and columns:
- Outer loop (
r
loop) iterates from 1 to 5. - Inner loop (
c
loop) iterates from 1 to 5. - Inside the inner loop, use conditional statements to determine the value to be printed based on the row and column indices:
- If
r
andc
are both 3, print "1 ". - If
r
orc
is 1, 2, 4, or 5, print "3 ". - If
r
orc
is 2, 3, or 4, print "2 ".
- If
- Print a newline character after each row.
- Outer loop (
- End the program.
#include<stdio.h>
int main(){
for(int r=1;r<=5;r++)
{
for(int c=1;c<=5;c++)
{
if(r==3&& c==3)
printf("1 ");
else if(c==1 || r==1 || c==5 || r==5)
printf("3 ");
else if(r==2 || r==3 || r==4 || c==2 || c==3 || c==4)
printf("2 ");
else
printf("3 ");
}
printf("\n");
}
}
Thus the required program is written and executed successfully.
To develop a program that prints the square numbers within a given range [a, b]
.
- Begin the program.
- Declare variables
a
,b
, andsum
of type int to store the range limits and the square of each number. - Prompt the user to input two integers
a
andb
representing the range. - Read the input values for
a
andb
usingscanf()
function. - Use a for loop with loop variable
i
initialized toa
and terminating conditioni <= b
. - Inside the loop:
- Calculate the square of
i
and store it insum
. - Check if
sum
is within the range[a, b]
:- If true, print
sum
.
- If true, print
- Calculate the square of
- End the program.
#include<stdio.h>
int main()
{
int a,b,sum=0;
scanf("%d%d",&a,&b);
for(int i=a;i<=b;i++)
{
sum=i*i;
if(sum>=a && sum <= b)
printf("%d ",sum);
}
}
Thus the required program is written and executed successfully.
To develop a program that prints the diagonal elements of a square matrix in reverse order.
- Begin the program.
- Declare variables
n
,i
, andj
of type int to store the size of the square matrix and loop indices. - Prompt the user to input the size of the square matrix
n
. - Read the input value for
n
usingscanf()
function. - Declare a 2D array
matrix
of sizen x n
. - Use nested for loops to input the elements of the square matrix:
- Outer loop (
i
loop) iterates over rows from 0 ton-1
. - Inner loop (
j
loop) iterates over columns from 0 ton-1
. - Inside the inner loop, input the matrix element at position
(i, j)
.
- Outer loop (
- Use a reverse for loop to print the diagonal elements of the matrix:
- Start the loop from
n-1
and iterate until 0. - Print the element
matrix[i][i]
at position(i, i)
of the diagonal.
- Start the loop from
- End the program.
#include <stdio.h>
int main() {
int n, i, j;
scanf("%d", &n);
int matrix[n][n];
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
scanf("%d", &matrix[i][j]);
}
}
for (i = n - 1; i >= 0; i--) {
printf("a[%d][%d] is %d\n", i, i, matrix[i][i]);
}
return 0;
}
Thus the required program is written and executed successfully.
To create a program that identifies and prints the odd elements of a square matrix.
- Begin the program.
- Declare variables
n
,i
, andj
of type int to store the size of the square matrix and loop indices. - Prompt the user to input the size of the square matrix
n
. - Read the input value for
n
usingscanf()
function. - Declare a 2D array
matrix
of sizen x n
. - Use nested for loops to input the elements of the square matrix:
- Outer loop (
i
loop) iterates over rows from 0 ton-1
. - Inner loop (
j
loop) iterates over columns from 0 ton-1
. - Inside the inner loop, input the matrix element at position
(i, j)
.
- Outer loop (
- Use nested for loops to traverse the matrix and identify odd elements:
- Outer loop (
i
loop) iterates over rows from 0 ton-1
. - Inner loop (
j
loop) iterates over columns from 0 ton-1
. - Inside the inner loop, check if the element at position
(i, j)
is odd (i.e.,matrix[i][j] % 2 != 0
). - If the element is odd, print its position and value.
- Outer loop (
- Print a newline after each row is traversed.
- End the program.
#include <stdio.h>
int main() {
int n, i, j;
scanf("%d", &n);
int matrix[n][n];
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
scanf("%d", &matrix[i][j]);
}
}
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
if (matrix[i][j] % 2 != 0) {
printf("a[%d][%d] is %d\n", i, j, matrix[i][j]);
}
}
printf("\n");
}
return 0;
}
Thus the required program is written and executed successfully.
To create a program that calculates and prints the sum, count of even numbers, and count of odd numbers in an array.
- Begin the program.
- Prompt the user to input the size of the array
size
. - Declare an array
arr
of sizesize
. - Use a for loop to input elements into the array:
- Iterate
i
from 0 tosize-1
. - Read the input value for
arr[i]
usingscanf()
function.
- Iterate
- Initialize variables
sum
,evenCount
, andoddCount
to store the sum, count of even numbers, and count of odd numbers respectively. - Use a for loop to traverse the array and calculate the sum and count of even and odd numbers:
- Iterate
i
from 0 tosize-1
. - Update
sum
by addingarr[i]
. - If
arr[i]
is even (i.e.,arr[i] % 2 == 0
), incrementevenCount
. - Otherwise, increment
oddCount
.
- Iterate
- Print the calculated sum, count of even numbers, and count of odd numbers.
- End the program.
#include <stdio.h>
int main() {
int size;
scanf("%d", &size);
int arr[size];
for (int i = 0; i < size; i++) {
scanf("%d", &arr[i]);
}
// Initialize variables to store sum, count of even and odd numbers
int sum = 0;
int evenCount = 0;
int oddCount = 0;
// Iterate through the array to calculate sum and count of even and odd numbers
for (int i = 0; i < size; i++) {
sum += arr[i];
if (arr[i] % 2 == 0)
evenCount++;
else
oddCount++;
}
// Print the results
printf("Total Sum: %d\n", sum);
printf("Even numbers: %d\n", evenCount);
printf("Odd numbers: %d\n", oddCount);
return 0;
}
Thus the required program is written and executed successfully.
To develop a program that identifies and counts duplicate elements in an array.
- Begin the program.
- Prompt the user to input the size of the array
size
. - Declare an array
arr
of sizesize
. - Use a for loop to input elements into the array:
- Iterate
i
from 0 tosize-1
. - Read the input value for
arr[i]
usingscanf()
function.
- Iterate
- Initialize a variable
count
to store the count of duplicate elements. - Use nested for loops to compare each element of the array with all subsequent elements:
- Outer loop (
i
loop) iterates from 0 tosize-1
. - Inner loop (
j
loop) iterates fromi+1
tosize-1
. - Inside the inner loop, check if
arr[i]
is equal toarr[j]
. - If a duplicate element is found, increment the
count
and break out of the inner loop.
- Outer loop (
- Print the total number of duplicate elements found in the array.
- End the program.
#include <stdio.h>
int main() {
int size;
scanf("%d", &size);
int arr[size];
for (int i = 0; i < size; i++) {
scanf("%d", &arr[i]);
}
int count = 0;
for (int i = 0; i < size; i++) {
for (int j = i + 1; j < size; j++) {
if (arr[i] == arr[j]) {
count++;
break;
}
}
}
printf("Total number of duplicate elements found in array = %d\n", count);
return 0;
}
Thus the required program is written and executed successfully.