Week 1 Lecture Code
// Pantea Aria
// Hello World!
#include <stdio.h>
int main() {
printf("Hello, World!\n");
return 0;
}
// Pantea Aria
// Variables
// if we want to store a value in memory
// we need a box in memory with a label
// we must know the type of the value in that box
// declaration
// Write a program that stores your age in a variable and prints it.
#include <stdio.h>
int main(void) {
// declare a variable and initialised it
double age;
// read it from user
// print it out
printf("%.2lf\n", age);
return 0;
}
// Pantea Aria
// scanf
// scanf is used to get input from the user during program execution.
// Write a program that:
// Asks the user to enter a number in integer
// Reads it using scanf
// Prints it back using printf
#include <stdio.h>
int main(void) {
printf ("enter a number in integer:");
int input;
scanf("%d", &input);
printf ("My number was %d\n", input);
return 0;
}
// Pantea Aria
// constants
// A constant is a value that does not change while the program runs
// Write a program that uses a constant double to
// store the value of π (3.14), and then prints:The value of PI is: 3.14
#include <stdio.h>
#define PI 3.14
int main(void) {
printf("The value of PI is: %.2f\n", PI);
return 0;
}
// Pantea Aria
// arithmetic operators
// Write a program that declares two integers num1 and num2,
// assigns them values, and then prints the result of:
// add, subtract, product, divide, remainder
#include <stdio.h>
int main(void) {
printf ("Enter two numbers in integer:");
int num1, num2;
scanf ("%d %d", &num1, &num2);
printf ("add result is %d\n", num1 + num2);
printf ("subtract result is %d\n", num1 - num2);
printf ("product result is %d\n", num1 * num2);
printf ("divide result is %d\n", num1 / num2);
printf ("remainder result is %d\n", num1 % num2);
return 0;
}
// 3/9/2025
// Pantea Aria
// What does printf do?
#include <stdio.h>
int main() {
printf("Hello, World!\n");
return 0;
}
// Pantea Aria
// Variables
// if we want to store a value in memory
// we need a box in memory with a label
// we must know the type of the value in that box
// declaration
// Write a program that stores your age in a variable and prints it.
// Pantea Aria
// scanf
// scanf is used to get input from the user during program execution.
// Write a program that:
// Asks the user to enter a number in integer
// Reads it using scanf
// Prints it back using printf
// Pantea Aria
// constants
// A constant is a value that does not change while the program runs
// Write a program that uses a constant double to
// store the value of π (3.14), and then prints:The value of PI is: 3.14
// Pantea Aria
// arithmetic operators
// Write a program that declares two integers num1 and num2,
// assigns them values, and then prints the result of:
// add, subtract, product, divide, remainder