Assignment # 1 Computer Programming (Lab)
Assignment # 1 Computer Programming (Lab)
Name: Reg.no:
Program: BS (CS) Instructor: Maira Ali
Semester: Marks: 10
1. Write your answers directly on the Black Board (recommended) or upload word file before the due date
on Blackboard.
2. Write your name and registration ID on the first page of your Word file.
3. Answer scripts can only be uploaded on Blackboard only during the submission time.
4. To avoid any unforeseen problems, you are advised to follow the Guide lines communicated by the
Faculty Members.
5. Submission of answer copy(ies) will be considered acceptable through Blackboard only. Therefore, do not
submit your document through email or any other medium.
6. Use 12 pt. font size and Times New Roman font style along with 1-inch page margins.
7. Follow the requirements of the word limit and the marking criteria while writing your answers.
8. Provide relevant, original and conceptual answers, as this exam aims to test your ability to examine,
explain, modify or develop concepts discussed in class.
9. Do not copy answers from the internet or other sources. The plagiarism of your answers may be checked
through Turnitin.
10. Double check your word file before uploading it on BlackBoard to ensure that you have uploaded the
correct file with your answers.
Experiment 1
Example
Program Output
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int main()
{ My First
system(“cls”); Program
printf("My First Program");
retrun 0;
}
Exercise
Carefully look at the following programs and write the output.
Program Output
#include<stdio.h> Write the output for the program
#include<conio.h> on left
int main()
{
printf("My First Program");
}
#include<stdio.h> Write the output for the program
#include<conio.h> on left
int main()
{
retrun 0;
printf("My First Program");
}
#include<stdio.h> Write the output for the program
#include<conio.h> on left
int main()
{
retrun 0;
printf("My First Program");
retrun 0;
}
#include<stdio.h> Write the output for the program
#include<conio.h> on left
#include<stdlib.h>
int main()
{
retrun 0;
system(“cls”);
printf("My First Program");
}
#include<stdio.h> Write the output for the program
#include<conio.h> on left
#include<stdlib.h>
int main()
{
retrun 0;
printf("My First Program");
system(“cls”);
}
#include<stdio.h> Write the output for the program
#include<conio.h> on left
#include<stdlib.h>
int main()
{
retrun 0;
system“cls”);
printf("My First Program");
retrun 0;
}
#include<stdio.h> Write the output for the program
#include<conio.h> on left
#include<stdlib.h>
int main()
{
retrun 0;
printf("My First Program");
retrun 0;
system(“cls);
}
Program Output
#include<stdio.h> Write the output for the program
#include<conio.h> on left
#include<stdlib.h>
int main()
{
system(“cls”);
retrun 0;
printf("My First Program");
retrun 0;
}
#include<conio.h> Write the output for the program
#include<stdio.h> on left
#include<stdlib.h>
int main()
{
system(“cls”);
printf("My First Program");
retrun 0;
}
#include”conio.h” Write the output for the program
#include”stdio.h” on left
#include”stdlib.h”
int main()
{
system(“cls”);
printf("My First Program");
retrun 0;
}
#include<conio.h> Write the output for the program
#include<stdio.h> on left
#include<stdlib.h>
main()
{
system(“cls”);
printf("My First Program");
retrun 0;
}
#include<CONIO.H> Write the output for the program
#include<STDIO.H> on left
#include<STDLIB.H>
int main()
{
system(“cls”);
printf("My First Program");
retrun 0;
}
Assignment
Write a C program using commands described in this lab to make a resume showing your
complete details.
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.
Escape Sequences are used to adjust spacing between lines or characters or the characters themselves.
Example
Program Output
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int main()
{ A
system(“cls”); 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);
retrun 0;
}
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int main()
{
system(“cls”); A Iqra University 2035.5 1234567
printf("\n%c %s %d %f %ld",'a',"Iqra
University",20,35.5,134567);
retrun 0;
}
Write the output for following programs and give reasons.
Program Output
#include<stdio.h> Write the output for the program
#include<conio.h>
#include<stdlib.h>
int main()
{
system(“cls”);
printf("\n%d",'a'); printf("\n%s","Iqra
University"); printf("\n%c",20);
printf("\n%f",35.5);
pri ntf("\n%ld", 1234567);
retrun 0;
}
Assignment
Write a C program to make your resume with format specifiers and escape sequences showing your complete details.
Experiment 3 .
Objective
Studying different data types, variables, variable names, variable declaration, variable definitionand variable initialization.
Theory
Variables are declared by first writing
data types followed by a variable name, e.g.
int a=10;
Here
Supported Example
No. Data Type Syntax format Specifier Value
1 Single Character char %c One character within single quotes char a=’a’;
2 Decimal Integer int %d Any whole number between -32,768 to 32,767 int a=10;
3 Long Integer long int %ld Any number between -2,147,483,648 to 2,147,483,647 long int a=12345;
4 Float float %f Any decimal point number between 10-38 to 1038 float a=1234.567;
0-308
5 Double double %lf Any decimal point number between 1 to 10308 double a=1 23456;
Variable Names
Variable names will always start with an alphabet.
Variable names can contain numbers (1,2,45,66) and underscores (_) but no other special characters (!@#$%^&*).
Variable names cannot resemble to any predefined word e.g. include, printf, getch, scanf etc..
A variable name cannot be used for multiple declarations.
Example
Program Output
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int main()
{
system(“cls”); a 12
char a='a'; 1234567
12.5
int b=12;
float c=12.5;
double d=1234567;
printf("%c %d %f %lf",a,b,c,d);
retrun 0;
}
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int main()
{
system(“cls”);
char a='a',a1=’b’; b 13
int b=12,b1=13; 1234568
13.5
float c=12.5,c1=13.5;
double d=1234567,d1=1234568;
printf("\n%c %d %f %lf",a,b,c,d);
printf("\n%c %d %f %lf",a1 ,b1 ,c1,d1);
retrun 0;
}
Write the output for following programs.
Program Output
#include<stdio.h> Write the output for the program on left
#include<conio.h>
#include<stdlib.h>
int main()
{
system(“cls”);
char a=97;
int b=’A’;
float c=12.5;
double d=1234567;
printf("%c %d %f %lf",a,b,c,d);
retrun 0;
}
Assignment
Write a C program to make a resume showing your complete details using variables.
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> Write the output for the program
#include<conio.h> on left
#include<stdlib.h>
int main()
{
system(“cls”);
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);
retrun 0;
}
#include<stdio.h> Write the output for the program
#include<conio.h> on left
#include<stdlib.h>
int main()
{
system(“cls”)
; 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);
retrun 0;
}#include<stdio.h> Write the output for the program
#include<conio.h> on left
#include<stdlib.h>
int main()
{
system(“cls”);
pri ntf("\nWhat is your section : ");
getche();
retrun 0;
}
Assignment
Write a C program to make a resume that takes input from the user and then displays the complete details.
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>
#include<stdlib.h>
int main()
{
system(“cls”);
float a=45,b=1,sn,cs,tn,snh,csh,tnh;
sn=sin(a);
cs=cos(a); Trignometric Functions
tn=tan(a); sin 45 = 0.85
cos 45 = 0.53
snh=sinh(b); tan 45 = 1.62
csh=cosh(b);
tnh=tanh(b);
Hyperbolic Functions
printf("\n\n\n Trigonometric 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);
retrun 0;
}
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>
#include<stdlib.h>
int main() Trignometric Functions
{ sin 45 = 0.85
system(“cls”); cos 45 = 0.53
printf("\n\n\n Trignometric Functions"); tan 45 = 1.62
printf("\nsin 45 = %.2f",sin(45));
printf("\ncos 45 = %.2f",cos(45)); Hyperbolic Functions
printf("\ntan 45 = %.2f",tan(45)); sinh 1 =1.18
cosh 1 = 1.54
printf("\n\n\n Hyperbolic Functions"); tanh 1 = 0.76
printf("\nsinh 1 = %.2f",sinh(1));
printf("\ncosh 1 = %.2f",cosh(1));
printf("\ntanh 1 = %.2f",tanh(1)); Math Functions
pow 2,3 = 8.00
printf("\n\n\n Math Functions"); sqrt 49 = 7.00
printf("\npow 2,3 = %.2f",pow(2,3));
printf("\nsqrt 49 = %.2f",sqrt(49));
retrun 0;
Operators
Exercise
Write output for following programs and give reasons.
Program Output
#include<stdio.h> Write the output for the program on
#include<conio.h> left
#include<stdlib.h>
main()
{
system(“cls”);
int a=5;
printf("\n%d %d",a++,a);
printf("\n%d ",a);
retrun 0;
}
#include<stdio.h> Write the output for the program on
#include<conio.h> left
#include<stdlib.h>
main()
{
system(“cls”);
int a=5;
printf("\n%d %d",++a,a);
printf("\n%d ",a);
retrun 0;
}
#include<stdio.h> Write the output for the program on
#include<conio.h> left
#include<stdlib.h>
main()
{
system(“cls”);
int a=5;
printf("\n%d %d",a - -,a);
printf("\n%d ",a);
retrun 0;
}
Program Output
#include<stdio.h> Write the output for the program
#include<conio.h> on left
#include<stdlib.h>
main()
{
system(“cls”);
int a=5;
printf("\n%d %d",- -a,a);
printf("\n%d ",a);
retrun 0;
}
#include<stdio.h> Write the output for the program
#include<conio.h> on left
#include<stdlib.h>
main()
{
system(“cls
”); int a=5;
printf("\n%d %d %d %d",a++,++a,a,a - -);
printf("\n%d ",a);
retrun 0;
}
#include<stdio.h> Write the output for the program
#include<conio.h> on left
#include<stdlib.h>
main()
{
system(“cls”);
int a=5;
printf("\n%d %d %dd",a+5,++a,a);
printf("\n%d ",a);
retrun 0;
}
#include<stdio.h> Write the output for the program
#include<conio.h> on left
#include<stdlib.h>
main()
{
system(“cls”);
int a=5;
printf("\n%d %d %dd",a+=5,++a,a);
printf("\n%d ",a);
retrun 0;
}
#include<stdio.h> Write the output for the program
#include<conio.h> on left
#include<stdlib.h>
main()
{
system(“cls”);
int a=5;
printf("\n%d %d %dd",a-=5,- -a,a);
printf("\n%d ",a);
retrun 0;
}
#include<stdio.h> Write the output for the program
#include<conio.h> on left
#include<stdlib.h>
main()
{
system(“cls”);
int a=5;
printf("\n%d %d %dd",a+=5,a++,a);
printf("\n%d ",a);
retrun 0;
}
Assignment
Loop Nested
Program Output Program Output
Loop
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<stdio.h> int main()
#i nclude<conio.h>
#include<stdlib.h> {
int main() system(“cls”);
{ for(int a=0;a<=3;a++)
For system(“cls”); {
For for(int b=0;b<=3;b++)
for(int a=0;a<=12;a++)
printf("%d x 2 = %d\n",a,a*2); {
retrun 0; printf("%d%d\t",a,b);
} }
printf("\n");
}
retrun 0;
}
#include<stdio.h>
#include<conio.h>
#include<stdio.h> #include<stdlib.h>
#incl ude<conio. h> int main()
#include<stdlib.h>
int main() 0x2=0 {
{ 1x2=2 system(“cls”);
system(“cls”); 2x2=4 int a=0,b;
int a=0; 3x2=6 while(a<=3)
While while(a<=12) 4x2=8 {
{ 5 x 2 = 10 b=0;
printf("%d x 2 = %d\n",a,a*2); 6 x 2 = 12 While while(b<=3) 00 01 02 03
a++; 7 x 2 = 14 { 10 11 12 13
} 8 x 2 = 16 printf("%d%d\t",a,b); 20 21 22 23
retrun 0; 9 x 2 = 18 b++; 30 31 32 33
} 10 x 2 = 20 }
11 x 2 = 22 printf("\n");
12 x 2 = 24 a++;
}
retrun 0;
}
#include<stdio.h>
#include<stdio.h>
#include<conio.h>
#i nclude<conio. h>
#include<stdlib.h>
#include<stdlib.h>
int main()
int main()
{ system(“ {
cls”); system(“cls”);
int a=0; int a=0,b;
Do while do
do {
{
printf("%d%d\t",a,b); b++;
printf("%d x 2 = %d\n",a,a*2); a+
+; }while(b<=3); printf("\n");
} a++;
while(a<=12); }while(a<=3);
retrun 0; retrun 0;
} }
This program of while loops takes continuous input until enter key is pressed.
Program Output
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int main()
{
system(“cls”); Type any sentence
int a=0; Iqra University
printf("Type any sentence\n"); Total Characters typed = 15
while(getche()!='\r')
a++;
printf("\nTotal Characters typed = %d",a);
retrun 0;
}
Exercise
Carefully observer the following program and write output with reasons.
Program Output
#include<stdio.h> Write the output for the program on left
#include<conio.h>
#include<stdlib.h>
int main()
{
system(“cls”);
for(int a=0;a<=12;a++);
printf("%d x 2 = %d\n",a,a*2);
retrun 0;
}
#include<stdio.h> Write the output for the program on left
#include<conio.h>
#include<stdlib.h>
int main()
{
system(“cls”);
for(int a=0;1 ;a++);
printf("%d x 2 = %d\n",a,a*2);
retrun 0; }
#include<stdio.h> Write the output for the program on left
#include<conio.h>
#include<stdlib.h>
int main()
{
system(“cls”);
int a=0;
while(0)
{
printf("%d x 2 = %d\n",a,a*2);
a++;
} retrun 0;
}
#include<stdio.h> Write the output for the program on left
#include<conio.h>
#include<stdlib.h>
int main()
{
system(“cls”);
int a=0,b;
while(a<=3);
{
b=0;
while(b<=3);
{
printf("%d%d\t",a,b);
b++;
}
printf("\n");
a++;
} retrun 0;
}
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>
#include<stdlib.h>
int main()
{
system("cls"); // clear screen system(“cls”);
int num=-1;
int sum=0;
do-while
for while while for for do-while do-while for while do-while
while
for( ) while( ) for( ) do while( ) do
{ { { { { {
while( ) for( ) d for( ) do while( )
} } o }while( ) while( ) }while( )
while( ) }
}
Example
sum += number; // sum = sum + number; sum += number; // sum = sum + number;
} }
return 0; return 0;
} }
Assignment
1. Input any number from user and generate its factorial e.g. factorial of 7 is 5040.
2. Write a program using loops to generate following pattern.
1
121
12321
1234321
123454321
12345654321
1234567654321
123456787654321
12345678987654320
4. Write a program using loops to generate following output.
12 x 2 = 24
11 x 2 = 22
10 x 2 = 20
9 x 2 = 18
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
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.
Experiment 7 .
Objective
Decision making and conditioning using If statements, If-else statements, switch-case.
Theory
If Nested If If-else Else-if Switch-case
if(cond) if(cond) if(cond) if(cond) switch(cond)
{ { { { {
Body If(cond) body body case’1‘:
} { } } body
body else else case’2‘:
} { 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>
#include<stdlib.h>
int main()
{
system(“cls”);
char ch;
int chr=0,wrd=1;
printf("Type any sentence\n");
while((ch=getche())!='\r') Type any sentence
{ Iqra Univ Khi Sindh Pak
chr++; Total Characters = 19
' '
if(ch==) Total Words = 5
{
wrd++;
chr--;
}
}
printf("\nTotal Characters = %d",chr);
printf("\nTotal Words = %d",wrd);
retrun 0;
}
#include<stdio.h> Write the output for the program on left
#include<conio.h>
#include<stdlib.h>
int main()
{
system(“cls”);
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");
retrun 0;
}
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>
#include<stdlib.h>
int main()
{
system(“cls”);
float gss,incr;
char ch;
printf("Think of a number Between 1 & 99\n");
printf("Press `g` for grateñn"); Think of a number between 1 and 99
printf("Press `l` for less\n"); Press `g` for greater
printf("Enter for exit\n"); Press `l` for less
Enter for exit
incr=gss=50;
Is your number greater, less or equal to 50
while(incr>1 .0)
{
printf("\nIs your number greater, less or equal to %.0f\n",gss); Is your number greater, less or equal to 75
incr/=2; Is your number greater, less or equal to 88
if((ch=getch())=='e')
break; You guessed 88
else if(ch=='g')
gss+=incr;
else
gss-=incr;
}
printf("You guessed %.0f",gss);
retrun 0;
}
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> Write the output for the program on left
#include<conio.h>
#include<stdlib.h>
int main()
{
system(“cls”);
char ch;
printf("Name : ");
while((ch=getche())!='\r')
{
if( (ch>=65 && ch<=90) || (ch>=97 && ch<=122) || ch==' ' );
else
printf("\b \b");
}
retrun 0;
}
this is a simple calculator program that adds or subtracts two numbers entered by the user.
Program Output
#include<stdio.h> Write the output for the program on left
#include<conio.h>
#include<stdlib.h>
int main()
{
system(“cls”);
float nm1 =1 .0,nm2=1 .0;
char op;
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> Write the output for the program on left
#include<conio.h>i
nt main()
{
system(“cls”);
float nm1 =1 .0,nm2=1 .0;
int op;
Assignment
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.