ZIAUDDIN UNIVERSITY FACULTY OF ENGINEERING SCIENCE AND TECHNOLOGY COURSE: PROGRAMMING FUNDAMENTALS TERM: _________, CLASS: Electrical Submitted By: _______________________________________________ (Name) TALHA JAMAL (Enrollment. No.) 17201 4-14/21/1 Submitted To: Engr. Abdul Salam/ Engr. Aaima Arshad Signed: Remarks: Score: INDEX SNO DATE LAB NO 01 12-01-22 01 02 20-01-22 02 03 26-01-22 03 26-01-22 04 04 LAB OBJECTIVE Build your understanding of the basic structure of C programming and get familiar with it Identification of Constants, Variables, Keywords & Data types & implementation of Input &Output Functions. To identify the different types of operators, Expressions and evaluation of formulas in C Developing the concept of conditional if statement and studying various variations of statements and identification of difference between them 05 02-02-22 05 To identify the nested if and switch case operations 06 02-02-22 06 Build the understanding of loop, operators and its variables 07 09-02-22 07 08 16-02-22 08 Determine and understanding the elements of an arrays 09 23-02-22 09 Identify and declaration of functions 10 30-03-22 10 Declaration of 2D Array elements Build the understanding implement conditional repetitions (while and do-while loops) and how to work with for-loops SIGN SNO DATE LAB NO LAB OBJECTIVE SIGN LAB 01 Task no 1: Write Program to display your personal information. CODE : #include <stdio.h> int main () { printf("First Name: Talha \n"); printf("Last Name: Jamal \n"); printf("Father Name: M.Shahid Jamal \n"); printf("Age: 20 \n"); printf("Email ID: talhajamal875@gmail.com \n"); printf("Phone Number: 03312376875 \n"); printf("CNIC: 4210126044103 \n"); printf("Education: DAE Holder "); return 0; } Task no 2 : Write a program to display your Marksheet CODE: #include <stdio.h> int main() { printf(" Federal Board Of Intermediate And Secondry Education Islamabad\n"); printf(" SSC MARKS CERTIFICATE\n\n"); printf("NAME : Talha Jamal\n"); printf("FATHERS NAME : M.Shahid Jamal\n"); printf("Date Of Birth : 27/07/2001\n"); printf("School Name printf(" Group : Army Public School And College System\n"); : Engineering\n\n"); printf("______________________________________________________________\n"); printf("| Subject | total marks | obtained marks |\n"); printf("|_________________________|________________|_________________|\n"); printf("| English | 100 | 088 |\n"); printf("| Urdu | 100 | 087 |\n"); printf("| Islamiyat | 100 | 045 printf("| Pakistan Studies | 100 printf("| Mathematics | 50 printf("| Physics | printf("| Chemistry 85 | printf("| Computer Studies 100 | |\n"); | 041 |\n"); | 062 | 099 |\n"); |\n"); | 099 100 |\n"); | 107 |\n"); printf("|_________________________|________________| ________________|\n"); printf("| Total | 1050 | 628 |\n"); printf("|_________________________|________________|_________________|\n"); printf("grade : C\n"); printf("percentage :60"); return 0; } Task no3: Design the following bill tempelate Code: #include <stdio.h> int main() { // Write C code here printf("KFC .\n"); printf ("Quetta Milinuem Mall\n"); printf("PAKISTAN, Quetta,Balochistan\n\n\n"); printf("Bill To Order To printf("Usaman Ejaz Invoice# SIR Shezad pk-069\n"); Invoice Date printf("Main Mall Dine IN P.0.# printf("PAKISTAN Quetta,Balochistan 18/01/2022\n"); 081-36349425\n"); DUE Date 19/01/2022\n\n"); printf("*****************************************************************************\n"); printf("_________________________\n"); printf(" QTY Disciption Unit Price Quantity Amount\n"); printf("_________________________\n"); printf(" 1 ZINGERburger 100.00 printf(" 2 large pizza 500.00 printf(" 3 ketchup 15.00 9 2 3 900.00\n"); 1000.00\n"); 45.00\n\n"); printf("| Total = 1945.00\n\n"); printf("| GST = 6.25% printf("| Total + GST = RS 2002\n"); \n\n"); return 0; } Task no 4: write a program that displays the results of the expression 4.1 code: #include <stdio.h> int main() { int a=3.0, b=5.0, d; d=a*b; printf("a*b=%d\n" ,d); return 0; } 4.2 code: #include <stdio.h> int main() { int a=7.1, b=8.3, c=2.2, d; d=a*b-c; printf("a*b-c=%d\n" ,d); return 0; } 4.3 code: #include <stdio.h> int main() { int a=3.2, b=6.1, c=5, d; d=a/(b*c); printf("a/(b*c)=%d\n" ,d); return 0; } 4.4 code: #include <stdio.h> int main () { int a=15, b=4, d; d=a/b; printf("a/b=%d\n" ,d); return 0; } 4.5 Code: #include <stdio.h> int main(){ int a=5, b=3 ,c=6 , d=4 ,e; e=a*b-(c*d); printf("a*b-(c*d)=%d\n",e); return 0; } Task no 5 : Calculate the temperature in celsius using integer values. CODE: #include <stdio.h> int main() { float celsius, fahrenheit; /* input temperature in celsius */ printf("enter temperature in celsius: "); scanf("%f", &celsius); /*celsius to fahrenheit conversion formula */ fahrenheit = (celsius * 9/5) + 32; printf("%.2f celsius = %.2f fahrenheit", celsius, fahrenheit); return 0; } Task no 6 : Calculate the area of circle. CODE: #include <stdio.h> int main () { int r; int b; int c; int i=3.14; printf("\n please enter the radius of circle:"); scanf("%d",&r); b=i*r^2; printf("\n the area of circle is :%d",b); c=2*i*r; printf("\n the circumference of circle is: %d",c); return 0; } Task no 7: Display the result of the expression: ((( a + b) * (c * e * d)) – e)/f CODE: #include <stdio.h> int main() { int a=1, b=2, c=3, d=4, e=5, f=6, z; z=(((a+b)*(c*e*d))-e)/f; printf("(((a+b)*(c*e*d))-e)/f=%d\n" ,z); return 0; } LAB 02 Task no 1: Write a code which take two integer values from user and perform mathematical operations(Addition, Subtraction, Multiplication and Division) on these two values. CODE: #include<stdio.h> int main(){ int a; int b; int c; int d; int e; int f; printf("please enter the 1st number:"); scanf("%d",&a); printf("please enter the 2nd number :"); scanf("%d",&b); c=a+b; d=a*b; e=a-b; f=a/b; printf("the sum of given numbers :%d \n",c); printf("the multiplication of give numbers :%d \n",d); printf("the subtraction of given numbers : %d \n",e); printf("the division of given number : %d \n",f); return 0; } Task no 2: Display the following results and take value of a, b, c, d, e, and f from user. ( ( ( b + 3 ) ^ ( 4 a c) ) / d ) * ( ( ( a * c ) + ( b * d ) ) * f ) CODE: #include<stdio.h> int main() { int a; int b; int c; int d; int e; int f; int g; printf("enter the value of a="); scanf("%d",&a); printf("enter the value of b="); scanf("%d",&b); printf("enter the value of c="); scanf("%d",&c); printf("enter the value of d="); scanf("%d",&d); printf("enter the value of e="); scanf("%d",&e); printf("enter the value of f="); scanf("%d",&f); g=(((b+3)^(4*a*c))/d)*(((a*c)+(b*d))*f); printf("result = %d",g); return 0; } Task no 3: Write a program and print the output of first equation of the motion. For values take input from user. CODE: #include <stdio.h> int main() { int a; int b; int c; int d; printf("Enter the value of initial velocity : "); scanf("%d",&a); printf("Enter the value of constant acceleration : "); scanf("%d",&b); printf("Enter the value of time : "); scanf("%d",&c); d=a+b*c; printf("The final velocity of first equation of motion is = %d",d); return 0; } 4. Task no 4: Take input of user personal information and HSSC/SSC subjects marks. By using the given information generate Marks Sheet. CODE : #include <stdio.h> char str[20]; char str[20]; int main() { int a; int b; int c; int d; int e; int f; int g; int h; int i; int j; int k; int l; printf("Name = "); gets(str); printf("Father's Name = "); gets(str); printf("Roll no = "); scanf("%d,&a"); printf("\nEnter your SSC marks"); printf("\nEnter your English marks ="); scanf("%d,&b"); printf("\nEnter your Urdu marks ="); scanf("%d",&c); printf("\nEnter your Pak.st marks ="); scanf("%d",&d); printf("\nEnter your Computer marks ="); scanf("%d",&e); printf("\nEnter your Chemistry marks ="); scanf("%d",&f); printf("\nEnter your Islamiat marks ="); scanf("%d",&g); printf("\nEnter your Mathematics marks ="); scanf("%d",&h); printf("\nEnter your Physics marks ="); scanf("%d",&i); j=b+c+d+e+f+g+h+i; printf("obtaing marks = %d\n\n",j); printf("percentage =60\n"); printf("grade = B"); } Task no 5: Calculate the quadratic equation by using three user given integer variables. CODE: #include <math.h> #include <stdio.h> int main() { int a, b, c, discriminant; double root1, root2; printf("Enter coefficients a:, b and c: \n"); scanf("%f %f %f", &a, &b, &c); discriminant = b * b - 4 * a * c; root1 = (-b + sqrt(discriminant)) / (2 * a); root2 = (-b - sqrt(discriminant)) / (2 * a); printf("root1 = %.2lf and root2 = %.2lf", root1, root2); } LAB 03 Task 1: Which of the following values can be assigned to variables of type float, double and decimal: 5, -5.01, 34.567839023; 12.345; 8923.1234857; 3456.091124875956542151256683467? Code: #include<stdio.h> int main () { int number1 = 5; float number2 = -5.01; float number3 = 12.345; double number4 = 34.567839023; double number5 = 8923.1234857; double number6 = 3456.091124875956542151256683467; printf("The Integer Number : %d\n",number1); printf("The Float Number : %f\n",number2); printf("The Float Number : %f\n",number3); printf("The Double Number : %lf\n",number4); printf("The Double Number : %lf\n",number5); printf("The Double Number : %lf\n",number6); return 0; } Task 2 : Declare two variables of type string and give them values "Hello" and "World". Assign the value obtained by the concatenation of the two variables of type string (do not miss the space in the middle) to a variable of type object. Code: #include<stdio.h> #include<string.h> int main() { char a[10]; strcpy(a, "HELLO "); strcat(a, "WORLD "); printf("%s\n", a); return 0; } Task 3: A given company has name, address, phone number, fax number, web site and manager. The manager has name, surname and phone number. Write a program that reads information about the company and its manager and then prints it on the console. Code: #include<stdio.h> int main () { char company_name[50],company_address[50],company_website[50],first_name[50],last_name[50]; long int phone_number,fax_number,number; int age; printf("Enter Company Name:\n"); scanf("%s",&company_name); printf("Enter Company Address:\n"); scanf("%s",&company_address); printf("Enter Phone Number:\n"); scanf("%d",&phone_number); printf("Enter Fax Number:\n"); scanf("%d",&fax_number); printf("Enter Company Website:\n"); scanf("%s",&company_website); printf("Enter Manager First Name:\n"); scanf("%s",&first_name); printf("Enter Manager Last Name:\n"); scanf("%s",&last_name); printf("Enter Manager Age:\n"); scanf("%d",&age); printf("Enter Manager Number:\n"); scanf("%d",&number); printf("|*|*|*|*|*|*|*|*|*|*|*|*|*|*|*|*|*|*|*|*|*|*|*|*|*|*|*|\n"); printf("|*|*|*|*|*| Company And Manager Information |*|*|*|*|*|\n"); printf("|*|*|*|*|*|*|*|*|*|*|*|*|*|*|*|*|*|*|*|*|*|*|*|*|*|*|*|\n"); printf("|*|*|*|*|*| Company Name : %s|*|*|*|*|*|\n",company_name); printf("|*|*|*|*|*| Company Address : %s|*|*|*|*|*|\n",company_address); printf("|*|*|*|*|*| Company Phone Number : %d|*|*|*|*|*|\n",phone_number); printf("|*|*|*|*|*| Company Fax Number : %d|*|*|*|*|*|\n",fax_number); printf("|*|*|*|*|*| Company Website : %s|*|*|*|*|*|\n",company_website); printf("|*|*|*|*|*| Manager First Name : %s|*|*|*|*|*|\n",first_name); printf("|*|*|*|*|*| Manager Last Name : %s|*|*|*|*|*|\n",last_name); printf("|*|*|*|*|*| Manager Age printf("|*|*|*|*|*| Manager Number : %d|*|*|*|*|*|\n",age); : %d|*|*|*|*|*|\n",number); printf("|*|*|*|*|*|*|*|*|*|*|*|*|*|*|*|*|*|*|*|*|*|*|*|*|*|*|*|"); } LAB 04 Task 1: Make a program of user login using if? (Username and Password) Code: #include <stdio.h> #include <string.h> #include <conio.h> int main() { char username[4]; char password[4]; printf("Enter your username: "); scanf("%s", username); printf("Enter your password: "); scanf("%s", password); if (strcmp(username, "Talha") == 0 &&strcmp(password, "777") == 0 || strcmp(username, "admin") == 0 &&strcmp(password, "222") == 0 || strcmp(username, "sparkGPON") == 0 &&strcmp(password, "makubatao") == 0 || strcmp(username, "xyz") == 0 &&strcmp(password, "000") == 0) printf("Logged In Successfully"); else { printf("Incorrect username or password, please try again! "); } } Task 2 : Determine the season of the month using month name by applying switch case statement. Code: #include <stdio.h> #include <string.h> #include <conio.h> int main() { int month; printf("Input Month Number : "); scanf("%d",&month); switch(month){ case 12: case 1: case 2: printf("Winter\n"); break; case 3: case 4: case 5: printf("Spring\n"); break; case 6: case 7: case 8: printf("Summer\n"); break; case 9: case10: case11: printf("Autumn\n"); break; default: printf("Invalid Month Name. \nPleasetryagain \n"); break; } return 0; } Task 3: Take intermediate percentage and program as input and determine for which program(s) the person is eligible in Ziauddin University according to his/her academic qualification. Code: #include<stdio.h> int main(){ int percentage; printf("Enter Your Percentage : "); scanf("%d",&percentage); if(percentage>=90 && percentage<=100) { printf("\nYou are Eligible For Medical"); } else if(percentage>=70 && percentage<=90){ printf("\nYou are Eligible For Engineering"); } else if(percentage>=50 && percentage<=70){ printf("\nYou are Eligible For BBA"); } else if(percentage>=40 ){ printf("\nYou are Eligible For BS Physical Therapy"); } else printf("\nYou are Not eligible For Admission"); return 0; } Task 4: Write a program using switch statement that applies bonus points to given scores in the range [1…9] by the following rules: a) If the score is between 1 and 3, the program multiplies it by 10. b) If the score is between 4 and 6, the program multiplies it by 100. c) If the score is between 7 and 9, the program multiplies it by 1000. d) If the score is 0 or more than 9, the program prints an error message. Code: #include<stdio.h> #include <conio.h> int main(){ int a ; printf("\nPlease write a number between 1 and 9: "); scanf("%d",&a); switch (a) { case 1: case 2: case 3: printf("\nThe BONUS score is: %d ", a * 10); break; case 4: case 5: case 6: printf("\nThe BONUS score is: %d ", a * 100); break; case 7: case 8: case 9: printf("\nThe BONUS score is: %d ", a*1000); break; default: printf("\nInvalid Score!"); break; } return 0; } LAB 05 Task 1: Write a Program using nested ifs in which the user is asked to do the following: Confirm if user wants to play the game “Guess the secret number” If false, then print message “Maybe next time”; if true, then ask “Enter your age” If age is less than 5 then print “You are too young to play”, else ask “Enter any number”. If number is equal to the secret number then print “You have successfully guessed the secret number; else print “You were not successful in guessing the secret number”. Code: #include<stdio.h> #include<conio.h> int main() { char p; int num, age, num1; num=7; printf("If you want to play This GameType YES=Y \n or \nIf you don't want to play game type NO=N: ",p); scanf("%c",&p); if (p == 'Y') { printf("ENTER YOUR AGE: ",age); scanf("%d",&age); if (age > 5) { printf("GUESS ANY NUMBER: ",num1); scanf("%d",&num1); if (num == num1) { printf("congratulations! you guessed the secret number"); } else { printf("you are not able to guess the secret number...better luck next time "); } } else { printf("you are to young to play this game...!"); } } else { printf("maybe next time"); } return 0; } Task 2 : Implement the program of library for the students in which the system ask them to enter the genre of the books. On the basis of that list of available books will be share with the students. Then student can check the details (author name, publisher name, publishing year, etc) of any book he want to search. Code: #include<stdio.h> #include<stdlib.h> #include<string.h> struct library { char bk_name[30]; char author[30]; int pages; float price; }; int main() { struct library l[100]; char ar_nm[30],bk_nm[30]; int i,j, keepcount; i=j=keepcount = 0; while(j!=6) { printf("\n\n1. Add book information\n2. Display book information\n"); printf("3. List all books of given author\n"); printf("4. List the title of specified book\n"); printf("5. List the count of books in the library\n"); printf("6. Exit"); printf ("\n\nEnter one of the above : "); scanf("%d",&j); switch (j) { /* Add book */ case 1: printf ("Enter book name = "); scanf ("%s",l[i].bk_name); printf ("Enter author name = "); scanf ("%s",l[i].author); printf ("Enter pages = "); scanf ("%d",&l[i].pages); printf ("Enter price = "); scanf ("%f",&l[i].price); keepcount++; break; case 2: printf("you have entered the following information\n"); for(i=0; i<keepcount; i++) { printf ("book name = %s",l[i].bk_name); printf ("\t author name = %s",l[i].author); printf ("\t pages = %d",l[i].pages); printf ("\t price = %f",l[i].price); } break; case 3: printf ("Enter author name : "); scanf ("%s",ar_nm); for (i=0; i<keepcount; i++) { if (strcmp(ar_nm, l[i].author) == 0) printf ("%s %s %d %f",l[i].bk_name,l[i].author,l[i].pages,l[i].price); } break; case 4: printf ("Enter book name : "); scanf ("%s",bk_nm); for (i=0; i<keepcount; i++) { if (strcmp(bk_nm, l[i].bk_name) == 0) printf ("%s \t %s \t %d \t %f",l[i].bk_name,l[i].author,l[i].pages,l[i].price); } break; case 5: printf("\n No of books in library : %d", keepcount); break; case 6: exit (0); } } return 0; } LAB 06 Task 1: Cube series (Use For loop) Code: #include <stdio.h> int main() { int j,ctr; printf("\n Enter Number Of Terms : "); scanf("%d", &ctr); for(j=1;j<=ctr;j++) { printf("\n Number is : %d and cube of the %d is :%d \n",j,j, (j*j*j)); } return 0; } Task 2 : 2. Make a game in C, in which give 5 tries to the user to guess the value of the number. Code: #include <math.h> #include <stdio.h> #include <stdlib.h> int main() { int num, time, guess, tries = 0; srand((5)); num = rand() % 100 + 1; printf("Guess My Number Game\n\n"); do { printf("Enter a guess between 1 and 100 : "); scanf("%d", &guess); tries++; if (guess > num) { printf("Too high!\n\n"); } else if (guess < num) { printf("Too low!\n\n"); } else { printf("\nCorrect! You got it in %d guesses!\n", tries); } }while (guess != num); return 0; } Task 3: 3. Generate Stars using for loops * ** *** **** ***** ****** Code: #include <stdio.h> int main() { int n,i,j; printf("Enter the number of rows: "); scanf("%d",&n); for(i=1;i<=n;i++) { for(j=1;j<=i;j++) { printf("* "); } printf("\n"); } return 0; } LAB 07 Task 1: 2. Print the square roots of the first 25 odd positive integers. (while loop). Code: #include <stdio.h> #include <math.h> int main() { int num; int n; double result; num=1; printf("Enter the value : "); scanf("%d",&n); while(num<=n) { if (num %2 !=0) printf("%d : ",num); num++; result=sqrt(num); printf("Square root value =%.2lf \n",result); } return 0; } Task 2: Write a program in which you have to calculate the cgpa of students of the students using for loop and while loop. Code: #include <stdio.h> int main() { int i =1; int num_sem = 0; float temp = 0; float sum = 0; float gpa = 0.0; printf("Enter number of semesters\n"); scanf("%d", &num_sem); printf("Enter CGPA per semester\n"); while(i<= num_sem) { printf("Enter %d Semester GPA\t", i); i++; scanf("%f", &temp); sum += temp; } gpa = sum/(float)num_sem; printf("Over all CGPA is\t%f\n", gpa ); return 0; } LAB 08 Task 1:Make a program in C in which take 5 numbers from user and then give sum and avg. of them. Using arrays. Code: #include<stdio.h> int main() { int arr[5], i, sum=0; float avg; printf("Enter Any 5 Numbers :"); for(i=0;i<5;++i) { scanf("%d",&arr[i]); sum += arr[i]; } printf("\nThe sum Of Number Is: %d",sum); avg=sum/i; printf("\nThe Average Of Number Is: %.1f",avg); return 0; } Task 2: Write a program, which creates an array of 20 elements of type integer and initializes each of the elements with a value equals to the index of the element multiplied by 5. Print the elements to the console. Code: #include<stdio.h> int main() { int arr[20], i, value; int multiply; printf("Enter 20 Numbers :"); value=5; for(i=0;i<20;++i) { scanf("%d",&arr[i]); multiply = value * arr[i]; } printf("Multiplied 20 Element by 5: %d",multiply); return 0; } LAB 09 Task 1: Sum of 3 numbers Code: #include<stdio.h> int value(int x, int y, int z) { int total; total = x + y + z; return total; } int main() { int x=40, y=120, z=80; int total = value(x,y,z); printf("The sum is = %d",total); return 0; } Task 2: Find maxium among two numbers Code: #include<stdio.h> int main() { int a,b,maxnum; printf("Enter First Number :"); scanf("%d",&a); printf("Enter Second Number :"); scanf("%d",&b); int result=(a,b); printf("-----------------------\n"); printf("Maximum Number Is : %d\n",result); return 0; } int maxnum(int a,int b) { if(a>b) { return a; } else { return b; } } Task 3: Print the table of 2 Code: #include<stdio.h> void table(int a); int main() { int number; printf("Press (2) to get the Table:"); scanf("%d",&number); printf("\nThe Table Of %d Is: \n",number); table (number); return 0; } void table(int number) { int i; for ( i = 1; i<=10; i++) { printf("%d x %d = %d \n", number, i, number * i); } } LAB 10 Task 1: How to access 2D array element? For 1st row + 2nd column a [0] [1] Code: #include <stdio.h> int main() { int arr1[50][50],brr1[50][50],crr1[50][50],i,j,n; printf("Input The Size Of The Square Matrix: "); scanf("%d", &n); printf("Input Elements In The First Matrix :\n"); for(i=0;i<n;i++) { for(j=0;j<n;j++) { printf("element - [%d],[%d] : ",i,j); scanf("%d",&arr1[i][j]); } } printf("Input Elements In The Second Matrix :\n"); for(i=0;i<n;i++) { for(j=0;j<n;j++) { printf("element - [%d],[%d] : ",i,j); scanf("%d",&brr1[i][j]); } } printf("\nThe First Matrix Is :\n"); for(i=0;i<n;i++) { printf("\n"); for(j=0;j<n;j++) printf("%d\t",arr1[i][j]); } printf("\nThe Second Matrix Is :\n"); for(i=0;i<n;i++) { printf("\n"); for(j=0;j<n;j++) printf("%d\t",brr1[i][j]); } for(i=0;i<n;i++) for(j=0;j<n;j++) crr1[i][j]=arr1[i][j]+brr1[i][j]; printf("\nThe Addition Of Two Matrix Is : \n"); for(i=0;i<n;i++){ printf("\n"); for(j=0;j<n;j++) printf("%d\t",crr1[i][j]); } printf("\n\n"); } END