Skip to main content

C-Programming Program 7(a)

7.    Write a C program that uses functions to perform the following operations:
             i) Reading a complex number
              ii) Writing a complex number
            iii)Addition of two complex numbers

Flowchart 





Program

#include<stdio.h>
struct complex{
    float real;
    float imag;
};
struct complex read_complex(float ral,float img)
{
struct complex result;
result.real=ral;
result.imag=img;
return result;
}
void write_complex(struct complex compl)
{
printf("Writing complex Number\n");
printf("%.2f + i%.2f\n\n", compl.real,compl.imag);
}
struct complex add_complex(struct complex comp1,struct complex comp2){
    struct complex temp;
    temp.real = comp1.real + comp2.real;
    temp.imag = comp1.imag + comp2.imag;
    return temp;
}
   main(){
    struct complex comp1,comp2,result;
    float i,j;
    printf("Enter Real and Imaginary values for complex No: \n");
    scanf("%f%f",&i, &j);
    comp1=read_complex(i,j);
    write_complex(comp1);
    printf("Enter Real and Imaginary values for complex No: \n");
    scanf("%f%f",&i, &j);
    comp2=read_complex(i,j);
    write_complex(comp2);
    printf("Addition of two complex No \n");
    result = add_complex(comp1,comp2);
    printf("Result after adding 2 complex No's \n");
    write_complex(result);
}

Comments

Popular posts from this blog

C-Programming with Record

C-Programming Programs Syllabus Click for full record  1.a) Write a C program to ask the user to enter one char ( Upper-Case letter) check  whether user  entered a Upper-case letter or not(by using relational and logical operators) and then if user has  entered a Upper-case letter  convert into a Lower-case letter? ( hint: Upper-case means capital letters,  use ASCII information to check for Upper-case and convert) b) Write a  C program to ask the user to enter two integers and apply all arithmetic operations on those  print the corresponding values?(hint : +,-,*,/,%)     c) Write a C program to Determine the ranges of char, short, int and long int variables both signed and  Unsigned   By using sizeof operator(ii) By printing appropriate values from standard header (limits.h )   2.a) Write a Program to Find the Roots of a Quadratic Equation using if else and Switch statements. b) Write a Program which Gener...

OOPS CPP Program-14

14. Write a program to find transpose of a given matrix of mxn size using unary operator overloading #include<iostream> using namespace std; class matrix {         int x[5][5],m,n; public:         matrix(){ }         matrix(int a,int b)         {                m=a;n=b;         }         void get();         void put();         matrix operator !(); }; void matrix::get() {         int i,j;         cout<<"\n Enter a matrix of order"<<m<<"x"<<n<<"\n";                for(i=0;i<m;i++)                    for(j=0;j<n;j++)                        ...

OOPS CPP Program-15

15. Write a program to concatenate one string to another using binary operator overloading. #include<iostream> #include<string.h> using namespace std; class str  {         char *st; public:         str(int len, char *s)         {                  int l=strlen(s);                st= new char[l];                st=s;          }         str()         {         }         void put();         str operator +(str); }; str str :: operator +(str s)  {  /*str abc; abc.st=strcat(st, s.st); return abc;*/         st=strcat(st, s.st);         return *this; } void str :: put() {   cout<<...