ENG1008_Lab_1.pdf
Document Details
Uploaded by IrreplaceableIndianArt5176
Tags
Full Transcript
ENG1008 Programming Lab 1 – Introduction 1a) Write a simple C program: Printing a line of text - Hello World #include // main begins program execution int main(void) { // print out "Hello World" using printf, "\n" is newline...
ENG1008 Programming Lab 1 – Introduction 1a) Write a simple C program: Printing a line of text - Hello World #include // main begins program execution int main(void) { // print out "Hello World" using printf, "\n" is newline printf("%s", "Hello World\n"); return 0; } // end main 1b) To compile the Hello World program and run it $ gcc helloworld.c $ gcc helloworld.c –o helloworld OR $ a $ helloworld 2) Problem statement : To calculate the sum of two integer numbers and to multiply the sum obtained by a factor (real number). #include int main(void) { // Declare input variables of type integer and floating point int no1, no2, total; float multiplier; // Prompt the user to enter 2 integers and the multiplier printf("%s", "Please enter the 2 integers "); scanf("%d", &no1); scanf("%d", &no2); printf("%s", "Please enter the multiplier "); scanf("%f", &multiplier); // Calculate the total or sum total = no1 + no2; // Print the results - integer output using %d printf("The result of %d + %d is %d\n", no1, no2,total); // Print the results - real number output using %f printf("Multiplying by %.2f is %f\n", multiplier, total*multiplier); return 0; } 1 3) Write a simple C program: To apply equality and relational operators. #include int main(void) { // To declare and read in 2 int variables a and b int a, b; printf("%s", "Please enter the number a "); scanf("%d", &a); printf("%s", "Please enter the number b "); scanf("%d", &b); // To apply equality operators printf("a == b is %d\n", a==b); printf("a != b is %d\n", a!=b); // To apply relational operators printf("a > b is %d\n", a>b); printf("a < b is %d\n", a=b); printf("a