PPS Lab Manual Ii Sem
PPS Lab Manual Ii Sem
2 MAX and MIN of set of given numbers & finding roots of quadratic equation Page 4-6
*
**
***
****
c. Write a C program to generate Pascal Triangle up to N rows.
6 Programs on Recursions Page 17-19
a. Write a C program to sort the elements in an Array using Bubble sort in ascending order.
b. Write a C program to sort the elements in an Array using Selection sort in ascending order.
9 Programs on Pointers: Pointers to Arrays, Pointer to Function Page 29-32
a. Write a C program to accept and display student details(Rollno, Name, Branch,Address) using
structures.
b. Create a union containing 6 strings: name, home_ address, hostel_ address, city, state and zip.Write a
C program to display your present address.
1. Byron Gottfried, “Theory and practice of Programming with C“, Schaum's Outline,McGraw-Hill
1996.
2. A.K. Sharma, -“Computer Fundamentals and Programming in C”, Universities Press, 2nd Edition,
2018.
3. E. Balaguruswamy –“Programming in ANSI C”, Tata McGraw-Hill Education,2008.
4. Brian W. Kernighan and Dennis M. Ritchie, “The C Programming Language”, Prentice Hall of
Indian 1988.
Web References:
1. http://www.geeksforgeeks.org/c
2. http://www.cprogramming.com/tutorial/c
3. http://www.cs.princeton.edu
LORDS INSTITUTE OF ENGINEERING & TECHNOLOGY [A]
1.BASIC PROGRAMS
#include<stdio.h>
#include<conio.h>
void main()
{
int n,r,sum=0,temp;
//clrscr();
printf("\n Enter the number");
scanf("%d",&n);
temp=n;
while(n>0)
{
r=n%10;
sum=sum+(r*r*r);
n=n/10;
}
if(temp==sum)
printf("\n Armstrong Number\n");
else
printf(" \n Not Armstrong Number\n");
getch();
}
Output:
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,j,count;
clrscr();
printf(“\n prime number of series \n”);
printf(“\n Enter any number \n”);
scanf(“%d”,&n);
printf(“\n The prime number is between 1 to %d\n”,n);
for(i=1;i<=n;i++)
{
count=0;
for(j=1;j<=i;j++)
if(i%j==0)
{
count++;
}
if(count==2)
{
printf(“%d\t”,i);
}
}
getch();
}
Output:
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c; char ch;
clrscr();
printf("Enter operator(either +,-,*,/)\n");
scanf("%c",&ch);
printf("Enter Two numbers\n");
scanf("%d%d",&a,&b);
switch(ch)
{
case '+': c=a+b;
printf("Addition Result=%d\n",c);
break;
case '-': c=a-b;
printf("Subtraction Result=%d\n",c);
break;
case '*': c=a*b;
printf("Multiplication Result=%d\n",c);
break;
case '/': c=a/b;
printf("Division Result=%d\n",c);
break;
case '%': c=a%b;
printf("Modulo Division Result=%d\n",c);
break;
default: printf("Invalid Operator\n");
}
getch();
}
Output:
Enter operator (either +,-,*,/)
*
Enter two numbers
35
Multiplication Result = 15
2.MAX and MIN of set of given numbers & finding roots of quadratic
equation
#include <stdio.h>
#include <math.h>/* Used for sqrt() */
#include<conio.h>
int main()
{
float a, b, c;
float root1, root2, imaginary;
float discriminant;
clrscr();
printf("\nEnter values of a, b, c of quadratic equation (aX^2 + bX + c):\n ");
scanf("%f%f%f", &a, &b, &c);
Output:
Enter values of a, b, c of quadratic equation (aX^2 + bX + c):
1 7 12
Two distinct and real roots exists: -3.00 and -4.00
#include<stdio.h>
#include<conio.h>
void main()
{
int arr[100],max,min,i,N;
clrscr();
printf("\nEnter the size of N elements \n");
scanf("%d",&N);
printf("\nEnter %d values ",N);
for(i=0;i<N;i++)
{
scanf("%d",&arr[i]);
}
max=min=arr[0];
for(i=0;i<N;i++)
{
if(arr[i]>max)
max=arr[i];
if(arr[i]<min)
min=arr[i];
}
printf("\nBiggest number among %d elements=%d",N,max);
printf("\n Smallest number among %d elements=%d\n",N,min);
getch();
}
Output:
Enter the size of N elements 5
Enter 5 values 50 -4 43 -32 34
Biggest number among 5 elements= 50
Smallest number among 5 elements= -32
The user should supply x and a positive integer n. We compute the sine of x using
the series and the computation should use all terms in the series up through the
term involving xn
#include <stdio.h>
#include<conio.h>
void main()
{
int i, j, n, fact, sign = - 1;
float x, p, sum = 0;
clrscr();
printf("\n\nEnter the value of x : ");
scanf("%f", &x);
printf("Enter the value of n : ");
scanf("%d", &n);
for (i = 1; i <= n; i += 2)
{
p = 1;
fact = 1;
for (j = 1; j <= i; j++)
{
p = p * x;
fact = fact * j;
}
sign = - 1 * sign;
sum += sign * p / fact;
}
printf("The value of Sin %0.2f = %f", x, sum);
getch();
}
Output:
Enter the value of x : 2
Enter the value of n : 3
The value of Sin 2.00 = 0.666667
The user should supply x and a positive integer n. We compute the cosine of x
using the series and the computation should use all terms in the series up through
the term involving xn
#include <stdio.h>
#include<conio.h>
void main()
{
int i, j, n, fact, sign = - 1;
float x, p, sum = 0;
clrscr();
printf("Enter the value of x : ");
scanf("%f", &x);
printf("Enter the value of n : ");
scanf("%d", &n);
for (i = 2; i <= n; i += 2)
{
p = 1;
fact = 1;
for (j = 1; j <= i; j++)
{
p = p * x;
fact = fact * j;
}
sum += sign * p / fact;
sign = - 1 * sign;
}
#include <stdio.h>
#include <math.h>
int binaryToDecimal(long binarynum)
{
int decimalnum = 0, temp = 0, remainder;
while (binarynum!=0)
{
remainder = binarynum % 10;
binarynum = binarynum / 10;
decimalnum = decimalnum + remainder*pow(2,temp);
temp++;
}
return decimalnum;
}
int main()
{
long binarynum;
//clrscr();
printf("\n\nEnter a binary number: ");
scanf("%ld", &binarynum);
printf("Equivalent decimal number is: %d", binaryToDecimal(binarynum));
getch();
return 0;
}
Output:
Enter a binary number: 1010111
Equivalent decimal number is: 87
#include <stdio.h>
#include <math.h>
long long convert(long long);
void main()
{
long long n, bin;
clrscr();
printf("\n\nEnter a decimal number: ");
scanf("%lld", &n);
bin = convert(n);
printf("%lld in decimal = %lld in binary", n, bin);
getch();
}
#include<stdio.h>
#include<conio.h>
int main()
{
int decnum, rem, i=0;
char hexnum[50];
// clrscr();
printf("\n\nEnter any decimal number: ");
scanf("%d", &decnum);
while(decnum!=0)
{
rem = decnum%16;
if(rem<10)
rem = rem+48;
else
rem = rem+55;
hexnum[i] = rem;
i++;
decnum = decnum/16;
}
printf("\nEquivalent Value in Hexadecimal = ");
for(i=i-1; i>=0; i--)
printf("%c", hexnum[i]);
getch();
return 0;
}
Output:
Enter any Decimal Number: 172
Equivalent value in Hexadecimal= AC
#include<stdio.h>
#include<string.h>
#include<conio.h>
#include<math.h>
int convert(char hexa[])
{
int i,size, deci=0, octa=0;
for(size=0; hexa[size]!='\0'; size++); //this loop calculates size of hexadecimal number
for(i=0; hexa[i]!='\0'; i++,size--)
{
if(hexa[i]>='0' && hexa[i]<='9')
{
deci= deci + (hexa[i]-'0')*pow(16,size-1);
}
if(hexa[i]>='A' && hexa[i]<='F')
{
deci = deci + (hexa[i]-55)*pow(16,size-1);
}
if(hexa[i]>='a' && hexa[i]<='f')
{
deci = deci + (hexa[i]-87)*pow(16,size-1);
}
} // deci contains the decimal value of given hexadecimal number.
i=1;
while(deci!=0)
{
octa = octa + (deci%8)*i;
deci = deci/8;
i = i*10;
}
return octa;
}
int main()
{
char hexa[20];
clrscr();
printf("Enter Hexadecimal Number : ");
scanf("%s",hexa);
printf("Equivalent Octal Value = %d",convert(hexa));
getch();
return 0;
}
Output:
Enter any Hexadecimal Number: 9
Equivalent Octal value = 11
1
1 2
1 2 3
1 2 3 4
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,N;
clrscr();
printf("Enter size of N");
scanf("%d",&N);
printf("Pattern upto %d rows is\n",N);
for(i=1;i<=N;i++)
{
for(j=1;j<=i;j++)
{
printf("%d ",j);
}
printf("\n");
}
getch();
}
Output:
Enter size of N 5
Pattern upto 5 rows is
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
*
**
***
****
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,N;
clrscr();
printf("Enter size of N ");
scanf("%d",&N);
printf("Pattern upto %d rows is\n",N);
for(i=1;i<=N;i++)
{
for(j=1;j<=i;j++)
{
printf("* ");
}
printf("\n");
}
getch();
}
Output:
Enter size of N 5
Pattern upto 5 rows is
*
**
***
****
*****
#include<stdio.h>
int fact(int k);
int ncr(int n,int r);
void main()
{
int N,rows,spaces,temp; printf("Enter N");
scanf("%d",&N);
printf("Pascal Triangle upto %d rows is\n",N);
for(rows=0;rows<N;rows++)
{
for(spaces=N-rows;spaces>0;spaces--)
{
printf(" ");
}
for(temp=0;temp<=rows;temp++)
{
printf("%d ",ncr(rows,temp));
}
printf("\n");
}
}
int ncr(int n,int r)
{
return fact(n)/(fact(r)*fact(n-r));
}
int fact(int k)
{
if(k==0||k==1)
return 1;
return k*fact(k-1);
}
Output:
Enter N 5
6.Programs on Recursions
a)GCD Program:
#include<stdio.h>
#include<conio.h>
int gcd(int n1,int n2);
void main()
{
int a,b,result;
clrscr();
printf("enter a and b values\n");
scanf("%d%d",&a,&b);
result=gcd(a,b);
printf("GCD of %d and %d is %d\n",a,b,result);
getch();
}
int gcd(int n1,int n2)
{
if(n2!=0)
return gcd(n2,n1%n2);
return n1;
}
Output:
enter a and b values
40 20
GCD of 40 and 20 is 20
b)Factorial Program:
#include<stdio.h>
#include<conio.h>
void main()
clrscr();
printf("\n\nEnter N");
scanf("%Lf",&N);
result=fact(N);
getch();
if(k==0||k==1)
return 1;
return k*fact(k-1);
Output:
Enter N 5
Factorial of 5 is 120
#include<stdio.h>
#include<conio.h>
int fib(int k);
void main()
{
int N,i;
clrscr();
printf("Enter Nth term for Fibonacci Series: ");
scanf("%d",&N);
printf("Fibonacci series up to %d is\n",N);
for(i=0;i<N;i++)
{
printf("%d ",fib(i));
}
printf("\n");
getch();
}
int fib(int k)
{
if(k==0)
return 0;
if(k==1)
return 1;
return fib(k-1)+fib(k-2);
}
Output:
Enter Nth term for Fibonacci Series: 5
Fibonacci series up to 5 is
0 1 1 2 3
#include<stdio.h>
#include<conio.h>
void main()
{
int a[100][100],b[100][100],c[100][100],i,j;
int rows,cols;
clrscr();
printf("Enter rows and columns for matrix\n");
scanf("%d%d",&rows,&cols);
printf("Enter values for 1st matrix\n");
for(i=0;i<rows;i++)
{
for(j=0;j<cols;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("\nEnter values for 2nd matrix\n");
for(i=0;i<rows;i++)
{
for(j=0;j<cols;j++)
{
scanf("%d",&b[i][j]);
}
}
for(i=0;i<rows;i++)
{
for(j=0;j<cols;j++)
{
c[i][j]=a[i][j]+b[i][j];
}
}
printf("Matrix Addition is\n");
for(i=0;i<rows;i++)
{
for(j=0;j<cols;j++)
{
printf("\t%d ",c[i][j]);
}
printf("\n");
}
getch();
}
Output:
Enter rows and columns for matrix
3 3
Enter values for 1st matrix
3 3 3
3 3 3
3 3 3
Enter values for 2nd matrix
3 3 3
3 3 3
3 3 3
Matrix Addition is
6 6 6
6 6 6
6 6 6
#include<stdio.h>
#include<conio.h>
void main()
{
int a[100][100],b[100][100],c[100][100],i,j,k;
int r1,r2,c1,c2;
clrscr();
printf("Enter rows and columns of 1st matrix\n");
scanf("%d%d",&r1,&c1);
printf("Enter values for 1st matrix\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("Enter rows and columns of 2nd matrix\n");
scanf("%d%d",&r2,&c2);
if(c1!=r2)
printf("Matrix multiplication not possible\n");
else
{
printf("Enter values for 2nd matrix\n");
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
{
scanf("%d",&b[i][j]);
}
}
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
c[i][j]=0;
for(k=0;k<c2;k++)
{
c[i][j]+=a[i][k]*b[k][j];
}
}
}
Output:
Enter rows and columns of 1st matrix
2 2
Enter values for 1st matrix
3 3
3 3
Enter rows and columns of 2nd matrix
2 2
Enter values for 1st matrix
4 4
4 4
Matrix product is
24 24
24 24
#include<stdio.h>
#include<conio.h>
void main()
{
int arr[100],N,i,key,flag=1;
clrscr();
printf("Enter the size of N elements ");
scanf("%d",&N);
printf("Enter %d values into array ",N);
for(i=0;i<N;i++)
scanf("%d",&arr[i]); printf("\nArray is\n");
for(i=0;i<N;i++)
printf("%d ",arr[i]);
printf("\nEnter key value to find ");
scanf("%d",&key);
for(i=0;i<N;i++)
{
if(key==arr[i])
{
printf("Value found at %d position\n",i+1);
flag=0; break;
}
}
if(flag)
printf("Value not found\n");
getch();
}
Output
Enter the size of N elements 5
Enter 5 values into array
10 20 40 30 50
Enter key value to find
40
Value found at 3 position
if(arr[mid]>key)
high=mid-1;
if(arr[mid]<key)
low=mid+1;
}
if(flag)
printf("Value Not found\n");
getch();
}
Output:
Enter the size of N elements 8
Enter 8 values
10 20 30 40 50 60 70 80
Array is
10 20 30 40 50 60 70 80
Enter key to find
70
Value found at 7 position
#include<stdio.h>
#include<conio.h>
void main()
{
int N,arr[100],i,j,temp,min;
clrscr();
printf("\nEnter the size of N elements to store ");
scanf("%d",&N);
printf("Enter %d values \n",N);
for(i=0;i<N;i++)
scanf("%d",&arr[i]);
printf("\nArray before sort is\n");
for(i=0;i<N;i++)
printf("%d ",arr[i]);
for(i=0;i<N;i++)
{
min=i;
for(j=i+1;j<N;j++)
{
if(arr[j]<arr[min])
{
min=j;
}
}
temp=arr[i];
arr[i]=arr[min];
arr[min]=temp;
}
printf("\n Array after using Selection sort is\n");
for(i=0;i<N;i++)
printf("\t%d ",arr[i]);
getch();
}
Output:
Enter N 5
Enter 5 values
40 30 20 10 5
Array before sort is
40 30 20 10 5
Array after using Selection sort is
5 10 20 30 40
#include <stdio.h>
#define MAX_SIZE 100 // Maximum string size
int main()
{
char str1[MAX_SIZE], str2[MAX_SIZE];
char * s1 = str1;
char * s2 = str2;
// Inputting 2 strings from user
//clrscr();
printf("\nEnter 1st string: ");
gets(str1);
printf("\nEnter 2nd string: ");
gets(str2);
// Moving till the end of str1
while(*(++s1));
// Coping str2 to str1
while(*(s1++) = *(s2++));
printf("\nConcatenated string: %s", str1);
getch();
return 0;
}
Output:
Enter 1st string: LORDS
Enter 1st string: COLLEGE
Concatenated string: LORDSCOLLEGE
#include<stdio.h>
#include<conio.h>
int main()
{
char str[100], *pt;
int i = 0;
clrscr();
printf("\nPointer Example Program : Find or Calculate Length of String \n");
printf("\nEnter Any string [below 100 chars] : ");
gets(str);
pt = str;
while (*pt != '\0')
{
i++;
pt++;
}
printf("\nLength of String : %d", i);
getch();
return 0;
}
Output:
Pointer Example Program : Find or Calculate Length of String
Enter Any string [below 100 chars] : LORDS-COLLEGE
Length of String : 13
#include<stdio.h>
#include<conio.h>
void main()
{
char string1[150],string2[150],*str1,*str2;
int i;
clrscr();
printf("\nEnter The First String: ");
scanf("%s",string1);
printf("\nEnter The Second String: ");
scanf("%s",string2);
str1 = string1;
str2 = string2;
while(*str1 == *str2)
{
if ( *str1 == '\0' || *str2 == '\0' )
break;
str1++;
str2++;
}
if( *str1 == '\0' && *str2 == '\0' )
printf("\n\nBoth Strings Are Equal.");
else
printf("\n\nBoth Strings Are Not Equal.");
getch();
}
Output:
Enter The First String: LORDS
Enter The Second String: LORDS
Both Strings Are Equal.
#include<stdio.h>
#include<conio.h>
void swap(int *x,int *y);
void main()
{
int a,b;
clrscr();
printf("Enter a and b values\n");
scanf("%d%d",&a,&b);
printf("Before swap,a=%d and b=%d\n",a,b);
swap(&a,&b);
printf("After swap,a=%d and b=%d\n",a,b);
getch();
}
void swap(int *x,int *y)
{
int temp;
temp=*x;
*x=*y;
*y=temp;
}
Output:
Enter a and b values
20 30
Before swap, a=20 and b=30
After swap, a=30 and b=20
#include<stdio.h>
#include<conio.h>
int main()
{
char string[140];
int length=0, flag=1,i;
clrscr();
printf("\n\nEnter string below [120] characters:\n");
gets(string);
for(i=0;string[i]!='\0';i++)
{
length++;
}
for(i=0;i< length/2;i++)
{
if( string[i] != string[length-1-i] )
{
flag=0;
break;
}
}
if(flag==1)
{
printf("\n%s is PALINDROME",string);
}
else
{
printf("\n%s is NOT PALINDROME",string);
}
getch();
return 0;
}
Output:
Enter string below [120] characters: RADAR
RADAR is PALINDROME
#include <stdio.h>
#include <stdlib.h>
#include<conio.h>
typedef struct
{
long int rollnum;
char name[30];
char Branch[20];
char Address[200];
}Student;
int main()
{
int i;
char ch;
Student students;
clrscr();
printf("Enter any one Student Details \n \n");
//Name
printf("Name: ");
scanf("%[^\n]s",students.name);
//Roll Number
printf("RollNumber: ");
scanf("%ld",&students.rollnum);
//Branch
printf("Branch: ");
scanf("%s",students.Branch);
//Address
printf("Address: ");
scanf("%s",students.Address);
//Displaying Students details
printf("\n-------------- Displaying Students Details ---------------\n");
printf("Name \t: ");
printf("%s \n",students.name);
printf("roll number \t: ");
printf("%ld \n",students.rollnum);
printf("Branch \t: ");
printf("%s \n",students.Branch);
printf("Address \t: ");
printf("%s \n",students.Address);
getch();
return 0;
}
Output:
Enter any one Student Details
Name: Vijay
RollNumber:564
Branch: CSE
Address: Hyd
-------------- Displaying Students Details ---------------
Name: Vijay
roll number: 564
Branch: CSE
Addres : Hyd
printf("\n %s",a.city);
strcpy(a.state,state);
printf("\n %s",a.state);
a.pincode=pincode;
printf("\n %ld",a.pincode);
getch();
}
Output:
enter student name : Vijay Kumar
enter student permanent home address : Flat no 32 Kukatpally
enter student hostel : Boys Hostel Mehdipatnam
enter city where you stay : Hyderabad
enter state you belong : Telangana
enter pincode where you belong : 500030
******* Displaying the STUDENT DETAILS :*********
Vijay Kumar
Flat no 32 Kukatpally
Mehdipatnam
Hyderabad
Telangana
500030
#include <stdio.h>
#include <stdlib.h>
int main()
{
char ch, file_name[25];
FILE *fp;
clrscr();
printf("Enter name of a file you wish to see the contents of it\n");
gets(file_name);
fp = fopen(file_name, "r"); // read mode
if (fp == NULL)
{
perror("Error while opening the file.\n");
exit(EXIT_FAILURE);
}
printf("The contents of %s file are:\n", file_name);
while((ch = fgetc(fp)) != EOF)
printf("%c", ch);
fclose(fp);
getch();
return 0;
}
Output:
Enter name of a file you wish to see
one.c
#include <stdio.h>
#include <stdlib.h>
int main()
{
char ch, source_file[30], target_file[30];
FILE *source, *target;
clrscr();
printf("Enter name of file to copy\n");
gets(source_file);
source = fopen(source_file, "r");
if( source == NULL )
{
printf("Press any key to exit...\n");
exit(EXIT_FAILURE);
}
printf("Enter name of target file\n");
gets(target_file);
target = fopen(target_file, "w");
if( target == NULL )
{
fclose(source);
printf("Press any key to exit...\n");
exit(EXIT_FAILURE);
}
while( ( ch = fgetc(source) ) != EOF )
fputc(ch, target);
printf("File copied successfully.\n");
fclose(source);
fclose(target);
getch();
return 0;
}
//Before executing try to create or give existing file for source file.
Output:
Enter name of file to copy
one.c
Enter name of target file
b1.txt
File copied successfully
c. Write a C program to count no of lines, words and characters in a given text file.
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE * file;
char path[100];
char ch;
int characters, words, lines;
clrscr();
/* Input path of files to merge to third file */
printf("Enter source file path: ");
scanf("%s", path);
/* Open source files in 'r' mode */
file = fopen(path, "r");
/* Check if file opened successfully */
if (file == NULL)
{
printf("\nUnable to open file.\n");
printf("Please check if file exists and you have read privilege.\n");
exit(EXIT_FAILURE);
}
/*Logic to count characters, words and lines. */
characters = words = lines = 0;
while ((ch = fgetc(file)) != EOF)
{
characters++;
/* Check new line */
if (ch == '\n' || ch == '\0')
lines++;
/* Check words */
if (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\0')
words++;
}
/* Increment words and lines for last word */
if (characters > 0)
{
words++;
lines++;
}
/* Print file statistics */
printf("\n");
printf("Total characters = %d\n", characters);
printf("Total words = %d\n", words);
printf("Total lines = %d\n", lines);
/* Close files to release resources */
fclose(file);
getch();
return 0;
}
//Any text file you can give while executing. Here i have given one.c file only
Output:
Enter source file path:
one.c
Total characters = 74
Total words = 9
Total lines =7