Write a simple C/C++ Win32 console application that will calculate and print out
ID: 3745598 • Letter: W
Question
Write a simple C/C++ Win32 console application that will calculate and print out the Voltage using Ohm’s Law with user entered values for Current and Resistance. The program upon execution will request for the Current and Resistance then print out the Voltage. All values should be integers requiring resistance in Ohms, current in Amps, and voltage in Volts.
The scanf_s() function reads data from the standard input stream stdin (this will be the keyboard) and writes the data into the location given by argument (this will be the variable(s)). For scanf_s() each argument must be a pointer to the variable (so we need to add & in front of the variable name) of the type that corresponds to a type specified in format (so each type(s) from format to argument must match). Printf() is similar to scanf_s() except it prints to stdout (the screen) and doesn’t require the variable to be a pointer. (& is not required in front of variable)
int scanf_s( const char *format [, argument]...); int printf( const char *format [, argument]...);
Examples of each: printf(“Enter a value ”); // no arguments just format // will print ‘Enter a value’ char character=’A’; printf(“The character is %c ”, character); // format of %c matches variable type of character // will print ‘The character is A’ char character; scanf_s(“%c”,&character); // format of %c matches the pointed to variable type character // character will equal entered value char string1[10]; scanf_s(“%s”,string1,sizeof(string1)); // format of %s matches the char array which is a string (char // array), the size of the array (sizeof(string1)) must follow // pointer. The pointer to the string is actually &string1[0] // but its name without indexing provides the pointer. // string1 will equal characters entered up to size
Explanation / Answer
#include <stdio.h>
#include <conio.h>
void main ()
{
clrscr();
float I;
float R;
float V;
printf_s("Enter the value of Current);
scanf_s("%f",&I);
printf_s("Enter the value of Resistance);
scanf_s("%f",&R);
V=I*R;
printf_s("The required Voltage,V=");
printf_s("%f",V);
getch();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.