c Programming Practical Assignment
c Programming Practical Assignment
c Programming Practical Assignment
USMANPURA
#include <stdio.h>
#include <conio.h>
int main()
MEET PATEL
scanf("%f", &time); // Calculate Simple
getch();
int main()
{ int a = 5;
int b = 10;
int temp;
swap temp = a; a = b;
#include <stdio.h>
#include <conio.h>
MEET PATEL
numbers: "); scanf("%d %d %d", &num1,
&num2, &num3);
} else {
} else {
} else {
getch();
#include <stdio.h>
MEET PATEL
printf("Enter your choice (1 or 2): "); scanf("%d",
&choice);
if (choice == 1) {
celsius, fahrenheit);
} else {
getch();
#include <stdio.h>
#include <conio.h>
int main()
int num, i;
getch();
}
scanf("%d", &num);
doesn't exist.\n");
} else {
{ factorial *= i;
getch();
#include <stdio.h>
#include <conio.h>
num, max;
MEET PATEL
// Prompt user for the number of inputs
&n);
inputs.\n");
return 1;
// Prompt user for the first input and assume it's the maximum
scanf("%d", &num);
getch();
#include <stdio.h>
#include <conio.h>
MEET PATEL
input printf("Enter an integer: ");
scanf("%d", &num);
last digit
getch();
#include <stdio.h>
main() {
int n, i;
t1 = t2; t2
= nextTerm;
printf("\n");
getch();
MEET PATEL
10. Demonstrate to find GCD and LCM of given 2 numbers.
.
#include <stdio.h>
#include<conio.h>
temp = b; b = a % b; a = temp;
return a;
{ return (a * b) / gcd;
findGCD(num1, num2);
getch();
MEET PATEL
#include <stdio.h> #include<conio.h> int main() {
while (num != 0) {
10;
// Check if the original number and the reversed number are the same
originalNum);
} else {
getch();
scanf("%d", &num);
MEET PATEL
// Check if number is less than 2
} else {
// Loop to check if the number is divisible by any number other than 1 and itself
if (num % i == 0) {
isPrime = 0; break;
num);
} else {
return 0;
**
MEET PATEL
***
****
LEFT ALIGNED :-
#include <stdio.h>
#include<conio.h>
rows = 5, i, j; for
(i = 1; i <= rows; +
+i) { for (j = 1;
j <= i; ++j)
{ printf("*
");
printf("\n");
getch();
RIGHT ALIGNED:-
#include <stdio.h> #include<conio.h> int main()
{ printf("* ");
printf("\n");
getch();
MEET PATEL
CENTER ALIGNED :-
#include <stdio.h>
#include<conio.h> int
main()
space
{ printf("*
");
printf("\n");
getch();
121
12321 1234321.
MEET PATEL
#include <stdio.h>
#include<conio.h> int
main() {
int n, i, j;
scanf("%d", &n);
printf("%d", j);
} for (j = i - 1; j >=
1; j--) { printf("%d",
j);
printf("\n");
getch();
#include <stdio.h>
#include<conio.h> int
main()
MEET PATEL
int numbers[10];
average;
printf("Enter 10 numbers:\n");
printf("\n");
#include <stdio.h>
#include<conio.h> void
MEET PATEL
{ int i, j, temp; for
+)
{ if (arr[j] >
arr[j+1])
arr[j+1] = temp;
int main() {
int n, i;
scanf("%d", &arr[i]);
bubbleSort(arr, n); //
MEET PATEL
}
printf("\n");
getch();
int main()
int i, j;
{ for (j = 0; j < 3; +
+j)
scanf("%d", &matrix1[i][j]);
MEET PATEL
{ for (j = 0; j < 3; +
+j)
(j = 0; j < 3; ++j)
[j]); if (j == 2) printf("\
n");
getch();
int i, j, k;
{ for (j = 0; j < 3; +
+j)
MEET PATEL
printf("Element [%d][%d]: ", i + 1, j + 1);
scanf("%d", &matrix1[i][j]);
{ for (j = 0; j < 3; +
+j)
scanf("%d", &matrix2[i][j]);
{ for (j = 0; j < 3; +
+j)
product[i][j] = 0;
{ for (j = 0; j < 3; +
+j)
MEET PATEL
}
{ for (j = 0; j < 3; +
+j)
if (j == 2) printf("\n");
getch();
7. Input a string from the user and check whether the string is
palindrome or not.
.
#include <stdio.h>
#include<conio.h> #include
str[100], reversedStr[100];
int i, len;
string
MEET PATEL
// Check if the original string is equal to the reversed string
palindrome.\n", str);
palindrome.\n", str);
getch();
#include <stdio.h>
#include<conio.h>
int fact(int n) { if (n == 0)
Recursive case
int main()
{ int num;
MEET PATEL
9. Demonstrate to convert lowercase string to uppercase string
(without including string.h).
.
#include <stdio.h>
#include<conio.h> void
toUppercase(char* str)
+)
'A';
int main()
{ char
str[100];
// Convert to uppercase
getch();
10. Take a lowercase string from the user and print its length
and uppercase string.
MEET PATEL
.
#include <stdio.h>
#include<conio.h>
stringLength(char* str)
(str[length] != '\0')
length++;
return length;
toUppercase(char* str)
+)
'z')
'A';
int main()
char str[100];
MEET PATEL
// Calculate length and convert to uppercase
#include <stdio.h>
#include<conio.h>
{ n /= 10;
return n % 10;
int main() {
int n, k, result;
&k);
MEET PATEL
// Call the function and get the result result = digit(n, k); //
#include <stdio.h>
#include<conio.h>
isPrime(int num)
if (num <= 1)
{ if (num % i ==
0)
return 0; // If divisible by any number other than 1 and itself, it's not prime
return 1; // If not divisible by any number other than 1 and itself, it's prime
int main()
MEET PATEL
int num;
scanf("%d", &num);
if (isPrime(num))
} else {
getch();
3. Structures and
Union
#include <stdio.h>
#include<conio.h>
name[50]; int
MEET PATEL
numberOfDistricts; long
totalPopulation;
};
State states[5];
int i;
states[i].name); printf("Number of
&states[i].numberOfDistricts); printf("Total
&states[i].totalPopulation);
%ld\n", states[i].totalPopulation);
getch();
MEET PATEL
array and modifies each element with an increase of 10% in
the price.
.
#include <stdio.h>
#include<conio.h>
Item
int code;
char name[50];
float price;
};
+)
items[i].price *= 1.10;
Item items[5];
int i;
&items[i].code);
MEET PATEL
items[i].name); printf("Price:
"); scanf("%f",
&items[i].price);
increasePrice(items, 5);
items[i].price);
getch();
#include <stdio.h>
#include<conio.h>
Date {
int month;
int day;
};
MEET PATEL
// Function to get the month name const
};
month";
date1, date2;
%s\n", getMonthName(date2.month));
getch();
#include <stdio.h>
MEET PATEL
#include<conio.h>
#include <string.h>
numberOfRooms;
};
// Function to print hotel details with room charges less than a given value
hotels[i].numberOfRooms);
printf("\n");
int main() {
int i;
scanf("%d", &hotels[i].numberOfRooms);
MEET PATEL
}
// Print hotels with room charges less than the given value
#include <stdio.h>
#include<conio.h>
#include <string.h>
medicalColleges; int
managementColleges;
int universities;
int totalColleges;
};
MEET PATEL
printf("Number of Engineering Colleges: ");
scanf("%d", &states[i].engineeringColleges);
scanf("%d", &states[i].medicalColleges);
scanf("%d", &states[i].managementColleges);
&states[i].universities);
states[i].totalColleges = states[i].engineeringColleges +
states[i].medicalColleges +
states[i].managementColleges +
states[i].universities;
states[maxCollegesIndex].totalColleges) { maxCollegesIndex = i;
// Display the details of the state with the highest number of colleges
states[maxCollegesIndex].totalColleges); getch();
MEET PATEL
.
#include <stdio.h>
#include<conio.h>
minutes;
int seconds;
};
// Calculate minutes if
= 1;
difference;
// Input start time printf("Enter start time (hh mm ss): "); scanf("%d %d %d",
// Input end time printf("Enter end time (hh mm ss): "); scanf("%d %d %d",
MEET PATEL
// Calculate the difference difference =
the difference
#include <stdio.h>
#include<conio.h>
#include <string.h>
literacyRate; long
perCapitaIncome;
};
State states[5];
int i;
highestIncomeIndex = 0; // Input
for 5 states:\n");
MEET PATEL
for (i = 0; i < 5; i++)
states[i].name);
printf("Population: ");
scanf("%ld", &states[i].population);
scanf("%f", &states[i].literacyRate);
scanf("%ld",
&states[i].perCapitaIncome); //
if (states[i].literacyRate >
states[highestLiteracyIndex].literac
yRate) { highestLiteracyIndex
= i;
states[highestIncomeIndex].perCapitaIncome) { highestIncomeIndex = i;
// Display the state with the highest literacy rate printf("\nState with the
states[highestLiteracyIndex].literacyRate);
// Display the state with the highest per capita income printf("\nState with the
states[highestIncomeIndex].perCapitaIncome); getch();
MEET PATEL
8. Define a structure employee with member variables having
employee name, basic pay, dearness allowance, house rent,
and net salary. Store data of 5 employees. Write a function
which calculates the net salary of employees and prints all
employee details in descending order of their net salary.
.
#include <stdio.h>
#include<conio.h>
#include <string.h>
float netSalary;
};
{ employees[i].netSalary = employees[i].basicPay
+ employees[i].dearnessAllowance
+ employees[i].houseRent;
MEET PATEL
if (employees[i].netSalary < employees[j].netSalary) {
temp = employees[i];
employees[i] = employees[j];
employees[j] = temp;
employees[i].netSalary);
employees[5];
int i;
employees[i].name);
scanf("%f", &employees[i].basicPay);
MEET PATEL
&employees[i].dearnessAllowance);
&employees[i].houseRent);
sortEmployeesByNetSalary(employees, 5);
#include <stdio.h>
#include<conio.h>
women;
};
};
State states[5];
int i;
MEET PATEL
// Input data for 5 states
scanf("%d", &states[i].population.men);
&states[i].population.women);
%d\n", states[i].population.women);
printf("\n");
getch();
#include <stdio.h>
#include<conio.h> // Define a union union Data {
int i; float f; char str[20];
};
int main() { union Data data; // Store an integer data.i = 10; printf("data.i: %d\n", data.i);
// Store a float data.f = 220.5; printf("data.f: %.1f\n", data.f);
// Store a string strcpy(data.str,
data.str); getch();
MEET PATEL
4. Pointers
void swap(int *a, int *b) { int temp; temp = *a; *a = *b;
*b = temp;
} int main()
int x, y;
swap(&x, &y);
#include <stdio.h>
#include<conio.h>
*sum = *a + *b;
*sub = *a - *b;
} int main()
div;
#include <stdio.h>
#include<conio.h>
length++;
return length;
int main()
{ char
str[100];
the length of the string using the stringLength function int length =
#include <stdio.h>
#include<conio.h>
// Function to find the maximum number between two numbers using pointers
MEET PATEL
}
num1, num2;
&num2);
// Find the maximum using the findMax function int max = findMax(&num1,
#include <stdio.h>
#include<conio.h>
MEET PATEL
for (int i = 0; i < size; i++)
printf("\n");
int main() {
sizeof(arr[0]);
inputArray(arr, size); //
printArray(arr, size);
getch();
#include <stdio.h>
#include<conio.h>
MEET PATEL
source array printf("Enter 10 elements for
scanf("%d", &source[i]);
printf("\n"); getch();
#include <stdio.h>
#include<conio.h>
str1++;
str2++;
return (*str1 == '\0' && *str2 == '\0'); // True if both strings end simultaneously
MEET PATEL
int main() { char str1[100], str2[100]; // Prompt user for input
gets(str2);
are equal.\n");
equal.\n");
getch();
#include <stdio.h>
#include<conio.h>
// Swap the characters from start and end until the pointers meet in the middle
temp = *start;
*start = *end;
*end = temp;
start++; end--;
MEET PATEL
}
#include <stdio.h>
#include<conio.h>
*(arr + j) = temp;
sizeof(arr) / sizeof(arr[0]);
printf("Enter 10 elements:\n");
MEET PATEL
for (i = 0; i < size; i++)
sortArray(arr, size); //
printf("Sorted array:\n");
printf("\n"); getch();
#include <stdio.h>
#include<conio.h>
str1++;
str2++;
*str1 = '\0';
MEET PATEL
int main() { char str1[100], str2[100]; // Prompt user for input
"); gets(str2);
MEET PATEL