Q:

C Program To Add Two Complex Numbers Using Structures And Functions

belongs to collection: Structure Programs in C Programming

0

C Program To Add Two Complex Numbers By Passing Structure With Function Using Structure In C 


What Is Structure

The structure is a user-defined data type in C which allows you to combine different data types to store a particular type of record. The structure is used to represent a record. Suppose you want to store a record of Student which consists of student name, address, roll number and age. You can define a structure to hold this information.


Defining a structure

struct keyword is used to define a structure. struct define a new data type which is a collection of different type of data.

Syntax :

struct structure_name

{
//Statements
};

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

#include <stdio.h>
typedef struct complex
{
float real;
float imag;
}complex;

complex add(complex n1,complex n2);

int main()
{
complex n1,n2,temp;
printf("For 1st complex number \n");
printf("Enter real and imaginary respectively:\n");
scanf("%f%f",&n1.real,&n1.imag);
printf("\nFor 2nd complex number \n");
printf("Enter real and imaginary respectively:\n");
scanf("%f%f",&n2.real,&n2.imag);
temp=add(n1,n2);
printf("Sum=%.1f+%.1fi",temp.real,temp.imag);
return 0;
}

complex add(complex n1,complex n2)
{
complex temp;
temp.real=n1.real+n2.real;
temp.imag=n1.imag+n2.imag;
return(temp);
}

 

Output:

For 1st complex number 

Enter real and imaginary respectively:

3.5

6.5

For 2nd complex number 

Enter real and imaginary respectively:

45.2

69.96

Sum=48.7+76.5i

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C Program To Find Difference Between Two Dates Usi... >>
<< C Program To Store Information of Students Using S...