Question:
Simple program "if condition" help?
Mar3ie
2013-03-15 11:48:58 UTC
I need help with a simple program I've written. The purpose of the program is to perform a set of different calculations on two given integers. What I want to do is the following:
After the calculations have been made and the results displayed, I want the program to ask the user "Do you want to perform any other calculations?". And then use the if, else to instruct the program to restart itself (meaning re-run from the beginning) if the user types "yes", and (return 0) if the user types "no". Here is what I have done so far:
#include


int main()
{
int num1, num2;
char answer;
printf("Enter two integers: ");
scanf("%d",&num1);
scanf("%d",&num2);
printf("The sum is=%d",num1+num2);
printf("\nThe product is=%d",num1*num2);
printf("\nThe difference is=%d",num1-num2);
printf("\nThe quotient is=%d",num1/num2);
printf("\nThe remainder is=%d",num1%num2);
printf("\nDo you want to perform other calculations?");
scanf("%c",&answer);
}
Three answers:
2013-03-15 12:16:11 UTC
use the if-else statement...type the whole program again after the if statement. this would make the program repeat itself if the answer of the user is yes, and terminate if the answer is no.

or better still make the whole program a loop to repeat itself on the condition that the answer is yes and terminate if the answer is no....that should work alright ;)
??????
2013-03-15 11:55:40 UTC
You must use a do while loop.

Type after your declarations of the variables (after "char answer;").



do

{



And after the last normal line (after "scanf("%c",&answer);" )



}

while ((c=='y') || (c=='Y'));



This will keep repeating the loop until you enter an answer other than 'y' or 'Y' to the question if you want to perform other calculations.

Remark : do { ... } while (...); executes the body at least once, which is needed here, while

while(...) { ... }; executes only the body from the beginning if the condition is right.
2016-08-09 13:02:03 UTC
Immoderate formulas and formatting in Excel create colossal processing overhead for Excel. Eventually, it will outcomes in degradation of efficiency. In case you have a obstacle that can be accommodated with the use of a number of hundred formulation, you must use a VBA event handler or macro. Here is an occasion handler on the way to location the depend of rows, where column B includes a 2 and column D comprises a 3, in mobilephone G3 without problems by using double clicking any telephone. Copy the following occasion handler to the clipboard (spotlight the complete code, correct click on throughout the highlighted area, and 'copy'): exclusive Sub Worksheet_BeforeDoubleClick(ByVal goal As range, _ Cancel As Boolean) Dim i, LastRow LastRow = variety("B" & Rows.Depend).End(xlUp).Row For i = 1 To LastRow If Cells(i, "B").Price = 2 And Cells(i, "D").Worth = 3 Then ctr = ctr + 1 finish If subsequent range("G3").Value = ctr target.Offset(0, 1).Pick end Sub decide upon the worksheet containing the columns to monitor and right click the sheet tab on the bottom. Decide upon 'View Code'. Paste the event handler into the white modifying area to the proper (correct click on inside the field and 'Paste'). Shut the VBE (purple button w/white 'x' - prime correct). Double click on any mobilephone to come back the rely of rows in cellphone G3.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...