PF Manual Muhammad Uzair 64498
PF Manual Muhammad Uzair 64498
Programming Fundamentals
Na me : M u h a mma d Uzair
Registration id : 64498
P r o g ra m : c y b e r s e c u r i ty
Experiment 1
Objective
Getting familiar with the IDE and working with Turbo C &Microsoft Visual Studio. Installing and copying the compiler,
changing directory settings. Making first program in C. Saving / copying files to USB or other storage devices. Theory
IDE
An integrated development environment (IDE) is a software suite that consolidates the basic tools developers need to write
and test software. Typically, an IDE contains a code editor, a compiler or interpreter and a debugger that the developer
accesses through a single graphical user interface (GUI). An IDE may be a standalone application, or it may be included as
part of one or more existing and compatible applications.
Example Program
Output
#include<stdio.h>
#include<conio.h>
void main(void)
{ clrscr(); printf("My First My First Program
Program"); getch();
}
Exercis
Program Output
#include<stdio.h>
#include<conio.h>
void main(void)
{
printf("My First Program");
}
#include<stdio.h>
#include<conio.h>
void main(void)
{
getch();
printf("My First Program");
}
#include<stdio.h>
#include<conio.h>
void main(void)
{
getch();
printf("My First Program");
getch();
}
#include<stdio.h>
#include<conio.h>
void main(void)
{
getch(); clrscr();
printf("My First Program");
}
#include<stdio.h>
#include<conio.h>
void main(void)
{
getch(); printf("My First
Program"); clrscr();
}
#include<stdio.h>
#include<conio.h>
void main(void)
{
getch();
clrscr();
printf("My First Program");
getch();
}
#include<stdio.h>
#include<conio.h>
void main(void)
{
getch();
printf("My First
Program"); getch();
clrscr();
}
Program Output
#include<stdio.h>
#include<conio.h>
void main(void)
{
clrscr();
getch();
printf("My First Program");
getch();
}
#include<conio.h>
#include<stdio.h>
void main(void)
{ clrscr(); printf("My
First Program"); getch();
}
#include”conio.h”
#include”stdio.h”
void main(void)
{
clrscr();
printf("My First Program");
getch();
}
#include<conio.h>
#include<stdio.h>
main()
{
clrscr();
printf("My First Program");
getch();
}
#include<CONIO.H>
#include<STDIO.H>
void main(void)
{ clrscr(); printf("My
First Program"); getch();
}
Assignment
□ Write a C program using commands described in this lab to make a resume showing your complete details.
#include <stdio.h>
#include<conio.h>
void main()
{
printf("Name:Muhammad Haroon \n");
printf("Address: DHA, karachi\n");
printf("Phone: +03172402969 \n");
printf("Email:muhammad@iqra.edu.pk \n\n");
printf("Education:\n");
printf(" Bachelor of Science in Computer Science\n");
printf(" Iqra University\n\n");
printf("Work Experience:\n");
printf("2 years experience on freelancing\n");
printf(" Student\n\n");
printf("Skills:\n");
printf(" - Programming Languages: C, C++\n"); printf("
- Problem Solving and Algorithm Design\n\n");
printf("Projects:\n");
printf(" DLD LAB: PIR sensor based security system\n");
_getch();
}
OUTPUT:
Experiment 2
Objective
Understanding and Using format specifier and escape sequences with printf.
Theory
The C library functionprintf() sends formatted output to stdout.Following is the declaration for printf() function.
format − This is the string that contains the text to be written to stdout. It can optionally contain embedded format tags
that are replaced by the values specified in subsequent additional arguments and formatted as requested.
Format specifiers are used to substitute and print values inside a printf or scanf statement which are further applicable on
variables. Below is a chart of format specifier examples using printf.
Example
Program Output
#include<stdio.h>
#include<conio.h>
void main(void)
{ A
clrscr(); Iqra University
printf("\n%c",'a'); 20
printf("\n%s","Iqra University"); 35.5
printf("\n%d",20); 1234567
printf("\n%f",35.5); pri
ntf("\n%ld", 1234567);
getch();
}
#include<stdio.h>
#include<conio.h>
void main(void)
{
clrscr(); printf("\n%c %s %d %f
%ld",'a',"Iqra A Iqra University 2035.5 1234567
University",20,35.5,134567);
getch();
EXERCISE:
Assignment
• Write a C program to make your resume with format specifiers and escape sequences showing your complete
details.
• #include <stdio.h>
#include<conio.h>
void main()
{
printf("************ Resume ************\n\n");
printf("Name:\t\t\Muhammad Haroon \n");
printf("Address:\t\DHA, karachi\n");
printf("Phone:\t\t\t03172402969 \n");
printf("Email:\t\t\muhamad@iqra.edu.pk \n\n");
printf("************ Education ************\n\n");
printf("Degree:\t\t\tBachelor of Science in Computer Science\n");
printf("School:\t\t\tIqra University\n");
printf("************ Work Experience ************\n\n");
printf("\t\t2 years experience on freelancing \n");
printf("\t\tStudent\n");
printf("************ Skills ************\n\n");
printf("Programming Languages:\tC, C++, javascript\n");
printf("Skills:\t\t\tProblem Solving, Algorithm Design\n\n");
printf("************ Projects ************\n\n");
printf("Project:\t\tPIR Sensor based security system\n\n");
printf("************ End of Resume ************\n");
_getch();
int main() {
printf("Create a new line.\n");
printf("Print a double quote (\") within a string.\n");
printf("Print a single quote (') within a string.\n");
printf("Print a Backslash\\ within a string.\n");
printf("Using Backspace\b within a string.\n");
printf("Using\tTab within a string.\n");
return 0;
}
Theory
Variables are declared by first writing data types followed by a variable name, e.g. int a=10;
Here
Example
Program Output
#include<stdio.h>
#include<conio.h>
void main(void)
{ clrscr();
#include<stdio.h>
#include<conio.h> void
main(void)
{ clrscr(); char
a='a',a1=’b’;
Assignment
Write a C program to declare multiple variable that shows an employee details such as Name, age, phone no, salary
address and designation. The program should print details of employee.
#include <stdio.h>
int main() {
char name[50]
="Haroon”;
int age = 20;
long long phoneNumber =172402969
; double salary = 56000;
char address[100] = "DHA ,karachi";
char designation[50] = "programmer";
printf("Employee Details:\n");
printf("Name: %s\n", name);
printf("Age: %d\n", age);
printf("Phone Number: %lld\n", phoneNumber);
printf("Salary: $%.2lf\n", salary);
printf("Address: %s\n", address);
printf("Designation: %s\n", designation);
return 0;
}
22
Experiment 4
Objective
Taking Input from the user at console screen using scanf and getche commands.
Theory
Scanf command can take input of different data types at a time.
Getche command can take only one character input.
Example
Write the output after supplying appropriate input on console screen.
Program Output
#include<stdio.h>
#include<conio.h
> void main(void)
{
clrscr();
char a;
int b;
float c;
double
d;
printf("\nEnter character ");
scanf("%c",&a);
printf("\nEnter integer ");
scanf("%d",&b);
printf("\nEnter float ");
scanf("%f",&c); printf("\nEnter
double "); scanf("%lf",&d);
printf("\n%c %d
%f
%lf",a,b,c,d); getch();
}
#include<stdio.h>
#include<conio.h
> void main(void)
{
clrscr();
char a;
int b;
float c;
double
d;
printf("Enter char integer float
double\n"); scanf("%c %d %f
%lf",&a,&b,&c,&d); printf("\n%c %d %f
%ld",a,b,c,d); getch();
}
#include<stdio.h>
#include<conio.h
> void main(void)
{ clrscr(); pri ntf("\nWhat is your
section : "); getche();
getch();
}
Assignment
Write a C program that takes input from the user and then displays the complete details such as his name, course, university,
program and location.
#include <stdio.h>
int main() {
18
char name[50], course[50], university[50], program[50], location[50];
printf("Enter your name: ");
scanf("%49[^\n]", name);
return 0;
}
Write a C program that takes two numbers as input and swap their values .
20
Experiment 5
Objective
Arithmetic operators, conditional operators, assignment operators, Increment/decrement operators.Studying Math
functions.
Theory
Math.h header file is included for the definitions of math functions listed below. It is written as #include<math.h>.
Example
The program below shows the result for math and trigonometric functions. The functions pass the values to variables which
are further used for printing in printf.
Program
Output
#include<stdio.h>
#include<conio.h>
#include<math.h> void
main(void)
{ clrscr();
float a=45,b=1,sn,cs,tn,snh,csh,tnh; sn=sin(a);
cs=cos(a); Trignometric
Functions
sin 45 = 0.85 cos
tn=tan(a); 45 = 0.53
snh=sinh(b); csh=cosh(b); tan 45 = 1.62
tnh=tanh(b);
Hyperbolic Functions
printf("\n\n\n Trignometric Functions"); sinh 1 = 1.18
printf("\nsin 45 = %.2f",sn); cosh 1 = 1.54
printf("\ncos 45 = %.2f",cs); tanh 1 = 0.76
printf("\ntan 45 = %.2f",tn);
printf("\n\n\n Hyperbolic Functions");
printf("\nsinh 1 = %.2f",snh);
printf("\ncosh 1 = %.2f",csh);
printf("\ntanh 1 = %.2f",tnh); getch();
}
The program below shows the result for math and trigonometric functions. It also demonstrates that some functions may be
called within the body of another function. For example here all the trigonometric functions are called inside printf function.
Program Output
#include<stdio.h>
#include<conio.h>
#include<math.h>
Operators
int a=2,b=4,c1,c2,c3,c4,d1,d2,d3,d4; 6 -2 8 0
c1=c2=c3=c4=5; 010101
d1=d2=d3=d4=8; 8 2 15 1
printf("\n%d %d %d %d",a+b,a-b,a*b,a/b); 8987
printf("\n%d %d %d %d %d %d",a>b,a<b,a>=b,a<=b,a==b,a!=b);
printf("\n%d %d %d %d",c1+=3,c2-=3,c3*=3,c4/3); printf("\n%d
%d %d %d",d1++,++d2,d3--,--d4); getch();
}
Exercise
Write output for following programs and give reasons.
Program Output
#include<stdio.h>
#include<conio.h
> main()
{
clrscr(); int a=5;
printf("\n%d
%d",a++,a);
printf("\n%d
",a);
getch();
}
#include<stdio.h>
#include<conio.h
> main()
{
clrscr(); int a=5;
printf("\n%d
%d",++a,a);
printf("\n%d
",a);
getch();
}
#include<stdio.h>
#include<conio.h
> main()
{
clrscr(); int a=5;
printf("\n%d
%d",a - -,a);
printf("\n%d
",a); getch();
}
Program Output
#include<stdio.h>
#include<conio.h>
main()
{
clrscr(); int
a=5; printf("\n%d
%d",- - a,a);
printf("\n%d ",a);
getch();
}
#include<stdio.h>
#include<conio.h>
main()
{
clrscr();
int a=5;
printf("\n%d %d %d %d",a++,++a,a,a
- -); printf("\n%d ",a); getch();
}
#include<stdio.h> #include<conio.h>
main()
{
clrscr(); int a=5; printf("\n%d %d
%dd",a+5,++a,a);
printf("\n%d ",a);
getch();
}
#include<stdio.h>
#include<conio.h>
main()
{
clrscr(); int a=5; printf("\n%d %d
%dd",a+=5,++a,a);
printf("\n%d ",a);
getch();
}
#include<stdio.h>
#include<conio.h>
main()
{
clrscr(); int a=5; printf("\n%d %d
%dd",a-=5,- -a,a);
printf("\n%d ",a);
getch();
}
#include<stdio.h> #include<conio.h>
main()
{
clrscr(); int a=5; printf("\n%d %d
%dd",a+=5,a++,a);
printf("\n%d ",a);
getch();
}
Assignment
24
#include <stdio.h>
#include <math.h>
int main() {
double x;
scanf("%lf", &x);
return 0;
// For the equation 3x^4 + 4x^3 + x^2 + 7x + 9, substitute the user-provided value of x and generate the
result double xValue;
printf("Enter a value for x: ");
scanf("%lf", &xValue);
double equationResult = 3 * pow(xValue, 4)+ 4 * pow(xValue, 3) + pow(xValue, 2) + 7 * xValue + 9;
printf("Result of the equation for x = %.2f: %.4f\n", xValue,
Experiment 6
Objective
Studying loops. For loops, nested for loops, while loops, nested while loops, do while loops, nested do while loops. Studying
loops with cross combination.
Theory
for while do while
initialization initialization; do
; {
for(initialization ; check range ; iteration) body
{ while(check
range) iteration;
body } while(check range)
{
} body
iteration;
}
Example: This table below shows nested loops. All the three programs have same output.
Loop Nested
Program Output Program Output
Loop
#include<stdio.h> 03
#include<conio.h> 13
void main(void) 23
{ 33
#include<stdio.h> clrscr();
#i nclude<conio. for(int a=0;a<=3;a++)
h>void main(void) {
{ for(int b=0;b<=3;b++)
clrscr(); For {
For printf("%d%d\t",a,b);
for(int
a=0;a<=12;a++) printf("%d }
printf("\n");
x 2= }
%d\n",a,a*2); getch(); getch();
} }
#include<stdio.h>
#include<conio.h>
void main(void)
{ clrscr();
int a=0,b;
0x2=0 while(a<=3)
1x2=2 {
2x2=4 b=0;
3x2=6 while(b<=3)
#include<stdio.h> 4x2=8 {
5 x 2 = 10 01 02
While #incl printf("%d%d\t",a,b);
ude<conio. h> 6 x 2 = 12 While b++; 11 12
void main(void) 7 x 2 = 14 } 21 22
{ clrscr(); int 8 x 2 = 16 printf("\n"); 31 32
a=0; 9 x 2 = 18 a++;
while(a<=12) }
10 x 2 = 20 getch();
{
11 x 2 = 22 }
printf("%d x 2 = %d\n",a,a*2);
a++; 12 x 2 = 24
}
getch();
}
#include<stdio.h>
#include<conio.h>
void main(void)
{
clrscr();
int
a=0,b;
do
{
Do while b=0;
Do while do
#include<stdio.h> {
#i nclude<conio. printf("%d%d\t",a,b);
h>void main(void) b++;
{ }while(b<=3);
clrscr() printf("\n");
; int a++; }while(a<=3);
a=0; do getch();
{
printf("%d x 2 = %d\n",a,a*2); }
a++;
}
while(a<=12);
getch(); 00
} 10
20
30
28
This program of while loops takes continuous input until enter key is pressed.
Program Output
#include<stdio.h>
#include<conio.h>void main(void)
{ clrscr();
}
Exercise
Carefully observer the following program and write output with reasons.
Program Output
#include<stdio.h>
#include<conio.h
> void main(void)
{
clrscr();
for(int a=0;a<=12;a++);
printf("%d x 2 = %d\n",a,a*2);
getch(); }
#include<stdio.h>
#include<conio.h
> void main(void)
{
clrscr();
for(int a=0;1 ;a++);
printf("%d x 2 = %d\n",a,a*2);
getch(); }
infinite result
#include<stdio.h>
#include<conio.h
> void main(void)
{ clrscr();
int a=0;
while(0)
{ printf("%d x 2 = %d\n",a,a*2); a++;
} getch();
}
#include<stdio.h>
#include<conio.h
> void main(void)
{ clrscr();
int a=0,b;
while(a<=3)
;
{
b=0;
while(b<=3);
{
printf("%d%d\t",a,b);
b++;
}
printf("\n");
a++;
} getch();
}
Program:
Following program gives the sum of all numbers input by the user using while loop. The program will display the
sum when user enters 0 as input.
#include<conio.h>
#include<stdio.h>
30
Understand the difference between break and continue by executing following programs.
break continue
int main() int main()
{ int { int
i; i;
double number, sum = 0.0; double number, sum = 0.0;
return 0;
return 0;
} }
do- do-
for while for do- while while do- while
while for while for while while
for( ) while( ) for( ) { do while( ) do
{ { for( d { for( ) { {
while( ) ) o while( }while( do while( )
} } ) while( ) }while(
)
} )
}
Example
} 00 01 02 03
getch(); 10 11 12 13
} 20 21 22 23
#include<stdio.h>
{ clrscr();
int a=0,b;
while(a<=3)
{
While For for(b=0;b<=3;b++)
{
printf("%d%d\t",a,b);
}
printf("\n");
a++;
}
getch();
}
Assignment
1. Input any number from user and generate its factorial e.g. factorial of 7 is 5040.
#include <stdio.h>
int main() {
int
number;
printf("Enter a number: ");
scanf("%d", &number);
if (number < 0) {
printf("Factorial is not defined for negative numbers.\n");
return 0;
}
unsigned long long factorial = 1;
int i;
for (i = 1; i <= number; ++i) {
factorial *= i;
}
printf("Factorial of %d is %llu\n", number, factorial);
return 0;
}
2. Write a program using loops to generate following pattern.
#include <stdio.h>
int main() {
int rows, i, j, k;
printf("Enter the number of rows for the star pyramid: ");
scanf("%d", &rows);
for (i = 1; i <= rows; ++i) {
for (j = 1; j <= rows - i; ++j) {
printf(" ");
}
for (k = 1; k <= 2 * i - 1; ++k) {
printf("*");
}
printf("\n");
}
return 0;
}
1
121
12321
1234321
123454321
12345654321
1234567654321
123456787654321
12345678987654320
#include <stdio.h>
int main() {
int rows, i, j;
printf("Enter the number of rows for the pattern: ");
scanf("%d", &rows);
for (i = 1; i <= rows; ++i) {
for (j = 1; j <= rows - i; ++j) {
printf(" ");
}
for (j = 1; j <= i; ++j) {
printf("%d", j);
}
for (j = i - 1; j >= 1; --j) {
printf("%d", j);
}
printf("\n");
}
return 0;
12 x 2 = 24
11 x 2 = 22
10 x 2 = 20
9 x 2 = 18
34
8 x 2 = 16
7 x 2 = 14
6 x 2 = 12
5 x 2 = 10
4 x 2 = 8
3 x 2 = 6
2 x 2 = 4
1 x 2 = 2
#include <stdio.h>
int main() {
int i;
for (i = 12; i >= 1; --i) {
printf("%d x 2 = %d\n", i, i * 2);
}
return 0;
}
5. The Fibonacci sequence is a series where the next term is the sum of pervious two terms. Write a C
program to generate this sequence.
#include <stdio.h>
int main() {
int n, i, term1 = 0, term2 = 1, nextTerm;
return 0;
}
Experiment 7
Objective
Decision making and conditioning using If statements, If-else statements, switch-case.
Theory
{ { { { {
} { } } body
} { If(cond) body
} Body { }
} Body
}
Example
This program illustrates simple if and nested if statements with else conditions.
Program Output
#include<stdio.h>
#include<conio.h>
void main(void)
{ clrscr(); char
ch; int
chr=0,wrd=1;
printf("Type any sentence\n");
36
#include<stdio.h>
#include<conio.h>
void main(void)
{ clrscr(); int cp=0; printf("Enter CP
marks between 1 & 100\n");
scanf("%d",&cp);
if(cp>=0 && cp<=100)
{
if(cp>=75) printf("\nGrade
A");
else if(cp>=50)
printf("\nGrade C");
}
else
printf("\nIncorrect Input");
getch();
}
This program lets the user choose a number between 1 and 99 and guesses it in less than 10 hints.
Program Output
#include<stdio.h>
#include<conio.h>
void main(void)
{ clrscr();
float gss,incr;
char ch;
printf("Think of a number Between 1 & 99\n");
This program prompts the user to type his/her name. If any thing other than Upper/lower case alphabets or a space is
entered for example 1 ,2,@#$% then it is detected and nothing is printed until a correct character is input.
Program Output
#include<stdio.h>
#include<conio.h
> void main(void)
{
clrscr(); char ch;
printf("Name : ");
while((ch=getche())!='\r')
{
if( (ch>=65 && ch<=90) || (ch>=97 && ch<=122) || ch==' '
); else
printf("\b \b");
}
getch();
This is a simple calculator program that adds or subtracts two numbers entered by the user.
Program Output
This is same as the previous one except it take numbers instead of operators to show that switch can also accept numbers.
Program Output
include<stdio.h>
#include<conio.h>
void main(void)
{
clrscr();
float nm1 =1 .0,nm2=1
.0; int op;
while(!(nm1==0.0 && nm2==0.0))
{
printf("\nType Number Operator Numbeñn");
printf("\nType 1 for addition and 2 for subtraction in
operatoñn"); scanf("%f %d %f",&nm1,&op,&nm2);
switch(op)
{
case 1:
printf(" = %f ",nm1+nm2);
break;
case 2:
printf(" = %f ",nm1-nm2);
break;
default:
printf("\nUnknown
Operatoñn"); }
printf("\n\n");
}
getch();
}#
Assignment
int main() {
int guess, number, attempts = 0;
srand(time(NULL));
number = rand() % 100 + 1; // Generates a random number between 1 and 100
do {
printf("Enter your guess: ");
scanf("%d", &guess);
attempts++;
switch (guess) {
case 0:
printf("Exiting the game. The number was %d.\n", number);
break;
case 1 ... 100:
if (guess < number) {
printf("Too low! Try again.\n");
} else if (guess > number) {
printf("Too high! Try again.\n");
} else {
printf("Congratulations! You guessed the number in %d attempts.\n", attempts);
}
break;
default:
printf("Invalid guess. Please enter a number between 1 and 100.\n");
}
return 0;
}
• Make an alphabet guessing program using if-else.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
char target, guess;
srand(time(NULL));
target = 'a' + rand() % 26;
printf("Welcome to the Alphabet Guessing Game!\n");
printf("Try to guess the letter between 'a' and 'z'.\n");
do {
printf("Enter your guess: ");
scanf(" %c", &guess);
if (guess >= 'a' && guess <= 'z') {
if (guess < target) {
printf("Too low! Try again.\n");
} else if (guess > target) {
printf("Too high! Try again.\n");
} else {
printf("Congratulations! You guessed the letter.\n");
}
} else {
printf("Invalid guess. Please enter a lowercase letter.\n");
}
return 0;
• Complete the simple calculator program for multiplication and division. Also make it using if-else.
#include <stdio.h>
int main() {
double num1, num2;
char operator;
// Input
printf("Enter first number: ");
scanf("%lf", &num1);
return 0;
}
• Make your resume such that in the name field it does not accept anything else than alphabets and space bar.
Look at the scenario below.
Take marks as input from user calculate grade for each subject,CGPA and
percentage.
Detect error for out ranged numbers e.g. below 0 or above 100.
#include <stdio.h>
return 2.0;
} else if (marks >= 50 && marks <= 59) {
return 2.25;
return 2.5;
return 2.75;
return 3.0;
return 3.5;
return 4.0;
} else {
return -1.0;
if (gpa == 4.0) {
return 'A';
return 'B';
return 'C';
return 'D';
} else {
return 'F';
42
}
int main() {
char name[50];
scanf("%d", &marksMath);
scanf("%d", &marksPhysics);
scanf("%d", &marksElectronics);
scanf("%d", &marksIslamiat);
scanf("%d", &marksProgramming);
if (marksMath < 0 || marksMath > 100 || marksPhysics < 0 || marksPhysics > 100 ||
marksElectronics < 0 || marksElectronics > 100 || marksIslamiat < 0 || marksIslamiat > 100 ||
printf("Subject\t\tMarks\tGrade\tGPA\n");
return 0;
}
2. Program the following.
44
• Ask User to select one option from the following.
• Calculate area of Circle [c]
• Calculate area of Rectangle [r]
• Calculate area of Triangle [t]
• If user enters c then ask user to enter radius and calculate area using following formula.
• If user enters r then ask user to enter length & width and calculate area using following formula.
• If user enters t then ask user to enter base & height and area using following formula.
#include <stdio.h>
int main() {
char
option;
float radius, length, width, base, height, area;
switch (option) {
case 'c':
// Calculate area of Circle
printf("Enter the radius of the cir