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

Is always the default constructor for fred fred::fred()?, Is always the def...

Is always the default constructor for Fred Fred::Fred()?

C code, get coding for padovan string

get coding for padovan string

C program to check prime numbers , C Program to check PRIME NUMBERS   ...

C Program to check PRIME NUMBERS   main() {           int i,k,r,flag;           clrscr();           printf("ENTER THE NO. TO CHECK IT IS PRIME OR NOT: ");

Cange to palindrome, A palindrome is a string that reads the same from the ...

A palindrome is a string that reads the same from the both the ends. Given a string S convert it to a palindrome by doing character replacement. Your takes is to convert S to palin

Define functions with arguments and no return values, Define Functions with...

Define Functions with arguments and no return values The calling function will read a data from the terminal and pass it on to called as function and this will work good as the

Object oriented programming, implementation of the power and factorial in p...

implementation of the power and factorial in programs

Class and object, how to write c++ code for financial system using class an...

how to write c++ code for financial system using class and object

Pseudo code, pseudo code that will determine if the number is prime or comp...

pseudo code that will determine if the number is prime or composite

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