You are on page 1of 32

Computing Fundamentals

Dr. Muhammad Yousaf Hamza


Deputy Chief Engineer, PIEAS
Increment and Decrement Operators

Dr. Muhammad Yousaf Hamza


Increment and Decrement
• Increment and decrement operators.
– Increment: ++ It increases the value by 1
i = 7; // i is a variable name
++i; // (i = i + 1 or i + = 1). It increases the value of i by 1
i = 7;
i++;

–Decrement: -- (similar to ++) It decreases the value by 1


i = 8;
--i; // --i is the same as : (i = i – 1 or i - = 1).
i = 8;
i--;

Dr. Muhammad Yousaf Hamza


Increment and Decrement
Pre-fix and Post-fix
• ++i means increment i then use it
• i++ means use i then increment it
int i= 6;
printf ("%d\n",i++); /* Prints 6 sets i to 7 */
printf ("%d\n",i); // 7

Note this important difference


int i= 6;
printf ("%d\n",++i); /* prints 7 and sets i to 7 */
printf ("%d\n",i); // 7
int i= 6;
printf ("%d\n",i = i+1); /* prints 7 and sets i to 7 */
printf ("%d\n",i); // 7
int i= 6;
printf ("%d\n",i+1); /* prints 7 and i remains 6 */
printf ("%d\n",i); // 6
All of the above also applies to --
Dr. Muhammad Yousaf Hamza
Increment and Decrement
Pre-fix
#include<stdio.h>
and Post-fix
int main()
{
int a = 7, b = 20, c, d;
c = a++;
printf("%d", c);
printf("\n%d",a);

d = ++b;
printf("\n%d", d);
printf("\n%d",b);

getchar();
return 0;
}
Dr. Muhammad Yousaf Hamza
Mathematical Functions

Dr. Yousaf, PIEAS


#include<stdio.h>
#include<math.h> // use of math.h
int main()
{
float y, PI = 3.14;

y = sin(PI/2.0); // argument is in radian

printf("%f", y);
getchar();
return 0;
}
Output: 1.000000

Dr. Yousaf, PIEAS


#include<stdio.h>
#include<math.h> // use of math.h
#define PI 3.14
int main()
{
float y;

y = sin(PI/2.0); // argument is in radian

printf("%f", y);
getchar();
return 0;
}
Output: 1.000000
Dr. Yousaf, PIEAS
// If given θ is in degree, then first convert it into radians.
#include<stdio.h>
#include<math.h> // use of math.h
#define PI 3.14
int main()
{
float y;
float theta_deg, theta_rad;
theta_deg = 90.0;
theta_rad = (PI/180)*theta_deg;
y = sin(theta_rad); // argument is in radian
printf("%f", y);
getchar(); return 0;
}
Output: 1.000000
Dr. Yousaf, PIEAS
Dr. Yousaf, PIEAS
• log() ……..> natural log
• log10() …………> base-10 logarithm

Dr. Yousaf, PIEAS


Dr. Yousaf, PIEAS
#include<stdio.h> k = floor(15.2);
#include<math.h>
printf(" k = %d\n",k);
int main() // output: 15
{
j = ceil (15.2);
int k, j, r; printf(" j = %d\n",j);
double x = 25.0, y, z;
// output: 16
y = sqrt(x); r = abs (-67);
printf(" r = %d\n",r);
printf("y = %lf\n", y); // y = 5.000000
// output: 67
z = pow(y,3); // y^3, z = 125.000000 getchar();
return 0;
printf(" z = %lf\n", z);
}

Dr. Yousaf, PIEAS


Dr. Yousaf, PIEAS
To Generate Random Number
#include <stdio.h>
#include <stdlib.h> rand generates random number
int main() value in thousands. So when we
use
{ n = rand()%100 + 1;
int n; whatever be the value of
random number, its modulus
n = rand()%100 + 1; with 100 will always be in the
range 0 to 99. By adding 1, n
will always be in the range from
printf("%d", n); 1 to 100.
Concept of Seed
getchar(); srand(Study it yourself)
return 0;
}

Dr. Yousaf, PIEAS


Type Conversion
#include<stdio.h>
int main()
{
int a= 23, b= 4;
float c;
c = a/b; // 5.00
printf("\n%f",c); // 5.00
getchar();
return 0;
}
Actually 23/4 = 5.75, but here output is 5.00 In order to have
correct answer (with decimal value), we need type casting.
Dr. Muhammad Yousaf Hamza
Casting of Variables
#include<stdio.h>
int main()
{
int a= 23, b= 4;
float c;
c = a/b;
printf("\n%f",c); // 5.00
Cast ints a and b to be float
c= (float)a/(float)b;
// c = (float)a/b; or c= a/(float)b; are also same.

printf("\n%f",c); // 5.75
getchar();
return 0;
}
Dr. Muhammad Yousaf Hamza
Type Conversion
• C allows for conversions between the basic types, implicitly
or explicitly. It is also called casting.
• A cast is a way of telling one variable type to temporarily
look like another.
• Explicit conversion uses the cast operator.
• Example :
int x=10;
float y, z=3.14;

y=(float) x; /* y=10.0 */

x=(int) z; /* x=3 */ OR x = z, both are OK


Dr. Muhammad Yousaf Hamza
Casting of Variables
By using (type) in front of a variable we tell
the variable to act like another type of
variable. We can cast between any type
usually.

Dr. Muhammad Yousaf Hamza


More types: const
const means a variable which doesn't vary – useful for
physical constants or things like pi or e
– You can also declare variables as being constants
– Use the const qualifier:
const double pi=3.1415926;
const int maxlength=2356;
const int val=(3*7+6)*5; #include<stdio.h>
int main()
•(scientific) notation {
(mantissa/exponent) const float n = 6.18e2;
const double n = 6.18e2; printf("%f",n); 618.00
getchar();
return 0;
• 6.18e2 = 6.18x10^2 }

Dr. Muhammad Yousaf Hamza


More types: const

Constants
– Constants are useful for a number of reasons
• Tells the reader of the code that a value does not
change
• Tells the compiler that a value does not change
– The compiler can potentially compile faster code

• Use constants where appropriate

Dr. Muhammad Yousaf Hamza


More types: const
#include<stdio.h>
int main()
{

const double pi=3.1415926;


double radius = 4.5, circum ;

circum = 2*pi*radius;

printf("\nCircumference is= %lf", circum); // 28.274334

getchar();
return 0;
}

Dr. Muhammad Yousaf Hamza


More types: const
#include<stdio.h>
int main()
{
const double pi=3.1415926;
float radius = 4.5,circum ;
circum = 2*pi*radius;
printf("\n%f", circum);

radius = 7.3;
pi = 2.9; // Error
circum = 2*pi*radius;
printf("\n%f", circum);
getchar();
return 0;
} Dr. Muhammad Yousaf Hamza
Decisions

Dr. Muhammad Yousaf Hamza


To decide even/odd
(Flow Chart)

Dr. Muhammad Yousaf Hamza


To decide even/odd
#include<stdio.h>
int main()
{
int num;
printf("Please enter an integer number:\n");
scanf("%d",&num);

if(num%2==0)
printf("\nThe number %d is an even number",num);

if(num%2!=0)
printf("\nThe number %d is an odd number",num);
getchar(); getchar();
return 0;
}
Dr. Muhammad Yousaf Hamza
To decide even/odd
#include<stdio.h>
int main()
{
int num;
printf("Please enter an integer number:\n");
scanf("%d",&num);

if(num%2==0) // Here only one check.


printf("\nThe number %d is an even number",num);

else
printf("\nThe number %d is an odd number\n",num);
printf(“Bye”);
getchar(); getchar();
return 0;
} Dr. Muhammad Yousaf Hamza
Flow Chart of If-Else Statements

Dr. Muhammad Yousaf Hamza


Control Statements

Dr. Yousaf, PIEAS


Conditional Tasks
• if it is the condition, then I will do task A
Real Life Examples:
• if it is the condition, then I will do task A, else
(i.e. otherwise), I will do task B.
Real Life Examples:
• if it is the condition, then I will do task A,
else if it is the condition then I will do task B,
else I will do task C.
Real Life Examples:
Dr. Yousaf, PIEAS
Sequential execution
• Sequential execution
– Statements executed one after the other in the
order written
– Sequence structures: Built into C.
Programs executed sequentially by default

Dr. Yousaf, PIEAS


Control Structures
Transfer of control
– When the next statement executed is not the
next one in sequence

• Selection structures: C has three types:


if, if/else, and switch

• Repetition structures: C has three types:


for, while, and do/while (Later)

Dr. Yousaf, PIEAS

You might also like