C Practical Assignment in Word (2)
C Practical Assignment in Word (2)
USMANPURA
NAME:-VANSH J SAVADIYA
ROLL NO:-1196
BATCH:-B2
COURSE NAME:-C PROGRAMMING PRACTICAL
COURSE CODE:-DSC-C-BCA-112 P
#include <stdio.h>
#include <conio.h>
int main() {
scanf("%f", &principal);
scanf("%f", &rate);
scanf("%f", &time);
VANSH SAVADIYA
// Display the result
getch();
#include <stdio.h>
#include<conio.h>
int main() {
int a = 5;
int b = 10;
int temp;
temp = a;
a = b;
b = temp;
getch();
#include <stdio.h>
#include <conio.h>
VANSH SAVADIYA
int main() {
} else {
} else {
} else {
getch();
#include <stdio.h>
#include <conio.h>
int main() {
int choice;
VANSH SAVADIYA
// Prompt the user for choice
scanf("%d", &choice);
if (choice == 1) {
// Fahrenheit to Celsius
scanf("%f", &fahrenheit);
} else if (choice == 2) {
// Celsius to Fahrenheit
scanf("%f", &celsius);
} else {
getch();
#include <stdio.h>
#include <conio.h>
int main() {
int num, i;
VANSH SAVADIYA
// Prompt user for input
scanf("%d", &num);
getch();
}
#include <stdio.h>
#include<conio.h>
int main() {
int num, i;
scanf("%d", &num);
if (num < 0) {
} else {
factorial *= i;
VANSH SAVADIYA
}
getch();
#include <stdio.h>
#include <conio.h>
int main() {
scanf("%d", &n);
if (n <= 0) {
return 1;
// Prompt user for the first input and assume it's the maximum
scanf("%d", &num);
max = num;
scanf("%d", &num);
max = num;
VANSH SAVADIYA
// Display the maximum number
getch();
#include <stdio.h>
#include <conio.h>
int main() {
scanf("%d", &num);
while (num != 0) {
getch();
#include <stdio.h>
VANSH SAVADIYA
#include <conio.h>
int main() {
int n, i;
scanf("%d", &n);
nextTerm = t1 + t2;
t1 = t2;
t2 = nextTerm;
printf("\n");
getch();
#include <stdio.h>
#include<conio.h>
while (b != 0) {
int temp = b;
b = a % b;
a = temp;
return a;
VANSH SAVADIYA
// Function to find LCM
return (a * b) / gcd;
int main() {
// Calculate GCD
// Calculate LCM
getch();
#include <stdio.h>
#include<conio.h>
int main() {
scanf("%d", &num);
while (num != 0) {
VANSH SAVADIYA
remainder = num % 10;
num /= 10;
// Check if the original number and the reversed number are the same
if (originalNum == reversed) {
} else {
getch();
#include <stdio.h>
#include<conio.h>
int main() {
scanf("%d", &num);
if (num <= 1) {
isPrime = 0;
} else {
// Loop to check if the number is divisible by any number other than 1 and itself
VANSH SAVADIYA
if (num % i == 0) {
isPrime = 0;
break;
if (isPrime) {
} else {
return 0;
**
***
****
LEFT ALIGNED :-
#include <stdio.h>
#include<conio.h>
int main() {
int rows = 5, i, j;
VANSH SAVADIYA
for (i = 1; i <= rows; ++i) {
printf("* ");
printf("\n");
getch();
RIGHT ALIGNED:-
#include <stdio.h>
#include<conio.h>
int main() {
printf("* ");
printf("\n");
getch();
CENTER ALIGNED :-
#include <stdio.h>
#include<conio.h>
int main()
VANSH SAVADIYA
int rows = 5, i, j, space;
printf("* ");
printf("\n");
getch();
121
12321
1234321.
#include <stdio.h>
#include<conio.h>
int main() {
int n, i, j;
scanf("%d", &n);
VANSH SAVADIYA
// Generate the pyramid pattern
printf("%d", j);
printf("%d", j);
printf("\n");
getch();
#include <stdio.h>
#include<conio.h>
int main()
int numbers[10];
int sum = 0;
float average;
printf("Enter 10 numbers:\n");
scanf("%d", &numbers[i]);
VANSH SAVADIYA
sum += numbers[i]; // Adding the number to sum
// Calculate average
printf("\n");
getch();
#include <stdio.h>
#include<conio.h>
int i, j, temp;
VANSH SAVADIYA
// Swap arr[j] and arr[j+1]
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
int main() {
int n, i;
scanf("%d", &n);
int arr[n];
scanf("%d", &arr[i]);
bubbleSort(arr, n);
printf("\n");
getch();
VANSH SAVADIYA
5 Demonstrate to find addition of two matrices of 3*3.
.
#include <stdio.h>
#include<conio.h>
int main()
int i, j;
scanf("%d", &matrix1[i][j]);
scanf("%d", &matrix2[i][j]);
VANSH SAVADIYA
sum[i][j] = matrix1[i][j] + matrix2[i][j];
if (j == 2) printf("\n");
getch();
#include <stdio.h>
#include<conio.h>
int main() {
int i, j, k;
scanf("%d", &matrix1[i][j]);
VANSH SAVADIYA
}
scanf("%d", &matrix2[i][j]);
product[i][j] = 0;
VANSH SAVADIYA
for (i = 0; i < 3; ++i)
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 <string.h>
int main() {
int i, len;
len = strlen(str);
VANSH SAVADIYA
// Check if the original string is equal to the reversed string
if (strcmp(str, reversedStr) == 0) {
} else {
getch();
#include <stdio.h>
#include<conio.h>
int fact(int n) {
if (n == 0) {
} else {
int main() {
int num;
scanf("%d", &num);
VANSH SAVADIYA
getch();
#include <stdio.h>
#include<conio.h>
int main()
char str[100];
// Convert to uppercase
toUppercase(str);
getch();
VANSH SAVADIYA
10 Take a lowercase string from the user and print its length and
uppercase string.
.
#include <stdio.h>
#include<conio.h>
int length = 0;
length++;
return length;
int main()
VANSH SAVADIYA
char str[100];
toUppercase(str);
getch();
#include <stdio.h>
#include<conio.h>
n /= 10;
return n % 10;
int main() {
int n, k, result;
VANSH SAVADIYA
printf("Enter the number N: ");
scanf("%d", &n);
scanf("%d", &k);
getch();
#include <stdio.h>
#include<conio.h>
if (num <= 1)
if (num % i == 0)
return 0; // If divisible by any number other than 1 and itself, it's not prime
VANSH SAVADIYA
}
return 1; // If not divisible by any number other than 1 and itself, it's prime
int main()
int num;
scanf("%d", &num);
if (isPrime(num))
} else {
getch();
#include <stdio.h>
#include<conio.h>
VANSH SAVADIYA
// Define structure for state
struct State {
char name[50];
int numberOfDistricts;
long totalPopulation;
};
int main() {
int i;
printf("Name: ");
scanf("%s", states[i].name);
scanf("%d", &states[i].numberOfDistricts);
scanf("%ld", &states[i].totalPopulation);
getch();
VANSH SAVADIYA
2 Define a structure called Item having member variables: Item
code, Item name, item price. Create an array of structure to
store five Items. Create a function which accepts the Item
array and modifies each element with an increase of 10% in
the price.
.
#include <stdio.h>
#include<conio.h>
struct Item
int code;
char name[50];
float price;
};
items[i].price *= 1.10;
int main() {
int i;
VANSH SAVADIYA
printf("Item %d:\n", i + 1);
printf("Code: ");
scanf("%d", &items[i].code);
printf("Name: ");
scanf("%s", items[i].name);
printf("Price: ");
scanf("%f", &items[i].price);
increasePrice(items, 5);
getch();
#include <stdio.h>
#include<conio.h>
struct Date {
VANSH SAVADIYA
int month;
int day;
};
};
} else {
int main() {
getch();
VANSH SAVADIYA
members that include name, address, grade, room charges,
grade and no of rooms. Write a function to print out all hotel
details with room charges less than a given value.
.
#include <stdio.h>
#include<conio.h>
#include <string.h>
struct Hotel {
char name[50];
char address[100];
int grade;
float roomCharges;
int numberOfRooms;
};
// Function to print hotel details with room charges less than a given value
printf("\n");
int main() {
VANSH SAVADIYA
struct Hotel hotels[5];
int i;
printf("Name: ");
scanf("%s", hotels[i].name);
printf("Address: ");
printf("Grade: ");
scanf("%d", &hotels[i].grade);
scanf("%f", &hotels[i].roomCharges);
scanf("%d", &hotels[i].numberOfRooms);
float maxCharges;
scanf("%f", &maxCharges);
// Print hotels with room charges less than the given value
printHotels(hotels, 5, maxCharges);
getch();
VANSH SAVADIYA
state, which is having highest number of colleges.
.
#include <stdio.h>
#include<conio.h>
#include <string.h>
struct State {
char name[50];
int engineeringColleges;
int medicalColleges;
int managementColleges;
int universities;
int totalColleges;
};
int main() {
int i, maxCollegesIndex = 0;
printf("Name: ");
scanf("%s", states[i].name);
scanf("%d", &states[i].engineeringColleges);
scanf("%d", &states[i].medicalColleges);
scanf("%d", &states[i].managementColleges);
scanf("%d", &states[i].universities);
VANSH SAVADIYA
// Calculate total number of colleges
states[i].totalColleges = states[i].engineeringColleges +
states[i].medicalColleges +
states[i].managementColleges +
states[i].universities;
maxCollegesIndex = i;
// Display the details of the state with the highest number of colleges
getch();
#include <stdio.h>
#include<conio.h>
struct Time {
int hours;
int minutes;
VANSH SAVADIYA
int seconds;
};
// Calculate seconds
end.seconds += 60;
end.minutes -= 1;
// Calculate minutes
end.minutes += 60;
end.hours -= 1;
// Calculate hours
return diff;
int main() {
VANSH SAVADIYA
// Display the difference
getch();
#include <stdio.h>
#include<conio.h>
#include <string.h>
struct State {
char name[50];
long population;
float literacyRate;
long perCapitaIncome;
};
int main() {
int i;
int highestLiteracyIndex = 0;
int highestIncomeIndex = 0;
VANSH SAVADIYA
printf("Name: ");
scanf("%s", states[i].name);
printf("Population: ");
scanf("%ld", &states[i].population);
scanf("%f", &states[i].literacyRate);
scanf("%ld", &states[i].perCapitaIncome);
highestLiteracyIndex = i;
highestIncomeIndex = i;
getch();
VANSH SAVADIYA
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>
struct Employee {
char name[50];
float basicPay;
float dearnessAllowance;
float houseRent;
float netSalary;
};
employees[i].netSalary = employees[i].basicPay +
employees[i].dearnessAllowance +
employees[i].houseRent;
temp = employees[i];
VANSH SAVADIYA
employees[i] = employees[j];
employees[j] = temp;
int main() {
int i;
printf("Name: ");
scanf("%s", employees[i].name);
scanf("%f", &employees[i].basicPay);
scanf("%f", &employees[i].dearnessAllowance);
scanf("%f", &employees[i].houseRent);
VANSH SAVADIYA
}
calculateNetSalary(employees, 5);
sortEmployeesByNetSalary(employees, 5);
printEmployeeDetails(employees, 5);
getch();
#include <stdio.h>
#include<conio.h>
struct Population {
int men;
int women;
};
struct State {
char name[50];
};
int main() {
int i;
VANSH SAVADIYA
// Input data for 5 states
printf("Name: ");
scanf("%s", states[i].name);
scanf("%d", &states[i].population.men);
scanf("%d", &states[i].population.women);
printf("\n");
getch();
#include <stdio.h>
#include<conio.h>
// Define a union
union Data {
int i;
float f;
VANSH SAVADIYA
char str[20];
};
int main() {
// Store an integer
data.i = 10;
// Store a float
data.f = 220.5;
// Store a string
strcpy(data.str, "Hello");
getch();
4. Pointers
#include <stdio.h>
#include<conio,h>
int temp;
temp = *a;
*a = *b;
VANSH SAVADIYA
*b = temp;
int main() {
int x, y;
scanf("%d", &x);
scanf("%d", &y);
swap(&x, &y);
getch();
#include <stdio.h>
#include<conio.h>
void calc(int *a, int *b, int *sum, int *sub, int *mul, float *div) {
*sum = *a + *b;
*sub = *a - *b;
VANSH SAVADIYA
*div = (float)(*a) / (*b); // Typecasting to float for division
int main() {
int x, y;
float div;
scanf("%d", &x);
scanf("%d", &y);
// Display results
getch();
#include <stdio.h>
#include<conio.h>
int length = 0;
VANSH SAVADIYA
length++;
return length;
int main() {
char str[100];
getch();
#include <stdio.h>
#include<conio.h>
// Function to find the maximum number between two numbers using pointers
return *a;
} else {
return *b;
VANSH SAVADIYA
int main() {
scanf("%d", &num1);
scanf("%d", &num2);
getch();
#include <stdio.h>
#include<conio.h>
VANSH SAVADIYA
for (int i = 0; i < size; i++) {
printf("\n");
int main() {
int arr[10];
inputArray(arr, size);
printArray(arr, size);
getch();
#include <stdio.h>
#include<conio.h>
int main() {
VANSH SAVADIYA
printf("Enter 10 elements for the source array:\n");
scanf("%d", &source[i]);
printf("\n");
getch();
#include <stdio.h>
#include<conio.h>
if (*str1 != *str2) {
str1++;
str2++;
return (*str1 == '\0' && *str2 == '\0'); // True if both strings end simultaneously
VANSH SAVADIYA
int main() {
gets(str2);
if (compareStrings(str1, str2)) {
} else {
getch();
#include <stdio.h>
#include<conio.h>
char temp;
end++;
// Swap the characters from start and end until the pointers meet in the middle
VANSH SAVADIYA
while (start < end) {
temp = *start;
*start = *end;
*end = temp;
start++;
end--;
int main() {
char str[100];
reverseString(str);
getch();
#include <stdio.h>
#include<conio.h>
int i, j, temp;
VANSH SAVADIYA
temp = *(arr + i);
*(arr + j) = temp;
int main() {
int arr[10], i;
printf("Enter 10 elements:\n");
sortArray(arr, size);
printf("Sorted array:\n");
printf("\n");
getch();
#include<conio.h>
VANSH SAVADIYA
void concatenate(char *str1, char *str2) {
str1++;
*str1 = *str2;
str1++;
str2++;
*str1 = '\0';
int main() {
gets(str2);
concatenate(str1, str2);
getch();
VANSH SAVADIYA
Extra Practice List:
1 Demonstrate printing Tables for the range given by user. E.g. start
no : 1,
ending no: 10 Program should print table(s) of 1 to 10 numbers.
ANS
#include<stdio.h>
#include<conio.h>
printf("\n");
int main() {
scanf("%d", &start);
scanf("%d", &end);
printTable(i);
getch();
VANSH SAVADIYA
2. Demonstrate to find sum of N numbers.
ANS
#include <stdio.h>
#include<conio.h>
int main() {
scanf("%d", &N);
scanf("%d", &num);
sum += num;
getch();
#include<conio.h>
int main() {
scanf("%d", &num);
VANSH SAVADIYA
while (num != 0) {
getch();
4. Demonstrate to find the sum of first 100 odd nos. and even nos.
ANS
#include<stdio.h>
#include<conio.h>
int main() {
int sumOdd = 0;
int sumEven = 0;
int i;
sumOdd += i;
sumEven += i;
getch();
VANSH SAVADIYA
}
#include<math.h>
#include<conio.h>
originalNum = num;
while (originalNum != 0) {
originalNum /= 10;
++n;
originalNum = num;
while (originalNum != 0) {
originalNum /= 10;
int main() {
int num;
VANSH SAVADIYA
printf("Enter an integer: ");
scanf("%d", &num);
if (isArmstrong(num)) {
} else {
getch();
#include<conio.h>
if (num <= 1) {
return 0;
if (num % i == 0) {
return 0;
return 1;
int main() {
VANSH SAVADIYA
for (int i = 50; i <= 100; i++) {
if (isPrime(i)) {
printf("%d\n", i);
getch();
#include<conio.h>
int main() {
int n, i;
scanf("%d", &n);
scanf("%f", &number);
sum += number;
getch();
VANSH SAVADIYA
8. Demonstrate to find 1+1/2+1/3+1/4+ …+1/n.
ANS
#include<stdio.h>
#include<conio.h>
int main() {
int n, i;
scanf("%d", &n);
sum += 1.0 / i;
printf("The sum of the series 1 + 1/2 + 1/3 + ... + 1/%d is: %.2f\n", n, sum);
getch();
#include<conio.h>
int main() {
int n, i, j;
scanf("%d", &n);
VANSH SAVADIYA
for (i = 1; i <= n; i++) {
printf("%d", j);
printf("%d", j);
printf("\n");
getch();
#include<conio.h>
int main() {
char ch = 'A';
printf("%c", ch);
ch++;
printf("\n");
VANSH SAVADIYA
}
getch();
#include<conio.h>
int main() {
int i, j;
char ch;
ch = 'A';
printf("%c", ch);
ch++;
ch--;
printf("%c", ch);
printf("\n");
getch();
VANSH SAVADIYA
}
#include<conio.h>
int main() {
if ((i + j) % 2 == 0) {
printf("1 ");
} else {
printf("0 ");
printf("\n");
getch();
VANSH SAVADIYA
13. Display this kind of output on screen.
C
CP
CPR
..
CPROGRAMING
.
.
CPR
CP
C.
ANS
#include<stdio.h>
#include<conio.h>
int main() {
printf("CCPCPR\n");
printf(". .CPROGRAMING..\n");
printf("CPRCPC\n");
getch();
#include<conio.h>
VANSH SAVADIYA
int searchElement(int *arr, int size, int element) {
if (*(arr + i) == element) {
int main() {
scanf("%d", &arr[i]);
scanf("%d", &element);
if (index != -1) {
} else {
getch();
VANSH SAVADIYA
15. Demonstrate to find sum, average of two numbers passed to
user defined
functions called sum and average.
ANS
#include<stdio.h>
#include<conio.h>
return a + b;
return (a + b) / 2.0;
int main() {
int total;
float avg;
scanf("%d", &num1);
scanf("%d", &num2);
getch();
VANSH SAVADIYA
}
#include<conio.h>
int fibonacci(int n) {
if (n <= 1) {
return n;
} else {
int main() {
int n, i;
scanf("%d", &n);
printf("\n");
getch();
VANSH SAVADIYA
#include <stdio.h>
int length = 0;
length++;
return length;
int main() {
char str[100];
getch();
18. Accept two strings from the user and print the message that
the strings are same
or not.
ANS
#include<stdio.h>
#include<string.h>
#include<conio.h>
VANSH SAVADIYA
while (*str1 && *str2) {
if (*str1 != *str2) {
str1++;
str2++;
return (*str1 == '\0' && *str2 == '\0'); // True if both strings end simultaneously
int main() {
gets(str2);
if (compareStrings(str1, str2)) {
} else {
getch();
VANSH SAVADIYA