SlideShare a Scribd company logo
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

PPTX
Decision making statements in C programming
PPTX
C Programming Language Tutorial for beginners - JavaTpoint
PDF
MANAGING INPUT AND OUTPUT OPERATIONS IN C MRS.SOWMYA JYOTHI.pdf
PPTX
Branching statements
PPTX
Loops in c
PPTX
Loops in c programming
PPTX
Unit 2. Elements of C
Decision making statements in C programming
C Programming Language Tutorial for beginners - JavaTpoint
MANAGING INPUT AND OUTPUT OPERATIONS IN C MRS.SOWMYA JYOTHI.pdf
Branching statements
Loops in c
Loops in c programming
Unit 2. Elements of C

What's hot (20)

DOCX
C programming Lab 1
DOCX
Programming fundamentals
PDF
Chapter 5 exercises Balagurusamy Programming ANSI in c
PDF
Chapter 4 : Balagurusamy Programming ANSI in C
PDF
Chapter 5 Balagurusamy Programming ANSI in c
PDF
Chapter 6 Balagurusamy Programming ANSI in c
PDF
Chapter 1 : Balagurusamy_ Programming ANsI in C
PPTX
Nested structure (Computer programming and utilization)
PPT
DOCX
C Programming
PDF
A NEW STUDY OF TRAPEZOIDAL, SIMPSON’S1/3 AND SIMPSON’S 3/8 RULES OF NUMERICAL...
DOC
C lab-programs
DOCX
C++ COMPUTER SCIENCE PROJECT
PPT
C program
PPTX
classes and objects in C++
DOCX
Practical File of C Language
PDF
Object Oriented Programming Using C++ Practical File
PPTX
Stack data structure
PPTX
Operators and expressions in c language
PPTX
Pointers in c++
C programming Lab 1
Programming fundamentals
Chapter 5 exercises Balagurusamy Programming ANSI in c
Chapter 4 : Balagurusamy Programming ANSI in C
Chapter 5 Balagurusamy Programming ANSI in c
Chapter 6 Balagurusamy Programming ANSI in c
Chapter 1 : Balagurusamy_ Programming ANsI in C
Nested structure (Computer programming and utilization)
C Programming
A NEW STUDY OF TRAPEZOIDAL, SIMPSON’S1/3 AND SIMPSON’S 3/8 RULES OF NUMERICAL...
C lab-programs
C++ COMPUTER SCIENCE PROJECT
C program
classes and objects in C++
Practical File of C Language
Object Oriented Programming Using C++ Practical File
Stack data structure
Operators and expressions in c language
Pointers in c++
Ad

Similar to C programming Lab 2 (20)

PPTX
C programming
PDF
C language concept with code apna college.pdf
DOCX
Fuzail_File_C.docx
DOCX
programs of c www.eakanchha.com
PPTX
Introduction to Basic C programming 02
PPTX
M2 (1).pptxisedepofengiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii
PPTX
Programming in C
PDF
Control Flow Statements and Datatypes in C
DOCX
Cs291 assignment solution
PPTX
Each n Every topic of C Programming.pptx
DOC
Let us c (5th and 12th edition by YASHVANT KANETKAR) chapter 2 solution
PPTX
the refernce of programming C notes ppt.pptx
PPT
c_tutorial_2.ppt
PPT
C tutorial
PPT
c-programming
PPTX
C programming language
PPT
Cbasic
PDF
learn basic to advance C Programming Notes
PPTX
presentation_c_basics_1589366177_381682.pptx
C programming
C language concept with code apna college.pdf
Fuzail_File_C.docx
programs of c www.eakanchha.com
Introduction to Basic C programming 02
M2 (1).pptxisedepofengiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii
Programming in C
Control Flow Statements and Datatypes in C
Cs291 assignment solution
Each n Every topic of C Programming.pptx
Let us c (5th and 12th edition by YASHVANT KANETKAR) chapter 2 solution
the refernce of programming C notes ppt.pptx
c_tutorial_2.ppt
C tutorial
c-programming
C programming language
Cbasic
learn basic to advance C Programming Notes
presentation_c_basics_1589366177_381682.pptx
Ad

More from Zaibi Gondal (8)

PPTX
Modal Verbs
DOCX
Parts of speech1
PPTX
Wirless Security By Zohaib Zeeshan
PDF
C project on a bookshop for saving of coustmer record
PPTX
Backup data
DOCX
Functional english
DOCX
application of electronics in computer
PPTX
Model Verbs
Modal Verbs
Parts of speech1
Wirless Security By Zohaib Zeeshan
C project on a bookshop for saving of coustmer record
Backup data
Functional english
application of electronics in computer
Model Verbs

Recently uploaded (20)

PPTX
COMPUTERS AS DATA ANALYSIS IN PRECLINICAL DEVELOPMENT.pptx
PPTX
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
PDF
BÀI TẬP TEST BỔ TRỢ THEO TỪNG CHỦ ĐỀ CỦA TỪNG UNIT KÈM BÀI TẬP NGHE - TIẾNG A...
PDF
TR - Agricultural Crops Production NC III.pdf
PDF
Insiders guide to clinical Medicine.pdf
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PPTX
Pharma ospi slides which help in ospi learning
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PPTX
GDM (1) (1).pptx small presentation for students
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PPTX
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
PDF
Anesthesia in Laparoscopic Surgery in India
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
COMPUTERS AS DATA ANALYSIS IN PRECLINICAL DEVELOPMENT.pptx
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
BÀI TẬP TEST BỔ TRỢ THEO TỪNG CHỦ ĐỀ CỦA TỪNG UNIT KÈM BÀI TẬP NGHE - TIẾNG A...
TR - Agricultural Crops Production NC III.pdf
Insiders guide to clinical Medicine.pdf
FourierSeries-QuestionsWithAnswers(Part-A).pdf
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
Pharma ospi slides which help in ospi learning
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
102 student loan defaulters named and shamed – Is someone you know on the list?
GDM (1) (1).pptx small presentation for students
STATICS OF THE RIGID BODIES Hibbelers.pdf
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
human mycosis Human fungal infections are called human mycosis..pptx
2.FourierTransform-ShortQuestionswithAnswers.pdf
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
Anesthesia in Laparoscopic Surgery in India
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
O5-L3 Freight Transport Ops (International) V1.pdf

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.