0% found this document useful (0 votes)
42 views

Assignment # 1 Computer Programming (Lab)

The document provides instructions for Assignment #1 in Computer Programming Lab. It outlines 10 points for students to follow when completing and submitting the assignment, including writing their name and registration ID, following formatting guidelines, submitting only through Blackboard, and avoiding plagiarism. Students are to complete exercises writing the output for sample code snippets and writing a C program to create a resume showing their details.

Uploaded by

Muhammad Zayn
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views

Assignment # 1 Computer Programming (Lab)

The document provides instructions for Assignment #1 in Computer Programming Lab. It outlines 10 points for students to follow when completing and submitting the assignment, including writing their name and registration ID, following formatting guidelines, submitting only through Blackboard, and avoiding plagiarism. Students are to complete exercises writing the output for sample code snippets and writing a C program to create a resume showing their details.

Uploaded by

Muhammad Zayn
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 28

ASSIGNMENT # 1

Computer Programming (Lab)

Name: Reg.no:
Program: BS (CS) Instructor: Maira Ali
Semester: Marks: 10

Please follow the instructions carefully:

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.

int printf(const char *format, ...)

Unit 2.1: Format specifier

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.

No. Type Syntax Value Example


1 Single Character %c One character within single quotes printf(“%c”,’a’);
2 String %s A sentence of an unfixed length within double quotes printf(“%s”,”Iqra Univ”);
3 Decimal Integer %d Any whole number between -32,768 to 32,767 printf(“%d”, 12345);
4 Long Integer %ld Any number between -2,147,483,648 to 2,147,483,647 printf(“%ld”,1234567);
5 Float %f Any decimal point number between 1 0-38
to 1 038 printf(“%f”, 1234.567);
6 Double %lf Any decimal point number between 10-308 to 10308 printf(“%lf”,12345678);

Unit 2.2: Escape sequences with printf

Escape Sequences are used to adjust spacing between lines or characters or the characters themselves.

No. Syntax Application Example


1 \n New Line printf(“\n”);
2 \t Tab eight spaces to right printf(“\t”);
3 \b Back space One space back printf(“\b”);
4 \r Carriage return Start of same line printf(“\r”);
5 \’ Printing single quote printf(“\’”);
6 \” Printing double quotes printf(“\””);
7 \\ Printing back space printf(“\\”);
8 \xdd Printing characters by Hexa decimal ASCII value printf(“\x45”);
9 \ddd Printing characters by decimal ASCII value printf(“\d45”);

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.

Unit 3.1: Data types & variables

Theory
Variables are declared by first writing
data types followed by a variable name, e.g.
int a=10;

Here

int is data type,


a is variable name
and after the equals to sign (=) is the value in it 10
the value is always followed by a terminator(;)

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.

Unit 3.2: Example codes

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.

Unit 4.1: Taking Input from the user

Theory
Scanf command can take input of different data types at a time.
Getche command can take only one character input.

Unit 4.2: Scenario example questions

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.

Unit 5.1: 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;

Unit 5.2: Arithmetic operators

Unit 5.3: conditional operators

Unit 5.4: Assignment operators

Unit 5.5: Increment/decrement operators

Operators

Arithmetic Relational Assignment Increment/decrement


operators operators operators operators
Add + Greater Than > Addition assignment += Increment ++
Subtract - Less Than < Subtraction assignment -= Decrement --
Multiplication * Greater or Equal >= Multiplication assignment *=
Division / Less or Equal <= Division assignment /=
Remainder % Equal Equal ==
Not Equal !=
Example
Explain the following program after careful study.
Program Output
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
main()
{
system(“cls”);
int a=2,b=4,c1,c2,c3,c4,d1,d2,d3,d4; 6 -28 0
c1=c2=c3=c4=5; 0 10 1 0 1
d1=d2=d3=d4=8; 8 215 1
printf("\n%d %d %d %d",a+b,a-b,a*b,a/b); 8 98 7
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);
retrun 0;
}

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

1. Program the following.


Implement the following equation
3x4sin(180x) + 4x3 cos(90x) + x2sin(tan(45)) + 7x + 9cos(90x2 )

where x may be user defined value.

2. Program the following.


· Prompt user to input distance in Kilometers and display it in meters.
· Input any number from user and generate its square e.g. square of 8 is 64
· Input any number from user and generate its cube e.g. cube of 8 is 512
· Input a 4 digit number in any integer type variable and sum all the four digits, e.g. int a =3487, result = 22
· Generate the table for a number input by the user.
· For the following equation 3x 4 + 4x3 + x2 + 7x + 9, substitute the user provided value of x and generate
the result.
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.
Unit 6.1: For loops
Unit 6.2: while loops & do while loops
Theory
for while do while
initialization; initialization;
do
for(initialization ; check range ; iteration) while(check range)
{
{ bo { bo
body
dy dy
iteration;
} iteration;
} while(check range)
}

Unit 6.3: Nested loops


ExamThis table below shows nested loops. All the three programs have same output.

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;

printf("\nEnter 0 to stop adding the numbers");


while (num != 0)
{
printf("\nEnter Number: ");
scanf_s("%d", &num);

sum = sum + num;


}
printf("\nSum = %d ", sum);
_retrun 0;
}

Unit 6.4: loops with cross combination

Studying loops with cross combination

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

The table below shows loops with cross combinations.


Loop Program Output
#include<stdio.h>
#i ncl ude<conio. h>
#include<stdlib.h>
int main()
{
system(“cls”);
int a,b=0;
for(a=0;a<=3;a++)
{
b=0;
For While while(b<=3)
{
printf("%d%d\t",a,b); b+
+;
}
printf("\n");
} 00 01 02 03
retrun 0; 10 11 12 13
}
20 21 22 23
#include<stdio.h>
#i ncl ude<conio. h>
30 31 32 33
#include<stdlib.h>
int main()
{
system(“cls”);
int a=0,b;
while(a<=3)
{
While For for(b=0;b<=3;b++)
{
printf("%d%d\t",a,b);
}
printf("\n");
a++;
} retrun 0;

Unit 6.5: Break & Continue


Understand the difference between break and continue by executing following programs.
break continue
int main() int main()
{ {
int i; int i;
double number, sum = 0.0; double number, sum = 0.0;

for(i=1; i <= 10; ++i) for(i=1; i <= 10; ++i)


{ {
printf("Enter a n%d: ",i); printf("Enter a n%d: ",i);
scanf("%lf",&number); scanf("%lf",&number);

// If user enters negative number, // If user enters negative number,


loop is terminated loop is terminated
if(number < 0.0) if(number < 0.0)
{ {
break; continue;
} }

sum += number; // sum = sum + number; sum += number; // sum = sum + number;
} }

printf("Sum = %.2lf",sum); printf("Sum = %.2lf",sum);

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.

3. 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;

while(!(nm1==0.0 && nm2==0.0))


{
printf("\nType Number Operatot Numbeñn");
scanf("%f %c %f",&nm1,&op,&nm2);
switch(op)
{
case'+':
printf(" = %f ",nm1+nm2);
break;
case'-':
printf(" = %f ",nm1-nm2);
break;
default:
printf("\nUnknown
Operatoñn"); }
printf("\n\n");
}
retrun 0;
}

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;

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");
}
retrun 0;
}

Assignment

1. Program the following.


· Make the number guessing program with switch case.
· Make an alphabet guessing program using if-else.
· Complete the simple calculator program for multiplication and division. Also make it using if-else.
· 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.

Marks Grade GPA


0-49 F 2
50-59 C 2.25
60-69 C+ 2.5
70-79 B 2.75
80-89 B+ 3
90-95 A 3.5
96-90 A+ 4

Design a marks sheet for a student with 5 subjects includingMath, Physics,


Electronics, Islamiat and Computer Programming.

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.

You might also like