0% found this document useful (0 votes)
83 views

CS 1160, Computing Fundamentals-Lab 2016/17: Exercise 1: Defining, Inputting and Printing Variables

The document provides instructions and examples for a series of programming exercises involving variables, input/output, calculations, and conditional statements in C. It defines variables and data types, demonstrates how to input values from the user and print outputs, provides examples of mathematical operations and conditional logic using if/else statements, and gives multiple short programs as examples to test out these concepts.

Uploaded by

Firas Abu taleb
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
83 views

CS 1160, Computing Fundamentals-Lab 2016/17: Exercise 1: Defining, Inputting and Printing Variables

The document provides instructions and examples for a series of programming exercises involving variables, input/output, calculations, and conditional statements in C. It defines variables and data types, demonstrates how to input values from the user and print outputs, provides examples of mathematical operations and conditional logic using if/else statements, and gives multiple short programs as examples to test out these concepts.

Uploaded by

Firas Abu taleb
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

CS 1160, Computing Fundamentals-Lab 2016/17 Week of Oct.

23-27, 2016

Exercise 1: Defining, inputting and printing variables:


Write a new program with the following code:
#include <stdio.h>
int main()
{
int myVar = 3;
printf("myVar = %d\n", myVar);
myVar = 15;
printf("myVar = %d\n", myVar);
printf("Please enter a new value for myVar: ");
scanf("%d", &myVar);
printf("myVar = %d\n", myVar);
return 0;
}
Compile the above program and run it.
As reminder, some types and their placeholders (format specifications)
Type C type Format Specifier
character char %c
integer value int %d
floating point number float %f
floating point number with double double %lf
precision

Exercise 2: Understanding variables, printf and scanf


Write a new program:
o Define two different variables of the type integer with an initial value of 1.
o Print both variables.
o Ask the user to enter new values for both variables.
o Print the new values.

Exercise 3: Making some calculations


As reminder, mathematical operators:
operation operator
addition +
difference -
multiplication *
division /

If we have an integer variable a, we can set a to the product of 5 and 10:


a = 5 * 10;
We can also set a variable c to the addition of two other variables a and b:
c = a + b;
 Write a program that:
o defines three integer variables a, b and c
o reads values for a and b
o assigns the sum of a and b to c then, prints c
o assigns the difference from a and b to c then, prints c
o assigns the multiplication from a and b to c then, prints c
o assigns the division from a by b to c then, prints c
CS 1160, Computing Fundamentals-Lab 2016/17 Week of Oct. 23-27, 2016

Exercise 4 – More calculations

Write a program that reads three integers (a, b, and c), computes the following formulas and stores the results in the
variables x1, x2 and prints the results of the program:

−𝒃+𝒄𝟒 𝟑(𝒂+𝒃𝟐 ) (𝒂+𝑏 3 )𝟐


𝒙𝟏 = − 𝒙𝟐 =
𝟐𝒂 𝒄 𝒄−𝑎2

Exercise 5: Conditions
As reminder, in C we use the following operators to formulate conditions:
Operator Meaning example Operator Meaning example
== equal to x == 5 ! not !x
!= not equal to x != 5 !(x > 5)
< less x<5 && and x > 5 && y < 7
<= less or equal x <= 5 12 <= x && x <= 20
> greater x>5 || or x != 0 || y > 3
>= greater or equal x >= 5 x < 12 || x > 20

When an expression output is true, its value is 1, otherwise its value is 0.


Given are the following variables:
i=1
j=2
k=3
m=2
Fill in the following table:
expression True or false? value (0 or 1)
k != 3 false 0
i==1
j==3
i>=1 && j < 4
m <= 99 && k < m
j >= i || k == m
k+m<j || 3-j >= k
!m
!(j-m)
!(k>m)
!(j>k)

Verify your table entries by running the following program and comparing the output to what you have noted in the table:
#include <stdio.h>

int main() {
int i=1,j=2,k=3,m=2;
printf("%d\n",i==1);
printf("%d\n",j==3);
printf("%d\n",i>=1 && j<4);
printf("%d\n",m<=99 && k<m);
printf("%d\n",j>=i || k==m);
printf("%d\n",k+m<j || 3-j >=k);
printf("%d\n",!m);
printf("%d\n",!(j-m));
printf("%d\n",!(k>m));
printf("%d\n",!(j>k));
return 0;
}
CS 1160, Computing Fundamentals-Lab 2016/17 Week of Oct. 23-27, 2016

Exercise 5: Getting to know the if statement (selection)

Please type in the following programs, compile them (if possible) and run them. Test all programs by entering 3, 0 and -5.
Make your notes next to the programs to remember your observations.

Program 1 Program 2

#include <stdio.h> #include <stdio.h>

int main() int main()


{ {
int value; int value;
printf("Please enter a value: "); printf("Please enter a value: ");
scanf("%d", &value); scanf("%d", &value);

if (value > 0) { if (value > 0) {


printf ("%d is a positive number\n", value); printf ("%d is a positive number\n",
} value);
printf("Bye\n"); } else {
return 0; printf("%d is a negative number\n", value);
} }

printf("Bye\n");
return 0;
}

Program 3 Program 4

#include <stdio.h> #include <stdio.h>

int main() int main()


{ {
int value; int value;
printf("Please enter a value: "); printf("Please enter a value: ");
scanf("%d", &value); scanf("%d", &value);

if (value > 0) { if (value == -5)


printf ("%d is a positive number\n", value); printf("The value %d is -5\n", value);
} else if (value == 0) { if (value == 3)
printf("%d is 0\n", value); printf("The value %d is 3\n", value);
} else { else
printf("%d is a negative number\n", value); printf("The value %d is not -5 and not 3\n", value);
}
printf("Bye\n");
printf("Bye\n"); return 0;
return 0; }
}

Program 5 Program 6

#include <stdio.h> #include <stdio.h>

int main() int main()


{ {
int value; int value;
printf("Please enter a value: "); printf("Please enter a value: ");
scanf("%d", &value); scanf("%d", &value);

if (value == -5) if (value == 0) {


printf("The value %d is -5\n", value); printf("The value was 0\n");
else printf("Thank you for your input\n");
if (value == 3) } else {
printf("The value %d is 3\n", value); printf("The value was not 0\n");
else printf("Thank you for your input\n");
printf("The value %d is not -5 and not 3\n",value); }
printf("Bye\n");
printf("Bye\n"); return 0;
return 0; }
}
CS 1160, Computing Fundamentals-Lab 2016/17 Week of Oct. 23-27, 2016

Exercise 7: Writing programs using if

1. Write a program that reads two numbers a and b. Print the maximum value of the two numbers.

2. Write a program that reads three input values. Output the maximum value of the three.

3. Write a C program that reads two float number and stores their values in variable x and y. the program then
computes the value (𝒙𝟐 − 𝒚𝟐 )⁄(𝒙 − 𝒚), stores the result in a float named z and prints the value of z. if the
computation division by zero, the program must avoid that by printing an error message and exiting before
computing the formula.

4. Use (if … else) to write a complete C application that asks the user to enter an integer Q Range ("deviation IQ") and
output the IQ Classification according to the following table:

Q Range ("deviation IQ") IQ Classification

130 and above Very Superior

120–129 Superior

110–119 High Average

90–109 Average

80–89 Low Average

70–79 Borderline

69 and below Extremely Low

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy