0% found this document useful (0 votes)
12 views13 pages

Ce Unit-4

Ce unit-4 Kkqjjjsnjjnkkkjqnnhbbenkallo2jhhvvauhwbhuahbvcxxrrtuujkklooak2koaonrjhvbji kalpak Phil ididjrbiekmemekeikej3ihbebkeks

Uploaded by

g.revathi0508
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views13 pages

Ce Unit-4

Ce unit-4 Kkqjjjsnjjnkkkjqnnhbbenkallo2jhhvvauhwbhuahbvcxxrrtuujkklooak2koaonrjhvbji kalpak Phil ididjrbiekmemekeikej3ihbebkeks

Uploaded by

g.revathi0508
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

11 – WEEK

1. Write a C function to calculate NCR value.


#include <stdio.h>
int fact(int n){
int i, f = 1;
for (i = 1; i <= n; i++){
f = f * i;
}
return f;
}
int main(){
int n, r, nCr;
printf("Enter the value of n: ");
scanf("%d", &n);
printf("Enter the value of r: ");
scanf("%d", &r);
nCr = fact(n) / (fact(r) * fact(n - r));
printf("The Value of C(%d, %d) is %d.", n, r, nCr);
return 0;
}
OUTPUT:
Enter the value of n: 4
Enter the value of r: 2
The Value of C(4, 2) is 6.

2. Write a C function to find the length of a string.


#include <stdio.h>
int mystrlen(char s[])
{
int i;
for(i=0;s[i]!='\0';i++);
return(i);
}
int main() {
char str[30];
printf("\nEnter string: ");
fflush(stdin);
scanf("%s",str);
int len = mystrlen(str);
printf("The length of the string '%s' is %d", str, len);
return 0;
}
OUTPUT:
Enter string: kurnool
The length of the string 'kurnool' is 7

3. Write a C function to transpose of a matrix.


#include <stdio.h>
void transposeMatrix(int matrix[][10], int rows, int cols, int result[][10]) {
int i, j;
for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) {
result[j][i] = matrix[i][j]; // Swap rows and columns
}
}
}
void displayMatrix(int matrix[][10], int rows, int cols) {
int i, j;
for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) {
printf("%d ", matrix[i][j]);
}
printf("\n");
}
}

int main() {
int matrix[10][10], transposed[10][10];
int rows, cols, i, j;
printf("Enter the number of rows and columns of the matrix: ");
scanf("%d %d", &rows, &cols);
printf("Enter the elements of the matrix:\n");
for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) {
scanf("%d", &matrix[i][j]);
}
}
printf("\nOriginal Matrix:\n");
displayMatrix(matrix, rows, cols);
transposeMatrix(matrix, rows, cols, transposed);
printf("\nTransposed Matrix:\n");
displayMatrix(transposed, cols, rows);
return 0;
}
OUTPUT:
Enter the number of rows and columns of the matrix: 2 2
Enter the elements of the matrix:
3
4
5
6

Original Matrix:
34
56

Transposed Matrix:
35
46
4. Write a C function to demonstrate numerical integration of
differential equations using Euler’s method
#include <stdio.h>
#include <math.h>
double f(double t, double y) {
return t + y; // dy/dt = t + y
}
void eulerMethod(double (*f)(double, double), double y0, double t0, double h, int n) {
double t = t0;
double y = y0;
int i;
printf("t = %.2f, y = %.4f\n", t, y);
for (i = 1; i <= n; i++) {
y = y + h * f(t, y);
t = t + h;
printf("t = %.2f, y = %.4f\n", t, y);
}
}
int main() {
double t0, y0, h;
int n;
printf("Enter the initial value t0: ");
scanf("%lf", &t0);
printf("Enter the initial value y0: ");
scanf("%lf", &y0);
printf("Enter the step size h: ");
scanf("%lf", &h);
printf("Enter the number of steps (n): ");
scanf("%d", &n);
printf("\nUsing Euler's Method to solve the differential equation:\n");
eulerMethod(f, y0, t0, h, n);
return 0;
}
OUTPUT:
Enter the initial value t0: 0
Enter the initial value y0: 1
Enter the step size h: 1
Enter the number of steps (n): 4

Using Euler's Method to solve the differential equation:


t = 0.00, y = 1.0000
t = 1.00, y = 2.0000
t = 2.00, y = 5.0000
t = 3.00, y = 12.0000
t = 4.00, y = 27.0000
WEEK – 12
1. Write a recursive function to generate Fibonacci series.
#include<stdio.h>
int fibs(int a)
{
if (a==0)
return 0;
else if(a==1)
return 1;
else
return (fibs(a-1)+fibs(a-2));
}
int main()
{
int a,i,fib;
printf("\n enter the fibonacci length:");
scanf("%d",&a);
printf("fibonacci series are:");
for(i=0;i<a;i++)
{
fib=fibs(i);
printf("%d\t",fib);
}
return 0;
}
OUTPUT:
enter the fibonacci length:10
fibonacci series are:0 1 1 2 3 5 8 13 21 34

2. Write a recursive function to find the LCM/GCD of two numbers.


#include<stdio.h>
int find_lcm(int, int);
int main()
{
int a, b, lcm;
printf("\n\nEnter 2 integers to find LCM of:\n");
scanf("%d%d", &a, &b);
lcm = find_lcm(a,b);
printf("\n\n LCM of %d and %d is: %d\n\n", a, b, lcm);
return 0;
}

int find_lcm(int a, int b)


{
static int temp = 1;
if(temp%a == 0 && temp%b == 0)
{
return temp;
}
else
{
temp++;
find_lcm(a,b);
return temp;
}
}
OUTPUT:
Enter 2 integers to find LCM of:
45
LCM of 4 and 5 is: 20

3. Write a recursive function to find the factorial of a number.


#include<stdio.h>
long int factorial(int n);
int main() {
int n;
printf("Enter a positive integer: ");
scanf("%d",&n);
printf("Factorial of %d = %ld", n, factorial(n));
return 0;
}

long int factorial(int n) {


if (n>=1)
return n*factorial(n-1);
else
return 1;
}
OUTPUT:
Enter a positive integer: 5
Factorial of 5 = 120

4. Write a C Program to implement Ackermann function using


recursion.
#include <stdio.h>
int ackermann(int m, int n) {
if (m == 0) {
return n + 1;
}
else if (m > 0 && n == 0) {
return ackermann(m - 1, 1);
}
else {
return ackermann(m - 1, ackermann(m, n - 1));
}
}
int main() {
int m, n;
printf("Enter values for m and n: ");
scanf("%d %d", &m, &n);
int result = ackermann(m, n);
printf("Ackermann(%d, %d) = %d\n", m, n, result);
return 0;
}
OUTPUT:
Enter values for m and n: 2 3
Ackermann(2, 3) = 9

5. Write a recursive function to find the sum of series.


#include <stdio.h>
int sumOfSeries(int n) {
if (n == 0) {
return 0;
} else {
return n + sumOfSeries(n - 1);
}
}
int main() {
int n;
printf("Enter the value of n: ");
scanf("%d", &n);
printf("Sum of the series 1 + 2 + 3 + ... + %d is: %d\n", n, sumOfSeries(n));

return 0;
}
OUTPUT:
Enter the value of n: 5
Sum of the series 1 + 2 + 3 + ... + 5 is: 15
WEEK – 13
1. Write a C program to swap two numbers using call by reference.
#include<stdio.h>
void swap(int *a, int *b) {
*a = *a + *b;
*b = *a - *b;
*a = *a - *b;
}
int main() {
int num1, num2;
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);
printf("Before swapping: num1 = %d, num2 = %d\n", num1, num2);
swap(&num1, &num2);
printf("After swapping: num1 = %d, num2 = %d\n", num1, num2);
return 0;
}
OUTPUT:
Enter two numbers: 10 20
Before swapping: num1 = 10, num2 = 20
After swapping: num1 = 20, num2 = 10

2. Demonstrate Dangling pointer problem using a C program.


#include <stdio.h>
#include <stdlib.h>
int* createDanglingPointer() {
int *ptr = (int*)malloc(sizeof(int));
*ptr = 42;
free(ptr);
return ptr;
}
int main() {
int *danglingPtr = createDanglingPointer();
printf("Accessing dangling pointer: %d\n", *danglingPtr);
danglingPtr = NULL;
return 0;
}
OUTPUT:
Accessing dangling pointer: 0

3. Write a C program to copy one string into another using pointer.


#include <stdio.h>
void copyString(char *dest, const char *src) {
while (*src != '\0')
{
*dest = *src;
dest++;
src++;
}
*dest = '\0';
}
int main() {
char source[100], destination[100];
printf("Enter a string: ");
scanf("%s", source);
copyString(destination, source);
printf("Copied String: %s\n", destination);
return 0;
}
OUTPUT:
Enter a string: kurnool
Copied String: Kurnool

4. Write a C program to find no of lowercase, uppercase, digits and


other characters using pointers.
#include <stdio.h>
void countCharacters(char *str, int *lower, int *upper, int *digits, int *others)
{
while (*str != '\0') {
if (*str >= 'a' && *str <= 'z') {
(*lower)++;
} else if (*str >= 'A' && *str <= 'Z') {
(*upper)++;
} else if (*str >= '0' && *str <= '9') {
(*digits)++;
} else {
(*others)++;
}
str++;
}
}
int main() {
char inputString[100];
int lower = 0, upper = 0, digits = 0, others = 0;
printf("Enter a string: ");
scanf("%s", inputString);
countCharacters(inputString, &lower, &upper, &digits, &others);
printf("Lowercase characters: %d\n", lower);
printf("Uppercase characters: %d\n", upper);
printf("Digits: %d\n", digits);
printf("Other characters: %d\n", others);
return 0;
}
OUTPUT:
Enter a string: HellO123World@#$
Lowercase characters: 7
Uppercase characters: 3
Digits: 3
Other characters: 3
14 - WEEK
1. Write a C program to write and read text into a file.

#include <stdio.h>
#include <stdlib.h>
int main() {
FILE *file;
char text[100];
char filename[] = "example.txt";
file = fopen(filename, "w");
printf("Enter text to write to the file : ");
scanf("%s", text);
fprintf(file, "%s", text);
fclose(file);
printf("Text successfully written to %s\n", filename);
file = fopen(filename, "r");
fscanf(file, "%s", text);
printf("Reading from file:\n%s", text);
fclose(file);
return 0;
}
OUTPUT:
Filename: example.txt
Enter text to write to the file : ravindracollege
Text successfully written to example.txt
Reading from file:
ravindracollege

2. Write a C program to write and read text into a binary file using
fread() and fwrite()
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
FILE *file;
char text[100];
char buffer[100];
char filename[] = "example.bin";
file = fopen(filename, "wb");
printf("Enter text to write to the binary file: ");
scanf("%s", text);
int textLength = strlen(text) ;
fwrite(text, sizeof(char), textLength, file);
fclose(file);
printf("Text successfully written to %s\n", filename);
file = fopen(filename, "rb");
fread(buffer, sizeof(char), textLength, file);
printf("Reading from binary file:\n%s", buffer);
fclose(file);
return 0;
}
OUTPUT:
Filename: example.bin
Enter text to write to the binary file: kurnool
Text successfully written to example.bin
Reading from binary file:
kurnool

3. Copy the contents of one file to another file.


#include <stdio.h>
int main() {
FILE *sourceFile, *destinationFile;
char text[100];

sourceFile = fopen("example.txt", "r");


destinationFile = fopen("destination.txt", "w");

if (sourceFile == NULL || destinationFile == NULL) {


printf("File opening failed.\n");
return 1;
}
while (fgets(text, sizeof(text), sourceFile) != NULL) {
fputs(text, destinationFile);
}
printf("File copied successfully.\n");

fclose(sourceFile);
fclose(destinationFile);
return 0;
}
OUTPUT:
File copied successfully.
4. Write a C program to merge two files into the third file using
command-line arguments.
#include <stdio.h>
int main(int argc, char *argv[]) {
if (argc != 4) {
printf("Usage: %s input_file1 input_file2 output_file\n", argv[0]);
return 1;
}
FILE *file1, *file2, *outputFile;
char text[100];
file1 = fopen(argv[1], "w");
fprintf(file1,"ravindra");
file2 = fopen(argv[2], "w");
fprintf(file2,"college");
file1 = fopen(argv[1], "r");
file2 = fopen(argv[2], "r");
outputFile = fopen(argv[3], "w");
if (file1 == NULL || file2 == NULL || outputFile == NULL) {
printf("File opening failed.\n");
return 1;
}
while (fgets(text, sizeof(text), file1) != NULL) {
fputs(text, outputFile);
}
while (fgets(text, sizeof(text), file2) != NULL) {
fputs(text, outputFile);
}
printf("Files merged successfully");
fclose(file1);
fclose(file2);
fclose(outputFile);
return 0;
}
OUTPUT:

5. Find no. of lines, words and characters in a file


#include <stdio.h>
#include <ctype.h>
int main() {
FILE *file;
char filename[100];
int lines = 0, words = 0, characters = 0;
int inWord = 0;
printf("Enter the filename: ");
scanf("%s", filename);
file = fopen(filename, "r");
if (file == NULL) {
printf("File opening failed.\n");
return 1;
}
int ch;
while ((ch = fgetc(file)) != EOF) {
characters++;
if (ch == '\n') {
lines++;
}
if (isspace(ch) && inWord) {
inWord = 0;
} else if (!isspace(ch) && !inWord) {
inWord = 1;
words++;
}
}
if (inWord) {
words++;
}
printf("Lines: %d\nWords: %d\nCharacters: %d\n", lines, words, characters);
fclose(file);
return 0;
}
OUTPUT:
Hello, this is a test file.
Enter the filename: example.txt
Lines: 1
Words: 6
Characters: 28

6. Write a C program to print last n characters of a given file.

#include <stdio.h>
int main() {
FILE *file;
char filename[100];
int n;
printf("Enter the filename: ");
scanf("%s", filename);
printf("Enter the number of characters to print from the end: ");
scanf("%d", &n);
file = fopen(filename, "r");
if (file == NULL) {
printf("File opening failed.\n");
return 1;
}

fseek(file, -n, SEEK_END);


int ch;
while ((ch = fgetc(file)) != EOF) {
putchar(ch);
}
fclose(file);
return 0;
}
OUTPUT:
Hello, this is a test file.
Enter the filename: example.txt
Enter the number of characters to print from the end: 10
test file.

You might also like