Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

C Practicals 1 To 3 by Nafis Parwez

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 17

C-Programming

Amity Institute of Information


Technology (AIIT)

Name – Nafis Parwez

Enrollment No - A710145023050

Subject - C Practicals

Course - MCA ( Semester 2 )

Subject Teacher – Ms. Gauri Deshpande


Index of C Programming

Sr. No. Practical Name Date Page No.

1 Module 1

1.1 a. Write a C program to find largest of three


numbers

b. Write a C program to check whether the


number is even or odd

1.2 Write a C program to swap a given variables.

1.3 Write a C program to compute the sum of


squares of n natural numbers.

1.4 Write a C program to generate Fibonacci


series upto n-terms

2 Module 2

2.1 Write a C program to search a number in an


array using linear search.

2.2 Write a C program to find the maximum and


minimum number in a given array

2.3 Write a C program to perform basic string


operations using string functions.

3 Module 3

3.1 Write a C program to determine whether the


entered character string is palindrome or not.

3.2 Write a C program to calculate net salary


statement of an employee using structure.
3.3 a. Write a C program to calculate the factorial
of a number using recursion.

b. Write a C program to generate a Fibonacci


series using recursion.
Module-1

1.1 a) Write a C program to find largest of three numbers

#include <stdio.h>

int main() {
int num1, num2, num3, largest;

printf("Enter three numbers: ");


scanf("%d %d %d", &num1, &num2, &num3);

largest = num1; // Assume the first number is largest initially

if (num2 > largest) {


largest = num2;
}
if (num3 > largest) {
largest = num3;
}

printf("%d is the largest number.\n", largest);

return 0;
}

1.1 b) Write a C program to find largest of three numbers


#include <stdio.h>

int main() {
int number;

printf("Enter an integer: ");


scanf("%d", &number);

// Check if the remainder of dividing by 2 is 0 (even) or 1 (odd)


if (number % 2 == 0) {
printf("%d is even.\n", number);
} else {
printf("%d is odd.\n", number);
}

return 0;
}

1.2 Write a C program to swap a given variables.


#include <stdio.h>

int main() {
int num1, num2, temp;

printf("Enter two numbers: ");


scanf("%d %d", &num1, &num2);

printf("Before swapping: num1 = %d, num2 = %d\n", num1, num2);

// Swap using a temporary variable


temp = num1;
num1 = num2;
num2 = temp;

printf("After swapping: num1 = %d, num2 = %d\n", num1, num2);

return 0;
}

1.3 Write a C program to compute the sum of squares of n natural numbers.


#include <stdio.h>

int main() {
int n, sum_of_squares = 0;

printf("Enter the number of natural numbers (n): ");


scanf("%d", &n);

// Handle negative input (optional)


if (n < 0) {
printf("Error: Please enter a non-negative number.\n");
return 1; // Indicate error
}

// Calculate sum of squares using a loop


for (int i = 1; i <= n; i++) {
sum_of_squares += i * i; // Square the current number and add to the sum
}

printf("The sum of squares of the first %d natural numbers is %d\n", n,


sum_of_squares);

return 0;
}

1.4 Write a C program to generate Fibonacci series upto n-terms


#include <stdio.h>

int main() {
int n, a = 0, b = 1, nextTerm;

printf("Enter the number of terms (n): ");


scanf("%d", &n);

printf("Fibonacci Series: ");

// Print the first two terms (special cases)


if (n >= 1) {
printf("%d ", a);
}
if (n >= 2) {
printf("%d ", b);
}

// Generate the remaining terms using a loop (n-2 terms)


for (int i = 3; i <= n; i++) {
nextTerm = a + b;
printf("%d ", nextTerm);
a = b;
b = nextTerm;
}

printf("\n");

return 0;
}
Module-2

2.1 Write a C program to search a number in an array using linear search.


#include <stdio.h>

int main() {
int arr[] = {10, 20, 30, 40, 50}; // Sample array
int n = sizeof(arr) / sizeof(arr[0]); // Size of the array
int numToSearch, i, found = 0;

printf("Enter a number to search: ");


scanf("%d", &numToSearch);

// Linear search
for (i = 0; i < n; i++) {
if (arr[i] == numToSearch) {
found = 1;
break;
}
}

if (found) {
printf("%d found at index %d\n", numToSearch, i);
} else {
printf("%d not found in the array\n", numToSearch);
}

return 0;
}
2.2 Write a C program to find the maximum and minimum number in a given
array.
#include <stdio.h>
#include <limits.h> // For INT_MAX and INT_MIN

int main() {
int arr[] = {10, 20, 30, 40, 50}; // Sample array
int n = sizeof(arr) / sizeof(arr[0]); // Size of the array
int max, min;

// Initialize max and min with first element (assuming non-empty array)
max = min = arr[0];

// Traverse the array to find actual minimum and maximum


for (int i = 1; i < n; i++) {
if (arr[i] > max) {
max = arr[i];
}
if (arr[i] < min) {
min = arr[i];
}
}

printf("Maximum element is: %d\n", max);


printf("Minimum element is: %d\n", min);

return 0;
}
2.3 Write a C program to perform basic string operations using string functions.

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

int main() {
char str1[100], str2[100]; // Arrays to store strings

printf("Enter the first string: ");


fgets(str1, 100, stdin); // Read string including spaces (use fgets)

// Remove the newline character from the first string (optional)


str1[strcspn(str1, "\n")] = '\0';

printf("Enter the second string: ");


fgets(str2, 100, stdin);

// Remove the newline character from the second string (optional)


str2[strcspn(str2, "\n")] = '\0';

// String operations using string functions:

// 1. String length (strlen)


int len1 = strlen(str1);
int len2 = strlen(str2);
printf("Length of string 1: %d\n", len1);
printf("Length of string 2: %d\n", len2);

// 2. String concatenation (strcat)


strcat(str1, " - concatenated");
printf("String 1 after concatenation: %s\n", str1);

// **Important note:** strcat modifies the first string (str1) in-place. Be cautious
with string sizes to avoid buffer overflows.

// 3. String copy (strcpy)


strcpy(str2, "Copied string");
printf("String 2 after copying: %s\n", str2);
// **Important note:** strcpy also modifies the destination string (str2) in-place.
Ensure the destination has enough space to hold the copied string.

// 4. String comparison (strcmp)


int compareResult = strcmp(str1, str2);
if (compareResult == 0) {
printf("Strings are equal.\n");
} else if (compareResult < 0) {
printf("String 1 is lexicographically smaller than string 2.\n");
} else {
printf("String 1 is lexicographically larger than string 2.\n");
}

return 0;
}
Module-3

3.1 Write a C program to determine whether the entered character string is palindrome or not.
#include <stdio.h>
#include <ctype.h> // For tolower()
#include <string.h> // For strcspn and strlen

int main() {
char str[100];
int len, i, isPalindrome = 1;

printf("Enter a string: ");


fgets(str, 100, stdin);

str[strcspn(str, "\n")] = '\0';

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


str[i] = tolower(str[i]);
}
len = strlen(str);
for (i = 0; i < len / 2; i++) {
if (str[i] != str[len - 1 - i]) {
isPalindrome = 0;
break;
}
}
if (isPalindrome) {
printf("%s is a palindrome.\n", str);
} else {
printf("%s is not a palindrome.\n", str);
}

return 0;
}
3.2 Write a C program to determine whether the entered character string is palindrome or not.

#include <stdio.h>
#include <string.h> // Included for strcspn

struct Employee {
char name[50];
int id;
float basic_salary, da, hra, gross_salary, net_salary;
};

int main() {
struct Employee emp;

printf("Enter employee details:\n");

// Get employee name


printf("Name: ");
fgets(emp.name, 50, stdin);
emp.name[strcspn(emp.name, "\n")] = '\0'; // Remove trailing newline from name

// Get employee ID
printf("ID: ");
scanf("%d", &emp.id);

// Get basic salary


printf("Basic Salary: ");
scanf("%f", &emp.basic_salary);

// Calculate DA (Dearness Allowance) - You can adjust the calculation formula as


needed
emp.da = 0.5 * emp.basic_salary;

// Calculate HRA (House Rent Allowance) - You can adjust the calculation formula as
needed
emp.hra = 0.2 * emp.basic_salary;

// Calculate gross salary


emp.gross_salary = emp.basic_salary + emp.da + emp.hra;
// Assume deductions (can be modified to include different deductions)
float deductions = 100; // Replace with actual deduction amount/calculation

// Calculate net salary


emp.net_salary = emp.gross_salary - deductions;

printf("\nEmployee Salary Statement:\n");


printf("Name: %s", emp.name);
printf("ID: %d\n", emp.id);
printf("Basic Salary: %.2f\n", emp.basic_salary);
printf("DA: %.2f\n", emp.da);
printf("HRA: %.2f\n", emp.hra);
printf("Gross Salary: %.2f\n", emp.gross_salary);
printf("Deductions: %.2f\n", deductions);
printf("Net Salary: %.2f\n", emp.net_salary);

return 0;
}
3.3 a) Write a C program to calculate the factorial of a number using recursion.

#include <stdio.h>

int factorial(int n) {
if (n == 0) { // Base case: factorial of 0 is 1
return 1;
} else {
return n * factorial(n - 1); // Recursive call: factorial(n) = n * factorial(n-
1)
}
}

int main() {
int number;

printf("Enter a non-negative integer: ");


scanf("%d", &number);

if (number < 0) {
printf("Error: Factorial is not defined for negative numbers.\n");
} else {
int fact = factorial(number);
printf("The factorial of %d is %d\n", number, fact);
}

return 0;
}
3.3 b) Write a C program to generate a Fibonacci series using recursion.

#include <stdio.h>

int fibonacci(int n) {
if (n <= 1) { // Base case: 0th and 1st terms are 0 and 1
return n;
} else {
return fibonacci(n - 1) + fibonacci(n - 2); // Recursive call: nth term is sum
of (n-1)th and (n-2)th terms
}
}

int main() {
int n, i;

printf("Enter the number of terms in the Fibonacci series: ");


scanf("%d", &n);

printf("Fibonacci Series: ");


for (i = 0; i < n; i++) {
printf("%d ", fibonacci(i));
}
printf("\n");

return 0;
}

You might also like