- 20th Mar 2020
- 03:41 am
- Ali Akavis
C Programming Assignment Problem Statement
To design and develop a program for students to practice their electric circuit skills. The students can choose a circuit type (parallel or series), the number of circuit elements (up to 5) and value of each circuit element. Based on the input, the circuit analysis is done and the results are displayed in a user-friendly format. The student can then continue with another circuit or exit the program. Voltage Source will be set to 12V.
C Programming Assignment Solution
#include
int main(void)
{
int num,ans;
float result;
/*A do while loop to accept circuit configurations as many
times as the user wants. */
do{
//Number of resistors in the cirucit
num=0;
//Set the answer to 0.
result = 0;
//Accept the number of circuit elements
printf("Enter number of cicuit elements (1-5):\t\n");
scanf("%d",&num);
//Ask the user for series or parallel
char c;
printf("Series or parallel ? (s or p):\t");
//Skip the enter key
scanf("%c",&c);
scanf("%c",&c);
//Accept as many resistance values as the number above
float temp; // a variable to accept input from the user
for(int i=0;i {
printf("\nEnter value of resistance %d:\t",i+1);
scanf("%f",&temp);
if(temp<=0) //invalid entry
{
printf("\nInvalid entry. Enter again!");
--i;
}
else
{
if(c=='s')
result = result + temp;
else if(c=='p')
result = result + (1.0/temp);
else
{
printf("\nInvalid entry.Exiting...");
return 0;
}
}
}
if(c=='p')
result = 1.0/result;
printf("\nResistance of the circuit = %f",result);
printf("\nCurrent in the circuit = %f",12.0/result);
//Ask the user if he wishes to enter another ciruit.
printf("\nPress 1 to enter another circuit, any other key to exit:\t");
scanf("%d",&ans);
}while(ans==1);
printf("\nBye! Thank you.");
return 0;
}