PPS Lab File With Solution
PPS Lab File With Solution
PPS Lab File With Solution
2020-21
Index
Sr. Practical Name Date Page Signature
No No
1 Familiarization with programming environment.
5
To demonstrate the importance of ampersand(&)operator in
scanf
Course Outcomes
The student will learn
• To formulate simple algorithms for arithmetic and logical problems.
• To translate the algorithms to programs (in C language).
• To test and execute the programs and correct syntax and logical errors.
• To implement conditional branching, iteration and recursion.
• To decompose a problem into functions and synthesize a complete
program using divide and conquer approach.
• To use arrays, pointers and structures to formulate algorithms and
programs. To apply programming to solve matrix addition and multiplication
problems and searching and sorting problems.
Laboratory Outcomes
• To formulate the algorithms for simple problems
• To translate given algorithms to a working and correct program
• To be able to correct syntax errors as reported by the compilers
• To be able to identify and correct logical errors encountered at run time
• To be able to write iterative as well as recursive programs
• To be able to represent data in arrays, strings and structures and manipulate them
through a program To be able to declare pointers of different types and use them
in defining selfreferential structures.
• To be able to create, read and write to and from simple text files.
constructs that map efficiently to machine instructions, and to require minimal run-time
support. Despite its low-level capabilities, the language was designed to encourage cross-
platform programming. A standards-compliant and portably written C program can be
compiled for a very wide variety of computer platforms and operating systems with few
changes to its source code. The language has become available on a very wide range of
platforms, from embedded microcontrollers to supercomputers.
Compiler: A compiler is a software program that compiles program source code files into
an executable program. It is included as part of the integrated development environment
IDE with most programming software packages.
The compiler takes source code files that are written in a high-level language, such as C,
BASIC, or Java, and compiles the code into a low-level language, such as machine code
or assembly code. This code is created for a specific processor type, such as and Intel
Pentium or PowerPC. The program can then be recognized by the processor and run from
the operating system.
List of best and free C/C++ compilers and IDEs for Programmers
For
F1 Alt+X Quit Ctrl+F9 Run
Help
Trace Toggle
F7 Ctrl+Del Clear Ctrl+F8
into breakpoint
Step Search
F8 Ctrl+L Ctrl+F5 Size/Move
over again
Previous
F9 Make Alt+F7 Alt+F3 Close
error
Next
F10 Menu Alt+F8 Alt+F5 User screen
error
Topic Previous
Ctrl+F1 Alt+F1
search topic
o boolean
o double floating point
o void
o wide character
2. User Defined Data Types:
o Structure
o Union
o Class
o Enumeration
3. Derived Data Types:
o Array
o Function
o Pointer
o Reference
The lists of modifiers used in C++ are:
• signed
• unsigned
• long
• short
float Floating point number. 4 bytes +/- 3.4e +/- 38 (~7 digits)
There is no fixed number of
digits before or after the
decimal point.
double Double precision floating 8 bytes +/- 1.7e +/- 308 (~15
point number. More digits)
accurate compared to float.
long double Long double precision 8 bytes +/- 1.7e +/- 308 (~15
floating point number. digits)
bool Boolean value. It can only take one 1 byte true or false
of two values: true or false.
C Keywords
asm else new this
auto enum operator throw
bool explicit private TRUE
break export protected try
case extern public typedef
catch FALSE register typeid
char float reinterpret_cast typename
class for return union
In addition,
the following
words are
also
reserved:
Functions Descriptions
abort stops the program
abs absolute value
acos arc cosine
asctime a textual version of the time
asin arc sine
assert stops the program if an expression isn't true
atan arc tangent
atan2 arc tangent, using signs to determine quadrants
sets a function to be called when the program
atexit
exits
atof converts a string to a double
atoi converts a string to an integer
atol converts a string to a long
bsearch perform a binary search
allocates and clears a two-dimensional chunk of
calloc
memory
ceil the smallest integer not less than a certain value
clearerr clears errors
returns the amount of time that the program has
clock
been running
cos cosine
Output:
#include <stdio.h>
int main()
{
int first, second, add, subtract, multiply;
float divide;
return 0;
}
Output :
Practical 4: WAP to swap two numbers with the help of third variable and without using
of third variable
int a,b,c;
clrscr();
printf("Enter the value of A & B:\t");
scanf("%d%d",&a,&b);
c=a;
a=b;
b=c;
printf("The value after swap is:\t%d\n%d",a,b);
getch();
}
Output
Output
Output
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
printf("enter the no.s");
scanf("%d%d%d",&a,&b,&c);
if(a>b)
{
if(a>c)
printf("a is big");
else
printf("c is big");
}
else if(b>c)
printf("b is big");
else
printf("c is big");
getch();
}
Output
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float a,b,c,r,r1,r2,d;
clrscr();
printf("\nEnter the co-efficients\n");
scanf("%f%f%f",&a,&b,&c);
d=(b*b)-(4*a*c);
printf("\nThe value of D is %f",d);
if(d<0)
printf("\nThe root are not real");
if(d>0)
{
r1=(-b+sqrt(d))/(2*a);
r2=(-b-sqrt(d))/(2*a);
printf("\nThe root one is %f",r1);
printf("\nThe root two is %f",r2);
}
if(d==0)
{
r=-b/(2*a);
printf("\nThe roots are equal\n");
printf("Root1=Root2=%f\n",r);
}
getch();
}
Output
Practical 8: WAP to find whether the entered character is upper case , lower case , digit
or special symbol.
#include<stdio.h>
#include<conio.h>
void main()
{
char ch;
clrscr();
printf("Enter a character:");
scanf("%c",&ch);
if(ch>=65 && ch<=90)
{
printf("\n Upper case letter");
}
else if(ch>=97 && ch<=122)
{
printf("\n Lower case letter");
}
else if(ch>=48 && ch<=57)
{
printf("\n Digit");
}
else if((ch>=0 && ch<=47) || (ch>=58&& ch<=64) || (ch>=91 && ch<=96) || (ch>=123
&& ch<=127))
{
printf("\n Special symbol");
}
getch();
}
Output1:
Enter a Character: H
Upper case letter
Output2:
Enter a Character: g
Lower case letter
Output3:
Enter a Character: 8
Digit
Output4:
Enter a Character: @
Special symbol
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int n,m,sum=0,i=1,term;
clrscr();
printf("Enter the power of series ");
scanf("%d",&n);
printf("Enter the last no. of series ");
scanf("%d",&m);
while(i<=m)
{
term=pow(i,n);
sum=sum+term;
i++;
}
printf("The sum of term is %d",sum);
getch();
}
Output
Practical 10: WAP to find factorial of given number by using while loop.
#include<stdio.h>
#include <conio.h>
void main()
{
int n,f=1,i=1;
printf("Enter a number: ");
scanf("%d",&n);
while(i<=n)
{
f = f * i;
i++;
}
printf("factorial= %d",f);
getch();
Practical 11: WAP to check whether the entered number is palindrome or not by using
do-while.
#include<stdio.h>
#include<conio.h>
int palindrome(int num);
int rev=0,rem=0;
void main()
{
int num;
printf("\n Enter a number: ");
scanf("%d",&num);
if(num==palindrome(num))
{
printf("\n %d is palindrome",num);
}
else
{
printf("\n %d is not palindrome",num);
}
getch();
}
int palindrome(int num)
{
do
{
rem=num%10;
rev=rev*10+rem;
num=num/10;
}while(num>0);
return rev;
}
Output:
Output
c[i][j]=a[i][k]*b[k][j]+c[i][j];
}
}
}
for(i=0;i<3;i++)
{
printf("\n");
for(j=0;j<3;j++)
{
printf("%d",c[i][j]);
printf("\t");
}
}
getch();
}
Output
function returns 0.
two.
strncat() Appends a given number of characters from one string to the end
of another.
pointer.
string as a pointer.
strstr() Searches for one string inside another string. The function
found.
strnstr() Searches for one string within the first n characters of the
another.
#include <stdio.h>
#include <string.h>
int main()
{
char password[]="taco";
char input[15];
int match;
printf("Password: ");
scanf("%s",input);
match=strcmp(input,password);
if(match==0)
puts("Password accepted");
else
puts("Invalid password. Alert the authorities.");
return(0);
}
Output
#include <stdio.h>
int main()
scanf("%d", &n);
scanf("%d", &array[c]);
swap = array[d];
array[d] = array[d+1];
array[d+1] = swap;
printf("%d\n", array[c]);
return 0;
Output of program:
#include <stdio.h>
int main()
{
int n, array[1000], c, d, t;
scanf("%d", &array[c]);
}
Output of program:
int main()
{
int array[100], n, c, d, position, swap;
return 0;
}
Output of program:
Practical 19: WAP to swap two numbers using call by value and call by reference.
//Call by Value
#include<stdio.h>
#include<conio.h>
void swap(int i,int j)
{
int temp=i;i=j;j=temp;
}
void main()
{
int a,b;
clrscr();
printf("enter the value ");
scanf("%d%d",&a,&b);
swap(a,b);
printf("The value after swap is %d\n%d",a,b);
getch();
}
Output
//Call by Reference
#include<stdio.h>
#include<conio.h>
void swap(int *i,int *j)
{
int temp=(*i);(*i)=(*j);(*j)=temp;
}
void main()
{
int a,b;
clrscr();
printf("enter the value ");
scanf("%d%d",&a,&b);
swap(&a,&b);
printf("The value after swap %d\n%d",a,b);
getch();
}
Output
Output
#include<stdio.h>
if(first<last){
pivot=first;
i=first;
j=last;
while(i<j)
while(number[i]<=number[pivot]&&i<last)
i++;
while(number[j]>number[pivot])
j--;
if(i<j){
temp=number[i];
number[i]=number[j];
number[j]=temp;
temp=number[pivot];
number[pivot]=number[j];
number[j]=temp;
quicksort(number,first,j-1);
quicksort(number,j+1,last);
int main(){
scanf("%d",&count);
for(i=0;i<count;i++)
scanf("%d",&number[i]);
quicksort(number,0,count-1);
for(i=0;i<count;i++)
printf(" %d",number[i]);
return 0;
}
Output of program :
#include<stdio.h>
int main()
{
int a[30],n,i;
printf("Enter no of elements:");
scanf("%d",&n);
printf("Enter array elements:");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
mergesort(a,0,n-1);
return 0;
}
int mid;
if(i<j)
{
mid=(i+j)/2;
mergesort(a,i,mid); //left recursion
mergesort(a,mid+1,j); //right recursion
merge(a,i,mid,mid+1,j); //merging of two sorted sub-arrays
}
}
Output of program :
Output
Output
c[i][j]=(*p)+(*p1);
printf("%d ",c[i][j]);
p++;
p1++;
}
printf("\n");
}
getch();
}
Output
Output
{
fgets(str,MAX,f);
printf("%s",str);
}
fclose(f);
getch();
}
Output
Output
Output
Output
Output
Output
void main()
{
int a;
clrscr();
printf("Enter the value");
scanf("%d",&a);
if(a>0)
printf("Entered no. is positive");
else if(a<0)
printf("Entered no. is negative");
else
printf("Entered no. is zero");
getch();
}
Output
Output
Output
Output
Output
clrscr();
b1=(int*)malloc(100*sizeof(int));
b2=(int*)calloc(100,sizeof(int));
printf("\nMemory allocated to buffer 1 from location %u",b1);
printf("\nMemory allocated to buffer 2 from location %u",b2);
free(b1);
free(b2);
printf("\nNow buffer 1 points to : %u",b1);
printf("\nNow buffer 2 points to : %u",b2);
getch();
}
Output
Output
Output
getch();
}
Output
Output
Output
Output
Output
Output
Output
Output
return (n * factorial(n-1));
}
int main()
{
int x,i;
double S=1;
printf("Please input x : ");
scanf("%d", &x); fflush(stdin);
for(i=1; i<=x; i++)
{
S += pow(x,i)/ factorial(i);
}
printf("%lf", S);
getch();
}
Output