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

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.

Posted Date: 4/15/2013 3:47:01 AM | Location : United States







Related Discussions:- Write a program to calculate the total resistance, Assignment Help, Ask Question on Write a program to calculate the total resistance, Get Answer, Expert's Help, Write a program to calculate the total resistance Discussions

Write discussion on Write a program to calculate the total resistance
Your posts are moderated
Related Questions
For the circuit shown in Figure, verify the p.d. across resistor R3. If the total resistance of the circuit is 100Ω, verify the current flowing by resistor R1. Find also the


A: It allows a program to reference an identifier in global scope which has been hidden by another identifier along with the same name in the local scope.

Aim: Write a program of multipication using inline function class Multiply {           int x,y;           public:           void getdata();           inline calc

Problem: (a) A GUI can contain text-fields, buttons, and other labels. A button usually triggers an event on the GUI. Explain the different processes in registering an event

Call by reference: Arguments to a function can be passed in two way; one by call by value and other by call by reference.  Call by value is passed either through a constant

I am an online tutor who can teach computer science and programming language. Is there any requirement for this?

The Main program is slightly more complicated and you need to think about the I/O process. First of all, for most applications you do not call the low level routines GetChar and P

A Padovan string P(n) for a natural number n is defined as: P(0) = ‘X’ P(1) = ‘Y’ P(2) = ‘Z’ P(n) = P(n-2) + P(n-3), n>2 where + denotes string concatenation. For a string of t

Specifying a Class: As discussed a class is defined to develop an algorithm and bind it together in a core shell. A class is an abstract data type (ADT).  The binding of dat