3Q) Implement structures in C (Add &Subtract complex numbers using tstruct)
#include <stdio.h>
#include <conio.h>
struct Complex {
float real;
float imaginary;
};
int main() {
struct Complex num1, num2, sum, difference;
clrscr();
printf("Enter real part of the first complex number: ");
scanf("%f", &num1.real);
printf("Enter imaginary part of the first complex number: ");
scanf("%f", &num1.imaginary);
printf("Enter real part of the second complex number: ");
scanf("%f", &num2.real);
printf("Enter imaginary part of the second complex number: ");
scanf("%f", &num2.imaginary);
sum.real = num1.real + num2.real;
sum.imaginary = num1.imaginary + num2.imaginary;
printf("\nSum: %.2f + %.2fi\n", sum.real, sum.imaginary);
difference.real = num1.real - num2.real;
difference.imaginary = num1.imaginary - num2.imaginary;
printf("Difference: %.2f + %.2fi\n", difference.real, difference.imaginary);
getch();
return 0;}
Q) Cacl length of str & Print reverse of str
#include <stdio.h>
#include <conio.h>
int calculateLength(char str[]) {
int length = 0;
while (str[length] != '\0') {
length++;
return length;
void printReverse(char str[]) {
int length = calculateLength(str);
for (int i = length - 1; i >= 0; i--) {
printf("%c", str[i]);
printf("\n");
int main() {
char str1[] = "101";
char str2[] = "13/3/27";
char str3[] = "2210 ^ 2";
int length1 = calculateLength(str1);
printf("Length of '101': %d\n", length1);
int result1 = 13 / 3 / 27;
printf("Result of 13/3/27: %d\n", result1);
int result2 = 2210 * 2210;
printf("2210 ^ 2: %d\n", result2);
printf("Reverse of '101': ");
printReverse(str1)
getch(); return 0;
Q) Two Dimensional array
#include <stdio.h>
#include <conio.h>
int main() {
int matrix1[3][3], matrix2[3][3], sum[3][3];
clrscr();
printf("Enter elements of matrix1 (3x3):\n");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
scanf("%d", &matrix1[i][j]);
printf("Enter elements of matrix2 (3x3):\n");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
scanf("%d", &matrix2[i][j]);
printf("\nSum of matrices:\n");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
sum[i][j] = matrix1[i][j] + matrix2[i][j];
printf("%d ", sum[i][j]);
printf("\n");
getch(); return 0;}
Q) One Dimensional Array
#include <stdio.h>
#include <conio.h>
int main() {
int numbers[5], sum = 0;
clrscr();
printf("Enter 5 numbers:\n");
for (int i = 0; i < 5; i++) {
scanf("%d", &numbers[i]);
sum += numbers[i]; // Add each number to the sum
printf("\nNumbers entered:\n");
for (int i = 0; i < 5; i++) {
printf("%d ", numbers[i]);
printf("\nSum of the numbers: %d\n", sum);
getch();
return 0;
Q) Well Commented Inout/Output Statements
#include <stdio.h>
#include <conio.h>
int main() {
clrscr(); // Clear the screen
// Declare variables to store student information
char firstName[50], middleName[50], lastName[50];
int rollNumber;
float percentage;
int birthDay, birthMonth, birthYear;
char branch[50], college[50];
// Input student information
printf("Enter student's name (First Middle Last): ");
scanf("%s %s %s", firstName, middleName, lastName);
printf("Enter roll number: ");
scanf("%d", &rollNumber);
printf("Enter percentage (up to 2 decimal places): ");
scanf("%f", &percentage);
printf("Enter date of birth (DD MM YYYY): ");
scanf("%d %d %d", &birthDay, &birthMonth, &birthYear);
printf("Enter branch and college: ");
scanf("%s %s", branch, college);
// Output student information
printf("\nStudent Information:\n");
printf("Name: %s %s %s\n", firstName, middleName, lastName);
printf("Roll Number: %d\n", rollNumber);
printf("Percentage: %.2f\n", percentage); // %.2f specifies two decimal places
printf("Date of Birth: %02d/%02d/%04d\n", birthDay, birthMonth, birthYear); // %02d pads with
leading zeros if needed
printf("Branch, College: %s, %s\n", branch, college);
getch(); // Wait for a key press
return 0;