0% found this document useful (0 votes)
69 views27 pages

PPS Lab Manual

Uploaded by

Harsh Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
69 views27 pages

PPS Lab Manual

Uploaded by

Harsh Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

PPS LAB MANUAL

PROGRAMMIN IN C LANGUAGE
Parul University Lab Manual

Program 1: Write a program to print "Hello World" without using a semicolon anywhere
in the code.
Program:
#include <stdio.h>
int main()
{
if (printf("Hello World"))
{
}
}
Program 2: Amazon Great Indian Sale
Amazon the top Ecommerce site has announced its sales for diwali where you wish to upgrade
your phone and you got an excellent deal for IPHONE 13 PRO MAX where you get maximum
discount in Exchange Scheme Now let’s make it interesting you are supposed to exchange two
products/values without third variable Using One line
Program:
#include<stdio.h>
int main()
{
int a,b,temp;
printf("Enter two numbers:\n");
scanf("%d %d",&a,&b);
b=(a+b)-(a=b);
printf("After Exchange: ");
printf("%d %d",a,b);
}

Program 3: The bermuda triangle


There once lived a famous scientist who wished to crack the mystery of bermuda triangle
So he decided to calculate the distance between three areas named Miami, Bermuda and
San Juan
So he came up with concept of a2 + b2 = c2 if this condition works he can decode mystery easily
so he needs your help to break this
Program:
#include<stdio.h>
#include<math.h>
int main()
{
double a,b,c;

printf("Enter a, b, c: ");
scanf("%d %d %d",&a,&b,&c);

a=pow(a,2);
b=pow(b,2);
c=pow(c,2);

if(a+b==c)
{
printf("Boom You Did It");
}
else
{
printf("Better Luck Next Time");
}
}
Program 4: Write a program in C which is a Menu-Driven Program to compute the area of
the various geometrical shape
Program:

#include<conio.h>
#include <stdio.h>
void main ()
{
int choice,r,l,w,b,h;
float area;

printf("Enter your choice.\n");


printf("1. Circle\n2. Rectange\n3. Triangle\n");
scanf("%d",&choice);

switch(choice)
{
case 1:
printf("Enter radius of the circle : ");
scanf("%d",&r);
area=3.14*r*r;
break;
case 2:
printf("Enter length and width of the rectangle : ");
scanf("%d%d",&l,&w);
area=l*w;
break;
case 3:
printf("Enter base and hight of the triangle :");
scanf("%d%d",&b,&h);
area=0.5*b*h;
break;
default:
printf("Invalid Input.");
}

printf("The area is : %f\n",area);


}
Program 5: Egyptian pyramids
Pyramids which are considered as most stable structures in world is based on geometry of
triangle and you are assigned a task of printing a stable triangular structure
Sample Input :
3
Sample Output:
*
***
*****
Program:

#include<stdio.h>
#include<conio.h>
void main()
{
int row;

printf("Enter row: ");


scanf("%d", &row);

for(int r=1; r<=row; r++) {

for(int sp=1; sp<=row-r; sp++)


printf(" ");

for(int c=1; c<=2*r-1; c++)


printf("* ");

printf("\n");
}
}
Program 6: Diamonds and Black Market
There is illegal trading going on in black market so they have to sell diamonds in black
market but just because of cops they can't sell it directly in the market so they decided to use
softwares to share virtual images of those things to get appropriate consignments just to print
diamond shape structure.

Input format:
Read n value as input

Output format:
Print diamond depends on value

Sample Input :
3
Sample Output:
*
***
*****
***
*

Input 1:
4
Output 1:
*
***
*****
*******
*****
***
*
Program:

#include<stdio.h>
#include<conio.h>
void main()
{
int row;

printf("Enter row: ");


scanf("%d", &row);

for(int r=1; r<=row; r++) {

for(int sp=1; sp<=row-r; sp++)


printf(" ");

for(int c=1; c<=2*r-1; c++)


printf("* ");

printf("\n");
}

for(int r=row-1; r>=1; r--) {

for(int sp=1; sp<=row-r; sp++)


printf(" ");

for(int c=1; c<=2*r-1; c++)


printf("* ");

printf("\n");
}
Program 7: write a program to find the in a given interval range.

#include<stdio.h>
void main(){

int l, u, flag;

printf("Enter lower and upper limit:\n");


scanf("%d %d", &l, &u);

printf("Prime numbers between %d and %d\n", l, u);

for(int num=l; num<=u; num++){


flag = 1;
if(num<2 || num%2 == 0 && num != 2)
flag = 0;
else{
for(int i=3; i*i<=num; i+=2){
if(num%i == 0){
flag = 0;
break;
}
}
}

if(flag)
printf("%d ", num);
}
}
Program 8: Magical Mathematics
Maths is always a magical so there is trick which goes like this take a integer number and
need to perform factorials of every digit and sum of that factorials of digits should give you
same input integer number

Program:

#include<stdio.h>
void main(){
int n,i;
int fact,rem;

printf("Enter the number: ");


scanf("%d",&n);

int sum = 0;
int temp = n;

while(n){
i = 1,fact = 1;
rem = n % 10;
while(i <= rem){
fact = fact * i;
i++;
}
sum = sum + fact;
n = n / 10;
}
if(sum == temp)
printf("Yes\n");
else
printf("No\n");
}
Program 9: Time Conversions:
Mr X is an employee in an organization where his Boss is from the USA. There was a
situation to interact with his Boss and has to create an event now he got confused with time
delay between USA and India help him to get the time converted into PM or AM According
to Input
Input Format:
15:45:56 (hh:mm:ss:AM/PM)
Output Format:
03:45:56
Sample Input:
12:00:00AM
Sample Output:
00:00:00
Program:

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {

char time[10];
char hh[3], mm[3], ss[3];
int hour, minute, second;

hh[2] = mm[2] = ss[2] = '\0';

printf("Enter the time:\n");


gets(time);
int index = 0;
for(int h=0; h<2; h++){
hh[h] = time[index++];
}

for(int m=0; m<2; m++){


mm[m] = time[++index];
}
index++;
for(int s=0; s<2; s++){
ss[s] = time[++index];
}

hour = atoi(hh);
minute = atoi(mm);
second = atoi(ss);
switch(time[8]){

case 'a':
case 'A':

if(hour==12)
printf("%02d:%02d:%02d", 00, minute, second);
else
printf("%02d:%d:%d", hour, minute, second);
break;

case 'p':
case 'P':

if(hour==12)
printf("%d:%d:%d", hour, minute, second);
else
printf("%d:%d:%d", 12+hour, minute, second);

break;

default:
printf("Invalid Input");

}
}
Program 10: Min element of an array from given input array elements

Program:

#include<stdio.h>
#include<conio.h>
void main()
{
int a[100],min,size;

printf("Enter the size of an array\n");


scanf("%d",&size);

printf("Enter %d integer values\n",size);

for(int i=0;i<size;i++)
scanf("%d",&a[i]);

min=a[0];

for(int i=1;i<size;i++)
{
if(a[i]<min)
{
min=a[i];

}
}
printf("The minimum element is: %d\n", min);
}
Program 11: Find average and rank of a student for given input marks using functions
Program:

#include<stdio.h>
#include<conio.h>
int total(int,int,int);
float average(int);
void main()
{
int s1,s2,s3,tot;
float avg;

printf("Enter the marks of s1, s2, s3\n");


scanf("%d %d %d",&s1,&s2,&s3);

tot=total(s1,s2,s3);
avg=average(tot);

printf("Total Mark: %d\n", tot);


printf("The average of 3 subjects is: %f\n",avg);

if(avg>=75 && avg<100)


{
printf("First Rank");
}
else if(avg>=50 && avg<75)
{
printf("Second Rank");
}
else if(avg>=35 && avg<50)
{
printf("Third Rank");
}
else
{
printf("Fail");
}

int total(int a,int b ,int c)


{

return a+b+c;
}
float average(int x)
{

return x/3.0;
}
Program 12: Find Factorial of the given number using recursion. Input number should be
1<=number<=20. Accept the input till the user says ‘NO’.

Program:
#include<stdio.h>
#include<conio.h>
#include<string.h>

double fact(int num){

if(num<=1)
return 1;

else
return num * fact(num-1);
}

void main(){

int num;
char answer = 'y';

while(answer=='y' || answer=='Y'){

printf("\nEnter num: ");


scanf("%d", &num);

if(num<1 || num>20)
printf("Invalid input.\n");
else
printf("%d! = %lf\n", num, fact(num));

printf("Would you like find factorial for another number(y/n): ");

fflush(stdin);
answer = getchar();
}

printf("Thank You.");
}
Program 13:Max element from given array using Pointers

Program:

#include<stdio.h>
#include<conio.h>
void main()
{
int a[100],*max, size, i;

printf("Enter the number of elements in array\n");


scanf("%d",&size);

printf("Enter %d integers\n",size);

for(i=0; i<size; i++)


scanf("%d",&a[i]);

max = a;

for(i=1;i<size;i++)
{
if(*(a+i) > *max)
max = a+i;

}
printf("Maximium element is: %d\n", *max);
}
Program 14: Display frequency count vowels, consonants, digits and spaces

Program:

#include <stdio.h>
#include<ctype.h>
#include<stdlib.h>
void main()
{
char line[150];
char ch;
int vowels, consonant, digit, space, special;

vowels = consonant = digit = space = special = 0;

printf("Enter a line of string: ");

fgets(line, sizeof(line), stdin);

for (int i = 0; line[i] != '\0'; ++i) {

ch = tolower(line[i]);
if(97<=ch && ch<=122){
if(ch=='a' || ch=='e' || ch=='i' || ch=='o' || ch=='u')
vowels++;
else
consonant++;
}

else if(ch == ' ')


space++;

else if(48<=ch && ch<=57)


digit++;

else
special++;
}

printf("Vowels: %d\n", vowels);


printf("Consonants: %d\n", consonant);
printf("Digits: %d\n", digit);
printf("White spaces: %d\n", space);
printf("Special Characters: %d\n", special-1);
}
Program 15: Swap two strings using pointers

Program:
#include<stdio.h>
#include<stdlib.h>

void swap(char **str1, char **str2){

char *temp = *str1;


*str1 = *str2;
*str2 = temp;

printf("Inside Swap Function:\n%s %s\n", str1, str2);


}

void main()
{
char *str1 = malloc(20*sizeof(char));

char *str2 = malloc(20*sizeof(char));

printf("Enter two string\n");

scanf("%s %s", str1, str2);

printf("Before Swapping:\n%s %s\n", str1, str2);

swap(str1, str2);

printf("After Swapping:\n%s %s\n", str1, str2);

}
Program 16: Write a program to implement Dynamic memory functions or
Write a program to create an integer array by allocating memory using dynamic memory
allocation functions and find sum of the array elements.

Program:

//Malloc function

#include<stdio.h>
#include<stdlib.h>
int main(){

int n,i,*ptr,sum=0;

printf("Enter number of elements: ");


scanf("%d",&n);

ptr=(int*)malloc(n*sizeof(int)); //memory allocated using malloc

if(ptr==NULL)
{
printf("Sorry! unable to allocate memory");
exit(0);
}

printf("Enter elements of array: ");


for(i=0;i<n;++i)
{
scanf("%d",ptr+i);
sum+=*(ptr+i);
}

printf("Sum=%d",sum);
free(ptr);
return 0;
}
// calloc function

//Malloc function
#include<stdio.h>
#include<stdlib.h>
int main(){

int n,i,*ptr,sum=0;

printf("Enter number of elements: ");


scanf("%d",&n);

ptr=(int*)calloc(n, sizeof(int)); //memory allocated using malloc

if(ptr==NULL)
{
printf("Sorry! unable to allocate memory");
exit(0);
}

printf("Enter elements of array: ");


for(i=0;i<n;++i)
{
scanf("%d",ptr+i);
sum+=*(ptr+i);
}

printf("Sum=%d",sum);
free(ptr);
return 0;
}
//realloc function

#include <stdio.h>
#include <stdlib.h>
int main()
{
int* ptr;
int n, n1, i;
n = 5;
printf("Enter number of elements:");
scanf("%d", &n);
ptr = (int*)calloc(n, sizeof(int));

if (ptr == NULL) {

printf("Memory not allocated.\n");


exit(0);
}
else {

printf("Enter %d numbers.\n", n);


for (i = 0; i < n; ++i)
scanf("%d", &ptr[i]);

printf("The elements of the array are: ");


for (i = 0; i < n; ++i)
printf("%d ", ptr[i]);

printf("\n\nEnter the new size of the array: \n");


scanf("%d", &n1);

ptr = realloc(ptr, n * sizeof(int));

printf("Memory successfully re-allocated using realloc.\n");


for (i = n; i < n1; ++i)
scanf("%d", &ptr[i]);

printf("Enter %d numbers.\n", n1-n);


for (i = 0; i < n1; ++i)
printf("%d ", ptr[i]);

free(ptr);
}
return 0;
}
Program 17: Structure of book information (title author publications house) with using
functions

Program:

#include<stdio.h>
#include<conio.h>
#include<string.h>

typedef struct books


{
char title[50];
char author[50];
char subject[50];
int bookid;
}book;

void printbook(struct books b);/*prototype*/

int main()
{
book b1, b2;

strcpy(b1.title,"C Language");
strcpy(b1.author,"BALA GURUSWAMY");
strcpy(b1.subject,"c tutorial");
b1.bookid=83463;

strcpy(b2.title,"C++ Langauage");
strcpy(b2.author,"ZARA ALI");
strcpy(b2.subject,"c++ tutorial");
b2.bookid=653436;

printbook(b1);
printbook(b2);

return 0;
}

void printbook(struct books b)


{
printf("%-15s: %s\n","Book Title", b.title);
printf("%-15s: %s\n","Book Author", b.author);
printf("%-15s: %s\n","Book subject", b.subject);
printf("%-15s: %d\n\n","Book BookId", b.bookid);
}
Program 18: Structure of book information (title author publications house) with using
pointers

Program:

#include<stdio.h>
#include<conio.h>
#include<string.h>
struct books
{
char *title;
char *author;
char *subject;
int bookid;
};

void printbook(struct books *b);

int main()
{
struct books b;

b.title = malloc(50);
b.author = malloc(50);
b.subject = malloc(50);

strcpy(b.title,"C Lnguage");
strcpy(b.author,"BALA GURUSWAMY");
strcpy(b.subject,"C Tutorial");
b.bookid=8746374;
printbook(&b);

return 0;
}
void printbook(struct books *b)
{
printf("%-15s: %s\n","Book Title", b->title);
printf("%-15s: %s\n","Book Author", b->author);
printf("%-15s: %s\n","Book subject", b->subject);
printf("%-15s: %d\n\n","Book BookId", b->bookid);
}
Program 19: Unions
Write a program to observe the memory difference between structures and unions
Sample Input and output:
size of union : 32 bytes
size of structure : 40 bytes
Program:
#include <stdio.h>
union u1
{
char x[32];
float b;
int c;
} uvar;
struct s1
{
char a[32];
float b;
int c;
} svar;

int main()
{
printf("size of union = %d bytes", sizeof(uvar));
printf("\nsize of structure = %d bytes", sizeof(svar));
return 0;
}
Program 20: Write a program to create a union of website and course and its fees and display
all union members
Sample Input and output :
WebSite : login.Bytexl.in
Subject : Principles of Programming
Price : 1000
Program:
#include<stdio.h>
#include<string.h>
typedef union ParulUniversity
{
char WebSite[50];
char Subject[50];
int Price;
}PU;
void main( )
{
PU p;

strcpy( p.WebSite, "login.Bytexl.in");


printf( "WebSite : %s\n", p.WebSite);

strcpy(p.Subject,"Principles of Programming");
printf( "Subject : %s\n", p.Subject);

p.Price=1000;
printf( "Price : %d\n", p.Price);
}
Program 21: Write a program for creating a file of employee name and salary
Program:
#include <stdio.h>
void main()
{
FILE *fptr;
int id;
char name[30];
float salary;

fptr = fopen("D:\\Emp_Details.txt", "w+");

if (fptr == NULL)
{
printf("File does not exists \n");
return;
}

printf("Enter the id\n");


scanf("%d", &id);

fprintf(fptr, "Id= %d\n", id);


printf("Enter the name \n");
scanf("%s", name);

fprintf(fptr, "Name= %s\n", name);


printf("Enter the salary\n");
scanf("%f", &salary);

fprintf(fptr, "Salary= %.2f\n", salary);


fclose(fptr);
}

You might also like