PPS Lab Manual
PPS Lab Manual
LAB MANUAL
YEAR : I YEAR
SEM. : I
printf(“controlstring”,arg1,arg2,…,a
rg-n);
The control string indicates how many arguments follow and what their types are. The
arguments arg1,arg2,…,arg-n are the variables whose values are formatted and printed
according to the specifications of the control string.
%w.ptype-specifier
where w specifies the total number of columns for the output value and p specifies the
number of digits to the right of the decimal point or the number of characters to be printed
from a string.
scanf(“controlstring”,arg1,arg2,…,arg-n);
Example:
scanf(“%d%c%f%s”,&count,&code,&ratio,name);
Commonly used formatting codes are given the following table.
CODE MEANING
%c Read a single character
%d Read a decimal integer
%e Read a floating-point value
%f Read a floating-point value
%g Read a floating-point value
%h Read a short integer
%i Read a decimal, hexadecimal,or octal integer
%o Read an octal integer
%s Read a string
%u Read an unsigned decimal integer
%x Read a hexadecimal integer
1(a) Take multiple inputs from the user and display them
AIM:
Write a C Program to take multiple inputs from the user and display them
ALGORITHM:
STEP 1: Start
STEP 2: Create a file named arith.c
STEP 3: declare a integers “a” & a float “b”.
STEP 4: Get the input values using scan function and store the input.
STEP 5: Print the values given by the user.
STEP 6: stop
PROGRAM:
#include <stdio.h>
int main()
{
int a;
float b;
printf("Enter integer and then a float: ");
scanf ("%d%f", &a, &b); // Taking multiple inputs
printf("You entered %d and %f", a, b);
return 0;
}
Sample Output:
Enter integer and then a float: -3
3.4
You entered -3 and 3.400000
AIM:
Write a C Program to Find Area of The Circle.
ALGORITHM:
STEP1: Read the radius r
STEP2: Calculate area=3.14*r*r
STEP3: Print area
STEP4: Stop
PROGRAM:
#include<stdio.h>
int main()
{
float a, r;
printf("Enter the radius:");
scanf ("%f”, &r);
a=3.14*r*r;
printf("Area: %f",a);
return 0;
}
Sample Output:
Enter the radius:5
Area:78.500000
1(c) SWAP TWO NUMBERS
AIM:
Write a C Program to swap two numbers.
ALGORITHM:
STEP1: Read the numbers a and b
STEP2: t=a
STEP3: a=b
STEP4: b=t
STEP5: Print a and b
STEP6: Stop
PROGRAM:
#include<stdio.h>
int main()
{
int a,b,t;
printf("Enter the numbers:");
scanf("%d%d”, &a,&b);
printf("\n Before Swapping“);
printf("\n a=%d",a);
printf("\n b=%d”,b);t=a;
a=b;b=t;
printf("\n After Swapping");
printf("\n a=%d",a);
printf("\n b=%d",b);
return 0;
}
Sample Output:
Enter the numbers:5 10
Before Swapping
a=5
b=10
After Swapping
a=10
b=5
Ex No: 2
DATA TYPES AND OPERATORS - I
Date:
An operator is a symbol that tells the computer to perform certain mathematical or logical
manipulations. Operators can be classified into a number of categories. They include
1. Arithmetic operators
2. Relational operators
3. Logical operators
4. Assignment operators
5. Increment and decrement operators
6. Conditional operators
7. Bitwise operators
8. Special operators.
Arithmetic Operators
C provides all the basic arithmetic operators. They are listed in the following table:
Operator Meaning
+ Addition or unary plus
- Subtraction or unary minus
* Multiplication
/ Division
% Modulo division
Relational Operators
The relational operators are used to compare two values. An expression containing
a relational operator is termed as a relational expression. The value of a relational
expression is either one or zero supports six relational operators. These operators and
their meanings are shown in the following table
Operator Meaning
< Is less than
<= Is Less Than or Equal to
> Is greater than
>= Is greater than or equal to
== Is equal to
!= Is not equal to
Operator Meaning
&& Logical AND
|| Logical OR
! Logical NOT
Assignment Operators
Assignment operators are used to assign the result of an expression to a variable.
The assignment operator is ‘=’. Assignment expressions that make use of this operator
are written in the form
identifier=expression;
++ and --
exp1? exp2:exp3;
Example:
c=(a>b)?a:b;
Bitwise Operators
The bitwise operators are used for testing the bits, or shifting them right or left.
Bitwise operators may not be applied to float or double. The following table lists the
bitwise operators and their meanings.
Operator Meaning
& Bitwise AND
| Bitwise OR
^ Bitwise exclusive
OR
<< Shift left
>> Shift right
~ One’s complement
Special Operators
C supports some special operators. They are
● comma operator
● size of operator
● pointer operators (& and *)
● member selection operator (. and ->)
Examples:
1. The statement value= (x=10, y=5, x+y);
first assigns the value10 to x,the n assigns 5 to y,and finally assigns 15 to value.
4. Exchanging values:t=x,x=y,y=t;
Example:
m=sizeof(sum);
n=sizeof(longint);
k=sizeof(234L);
Ex No: 3
DATA TYPES AND OPERATORS - II
Date:
AIM:
Write a C Program to perform arithmetic operations.
ALGORITHM:
STEP1: Read the numbers a and b
STEP2: c=a+b
STEP3: d=a-b
STEP4: e=a*b
STEP5: f=a/b
STEP6: Print c, d,e,f
STEP7: Stop
PROGRAM:
#include<stdio.h>
int main()
{
float a,b,c,d,e,f;
printf("Enter the numbers");
scanf(“%f%f”,&a,&b);
c=a+b;
d=a-b;
e=a*b;
f=a/b;
printf("\na+b=%f",c);
printf("\na-b=%f",d);
printf("\na*b=%f",e);
printf("\na/b=%f",f);
return 0;
}
Sample Output:
AIM:
Write a C program to demonstrate increment and decrement operators
ALGORITHM:
STEP1: Read the value of i
STEP2: Print i++
STEP3: Print ++i
STEP4: Print i--
STEP5: Print --i
STEP6: Stop
PROGRAM:
#include<stdio.h>
int main()
{
int i;
printf("Enter the value of i");
scanf("%d",&i);
printf("\n%d",i++);
printf("\n%d",++i);
printf("\n%d",i--);
printf("\n%d",--i);
return 0;
}
Sample Output:
Enter the value of i 10 10
10 12 12 10
3(c) Find the size of different data types
AIM:
Write a C program to demonstrate increment and decrement operators
ALGORITHM:
STEP1: Declare the variables
STEP2: Print sizeof int
STEP3: Print float
STEP4: Print double
STEP5: Print char
STEP6: Stop
PROGRAM:
#include<stdio.h>
int main()
{
int int Type;
float float Type;
double double Type;
char char Type;
// sizeof evaluates the size of a variable
printf("Size of int: %zu bytes\n", sizeof(intType));
printf("Size of float: %zu bytes\n", sizeof(floatType));
printf("Size of double: %zu bytes\n", sizeof(doubleType));
printf("Size of char: %zu byte\n", sizeof(charType));
return 0;
}
Sample Output:
Size of int: 4 bytes
Size of float: 4 bytes
Size of double: 8 bytes
Size of char: 1 byte
Ex No: 4
CONTROL STATEMENTS (BRANCHING AND LOOPING)
Date:
1. if statement
2. switch statement
3. Conditional operator statement
4. go to statement
if(condition)
It allows the computer to evaluate the condition first and then, depending on whether the value of the
condition is ‘true’ (non-zero) or ‘false’ (zero), it transfers the control to a particular statement.
Types of if statements
1. Simple if statement
2. if…. else statement
3. Nested if…. else statement
4. else if ladder
Simple if Statement
if(condition)
Statement-block;
Statement-x;
4(a) GREATEST AMONG TWO NUMBERS
AIM:
Write a C program to find greatest among two numbers using simple-if statement.
ALGORITHM:
STEP1: Read a and b
STEP 2: if (a>b) then Print “A is greatest”
STEP 3: if (b>a) then Print “B is greatest”
STEP4: Stop
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
printf(“Enter the numbers:”);
scanf(“%d%d”,&a,&b);
if(a>b)
printf(“A is greatest”);
if(b>a)
printf(“B is greatest”);
}
Sample Output:
When a series of decisions are involved, we may have to use more than one if…else statement in nested
form as follows:
If the condition-1 is false, the statement-3 will be executed; otherwise, it continues to perform the
second test. If the condition-2 is true, the statement-1 will be evaluated; otherwise, the statement-2 will
be evaluated and then the control is transferred to the statement-x.
4(b) GREATEST AMONG THREE NUMBERS
AIM:
Write a C program to find greatest among three numbers using nested-if statement.
ALGORITHM:
STEP1: Read a,b and c
STEP2: if (a>b) then if (a>c) then Print“A is greatest” else
Print“C is greatest”
STEP3: else if (b>c) then Print“B is greatest” else
Print“C is greatest”
STEP4: Stop
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
printf(“Enter the numbers : “);
scanf(“%d%d%d”,&a,&b,&c);if(a>b)
{
if(a>c)
printf(“A is greatest”);
else
printf(“C is greatest”);
}
else
{
if(b>c)
printf(“B is greatest”);
else
printf(“C is greatest”);
}
getch();
}
Sample Output:
A multi path decision is a chain of ifs in which the statement associated with each else is an if. The
general form of if…else if statement is
4(c) CHECK POSITIVE, NEGATIVE OR ZERO
ALGORITHM:
STEP1: Read the number n
STEP2: if (n>0) then Print“Positive”
STEP3: else if (n<0) then Print“Negative”
STEP4: else Print“Zero”
STEP5: Stop
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
int n;
clrscr();
printf(“Enter the numbers:“);
scanf(“%d”,&n);
if(n>0)
printf(“The given number is Positive”);
else if(n<0)
printf(“The given number is Negative”);
else
printf(“The given number is Zero”);
getch();
}
Sample Output:
The switch statement is a built-in multiway decision statement. The switch statement tests the value of
a given variable against a list of case values and when a match is found, a block of statements
associated with that case is executed.
AIM:
Write a C program to perform arithmetic operations using switch statement.
ALGORITHM:
STEP1: Display the menu
STEP2: Read the numbers a and b
STEP3: Read the choice
STEP4: if choice = 1 then c=a+b
STEP 5: if choice =2 then c=a-b
STEP6: if choice =3 then c=a*b
STEP7: if choice=4 then c=a/b
STEP 8: else Print “Invalid Choice” and exit
STEP9: Print c
STEP10: Stop
PROGRAM:
#include<stdio.h>
int main( )
{
int a, b, choice;
printf(“\nEnter Two Numbers: ”);
scanf(“%d%d”, &a,&b);
printf(“\n Enter 1 for Addition”);
printf(“\n Enter 2 for Subtraction”);
printf(“\n Enter 3 for Multiplication”);
printf(“\n Enter 4 for Division”);
printf(“ Enter your Choice”);
scanf(“%d”,&choice)
switch (choice)
{
case 1:
printf(“Sum is : %d”, a+b);
break;
case 2:
printf(“Difference is : %d”, a-b);
break;
case 3:
printf(“Multiplication is : %d”, a*b);
break;
case 4:
printf(“Difference is : %d”, a/b);
break;
default:
printf(“Invalid Choice:”);
}
getch( );
}
Sample Output:
The C language provides for three loop constructs for performing loop operations. They are:
1. The while statement.
2. The do statement
3. The for statement.
AIM:
Program to find sum of digits using while statement.
ALGORITHM:
STEP1: Start
STEP2: Read the value of n
STEP3: Repeat Step4toStep5if(n>0)
STEP4: Computes=s+(n%10)
STEP5: Compute n=n/10
STEP6: Print s
STEP7: Stop
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
int n,s=0;
clrscr();
printf(“Enter the value of N:”);
scanf(“%d”,&n);
while(n>0)
{
s=s+(n%10);
n=n/10;
}
printf(“Sum of digits:%d”,s);
getch();
}
Sample Output:
AIM:
Write a C Program to print multiplication table
ALGORITHM:
STEP1: Initialize i=1
STEP2: Read the value of n
STEP3: Print n xi=n*i
STEP4: Compute i=i+1
STEP5: if(i<=10) go to Step3
STEP6: Stop
PROGRAM:
#include<stdio.h>
#include<conio.h>
int main()
{
int n,i=1;
clrscr();
printf("Enter the value of n:");
scanf("%d",&n);
do
{
printf("\n%d*%d=%d",n,i,n*i);
i++;
}
while(i<=10);
getch();
}
Sample Output:
AIM:
Write a C Program to find sum of ‘N’ numbers using for statement.
ALGORITHM:
STEP1: Initializes=0
STEP2: Read the value of n
STEP3: For i=1 t on, do Step4, and Step5
STEP4: Read a
STEP5: Computes=s+a
STEP6: Print s
STEP7: Stop
PROGRAM:
#include<stdio.h>
#include<conio.h>
int main()
{
int n,i,a,s=0;
clrscr();
printf("Enter the value of n:");
scanf("%d",&n);
printf("Enter the numbers \n");
for(i=1;i<=n;i++)
{
scanf(“%d”,&a);
s+=a;
}
printf("Sum:%d",s);
getch();
}
Sample Output:
Arrays
An array is a group of related data items that share a common name. For example,
a[10] represents any 10 values. While the complete set of values is referred to as an array, the
individual values are called elements.
One-Dimensional Arrays
A list of items can be given one variable name using only one subscript and such
a variable is called a single-subscripted variable or a one-dimensional array. The subscript
can begin with number 0. That is a[0] is allowed. For example, if we want to represent
a set of five numbers by an array variable number, then we may declare the variable no
as follows int no[5]; and the computer reserves five storage locations as shown below:
no[0]
no[1]
no[2]
no[3]
no[4]
AIM:
Write a C Program to find maximum of ‘N’ numbers using array.
ALGORITHM:
STEP1: Initializes=0
STEP2: Read the value of n
STEP3: For i=1 t on, do Step4
STEP4: Read a[i]
STEP5:max=a[1]
STEP6: For i=2 ton,do Step6
STEP7: If(max<a[i]) then max=a[i]
STEP8: Print s
STEP9: Stop
PROGRAM:
#include <stdio.h>
int main()
{
int arr[5] = { 1, 2, 4, 8,16}
for (int i = 0; i < 5; i++)
{
printf("%d ", arr[i]);
}
printf("\n");
arr[3] = 9721;
for (int i = 0; i < 5; i++)
{
printf("%d ", arr[i]);
}
return 0;
}
Sample Output:
1 2 4 8 16
1 2 4 9721 16