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
Byteland county is very famous for luminous jewels. Luminous jewels are used in making beautiful necklaces. A necklace consists of various luminous jewels of particular colour. Nec

Example of structure: struct item {                 int element;                 node_ptr next; }; typedef node_ptr stack; stack create(void) {

Can I drop the [] while deleteing array of some built-in type (char, int, etc)? A: No. you can't Sometimes programmers think that the [] in the delete[] p only present so the

Project Description: 1) Develop banking software 2) Payment Gateway System There is some other system available for development. I'm willing to show long-term opportunity

find the greater of the two variables, without using conditional loops or ternary operators?

What are the debugging methods you employ while came across a problem? A: Debugging with tools such as: 1.      DBG, GDB ,Forte, Visual Studio. 2.      Using tusc to trace

The #undef Directive This directive undefines a previously explained macro. For, example the following will give an error since PI is undefined.                 #define PI 3

This project simulates an application called tracker for the Department of Transportation (DOT) in which highway traffic data is accumulated in real time using various sensing equi

Ravi is a newbie to the programming and while learning the programming language he came to know the following rules:

Write a program to find the area under the curve y = f(x) between x = a and x = b, integrate y = f(x) between the limits of a and b. The area under a curve between two points can b