Lab 1
Lab 1
Applied Sciences
Computing Fundamentals
Laboratory Exercise-01
Programming Tool
For programming in C we need
1. An editor to create files to write code
2. A Compiler that will check our C code and will translate the code to machine code
3. A program that will indicate errors in our program
4. A program that will let us check the output of our program , in this lab we are using Borland C IDE, IDE stands
for Integrated Development Environment, it is a program that provides us all the tools necessary for
developing code
2. Create File
Next, click File menu, select New and select Text Edit , this will allow you to create a new text file, save
this file with c or cpp extension.
#include<stdio.h>
#include<conio.h>
void main()
{
printf("hello winter");
getch();
}
Activity 1
The general format to declare any variable in c is as follows, however the value part is optional i.e. when declaring
variable it is not necessary to provide a value as well, and this can be done later in the program.
Example
Int x= 100 ;
float y= 2.5 ;
#include<stdio.h>
#include<conio.h>
void main(){
// variable declaration
int a, b;
int c;
// initialization
a = 10;
b = 20;
c = a + b;
#include<stdio.h>
#include<conio.h>
void main(){
int a=10,b=20;
printf(“Starting Values of a and b are \n”);
printf(“a=%d,b=%d \n”,a,b);
//Re-Initializing
a = 30;
b = 40;
printf(“\nNew values of a and b are \n”);
printf(“a=%d,b=%d\n”,a,b);
getch();
}
Activity 2
Try to write the output of the following program, note down your answer, execute the program and compare
your answer with program output.
#include<stdio.h>
#include<conio.h>
void main ()
Solving Problems
In the following section a number of example problems will be introduced along with their solution, read the
problems and try to understand the solution. If you can understand the concept try to solve the problems given in
this section.
Solution:
#include<stdio.h>
#include<conio.h>
Format string
scanf(“%d”,&a) ;
Variable address
void main(){
int length,width,area;
printf(“Enter Length = “);
scanf(“%d”,&length);
printf(“Enter Width = “);
scanf(“%d”,&width);
area=length*width;
printf(“Area of rectangle is =%d”,area);
getch();
}
There are special characters in C that modifies the output of printf, some them are given below, each special
character starts with ‘\’
Program-01:
#include <stdio.h>
#include <conio.h>
main(void)
{
printf(”This is a single sentence ”);
printf(“and will not be printed on two lines ”);
getch();
}
Learning: printf itself do not print a new line , programmer has to add suitable command for new lines
Program-01A
#include <stdio.h>
#include <conio.h>
void main(void)
{
printf(“This is not a \n single line “);
getch();
}
#include <stdio.h>
#include <conio.h>
void main(void){
printf(“Characters in capitals will be printed far from each Other\n
A\t\t\t\tB\t\t\tC\t\t\tD”);
getch();
}
Activity 4
Use printf , new line character \n, tab character \t and * (stars, use shift+8 or num‐pad to type star character)
to Produce The Following Outputs
Output 1
*******************
* *
* *
* *
* *
*******************
Format Specifiers
Format specifiers are used in printf function for printing numbers and characters, A format specifier acts like a place holder, it
reserves a place in a string for numbers and characters, for more understanding see the example below
Write the following example programs and see the outputs, by comparing the results of example understand the
use of format specifiers.
Program-01
#include <stdio.h>
#include <conio.h>
void main(void){
getch();
The %d is a format specifier or place holder for number 7, this is useful when we have to print the values stored in variables,
see the example below
Program-02
#include <stdio.h>
#include <conio.h>
void main(void){
int num_days=365;
getch();
Program-02B
#include <stdio.h>
#include <conio.h>
void main(void){
int num_days=365;
printf(“There are number of days in a year %d “, num_days);
getch();
}
Program-03
#include <stdio.h>
#include <conio.h>
void main(void)
{
int random_num=10;
printf(“The value of random variable is = %d \n“ , random_num);
Activity 5
Try to predict the output of the following programs and try to answer the question if any.
Program-01
#include <stdio.h>
#include <conio.h>
void main(void)
{
int a,b,c;
a=10;
b=40;
c=a+b;
printf(”The sum of a and b = %d“, c)
getch();
}
#include <stdio.h>
#include <conio.h>
main(void)
{
int a,b,c;
a=10;
b=20;
c=30;
printf(” A = %d“,a);
printf(” B = %d“,b);
printf(” C = %d “,c);
getch();
}
#include <stdio.h>
#include <conio.h>
main(void)
{
int a,b,c;
a=10;
b=20;
c=30;
printf(” A = %d\n B = %d\n C = %d“,a,b,c);
getch();
}
Name:
Email:
City:
Education:
Work Experience
(Few lines of your choice)
References
(Few lines of your choice)
There are two such operators, denoted as ++ and ‐‐ (double minus and plus sings), the operators can be used with
integer type data i.e. integer variables .The operators are not used independently, rather are appended before or
after an integer variable, such as ++a, or a++ similarly ‐‐a or a‐‐ where a is an integer variable. The operators adds
or subtracts 1 from the variable with which these are used.
Sample Program
The following program demonstrate use of increment and decrement operators
#include <conio.h>
#include <stdio.h>
int main(){
int a,b,c;
a=10;
b=20;
c=30;
printf(“Starting value of a,b,c are\n“);
printf(“a=%d,b=%d,c=%d\n“,a,b,c);
printf(“after a++,b++,c++ value are\n”);
a++;
b++;
c++;
printf(“a=%d, b=%d ,c=%d \n“,a,b,c);
getch();
return 0;
1. Write program for the following expressions, where a,b,c are integer variables, evaluate the
expression and display the final result (final value of variable a)
Initial values of a,b,c are 10,20,30
a=b*c;
b*=b+c;
c=a++;
a=++b+c++;
2. Enhance the code and get the values of a,b and c from the user