Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

how would i modify this code to output the result to a file, the whole statment.

ID: 3729845 • Letter: H

Question

how would i modify this code to output the result to a file, the whole statment.


#include
#include
#include
int main()
{
char ch;
float voltage , current , resistance , result, R1, R2;

printf("Choose from 1 to 6 from the following: ");
printf("1.Ohm's Law To Find the voltage. ");
printf("2.Ohm's Law To Find the current. ");
printf("3.Ohm's Law To Find the resistance. ");
printf("4.Voltage Divider To find the voltage accros R1. ");
printf("5.Voltage Divider To find the voltage accros R2. ");
printf("6.Current Divider To find the Current passing through R1. ");
printf("7.Current Divider To find the Current passing through R2. ");
printf("Anything else to quit. ");

scanf("%c",&ch);
switch(ch)
{
case '1' :
printf("Current Value? ");
scanf("%f",¤t);
printf("Resistance Value? ");
scanf("%f",&resistance);
result = current * resistance;
printf("The voltage is %0.2f V. ",result);
break;
case '2' :
printf(" voltage value? ");
scanf("%f",&voltage);
printf("Resistance value? ");
scanf("%f",&resistance);
result = voltage / resistance;
printf("The current is %0.2f A. ",result);
break;
case '3' :
printf("voltage value? ");
scanf("%f",&voltage);
printf("current Value? ");
scanf("%f",¤t);
result = voltage / current;
printf("The resistance is %0.2f ohms. ",result);

case '4' :
printf("R1 Value? ");
scanf("%f",&R1);
printf("R2? ");
scanf("%f",&R2);
printf("Voltage Value? ");
scanf("%f",&voltage);
result = R1*voltage/(R1+R2);
printf("The voltage is %0.2f V. ",result);
break;

case '5' :
printf("R1 Value? ");
scanf("%f",&R1);
printf("R2? ");
scanf("%f",&R2);
printf("Voltage Value? ");
scanf("%f",&voltage);
result = R2*voltage/(R1+R2);
printf("The voltage is %0.2f V. ",result);
break;
case '6' :
printf("R1 Value? ");
scanf("%f",&R1);
printf("R2 value? ");
scanf("%f",&R2);
printf("current Value? ");
scanf("%f",¤t);
result = R2*current/(R1+R2);
printf("The current is %0.2f A. ",result);
break;
case '7' :
printf("R1 Value? ");
scanf("%f",&R1);
printf("R2 Value? ");
scanf("%f",&R2);
printf("current Value? ");
scanf("%f",¤t);
result = R1*current/(R1+R2);
printf("The current is %0.2f A. ",result);
break;
break;
default :
exit(0);
return 0;}
return 0;
}

Explanation / Answer

Program to write result to file

This program will write the result of each case to file out.txt

#include<stdio.h>

int main()
{
char ch;
float voltage , current , resistance , result, R1, R2;
static const char filename[] = "out.txt";   //Declare file name
FILE *fptr = fopen(filename, "w");          //Open file in write mode


   if(fptr == NULL)
   {
      printf("Error!");
      exit(1);
   }

printf("Choose from 1 to 6 from the following: ");
printf("1.Ohm's Law To Find the voltage. ");
printf("2.Ohm's Law To Find the current. ");
printf("3.Ohm's Law To Find the resistance. ");
printf("4.Voltage Divider To find the voltage across R1. ");
printf("5.Voltage Divider To find the voltage across R2. ");
printf("6.Current Divider To find the Current passing through R1. ");
printf("7.Current Divider To find the Current passing through R2. ");
printf("Anything else to quit. ");

scanf("%c",&ch);
switch(ch)
{
case '1' :
printf("Current Value? ");
scanf("%f",&current);
printf("Resistance Value? ");
scanf("%f",&resistance);
result = current * resistance;
printf("The voltage is %0.2f V. ",result);
fprintf(fptr,"%f",result);         //Write result to file

break;
case '2' :
printf(" voltage value? ");
scanf("%f",&voltage);
printf("Resistance value? ");
scanf("%f",&resistance);
result = voltage / resistance;
printf("The current is %0.2f A. ",result);
fprintf(fptr,"%f",result);     //Write result to file
break;
case '3' :
printf("voltage value? ");
scanf("%f",&voltage);
printf("current Value? ");
scanf("%f",&current);
result = voltage / current;
printf("The resistance is %0.2f ohms. ",result);
fprintf(fptr,"%f",result);     //Write result to file
break;
case '4' :
printf("R1 Value? ");
scanf("%f",&R1);
printf("R2? ");
scanf("%f",&R2);
printf("Voltage Value? ");
scanf("%f",&voltage);
result = R1*voltage/(R1+R2);
printf("The voltage is %0.2f V. ",result);
fprintf(fptr,"%f",result);     //Write result to file
break;

case '5' :
printf("R1 Value? ");
scanf("%f",&R1);
printf("R2? ");
scanf("%f",&R2);
printf("Voltage Value? ");
scanf("%f",&voltage);
result = R2*voltage/(R1+R2);
printf("The voltage is %0.2f V. ",result);
fprintf(fptr,"%f",result);     //Write result to file
break;
case '6' :
printf("R1 Value? ");
scanf("%f",&R1);
printf("R2 value? ");
scanf("%f",&R2);
printf("current Value? ");
scanf("%f",&current);
result = R2*current/(R1+R2);
printf("The current is %0.2f A. ",result);
fprintf(fptr,"%f",result);     //Write result to file
break;
case '7' :
printf("R1 Value? ");
scanf("%f",&R1);
printf("R2 Value? ");
scanf("%f",&R2);
printf("current Value? ");
scanf("%f",&current);
result = R1*current/(R1+R2);
printf("The current is %0.2f A. ",result);
fprintf(fptr,"%f",result);     //Write result to file
break;

default :
exit(0);

}
fclose(fptr);

return 0;
}