Write a program to calculate the total resistance, C/C++ Programming

Assignment Help:

Write a program to calculate the total resistance of a series or parallel circuit. The maximum number of resistors is two.

1758_Write a program to calculate the total resistance.png

  We need to decide whether the user wants the total resistance of a parallel or series circuit. Let us use scanf:
 
  
  printf("Parallel (P) or Series (S) \n\r");
  scanf("\n%c",&type);
 
Note: "\n%c" is used to remove the carriage return, when we enter in P we follow it by a carriage return i.e. 2 characters so "\n" removes the second character from the awaiting buffer. Let us Input the resistor values

  printf(" Resistor Value R1\n\r");
  scanf("%f",&r1);
  printf(" Resistor Value R2\n\r");
  scanf("%f",&r2);
 
The If statement allows us to test type

  if (type == 'p' )
    {
      rt = (r1*r2)/(r1+r2);
    }
  else
    {
      rt = r1 + r2;
    }
      
We can print out the total resistance
 
  printf("Total resistance = %5.2f\n\r",rt); 

The whole program is  
 
  include
  include
  include
  include
  include
  void main()
  {
    char prompt;
     char type ;
    float r1,r2,rt;
    printf("Parallel (P) or Series (S) \n\r");
    scanf("\n%c",&type);
    printf(" Resistor Value R1\n\r");
    scanf("%f",&r1);
    printf(" Resistor Value R2\n\r");
    scanf("%f",&r2);
    if (type == 'p') 
    {
      rt = (r1*r2)/(r1+r2);
    }
    else
    {
      rt = r1 + r2;
    }
    printf("Total resistance = %5.2f\n\r",rt);   
    printf("Press any key to exit \n\r");
    scanf("\n%c",&prompt);
    }
 
  It is worth noting that the If statement is case sensitive i.e. try 'p' instead of 'P'. Care must be taken and code should cater for upper and lower type. Considering the above program what if we want to calculate ten sets of resistor combinations, we would have to repeat this section of code ten times. Within C we have the ability to repeat sections of code using three different types of code namely:  while, do while and For statements.


Related Discussions:- Write a program to calculate the total resistance

Dynamic memory management, C and C++ require explicit dynamic memory manage...

C and C++ require explicit dynamic memory management, using new and delete or malloc() and free(). It is helpful to understand where variables exist (usually the stack or the he

What is the reward of operator overloading?, A: you can exploit by overload...

A: you can exploit by overloading standard operators on a class, the intuition of the users of that class. This allow users program in the language of the problem domain instead of

Expression, i need expression and its types with example programs in c++

i need expression and its types with example programs in c++

Programming an odds betting website, Programming an odds betting website ...

Programming an odds betting website Project Description: We want someone to program a odds betting website, Require someone with experience and skills! Skills required are

Asset pricing project, In the final project assignment you are asked to dev...

In the final project assignment you are asked to develop an OOP C++ class hierarchy for derivative pricing, using the binomial tree and Black-Scholes option pricing methods. You wi

Decode, Problem Description Smugglers are becoming very smart day by day. N...

Problem Description Smugglers are becoming very smart day by day. Now they have developed a new technique of sending their messages from one smuggler to another. In their new techn

Described assignment operator?, Default assignment operator mange assigning...

Default assignment operator mange assigning one object to another object of the same class. It is member to member copy as shallow copy.

Define the stream oriented data files, Define the Stream Oriented Data File...

Define the Stream Oriented Data Files? There are two dissimilar types of data files called stream-oriented (or standard) data files and system-oriented (or low-level) data file

Addition in a existing c++ unification algorithm, Addition in a existing c+...

Addition in a existing c++ unification algorithm Project Description: I have a existing code for a algorithm and need to add two modules into it, it is a unification algorith

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd