SlideShare a Scribd company logo
1 of 13
1
University of Sargodha
Mandi Bahauddin campus
LAB Sheet#2
C Lab Report Submitted By:
Name: M Umar
RollNo: BSSE-F17-18
Submitted To:
Department of CS & IT University of Sargodha M.B.Din
Lab Date:2 January 2018 Marks & Signature
Submission Date: 16th
January 2018
2
Program No. 1
Title
Write a program to declare two integer and one float variables then initialize them to 10,
15, and 12.6. Also print the variable values in the screen.
Problem Analysis:
In this problem we will need three variables. The data type of two variables will
be integer and one will be float. In this we have to simply declare three variables and
assign them values.
Input Variables
Processing
variables/calculatio
ns
Output Variables Header files
Int a,b printf --------- Stdio.h
Float c
Algorithm:
1. Start.
2. Declare three variables (int a,b, float c)
3. Assign values to variables
4. Use printf to display the output.
5. Stop
Code:
#include<stdio.h>
int main(void)
{
int a,b;
float c;
a=10;b=15;c=12.6;
printf("%d %d %.1f",a,b,c);
return 0;
}
Output:
10 15 12.6
Program No. 2
Title:
Write a C program to prompt the user to input 3 integer values and print these values in
forward
and reversed order.
Problem Analysis:
In this we have to get three variables of data type integer from the user. We will
need three integers int a, int b, int c. We will use scanf () to get values from the user and
simply use
printf () to print values in forward and reverse order.
3
Input variable
Processing
variables/calculation
s
Output variables Header Files
int a,b,c; printf() ----- stdio.h
Algorithm:
1. Start
2. Declare three variables int a, b, c
3. Use scanf to get values from user
4. Use printf to print values in forward and reverse order
5. Display the result
6. Stop
Code:
#include<stdio.h>
int main(){
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
printf("Values in forward order = %dt%dt%d",a,b,c);
printf("nnValues in reverse order = %dt%dt%d",c,b,a);
return 0;}
Output:
5 6 7
Values in forward order = 5 6 7
Values in reverse order = 7 6 5
Program No. 3
Title:
To calculate simple and compound interest.
Problem Analysis:
In the above problem, we have to calculate two things, Simple Interest
and compound interest. We need three input variables for both simple and compound
interest. The three variables will be of float data type. We need the values of principle,
time and rate. Hence, the data types of these values will be float.
Si=(p*r*t)/100 : ci=p*(1+r/100)^t-p;
To use the power function we will have to include math.h header file.
Input variables Processing variables Output variables Header files
float p si=(p*r*t)/100 float si stdio.h
float r ci=p*(1+r/100)^t-p float ci Math.h
float t
4
Algorithm:
1. Start
2. Declare three variables of data type float float p,r,t
3. Use scanf() to get values from the user.
4. Declare an output variable of data type float, float si, to calculate and store the
values of Simple interest. Si=(p*r*t)/100.
5. Declare another output variable of data type float, float ci, to calculate and
store the values of compound interest. ci=p*(1+r/100)^t-p
6. Use printf() to show the output.
7. Stop
Code:
#include<stdio.h>
#include<math.h>
int main()
{
float p,r,t,si,ci;
printf("Enter the values of principle, rate and time =");
scanf("%f%f%f",&p,&r,&t);
si=(p*t*r)/100;
ci=p*pow(1+r/100,t)-p;
printf("Simple interest =%fncompound interest =%f",si,ci);
return 0;
}
Output:
Enter the values of principle, rate and time =2000 3.4 32
Simple interest =2176.000000
compound interest =3830.257813
Program No. 4
Title:
To swap two integers using a third integer.
Problem analysis:
In this we have to declare two variables and get the values from users by using
scanf(). Then declare another temporary variable temp to swap the values. Temp=a;
A=b; B=temp;
Input Varibles
Processing
variables
Output variables Header files
5
int a; Temp=a; -------- Stdio.h
int b; A=b;
int temp; B=temp;
Algorithm:
1. Start
2. Declare three variables int a,b,temp;
3. Use scanf to get values from the user.
4. To swap th values ,we will use an expression. temp=a;a=b;b=temp;
5. Use printf() to show output.
6. Stop
Code:
#include<stdio.h>
int main(){
int a,b,temp;
scanf("%d%d",&a,&b);
printf("values before swapping= %dt%d",a,b);
temp=a;a=b;b=temp;printf("nnvalues after swapping= %dt%d",a,b);
return 0;}
Output:
6 9
values before swapping= 6 9
values after swapping= 9 6
To swap two values without using third variable.
Problem Analysis:
In this we have to declare two variables of data type integer int a, int b. We will
use scanf() to get the values of a & b from the user. To swap the values simply use
printf() statement.
Printf(โ€œ%dt %dโ€,b,a);
Input variable processing variables Output variable Header files
int a; printf stdio.h
int b;
Algorithm:
1. Start
2. Declare two variables of data type integer int a,b.
3. Use scanf() to get values from the user.
4. Use printf() to swap th values.
5. Display the result
6
6. Stop
Code:
#include<stdio.h>
int main()
{
int a,b;
scanf("%d%d",&a,&b);
printf("values before swapping= %dt%d",a,b);
printf("nnvalues after swapping= %dt%d",b,a);
return 0;
}
Output:
7 2
values before swapping= 7 2
values after swapping= 2 7
Program No. 5
Title:
To check that the input number is even or odd (a). Modulus Operator.
Problem Analysis:
In this we will have to get a number from the user. The data type of the number will be
integer int n. We will use modulus operator. If the mode of n%2=0 then it is even,
otherwise it will be an odd number. We will use if-else statement.
Input variable Processing variables Output variable Header files
int n; if(a%2==0),even stdio.h
else odd
Algorithm:
1. Start
2. Declare an variable int n, and use scanf() to get values from the user.
3. Use modulus operator and if-else statement to check whether the number is
even or odd.
4. If(a%2==0) then number is even else the number will be odd.
5. Display the result
6. Stop
Code:
#include<stdio.h>
int main()
{
int n;
7
scanf("%d",&n);
if(n%2==0)
{
printf("%d is an even number",n);
}
else{
printf("%d is an odd number",n);
}
return 0;
}
Output:
57
57 is an odd number
To check that the input number is even or odd.(b). Using bitwise operator.
Problem Analysis:
We can check that the number is even or odd by using AND (&) bitwise
operator. In this we will use scanf() to get value from the user and use AND to check
whether it is even
or odd. If the AND of n will be equal to 1, than it will be odd number, otherwise it will be
even number.
Algorithm:
1. Start
2. Declare an variable of data type integer to get number from user, int n.
3. Use scanf() to get n from user.
4. Use if(n&1) to check whether the number is even or odd.
5. If n&1 , then it is odd number else it is even.
6. Use printf() to show the output.
7. Stop
Code:
#include<stdio.h>
int main(){
int n;
scanf("%d",&n);
if(n&1){
printf("%d is an ODD number",n);
}else{
printf("%d is an EVEN number",n);}
return 0;}
Output:
8
4
4 is an EVEN number
To check that the input number is even or odd. (C). Without Using Modulus and
bitwise operator.
Problem Analysis:
In this we have to check the input number whether it is even or odd without
using modulus or bitwise operator. We will use expression n/2*2==n.
Algorithm:
1. Start
2. Declare an variable int n.
3. Use scanf() to get values from the user.
4. If(n/2*2==n) then it is even else it is an odd number
5. Display the result
6. Stop
Code:
#include<stdio.h>
int main()
{
int n;
scanf("%d",&n);
if(n/2*2==n)
{
printf("%d is an Even number",n);
}
else{
printf("%d is an odd number",n);
}return 0;}
Output:
5
5 is an odd number
Write a program to check odd or even number. (d) using conditional operator.
Problem analysis:
In this we will have to get a number from the user. The data type of the number will be
integer int n. We will use conditional operator. If the mode of n%2=0 then โ€œ?โ€ statement
will execute else โ€œ:โ€ statement will execute.
Input variable Processing variables Output variable Header files
int n; (a%2==0) ? ------- stdio.h
:
Algorithm:
9
1. Start
2. Declare an variable int n, and use scanf() to get values from the user.
3. Use modulus operator and if-else statement to check whether the number
is even or odd.
4. Display the result
5. Stop
Code:
#include<stdio.h>
int main()
{
int n;
printf("Enter an integern");
scanf("%d",&n);
n%2 == 0 ? printf("Even numbern"): printf("Odd numbern");
return 0;
}
Output:
6
6 is an Even number
Program No. 6
Title:
Print the value of y for given x=2 & z=4 and analyze the output.
(A). y = x++ + ++x;
Code:
#include<stdio.h>
int main()
{
int x=2,z=4,y;
y=x++ + ++x;
printf("%d",y);
return 0;
}
Output:
6
Analyzing Output:
10
As we are using y=x++ + ++x;
When this statement will be executed, at first the value of x=2, and is used and after that
the value of x
is incremented by 1, now x=3. For second x in the expression, the value of x will
preincrement and now
x=4 will be used in place of second x.
So, y=2+4;
Initial value of x=2, and is used in first x in the expression. Then x is incremented by 1,
now x=3.
For second x, the value of x will preincrement and then x=4.
y=2+4;
(b). y=++x + ++x;
Code:
#include<stdio.h>
int main()
{
int x=2,z=4,y;
y=++x + ++x;
printf("%d",y);
return 0;
}
Output:
8
Analyzing Output:
As we are using y=++x + ++x;
++x means first increase the value of x by 1 and then use it. Whereas x++ means first
use the value of x and then increment itโ€™s value by 1.
So the value of x increases from 2 to 3 and the value of z after increases from 4 to 5.
x=3, z=5
3+5=8
(c). y= ++x + ++x + ++x;
Code:
11
#include<stdio.h>
int main()
{
int x=2,z=4,y;
y=++x + ++x + ++x;
printf("%d",y);
return 0;
}
Output:
13
Analyzing output:
As we are using y=++x + ++x + ++x;
++x means first increase the value of x by 1 and then use it
(d). y=x>z;
Code:
#include<stdio.h>
int main()
{
int x=2,z=4,y;
y=x>z ;
printf("%d",y);
return 0;
}
Output:
0
Analyzing Output:
Y=x>z;
In the above statement, we are using a relational operator โ€œ>โ€. When the statement
y=x>z; will run. The
condition x>z will be evaluated. Depending on condition this will return a binary digit 0 or
1. If the
condition is true it will return 1, and if condition is false it will return 0. As x=2, z=4,
y=2>4.As 2 is not
greater than 4, so it will retrun 0 to y.
Y=0.
12
(e). y=x>z?x:z;
Code:
#include<stdio.h>
int main(void)
{
int x=2,z=4,y;
y=x>z?x:z;
printf("%d",y);
return 0;
}
Output:
4
Analyzing Output:
Y=x>z?x:z;
In the above statement a conditional ternary operator โ€œ?โ€ is used. The syntax of
conditional ternary
operator is condition? result 1 : result 2;
If condition will be true result 1 will be execute otherwise result 3 will be executed.
(f). y=x&z.
Code:
#include<stdio.h>
int main()
{
int x=2,z=4,y;
y=x&z;
printf("%d",y);
return 0;
}
Output:
0
Analyzing Output:
Y=x&z;
A bitwise operator AND โ€œ&โ€ is used. The output of bitwise AND is 1 if the corresponding
bits of two operands is 1. If either bit of an operand is 0, the result of corresponding bit
is evaluated to 0. In the above program bitwise operator is applied on two variables x=2
and z=4.
Applying & operator= 0000
Hence the value 0 will be returned. Therefore y=0.
As 2 in binary= 0010
As 4 in binary= 0100
13
(g). y= x>>2 + z<<1;
Code:
#include<stdio.h>
int main()
{
int x=2,z=4,y;
y= x>>2 + z<<1;
printf("%d",y);
return 0;
}
Output:
0
Analyzing Output:
We are using bitwise right shift and bitwise left shift operators in above program. Bit
Pattern of the data can be shifted by specified number of Positions to Right and Bit
Pattern of the data can be shifted by specified number of Positions to Left. When Data
is Shifted Right , leading zeroโ€™s are filled with zero and When Data is Shifted Left ,
trailing zeroโ€™s are filled with zero. Right shift Operator is Binary Operator [Bi โ€“ two] and
left shift Operator is also Binary Operator [Bi โ€“ two]. Binary means , Operator that
require two arguments.

More Related Content

What's hot

Operators in c language
Operators in c languageOperators in c language
Operators in c languageAmit Singh
ย 
Unit 2. Elements of C
Unit 2. Elements of CUnit 2. Elements of C
Unit 2. Elements of CAshim Lamichhane
ย 
Storage class in C Language
Storage class in C LanguageStorage class in C Language
Storage class in C LanguageNitesh Kumar Pandey
ย 
Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...
Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...
Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...Jayanshu Gundaniya
ย 
Operator Overloading
Operator OverloadingOperator Overloading
Operator OverloadingNilesh Dalvi
ย 
C program report tips
C program report tipsC program report tips
C program report tipsHarry Pott
ย 
Presentation on Function in C Programming
Presentation on Function in C ProgrammingPresentation on Function in C Programming
Presentation on Function in C ProgrammingShuvongkor Barman
ย 
Control statements in c
Control statements in cControl statements in c
Control statements in cSathish Narayanan
ย 
Types of operators in C
Types of operators in CTypes of operators in C
Types of operators in CPrabhu Govind
ย 
MANAGING INPUT AND OUTPUT OPERATIONS IN C MRS.SOWMYA JYOTHI.pdf
MANAGING INPUT AND OUTPUT OPERATIONS IN C    MRS.SOWMYA JYOTHI.pdfMANAGING INPUT AND OUTPUT OPERATIONS IN C    MRS.SOWMYA JYOTHI.pdf
MANAGING INPUT AND OUTPUT OPERATIONS IN C MRS.SOWMYA JYOTHI.pdfSowmyaJyothi3
ย 
FUNCTIONS IN PYTHON[RANDOM FUNCTION]
FUNCTIONS IN PYTHON[RANDOM FUNCTION]FUNCTIONS IN PYTHON[RANDOM FUNCTION]
FUNCTIONS IN PYTHON[RANDOM FUNCTION]vikram mahendra
ย 
Looping statements in C
Looping statements in CLooping statements in C
Looping statements in CJeya Lakshmi
ย 
Basics of C programming
Basics of C programmingBasics of C programming
Basics of C programmingavikdhupar
ย 
Data Types and Variables In C Programming
Data Types and Variables In C ProgrammingData Types and Variables In C Programming
Data Types and Variables In C ProgrammingKamal Acharya
ย 
Operators in c programming
Operators in c programmingOperators in c programming
Operators in c programmingsavitamhaske
ย 

What's hot (20)

Operators in c language
Operators in c languageOperators in c language
Operators in c language
ย 
Unit 2. Elements of C
Unit 2. Elements of CUnit 2. Elements of C
Unit 2. Elements of C
ย 
Storage class in C Language
Storage class in C LanguageStorage class in C Language
Storage class in C Language
ย 
Ansi c
Ansi cAnsi c
Ansi c
ย 
Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...
Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...
Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...
ย 
Operator Overloading
Operator OverloadingOperator Overloading
Operator Overloading
ย 
CONDITIONAL STATEMENT IN C LANGUAGE
CONDITIONAL STATEMENT IN C LANGUAGECONDITIONAL STATEMENT IN C LANGUAGE
CONDITIONAL STATEMENT IN C LANGUAGE
ย 
Operators
OperatorsOperators
Operators
ย 
C program report tips
C program report tipsC program report tips
C program report tips
ย 
Presentation on Function in C Programming
Presentation on Function in C ProgrammingPresentation on Function in C Programming
Presentation on Function in C Programming
ย 
Control statements in c
Control statements in cControl statements in c
Control statements in c
ย 
Structure in C language
Structure in C languageStructure in C language
Structure in C language
ย 
Types of operators in C
Types of operators in CTypes of operators in C
Types of operators in C
ย 
MANAGING INPUT AND OUTPUT OPERATIONS IN C MRS.SOWMYA JYOTHI.pdf
MANAGING INPUT AND OUTPUT OPERATIONS IN C    MRS.SOWMYA JYOTHI.pdfMANAGING INPUT AND OUTPUT OPERATIONS IN C    MRS.SOWMYA JYOTHI.pdf
MANAGING INPUT AND OUTPUT OPERATIONS IN C MRS.SOWMYA JYOTHI.pdf
ย 
FUNCTIONS IN PYTHON[RANDOM FUNCTION]
FUNCTIONS IN PYTHON[RANDOM FUNCTION]FUNCTIONS IN PYTHON[RANDOM FUNCTION]
FUNCTIONS IN PYTHON[RANDOM FUNCTION]
ย 
Looping statements in C
Looping statements in CLooping statements in C
Looping statements in C
ย 
Basics of C programming
Basics of C programmingBasics of C programming
Basics of C programming
ย 
Loops in c
Loops in cLoops in c
Loops in c
ย 
Data Types and Variables In C Programming
Data Types and Variables In C ProgrammingData Types and Variables In C Programming
Data Types and Variables In C Programming
ย 
Operators in c programming
Operators in c programmingOperators in c programming
Operators in c programming
ย 

Similar to C programming Lab 2

C important questions
C important questionsC important questions
C important questionsJYOTI RANJAN PAL
ย 
Practical write a c program to reverse a given number
Practical write a c program to reverse a given numberPractical write a c program to reverse a given number
Practical write a c program to reverse a given numberMainak Sasmal
ย 
Practical write a c program to reverse a given number
Practical write a c program to reverse a given numberPractical write a c program to reverse a given number
Practical write a c program to reverse a given numberMainak Sasmal
ย 
Cs291 assignment solution
Cs291 assignment solutionCs291 assignment solution
Cs291 assignment solutionKuntal Bhowmick
ย 
Core programming in c
Core programming in cCore programming in c
Core programming in cRahul Pandit
ย 
B.Com 1year Lab programs
B.Com 1year Lab programsB.Com 1year Lab programs
B.Com 1year Lab programsPrasadu Peddi
ย 
SIMPLE C PROGRAMS - SARASWATHI RAMALINGAM
SIMPLE C PROGRAMS - SARASWATHI RAMALINGAMSIMPLE C PROGRAMS - SARASWATHI RAMALINGAM
SIMPLE C PROGRAMS - SARASWATHI RAMALINGAMSaraswathiRamalingam
ย 
Control structure of c
Control structure of cControl structure of c
Control structure of cKomal Kotak
ย 
Qust & ans inc
Qust & ans incQust & ans inc
Qust & ans incnayakq
ย 
Computer programming subject notes. Quick easy notes for C Programming.Cheat ...
Computer programming subject notes. Quick easy notes for C Programming.Cheat ...Computer programming subject notes. Quick easy notes for C Programming.Cheat ...
Computer programming subject notes. Quick easy notes for C Programming.Cheat ...DR B.Surendiran .
ย 
Pointers in C Language
Pointers in C LanguagePointers in C Language
Pointers in C Languagemadan reddy
ย 
C and Data structure lab manual ECE (2).pdf
C and Data structure lab manual ECE (2).pdfC and Data structure lab manual ECE (2).pdf
C and Data structure lab manual ECE (2).pdfjanakim15
ย 
Write a program to check a given number is prime or not
Write a program to check a given number is prime or notWrite a program to check a given number is prime or not
Write a program to check a given number is prime or notaluavi
ย 
Linux_C_LabBasics.ppt
Linux_C_LabBasics.pptLinux_C_LabBasics.ppt
Linux_C_LabBasics.pptCharuJain396881
ย 

Similar to C programming Lab 2 (20)

Progr3
Progr3Progr3
Progr3
ย 
C important questions
C important questionsC important questions
C important questions
ย 
C programs
C programsC programs
C programs
ย 
Practical write a c program to reverse a given number
Practical write a c program to reverse a given numberPractical write a c program to reverse a given number
Practical write a c program to reverse a given number
ย 
Practical write a c program to reverse a given number
Practical write a c program to reverse a given numberPractical write a c program to reverse a given number
Practical write a c program to reverse a given number
ย 
Cs291 assignment solution
Cs291 assignment solutionCs291 assignment solution
Cs291 assignment solution
ย 
Core programming in c
Core programming in cCore programming in c
Core programming in c
ย 
C lab-programs
C lab-programsC lab-programs
C lab-programs
ย 
C code examples
C code examplesC code examples
C code examples
ย 
Programming egs
Programming egs Programming egs
Programming egs
ย 
B.Com 1year Lab programs
B.Com 1year Lab programsB.Com 1year Lab programs
B.Com 1year Lab programs
ย 
SIMPLE C PROGRAMS - SARASWATHI RAMALINGAM
SIMPLE C PROGRAMS - SARASWATHI RAMALINGAMSIMPLE C PROGRAMS - SARASWATHI RAMALINGAM
SIMPLE C PROGRAMS - SARASWATHI RAMALINGAM
ย 
Control structure of c
Control structure of cControl structure of c
Control structure of c
ย 
Qust & ans inc
Qust & ans incQust & ans inc
Qust & ans inc
ย 
Computer programming subject notes. Quick easy notes for C Programming.Cheat ...
Computer programming subject notes. Quick easy notes for C Programming.Cheat ...Computer programming subject notes. Quick easy notes for C Programming.Cheat ...
Computer programming subject notes. Quick easy notes for C Programming.Cheat ...
ย 
C-LOOP-Session-2.pptx
C-LOOP-Session-2.pptxC-LOOP-Session-2.pptx
C-LOOP-Session-2.pptx
ย 
Pointers in C Language
Pointers in C LanguagePointers in C Language
Pointers in C Language
ย 
C and Data structure lab manual ECE (2).pdf
C and Data structure lab manual ECE (2).pdfC and Data structure lab manual ECE (2).pdf
C and Data structure lab manual ECE (2).pdf
ย 
Write a program to check a given number is prime or not
Write a program to check a given number is prime or notWrite a program to check a given number is prime or not
Write a program to check a given number is prime or not
ย 
Linux_C_LabBasics.ppt
Linux_C_LabBasics.pptLinux_C_LabBasics.ppt
Linux_C_LabBasics.ppt
ย 

More from Zaibi Gondal

Modal Verbs
Modal VerbsModal Verbs
Modal VerbsZaibi Gondal
ย 
Parts of speech1
Parts of speech1Parts of speech1
Parts of speech1Zaibi Gondal
ย 
Wirless Security By Zohaib Zeeshan
Wirless Security By Zohaib ZeeshanWirless Security By Zohaib Zeeshan
Wirless Security By Zohaib ZeeshanZaibi Gondal
ย 
C project on a bookshop for saving of coustmer record
C project on a bookshop for saving of coustmer recordC project on a bookshop for saving of coustmer record
C project on a bookshop for saving of coustmer recordZaibi Gondal
ย 
Backup data
Backup data Backup data
Backup data Zaibi Gondal
ย 
Programming fundamentals
Programming fundamentalsProgramming fundamentals
Programming fundamentalsZaibi Gondal
ย 
Functional english
Functional englishFunctional english
Functional englishZaibi Gondal
ย 
application of electronics in computer
application of electronics in computerapplication of electronics in computer
application of electronics in computerZaibi Gondal
ย 
Model Verbs
Model VerbsModel Verbs
Model VerbsZaibi Gondal
ย 

More from Zaibi Gondal (9)

Modal Verbs
Modal VerbsModal Verbs
Modal Verbs
ย 
Parts of speech1
Parts of speech1Parts of speech1
Parts of speech1
ย 
Wirless Security By Zohaib Zeeshan
Wirless Security By Zohaib ZeeshanWirless Security By Zohaib Zeeshan
Wirless Security By Zohaib Zeeshan
ย 
C project on a bookshop for saving of coustmer record
C project on a bookshop for saving of coustmer recordC project on a bookshop for saving of coustmer record
C project on a bookshop for saving of coustmer record
ย 
Backup data
Backup data Backup data
Backup data
ย 
Programming fundamentals
Programming fundamentalsProgramming fundamentals
Programming fundamentals
ย 
Functional english
Functional englishFunctional english
Functional english
ย 
application of electronics in computer
application of electronics in computerapplication of electronics in computer
application of electronics in computer
ย 
Model Verbs
Model VerbsModel Verbs
Model Verbs
ย 

Recently uploaded

Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxDr.Ibrahim Hassaan
ย 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersSabitha Banu
ย 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
ย 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
ย 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designMIPLM
ย 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Celine George
ย 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfMr Bounab Samir
ย 
AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.arsicmarija21
ย 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Mark Reed
ย 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for BeginnersSabitha Banu
ย 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
ย 
Quarter 4 Peace-education.pptx Catch Up Friday
Quarter 4 Peace-education.pptx Catch Up FridayQuarter 4 Peace-education.pptx Catch Up Friday
Quarter 4 Peace-education.pptx Catch Up FridayMakMakNepo
ย 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
ย 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Celine George
ย 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
ย 
call girls in Kamla Market (DELHI) ๐Ÿ” >เผ’9953330565๐Ÿ” genuine Escort Service ๐Ÿ”โœ”๏ธโœ”๏ธ
call girls in Kamla Market (DELHI) ๐Ÿ” >เผ’9953330565๐Ÿ” genuine Escort Service ๐Ÿ”โœ”๏ธโœ”๏ธcall girls in Kamla Market (DELHI) ๐Ÿ” >เผ’9953330565๐Ÿ” genuine Escort Service ๐Ÿ”โœ”๏ธโœ”๏ธ
call girls in Kamla Market (DELHI) ๐Ÿ” >เผ’9953330565๐Ÿ” genuine Escort Service ๐Ÿ”โœ”๏ธโœ”๏ธ9953056974 Low Rate Call Girls In Saket, Delhi NCR
ย 
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxGrade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxChelloAnnAsuncion2
ย 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
ย 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...JhezDiaz1
ย 

Recently uploaded (20)

Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptx
ย 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginners
ย 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
ย 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
ย 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
ย 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-design
ย 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17
ย 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
ย 
AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.
ย 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)
ย 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for Beginners
ย 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ย 
Quarter 4 Peace-education.pptx Catch Up Friday
Quarter 4 Peace-education.pptx Catch Up FridayQuarter 4 Peace-education.pptx Catch Up Friday
Quarter 4 Peace-education.pptx Catch Up Friday
ย 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
ย 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17
ย 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ย 
call girls in Kamla Market (DELHI) ๐Ÿ” >เผ’9953330565๐Ÿ” genuine Escort Service ๐Ÿ”โœ”๏ธโœ”๏ธ
call girls in Kamla Market (DELHI) ๐Ÿ” >เผ’9953330565๐Ÿ” genuine Escort Service ๐Ÿ”โœ”๏ธโœ”๏ธcall girls in Kamla Market (DELHI) ๐Ÿ” >เผ’9953330565๐Ÿ” genuine Escort Service ๐Ÿ”โœ”๏ธโœ”๏ธ
call girls in Kamla Market (DELHI) ๐Ÿ” >เผ’9953330565๐Ÿ” genuine Escort Service ๐Ÿ”โœ”๏ธโœ”๏ธ
ย 
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxGrade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
ย 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
ย 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ย 

C programming Lab 2

  • 1. 1 University of Sargodha Mandi Bahauddin campus LAB Sheet#2 C Lab Report Submitted By: Name: M Umar RollNo: BSSE-F17-18 Submitted To: Department of CS & IT University of Sargodha M.B.Din Lab Date:2 January 2018 Marks & Signature Submission Date: 16th January 2018
  • 2. 2 Program No. 1 Title Write a program to declare two integer and one float variables then initialize them to 10, 15, and 12.6. Also print the variable values in the screen. Problem Analysis: In this problem we will need three variables. The data type of two variables will be integer and one will be float. In this we have to simply declare three variables and assign them values. Input Variables Processing variables/calculatio ns Output Variables Header files Int a,b printf --------- Stdio.h Float c Algorithm: 1. Start. 2. Declare three variables (int a,b, float c) 3. Assign values to variables 4. Use printf to display the output. 5. Stop Code: #include<stdio.h> int main(void) { int a,b; float c; a=10;b=15;c=12.6; printf("%d %d %.1f",a,b,c); return 0; } Output: 10 15 12.6 Program No. 2 Title: Write a C program to prompt the user to input 3 integer values and print these values in forward and reversed order. Problem Analysis: In this we have to get three variables of data type integer from the user. We will need three integers int a, int b, int c. We will use scanf () to get values from the user and simply use printf () to print values in forward and reverse order.
  • 3. 3 Input variable Processing variables/calculation s Output variables Header Files int a,b,c; printf() ----- stdio.h Algorithm: 1. Start 2. Declare three variables int a, b, c 3. Use scanf to get values from user 4. Use printf to print values in forward and reverse order 5. Display the result 6. Stop Code: #include<stdio.h> int main(){ int a,b,c; scanf("%d%d%d",&a,&b,&c); printf("Values in forward order = %dt%dt%d",a,b,c); printf("nnValues in reverse order = %dt%dt%d",c,b,a); return 0;} Output: 5 6 7 Values in forward order = 5 6 7 Values in reverse order = 7 6 5 Program No. 3 Title: To calculate simple and compound interest. Problem Analysis: In the above problem, we have to calculate two things, Simple Interest and compound interest. We need three input variables for both simple and compound interest. The three variables will be of float data type. We need the values of principle, time and rate. Hence, the data types of these values will be float. Si=(p*r*t)/100 : ci=p*(1+r/100)^t-p; To use the power function we will have to include math.h header file. Input variables Processing variables Output variables Header files float p si=(p*r*t)/100 float si stdio.h float r ci=p*(1+r/100)^t-p float ci Math.h float t
  • 4. 4 Algorithm: 1. Start 2. Declare three variables of data type float float p,r,t 3. Use scanf() to get values from the user. 4. Declare an output variable of data type float, float si, to calculate and store the values of Simple interest. Si=(p*r*t)/100. 5. Declare another output variable of data type float, float ci, to calculate and store the values of compound interest. ci=p*(1+r/100)^t-p 6. Use printf() to show the output. 7. Stop Code: #include<stdio.h> #include<math.h> int main() { float p,r,t,si,ci; printf("Enter the values of principle, rate and time ="); scanf("%f%f%f",&p,&r,&t); si=(p*t*r)/100; ci=p*pow(1+r/100,t)-p; printf("Simple interest =%fncompound interest =%f",si,ci); return 0; } Output: Enter the values of principle, rate and time =2000 3.4 32 Simple interest =2176.000000 compound interest =3830.257813 Program No. 4 Title: To swap two integers using a third integer. Problem analysis: In this we have to declare two variables and get the values from users by using scanf(). Then declare another temporary variable temp to swap the values. Temp=a; A=b; B=temp; Input Varibles Processing variables Output variables Header files
  • 5. 5 int a; Temp=a; -------- Stdio.h int b; A=b; int temp; B=temp; Algorithm: 1. Start 2. Declare three variables int a,b,temp; 3. Use scanf to get values from the user. 4. To swap th values ,we will use an expression. temp=a;a=b;b=temp; 5. Use printf() to show output. 6. Stop Code: #include<stdio.h> int main(){ int a,b,temp; scanf("%d%d",&a,&b); printf("values before swapping= %dt%d",a,b); temp=a;a=b;b=temp;printf("nnvalues after swapping= %dt%d",a,b); return 0;} Output: 6 9 values before swapping= 6 9 values after swapping= 9 6 To swap two values without using third variable. Problem Analysis: In this we have to declare two variables of data type integer int a, int b. We will use scanf() to get the values of a & b from the user. To swap the values simply use printf() statement. Printf(โ€œ%dt %dโ€,b,a); Input variable processing variables Output variable Header files int a; printf stdio.h int b; Algorithm: 1. Start 2. Declare two variables of data type integer int a,b. 3. Use scanf() to get values from the user. 4. Use printf() to swap th values. 5. Display the result
  • 6. 6 6. Stop Code: #include<stdio.h> int main() { int a,b; scanf("%d%d",&a,&b); printf("values before swapping= %dt%d",a,b); printf("nnvalues after swapping= %dt%d",b,a); return 0; } Output: 7 2 values before swapping= 7 2 values after swapping= 2 7 Program No. 5 Title: To check that the input number is even or odd (a). Modulus Operator. Problem Analysis: In this we will have to get a number from the user. The data type of the number will be integer int n. We will use modulus operator. If the mode of n%2=0 then it is even, otherwise it will be an odd number. We will use if-else statement. Input variable Processing variables Output variable Header files int n; if(a%2==0),even stdio.h else odd Algorithm: 1. Start 2. Declare an variable int n, and use scanf() to get values from the user. 3. Use modulus operator and if-else statement to check whether the number is even or odd. 4. If(a%2==0) then number is even else the number will be odd. 5. Display the result 6. Stop Code: #include<stdio.h> int main() { int n;
  • 7. 7 scanf("%d",&n); if(n%2==0) { printf("%d is an even number",n); } else{ printf("%d is an odd number",n); } return 0; } Output: 57 57 is an odd number To check that the input number is even or odd.(b). Using bitwise operator. Problem Analysis: We can check that the number is even or odd by using AND (&) bitwise operator. In this we will use scanf() to get value from the user and use AND to check whether it is even or odd. If the AND of n will be equal to 1, than it will be odd number, otherwise it will be even number. Algorithm: 1. Start 2. Declare an variable of data type integer to get number from user, int n. 3. Use scanf() to get n from user. 4. Use if(n&1) to check whether the number is even or odd. 5. If n&1 , then it is odd number else it is even. 6. Use printf() to show the output. 7. Stop Code: #include<stdio.h> int main(){ int n; scanf("%d",&n); if(n&1){ printf("%d is an ODD number",n); }else{ printf("%d is an EVEN number",n);} return 0;} Output:
  • 8. 8 4 4 is an EVEN number To check that the input number is even or odd. (C). Without Using Modulus and bitwise operator. Problem Analysis: In this we have to check the input number whether it is even or odd without using modulus or bitwise operator. We will use expression n/2*2==n. Algorithm: 1. Start 2. Declare an variable int n. 3. Use scanf() to get values from the user. 4. If(n/2*2==n) then it is even else it is an odd number 5. Display the result 6. Stop Code: #include<stdio.h> int main() { int n; scanf("%d",&n); if(n/2*2==n) { printf("%d is an Even number",n); } else{ printf("%d is an odd number",n); }return 0;} Output: 5 5 is an odd number Write a program to check odd or even number. (d) using conditional operator. Problem analysis: In this we will have to get a number from the user. The data type of the number will be integer int n. We will use conditional operator. If the mode of n%2=0 then โ€œ?โ€ statement will execute else โ€œ:โ€ statement will execute. Input variable Processing variables Output variable Header files int n; (a%2==0) ? ------- stdio.h : Algorithm:
  • 9. 9 1. Start 2. Declare an variable int n, and use scanf() to get values from the user. 3. Use modulus operator and if-else statement to check whether the number is even or odd. 4. Display the result 5. Stop Code: #include<stdio.h> int main() { int n; printf("Enter an integern"); scanf("%d",&n); n%2 == 0 ? printf("Even numbern"): printf("Odd numbern"); return 0; } Output: 6 6 is an Even number Program No. 6 Title: Print the value of y for given x=2 & z=4 and analyze the output. (A). y = x++ + ++x; Code: #include<stdio.h> int main() { int x=2,z=4,y; y=x++ + ++x; printf("%d",y); return 0; } Output: 6 Analyzing Output:
  • 10. 10 As we are using y=x++ + ++x; When this statement will be executed, at first the value of x=2, and is used and after that the value of x is incremented by 1, now x=3. For second x in the expression, the value of x will preincrement and now x=4 will be used in place of second x. So, y=2+4; Initial value of x=2, and is used in first x in the expression. Then x is incremented by 1, now x=3. For second x, the value of x will preincrement and then x=4. y=2+4; (b). y=++x + ++x; Code: #include<stdio.h> int main() { int x=2,z=4,y; y=++x + ++x; printf("%d",y); return 0; } Output: 8 Analyzing Output: As we are using y=++x + ++x; ++x means first increase the value of x by 1 and then use it. Whereas x++ means first use the value of x and then increment itโ€™s value by 1. So the value of x increases from 2 to 3 and the value of z after increases from 4 to 5. x=3, z=5 3+5=8 (c). y= ++x + ++x + ++x; Code:
  • 11. 11 #include<stdio.h> int main() { int x=2,z=4,y; y=++x + ++x + ++x; printf("%d",y); return 0; } Output: 13 Analyzing output: As we are using y=++x + ++x + ++x; ++x means first increase the value of x by 1 and then use it (d). y=x>z; Code: #include<stdio.h> int main() { int x=2,z=4,y; y=x>z ; printf("%d",y); return 0; } Output: 0 Analyzing Output: Y=x>z; In the above statement, we are using a relational operator โ€œ>โ€. When the statement y=x>z; will run. The condition x>z will be evaluated. Depending on condition this will return a binary digit 0 or 1. If the condition is true it will return 1, and if condition is false it will return 0. As x=2, z=4, y=2>4.As 2 is not greater than 4, so it will retrun 0 to y. Y=0.
  • 12. 12 (e). y=x>z?x:z; Code: #include<stdio.h> int main(void) { int x=2,z=4,y; y=x>z?x:z; printf("%d",y); return 0; } Output: 4 Analyzing Output: Y=x>z?x:z; In the above statement a conditional ternary operator โ€œ?โ€ is used. The syntax of conditional ternary operator is condition? result 1 : result 2; If condition will be true result 1 will be execute otherwise result 3 will be executed. (f). y=x&z. Code: #include<stdio.h> int main() { int x=2,z=4,y; y=x&z; printf("%d",y); return 0; } Output: 0 Analyzing Output: Y=x&z; A bitwise operator AND โ€œ&โ€ is used. The output of bitwise AND is 1 if the corresponding bits of two operands is 1. If either bit of an operand is 0, the result of corresponding bit is evaluated to 0. In the above program bitwise operator is applied on two variables x=2 and z=4. Applying & operator= 0000 Hence the value 0 will be returned. Therefore y=0. As 2 in binary= 0010 As 4 in binary= 0100
  • 13. 13 (g). y= x>>2 + z<<1; Code: #include<stdio.h> int main() { int x=2,z=4,y; y= x>>2 + z<<1; printf("%d",y); return 0; } Output: 0 Analyzing Output: We are using bitwise right shift and bitwise left shift operators in above program. Bit Pattern of the data can be shifted by specified number of Positions to Right and Bit Pattern of the data can be shifted by specified number of Positions to Left. When Data is Shifted Right , leading zeroโ€™s are filled with zero and When Data is Shifted Left , trailing zeroโ€™s are filled with zero. Right shift Operator is Binary Operator [Bi โ€“ two] and left shift Operator is also Binary Operator [Bi โ€“ two]. Binary means , Operator that require two arguments.