PPS Lab Manual
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);
}
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;
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.");
}
#include<stdio.h>
#include<conio.h>
void main()
{
int row;
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("\n");
}
printf("\n");
}
Program 7: write a program to find the in a given interval range.
#include<stdio.h>
void main(){
int l, u, flag;
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;
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;
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;
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;
tot=total(s1,s2,s3);
avg=average(tot);
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>
if(num<=1)
return 1;
else
return num * fact(num-1);
}
void main(){
int num;
char answer = 'y';
while(answer=='y' || answer=='Y'){
if(num<1 || num>20)
printf("Invalid input.\n");
else
printf("%d! = %lf\n", num, fact(num));
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 %d integers\n",size);
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;
ch = tolower(line[i]);
if(97<=ch && ch<=122){
if(ch=='a' || ch=='e' || ch=='i' || ch=='o' || ch=='u')
vowels++;
else
consonant++;
}
else
special++;
}
Program:
#include<stdio.h>
#include<stdlib.h>
void main()
{
char *str1 = malloc(20*sizeof(char));
swap(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;
if(ptr==NULL)
{
printf("Sorry! unable to allocate memory");
exit(0);
}
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;
if(ptr==NULL)
{
printf("Sorry! unable to allocate memory");
exit(0);
}
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) {
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>
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;
}
Program:
#include<stdio.h>
#include<conio.h>
#include<string.h>
struct books
{
char *title;
char *author;
char *subject;
int bookid;
};
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.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;
if (fptr == NULL)
{
printf("File does not exists \n");
return;
}