Skip to main content

C Program to Add Two Complex Numbers by Passing Structure to a Function

 

Add Two Complex Numbers

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

complex add(complex n1, complex n2);

int main() {
    complex n1, n2, result;

    printf("For 1st complex number \n");
    printf("Enter the real and imaginary parts: ");
    scanf("%f %f", &n1.real, &n1.imag);
    printf("\nFor 2nd complex number \n");
    printf("Enter the real and imaginary parts: ");
    scanf("%f %f", &n2.real, &n2.imag);

    result = add(n1, n2);

    printf("Sum = %.1f + %.1fi", result.real, result.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 the real and imaginary parts: 2.1
-2.3

For 2nd complex number
Enter the real and imaginary parts: 5.6
23.2
Sum = 7.7 + 20.9i

Comments

Popular posts from this blog

C Program to Calculate Difference Between Two Time Periods

  Calculate Difference Between Two Time Periods # include <stdio.h> struct TIME { int seconds; int minutes; int hours; }; void differenceBetweenTimePeriod (struct TIME t1, struct TIME t2, struct TIME *diff) ; int main () { struct TIME startTime , stopTime , diff ; printf ( "Enter the start time. \n" ); printf ( "Enter hours, minutes and seconds: " ); scanf ( "%d %d %d" , &startTime.hours, &startTime.minutes, &startTime.seconds); printf ( "Enter the stop time. \n" ); printf ( "Enter hours, minutes and seconds: " ); scanf ( "%d %d %d" , &stopTime.hours, &stopTime.minutes, &stopTime.seconds); // Difference between start and stop time differenceBetweenTimePeriod(startTime, stopTime, &diff); printf ( "\nTime Difference: %d:%d:%d - ...

which is the top IIT for computer science for mtech in india

Indian Institute of Technology (IIT) Delhi is considered to be the top IIT for Computer Science for M.Tech in India. The Department of Computer Science and Engineering at IIT Delhi offers a comprehensive M.Tech program in Computer Science and Engineering, which covers both theoretical and practical aspects of the field.  IIT Delhi has a distinguished faculty, state-of-the-art infrastructure, and research opportunities, which make it an ideal place to pursue M.Tech in Computer Science. Additionally, the institute has collaborations with leading organizations and industries, which provides students with exposure to cutting-edge research and industry trends.  However, it is important to note that other IITs such as IIT Bombay, IIT Kanpur, IIT Madras, and IIT Kharagpur also offer excellent M.Tech programs in Computer Science and Engineering. The choice of the institute ultimately depends on your personal preferences, research interests, and career goals.

C Program to Read a Line From a File and Display it

  Program to read text from a file # include <stdio.h> # include <stdlib.h> // For exit() function int main () { char c[ 1000 ]; FILE *fptr; if ((fptr = fopen( "program.txt" , "r" )) == NULL ) { printf ( "Error! opening file" ); // Program exits if file pointer returns NULL. exit ( 1 ); } // reads text until newline is encountered fscanf (fptr, "%[^\n]" , c); printf ( "Data from the file:\n%s" , c); fclose(fptr); return 0 ; } If the file is found, the program saves the content of the file to a string  c  until  '\n'  newline is encountered. Suppose the  program.txt  file contains the following text in the current directory. C programming is awesome. I love C programming. How are you doing? The output of the program will be: Data from the file: C programming is awesome. If the file  program.txt  is not found, this program prints an error message.