PIC Experiment 1-8
PIC Experiment 1-8
Experiment 1
a)
Code
#include <stdio.h>
int main()
{
char x;
Output
b)
Code
#include <stdio.h>
int main()
{
#define PRIME 0;
#define COMPOSITE 1;
int x;
int toggle = PRIME;
Output
c)
Code
#include <stdio.h>
int main()
{
int x, y, hcf, lcm;
printf("Enter value x: ");
scanf("%d", &x);
lcm = (x * y) / hcf;
printf("The LCM of %d and %d is %d.\n", x, y, lcm);
return 0;
}
Output
Experiment 2
a)
Code
#include <stdio.h>
int main()
{
int x[5];
Output
b)
Code
#include <stdio.h>
int main()
{
int n;
int x[n];
average = sum / n;
printf("\nThe average of given numbers is: %f. \n", average);
return 0;
}
Output
c)
Code
#include <stdio.h>
int main()
{
for (int i = 1; i <= 4; i++)
{
for (int j = 1; j <= i; j++)
{
switch (i)
{
case 1:
printf("#");
break;
case 2:
printf("*");
break;
case 3:
printf("&");
break;
case 4:
printf("@");
break;
}
}
printf("\n");
}
return 0;
}
Output
d)
Code
#include <stdio.h>
int main()
{
int n;
int x[n];
Output
Experiment 3
a)
Code
#include <stdio.h>
int main()
{
int n;
int x[n];
Output
b)
Code
// Problem Statement: Write C program squaring the even positioned and cubing
the odd positioned elements.
#include <stdio.h>
int main()
{
int n;
return 0;
}
Output
c)
Code
#include <stdio.h>
int main()
{
int n;
printf("Enter number of elements: ");
scanf("%d", &n);
int x[n];
int left = 0;
int right = n - 1;
Output
Experiment 4
a)
Code
#include <stdio.h>
int main()
{
int m = 3;
int n = 3;
printf("\n");
return 0;
}
Output
b)
Code
#include <stdio.h>
int main()
{
int n;
if (x[i] % 2 == 0)
{
even++;
}
else
{
odd++;
}
}
Output
c)
Code
#include <stdio.h>
int main()
{
int m;
int n;
d)
Code
#include <stdio.h>
int main()
{
int m = 7;
int n = 2;
float A[m][n];
printf("\n");
return 0;
}
Output
e)
Code
#include <stdio.h>
int main()
{
int l = 3; // Side
int m = 2; // Row
int n = 2; // Column
int A[l][m][n];
Output
Experiment 5
a)
Code
// Problem Statement: Write a program in C to input a string and print reverse
string.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main()
{
char x[255];
printf("%s", strrev(x));
printf("\n");
return 0;
}
Option 1 B
// If the above code gives a function not declared error, it means
your compiler does not support strrev.
// Add the below code above the int main() function.
int n = 0;
while (src[n] != '\0')
{
n++;
}
return src;
}
Option 2
#include <stdio.h>
#include <string.h>
int main()
{
char x[255];
char y[255];
int i, j;
j = strlen(x) - 1;
b)
Code
#include <stdio.h>
#include <string.h>
int main()
{
strcpy(y, x);
printf("%s", y);
printf("\n");
return 0;
}
Output
c)
Code
// Problem Statement: Write a C program to count digits, spaces, special
character, alphabets in string.
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main()
{
char x[255];
Option 2
// Problem Statement: Write a C program to count digits, spaces, special
character, alphabets in string.
#include <stdio.h>
#include <string.h>
int main()
{
char x[255];
Output
d)
Code
#include <stdio.h>
#include <string.h>
int main()
{
char x[5][255];
if (x[i][a] == '\n')
{
x[i][a] = '\0';
}
}
for (int i = 0; i < 5 - 1; i++)
{
for (int j = i + 1; j < 5; j++)
{
int comparison = strcmp(x[i], x[j]);
if (comparison > 0)
{
char temp[255];
strcpy(temp, x[i]);
strcpy(x[i], x[j]);
strcpy(x[j], temp);
}
}
}
printf("Sorted string is: \n");
for (int i = 0; i < 5; i++)
{
printf("%s \n", x[i]);
}
return 0;
}
Output
e)
Code
#include <stdio.h>
#include <string.h>
int main()
{
int size;
printf("Enter number of names in the database: ");
scanf("%d", &size);
getchar();
char x[size][255];
if (x[i][a] == '\n')
{
x[i][a] = '\0';
}
}
char username[255];
int toggle = 0;
int b = strlen(username) - 1;
if (username[b] == '\n')
{
username[b] = '\0';
}
if (toggle)
{
int n, factorial = 1;
printf("Enter a number: ");
scanf("%d", &n);
Output
Experiment 6
a)
Code
#include <stdio.h>
struct Book
{
char book[50];
float price;
int pages;
};
int main()
{
struct Book book1;
printf("\nBook Information:\n");
printf("Title: %s", book1.book);
printf("Price: $%.2f\n", book1.price);
printf("Pages: %d\n", book1.pages);
return 0;
}
Output
b)
Code
#include <stdio.h>
struct Time
{
int hours;
int minutes;
int seconds;
};
int main()
{
struct Time startTime, endTime, difference;
if (difference.seconds < 0)
{
difference.seconds += 60;
difference.minutes--;
}
if (difference.minutes < 0)
{
difference.minutes += 60;
difference.hours--;
}
if (difference.hours < 0)
{
difference.hours += 24;
}
printf("Difference: %d:%d:%d\n", difference.hours, difference.minutes,
difference.seconds);
return 0;
}
Output
c)
Code
// Problem Statement: Write a program that compares two given dates. To store
data use a structure say date that contains three members namely date, month and
year. If the dates are equal, then display the message as Equal otherwise Unequal.
#include <stdio.h>
struct Date
{
int day;
int month;
int year;
};
int main()
{
struct Date date1, date2;
return 0;
}
Output
d)
Code
// Problem Statement: Initialise and print the following union variables: 7,
and 7.0 using the following program that has a union and a member to track what's
in the union inside a structure.
#include <stdio.h>
struct MyStructure
{
char type;
union
{
int integerValue;
float floatValue;
} data;
};
int main()
{
struct MyStructure s1;
s1.type = 'i';
s1.data.integerValue = 7;
printf("Integer value: %d\n", s1.data.integerValue);
s1.type = 'f';
s1.data.floatValue = 7.0;
printf("Float value: %.2f\n", s1.data.floatValue);
return 0;
}
Output
Experiment 7
a)
Code
// Problem Statement: Create a structure named company which has name,
address, phone and noofemployee as member variables. Read name of 10 companies, its
address, phone and noOfEmployee. Finally display these members' value.
#include <stdio.h>
#include <string.h>
struct company
{
char name[255];
char address[1024];
char phone[15];
int noOfEmployee;
};
int main()
{
int numCompanies = 10;
printf("Enter the number of companies: ");
scanf("%d", &numCompanies);
getchar();
printf("Address: ");
fgets(companies[i].address, sizeof(companies[i].address), stdin);
companies[i].address[strlen(companies[i].address) - 1] = '\0';
printf("Phone: ");
fgets(companies[i].phone, sizeof(companies[i].name), stdin);
companies[i].phone[strlen(companies[i].phone) - 1] = '\0';
printf("\nCompany Details:\n");
for (int i = 0; i < numCompanies; ++i)
{
printf("Company %d\n", i + 1);
printf(" | Name: %s", companies[i].name);
printf(" | Address: %s", companies[i].address);
printf(" | Phone: %s", companies[i].phone);
printf(" | Number of Employees: %d\n\n", companies[i].noOfEmployee);
}
return 0;
}
Output
b)
Code
// Problem Statement: Enter the marks of 5 students in Chemistry, Mathematics
and Physics (each out of 100) using a structure named Marks having elements rol
no., name, chem _marks, maths marks and phy_marks and then display the percentage
of each student.
#include <stdio.h>
struct Marks
{
int rollNo;
char name[255];
int chemMarks;
int mathsMarks;
int phyMarks;
};
int main()
{
int numStudents;
printf("Enter the number of students: ");
scanf("%d", &numStudents);
getchar();
printf("Name: ");
fgets(students[i].name, sizeof(students[i].name), stdin);
students[i].name[strlen(students[i].name) - 1] = '\0';
c)
Code
// Problem Statement: Write a program to add two distances in inch-feet using
structure. The values of the distances is to be taken from the user. The distances
are entered in inch-feet. The sum of the distances is to be displayed in inch-feet.
#include <stdio.h>
struct Distance
{
int feet;
int inch;
};
int main()
{
int numDistancesInput = 2;
struct Distance distancesInput[numDistancesInput];
struct Distance distancesSum;
distancesSum.feet = 0;
distancesSum.inch = 0;
return 0;
}
Output
Experiment 8
a)
// Problem Statement: Write a program in C to check whether two given strings
are an anagram.
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main()
{
char x[100], y[100];
if (strlen(x) != strlen(y))
{
printf("Both strings are not anagrams.\n");
return 0;
}
toLowerCase(x);
toLowerCase(y);
sortString(x);
sortString(y);
if (strcmp(x, y) == 0)
{
printf("Both strings are anagrams.\n");
}
else
{
printf("Both strings are not anagrams.\n");
}
return 0;
}
Output
b)
Code
// Problem Statement: Write a program in C to check armstrong number between 1
to 500.
#include <stdio.h>
#include <math.h>
int main()
{
printf("List of armstrong numbers: ");
for (int i = 1; i <= 500; i++)
{
int sum = 0, n = i;
while (n > 0)
{
sum += pow((n % 10), 3);
n /= 10;
}
if (sum == i)
{
printf("%d \n", i);
}
}
printf("\n");
return 0;
}
Output
c)
Code
#include <stdio.h>
#include <math.h>
int main()
{
int n;
printf("Enter the number to be checked: ");
scanf("%d", &n);
int sum = 0;
for (int i = 1; i < n; i++)
{
if (n % i == 0)
sum += i;
}
if (sum == n)
{
printf("The entered number is a perfect number.");
}
else
{
printf("The entered number is not a perfect number. ");
}
return 0;
}
Output