Programming Fundamentals
Lab Tasks Final Report
Submitted by: Muhammad Hammad
Roll Number: 1442-122008
Submitted to: Miss Saira Kishwar
19th feburary 2022
Question
Q#1: Make a simple calculator using c language Arithmetic operators.
#include <stdio.h>
int main(){
int a,b;
int add,sub,mul,div;
printf("Enter the value of a: ");
scanf("%d",&a);
printf("Enter the value of b: ");
scanf("%d",&b);
add = a+b;
sub = a-b;
mul = a*b;
div = a/b;
printf("Add = %d\n",add);
printf("Sub = %d\n",sub);
printf("Mul = %d\n",mul);
printf("Div = %d",div);
return 0;
}
Output
Calculator using switch case
#include <stdio.h>
int main()
{
char op;
float num1, num2, result;
printf("Enter [number 1] [+ - * /] [number 2]\n");
scanf("%f %c %f", &num1, &op, &num2);
switch(op)
{
case '+':
result = num1 + num2;
break;
case '-':
result = num1 - num2;
break;
case '*':
result = num1 * num2;
break;
case '/':
result = num1 / num2;
break;
default:
printf("Invalid operator: ");
}
printf("%f %c %f = %f", num1, op, num2, result);
return 0;
}
Output:
26th feburary 2022
Q#2: Take a number as input from user and print whether the number is even or
odd
#include <stdio.h>
int main() {
int num;
printf("Enter a Number: ");
scanf("%d", &num);
if(num % 2 == 0)
printf("%d is even.", num);
else
printf("%d is odd.", num);
return 0;
Output:
Q#3: Take a number as input from user and print whether the number
is positive or negative.
#include <stdio.h>
int main() {
int num;
printf("Enter a Number: ");
scanf("%d", &num);
if(num>=0)
printf("%d is Positive number.", num);
else
printf("%d is Negative number.", num);
return 0;
}
Output
05th March 2022
Q#4: Write a program to print the grades for the number entered by user using
switch.
#include <stdio.h>
int main(){
int marks;
printf("Enter marks: ");
scanf("%d",&marks);
switch(marks/10)
{
case 10:
case 9:
printf("Grade = A");
break;
case 8:
printf("Grade = B");
break;
case 7:
printf("Grade = C");
break;
case 6:
printf("Grade = D");
break;
default:
printf("Fail");
}
return 0;
}
Output:
Give Grade of students Marks
#include <stdio.h>
int main() {
int m;
printf("Enter the Marks: ");
scanf("%d",&m);
if (m>=90 && m<=100)
printf("Grade is A");
else if (m>=80)
printf("Grade is B");
else if (m>=70)
printf("Grade is C");
else if (m>=60)
printf("Grade is D");
else if (m<60)
printf("Fail");
return 0;
}
Output:
05th March 2022
Q#5: write a program to print whether the Entered character is vowel
or constant
#include <stdio.h>
int main() {
char c;
int vowel;
printf("Enter Character: ");
scanf("%c", &c);
if (c=='a' || c =='e' || c == 'i' || c == 'o' || c=='u')
printf ("%c is a Lower case",c);
else if (c=='A' || c =='E' || c == 'I' || c == 'O' || c=='U')
printf ("%c is a Upper case",c);
else
printf("it is a constant");
return 0;
}
Output:
19th March 2022
Q#6: Write a program to print table of a number entered by user.
#include <stdio.h>
int main() {
int n, i;
printf("Enter an integer: ");
scanf("%d", &n);
for (i = 1; i <= 10; ++i) {
printf("%d * %d = %d \n", n, i, n * i);
return 0;
}
Output:
26th March 2022
Q#7: Write a program to find the sum of natural number.
#include <stdio.h>
int main() {
int n,i,sum = 0;
printf("Enter the value of n(positive integer): ");
scanf("%d",&n);
for(i=1; i <= n; i++)
sum = sum + i;
printf("\nSum of first %d natural numbers is %d",n, sum);
return 0;
}
Output:
02th April 2022
Q#8: Write a C program to check a given array of integers and return
true if the given array contains two 5's next to each other, or two 5
separated by one element
#include <stdio.h>
int main(){
int arr_size;
int array1[] = {1, 5, 5, 5, 5};
arr_size = sizeof(array1)/sizeof(array1[0]);
printf("%d",test(array1, arr_size));
int array2[] = { 2, 7, 8, 10};
arr_size = sizeof(array2)/sizeof(array2[0]);
printf("\n%d",test(array2, arr_size));
int array3[] = {3, 3, 3, 5, 7, 5};
arr_size = sizeof(array3)/sizeof(array3[0]);
printf("\n%d",test(array3, arr_size));
int test(int numbers[], int arr_size)
int len = arr_size;
for (int i = 0; i < len - 1; i++)
if (numbers[i] ==5 && numbers[i + 1] == 5) return 1;
if (i + 2 < len && numbers[i] == 5 && numbers[i + 2] == 5) return 1;
return 0;
}
Output:
09th April 2022
Q#9: Write a C program to check a given array of integers and return
true if there is a 3 with a 5 somewhere later in the given array
#include <stdio.h>
int main(){
int arr_size;
int array1[] = {1, 2, 3, 4 };
arr_size = sizeof(array1)/sizeof(array1[0]);
printf("%d",test(array1, arr_size));
int array2[] = {3, 3, 5, 5, 5, 5};
arr_size = sizeof(array2)/sizeof(array2[0]);
printf("\n%d",test(array2, arr_size));
int array3[] = {2, 5, 5, 7, 8, 10};
arr_size = sizeof(array3)/sizeof(array3[0]);
printf("\n%d",test(array3, arr_size));
int test(int numbers[], int arr_size)
int three = 0;
for (int i = 0; i < arr_size; i++)
if (three && numbers[i] == 5) return 1;
if (numbers[i] == 3) three = 1;
return 0;
}
Output:
16th April 2022
Q# 10: Write a C program to check a given array of integers and return
true if the given array contains either 2 even or 2 odd values all next
to each other
#include <stdio.h>
#include <stdlib.h>
int main(){
int arr_size;
int array1[] = {1, 2, 3, 4 };
arr_size = sizeof(array1)/sizeof(array1[0]);
printf("%d",test(array1, arr_size));
int array2[] = {3, 3, 5, 5, 5, 5};
arr_size = sizeof(array2)/sizeof(array2[0]);
printf("\n%d",test(array2, arr_size));
int array3[] = {2, 5, 5, 7, 8, 10};
arr_size = sizeof(array3)/sizeof(array3[0]);
printf("\n%d",test(array3, arr_size));
int test(int numbers[], int arr_size)
int tot_odd = 0, tot_even = 0;
for (int i = 0; i < arr_size; i++)
if (tot_odd < 2 && tot_even < 2)
if (numbers[i] % 2 == 0)
{
tot_even++;
tot_odd = 0;
else
tot_odd++;
tot_even = 0;
return tot_odd == 2 || tot_even == 2;
Output:
23th April 2022
Q#11: Write a C program to check a given array of integers and
return true if the value 5 appears 5 times and there are no 5 next
to each other
#include <stdio.h>
int main(){
int arr_size;
int array1[] = { 3, 5, 1, 5, 3, 5, 7, 5, 1, 5 };
arr_size = sizeof(array1)/sizeof(array1[0]);
printf("%d",test(array1, arr_size));
int array2[] = {3, 5, 5, 5, 5, 5, 5};
arr_size = sizeof(array2)/sizeof(array2[0]);
printf("\n%d",test(array2, arr_size));
int array3[] = {3, 5, 2, 5, 4, 5, 7, 5, 8, 5};
arr_size = sizeof(array3)/sizeof(array3[0]);
printf("\n%d",test(array3, arr_size));
int array4[] = {2, 4, 5, 5, 5, 5};
arr_size = sizeof(array4)/sizeof(array4[0]);
printf("\n%d",test(array4, arr_size));
}
int test(int numbers[], int arr_size)
{
int flag = 0;
int five = 0;
for (int i = 0; i < arr_size; i++)
{
if (numbers[i] == 5 && !flag)
{
five++;
flag = 1;
}
else
{
flag = 0;
}
}
return five == 5;
}
Output:
30th April 2022
Q#12: Write a program in C to demonstrate how to handle the
pointers in the program
#include <stdio.h>
int main()
int* ab;
int m;
m=29;
printf("\n\n Pointer : How to handle the pointers in the program :\n");
printf("------------------------------------------------------------\n");
printf(" Here in the declaration ab = int pointer, int m= 29\n\n");
printf(" Address of m : %p\n",&m);
printf(" Value of m : %d\n\n",m);
ab=&m;
printf(" Now ab is assigned with the address of m.\n");
printf(" Address of pointer ab : %p\n",ab);
printf(" Content of pointer ab : %d\n\n",*ab);
m=34;
printf(" The value of m assigned to 34 now.\n");
printf(" Address of pointer ab : %p\n",ab);
printf(" Content of pointer ab : %d\n\n",*ab);
*ab=7;
printf(" The pointer variable ab is assigned the value 7 now.\n");
printf(" Address of m : %p\n",&m);
printf(" Value of m : %d\n\n",m);
return 0;
Output:
07th May 2022
Q#13: Write a program in C to demonstrate the use of &(address of)
and *(value at address) operator.
#include <stdio.h>
int main()
int m=300;
float fx = 300.60;
char cht = 'z';
printf("\n\n Pointer : Demonstrate the use of & and * operator :\n");
printf("--------------------------------------------------------\n");
int *pt1;
float *pt2;
char *pt3;
pt1= &m;
pt2=&fx;
pt3=&cht;
printf ( " m = %d\n",m);
printf ( " fx = %f\n",fx);
printf ( " cht = %c\n",cht);
printf("\n Using & operator :\n");
printf("-----------------------\n");
printf ( " address of m = %p\n",&m);
printf ( " address of fx = %p\n",&fx);
printf ( " address of cht = %p\n",&cht);
printf("\n Using & and * operator :\n");
printf("-----------------------------\n");
printf ( " value at address of m = %d\n",*(&m));
printf ( " value at address of fx = %f\n",*(&fx));
printf ( " value at address of cht = %c\n",*(&cht));
printf("\n Using only pointer variable :\n");
printf("----------------------------------\n");
printf ( " address of m = %p\n",pt1);
printf ( " address of fx = %p\n",pt2);
printf ( " address of cht = %p\n",pt3);
printf("\n Using only pointer operator :\n");
printf("----------------------------------\n");
printf ( " value at address of m = %d\n",*pt1);
printf ( " value at address of fx= %f\n",*pt2);
printf ( " value at address of cht= %c\n\n",*pt3);
}
Output:
Q#14: Write a program in C to swap two numbers
using function
#include<stdio.h>
void swap(int *,int *);
int main(){
int n1,n2;
printf("\n\n Function : swap two numbers using function :\n");
printf("------------------------------------------------\n");
printf("Input 1st number : ");
scanf("%d",&n1);
printf("Input 2nd number : ");
scanf("%d",&n2);
printf("Before swapping: n1 = %d, n2 = %d ",n1,n2);
swap(&n1,&n2);
printf("\nAfter swapping: n1 = %d, n2 = %d \n\n",n1,n2);
return 0;
void swap(int *p,int *q)
int tmp;
tmp = *p;
*p=*q;
*q=tmp;
}
Output:
21th May 2022
Q#15: Write a program in C to check a given number
is even or odd using the function.
28th May 2022
Python
Q#16: Write a function that returns the maximum of two numbers?
num1 = int(input("Enter the first number: "))
num2 = int(input("Enter the second number: "))
if(num1 > num2):
print(num1, "is greater")
elif(num1 < num2):
print(num2, "is greater")
else:
print("Both are equal")
Output:
04th Jun 2022
Q#17: Write a function that prints all the prime numbers
between 0 and limit where limit is a parameter?
lower = 0
upper = 100
print("Prime numbers between", lower, "and", upper, "are:")
for num in range(lower, upper + 1):
# all prime numbers are greater than 1
if num > 1:
for i in range(2, num):
if (num % i) == 0:
break
else:
print(num)
Output:
11th Jun 2022
Q#18: Write a Program and Check if the first and last number of
a list is the same?
test_list = [1, 5, 6, 7, 4]
print ("The original list is : " + str(test_list))
res = [ test_list[0], test_list[-1] ]
print ("The first and last element of list are : " + str(res))
Output:
Q#19: Display numbers divisible by 5 from a list?
def result(N):
for num in range(N):
if num % 3 == 0 and num % 5 == 0:
print(str(num) + " ", end = "")
else:
pass
if __name__ == "__main__":
N = 100
result(N)
Output:
Q #20: Print multiplication table from 1 to 10?
for i in range(1,11):
print("\n\nMULTIPLICATION TABLE FOR %d\n" %(i))
for j in range(1,11):
print("%-5d X %5d = %5d" % (i, j, i*j))
Output: