In: Computer Science
Code1:
#include
int main(){
int res1, res2, res;
printf("\nEnter Value of Each Resistance 1: ");
scanf("%d", &res1);
printf("\nEnter Value of Each Resistance 2: ");
scanf("%d", &res2);
res = res1+res2;
printf("\nEquivalent Series Resistance : %d Kohm\n",
res);
return (0);
}
----------------------------------------------------------------------------------------------------------
Code2:
#include
int main(){
int res1, res2, res;
printf("\nEnter Value of Each Resistance 1: ");
scanf("%d", &res1);
printf("\nEnter Value of Each Resistance 2: ");
scanf("%d", &res2);
res = (res1*res2)/(res1+res2);
printf("\nEquivalent Parellel Resistance : %d Kohm\n",
res);
return (0);
}
Take the two programs and combine them into a single program(using c not c++) that uses an if and else statement to calculate R total either in series or parallel based on the users preference (ask the users which way they want the resistance calculated).
main.c
#include<stdio.h>
int main(){
int res1, res2, res,start;
printf("Enter Value of Each Resistance 1: ");
scanf("%d", &res1);
printf("\nEnter Value of Each Resistance 2: ");
scanf("%d", &res2);
printf("\nEnter 0 to quite\nEnter 1 for series
circuit\nEnter 2 for parallel circuit: ");
scanf("%d",&start);
if (start==0){
return 0;
}
else if (start==1){
res = res1+res2;
printf("\nEquivalent Series Resistance : %d Kohm\n",
res);
}
else if (start==2){
res = (res1*res2)/(res1+res2);
printf("\nEquivalent Parellel Resistance : %d Kohm\n",
res);
}
else {
printf("Error!");}
return (0);
}
Output:-
Enter Value of Each Resistance 1: 100 Enter Value of Each Resistance 2: 200 Enter 0 to quite Enter 1 for series circuit Enter 2 for parallel circuit: 2 Equivalent Parellel Resistance : 66 Koh