OldSchoolCode

.com



This tutorial is on Operators.
The commonly used operators are:

+ (addition)
- (subtraction)
/ (division)
* (multiplication)
= (equals)

You can use these on any variables (remember the variables just reference slots in memory).
int b = 5;
int a = b + 2;

//you put the "f" after the fractional part of 
//the number to tell the computer to use it as a float
float c = a / 2.0f;
you can also use shorthand: with all the operators

b *= a; //is the same as b = b * a
b /= a; //is the same as b = b / a

Now you'll notice that a float prints out a bunch of numbers after the decimal point. You can truncate it down, for example if you do

printf ( "%.2f" );
it will give you only 2 numbers after the decimal :)

Also you can consolidate your "if" statements. You use the double pipe which means "or". The pipe is this symbol: "|". It works like this
if ( c == 5 || c == 2 )
This means
if c is equal to 5 
or
if c is equal to two
Make sure you go over and over this till it is all familar to you.

__________________________________________________________
#define _CRT_SECURE_NO_DEPRECATE   
   
#include <stdio.h>
#include <conio.h>

void main( void )
{
   int input;

   float a,b,c;

   printf ("\n Enter access code:   ");

   scanf( "%d", &input );

   if ( input == 123 || input == 456 || input == 789 )
   {
      printf( "\n Enter a number: " );
      scanf( "%f",&a );

      printf( "\n Enter a second number: " );
      scanf( "%f",&b );
      
      c = a + b;
      printf("%.2f + %.2f = %.2f\n", a, b, c );
      
      c = a - b;
      printf( "%.2f - %.2f = %.2f\n", a, b ,c );
      
      c = a * b;
      printf( "%.2f * %.2f = %.2f\n", a, b, c );
      
      c = a / b;
      printf( "%.2f / %.2f= %.2f\n", a, b, c );
   }
   else
   {
      printf( "\n Incorrect access code", input );
   }

   getch( );
}