0% found this document useful (0 votes)
23 views

C Programming MCA File

The document contains the practical file of a lab course on principles of programming using C. It contains the weekly tasks assigned which include programs to find sum of digits, Fibonacci series, prime numbers, largest and smallest number in array, matrix addition and multiplication using functions, checking even, odd, vowel, consonant, positive and negative numbers, calculating sum and power using recursion, transposing a matrix, reading and manipulating complex numbers using structures, copying and modifying files.

Uploaded by

himansh786shri
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

C Programming MCA File

The document contains the practical file of a lab course on principles of programming using C. It contains the weekly tasks assigned which include programs to find sum of digits, Fibonacci series, prime numbers, largest and smallest number in array, matrix addition and multiplication using functions, checking even, odd, vowel, consonant, positive and negative numbers, calculating sum and power using recursion, transposing a matrix, reading and manipulating complex numbers using structures, copying and modifying files.

Uploaded by

himansh786shri
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

Session:- 2022-2023

Practical File
Of
Principles of Programming Using C Lab
(KCA – 151)

Submitted To Submitted By
Mr. Sonu Kumar PRAFUL PATHAK
(Assistant Professor) MCA – 1st Year
MCA Department

1|Page
INDEX
Week 1
A) Write a C program to find the sum of individual digits of a positive integer.
B) A Fibonacci sequence is defined as follows: the first and second terms in the
sequence are 0 and 1. Subsequent term are found by adding the preceding two
terms in the sequence. Write a C program to generate the first n terms of the
sequence.
C) Write a C program to generate all the prime numbers between 1 and n, where n
is a value supplied by the user.

Week 2
A) Write a C program to find the both the largest and smallest number in a list of
integers.
B) Write a C program that uses functions to perform the following:
1) Addition of Two Matrices 2) Multiplication of Two Matrices

Week 3
A) Write a C program to check weather a number is even or odd.
B) Write a C program to check weather a number is a Vowel or Consonant.
C) Write a C program to check weather a number is positive or negative.

Week 4
A) Write a C program to find the sum of natural numbers using Recursion.
B) Write a C program to Calculate the power using Recursion.
C) Write a C program to find Transpose of a matrix.

Week 5
A) Write a C program that users functions to perform the following:
1) Reading a Complex Number
2) Writing a Complex Number
3) Addition of two Complex Number
4) Multiplication of two Complex Number
(Note: Represent complex number using a structure)

2|Page
Week 6
A) Write a C program which copies one file to another.
B) Write a C program to reverse the first n characters in a file.

Week 7
C) Write a C program to generate Pascal’s Triangle.
D) Write a C program to Construct a Pyramid of numbers.

3|Page
Week 1
A) Write a C program to find the sum of individual digits of a positive integer.

Code:- #include<stdio.h>
#include<conio.h>
void main()
{
int n,sum=0;
Printf("enter a +ve integer");
Scanf("%d",&n);
while(n>0)
{
sum=sum+n%10; // sum + remainder value
n=n/10;
}
Printf(“sum of individual digits of a positive integer is %d”,sum);
getch();
}

Output:-
enter a +ve integer 456
sum of individual digits of a positive integer is 15

4|Page
B) A Fibonacci sequence is defined as follows: the first and second terms in the
sequence are 0 and 1. Subsequent term are found by adding the preceding two
terms in the sequence. Write a C program to generate the first n terms of the
sequence.

Code:-

#include <stdio.h>
#include<conio.h>
void main() {
int t1 = 0, t2 = 1, nextTerm = 0, n;
printf("Enter a positive number: ");
scanf("%d", &n);
printf("Fibonacci Series: %d, %d, ", t1, t2);
nextTerm = t1 + t2;
while (nextTerm <= n) {
printf("%d, ", nextTerm);
t1 = t2;
t2 = nextTerm;
nextTerm = t1 + t2;
}
getch();
}
Output:-
Enter a positive number: 100
Fibonacci Series: 0,1,1,2,3,5,8,13,21,34,55,89

5|Page
C) Write a C program to generate all the prime numbers between 1 and n, where n
is a value supplied by the user.

Code:-
#include<stdio.h>
#include<conio.h>
void main()
{
int n, i, j, count;
clrscr();
printf("Prime no.series\n");
printf("Enter any number\n");
scanf("%d", &n);
printf("The prime numbers between 1 to %d\n",n);
for(i = 1; i <= n; i++)
{
count = 0;
for(j = 1; j <=i; j++)
if(i % j == 0)
{
count++;
}
if(count == 2)
{
printf("%d\t", i);
}
}
getch();
}
Output:- Prime no. series
Enter any number 10
The prime numbers between 1 to 10
2,3,5,7

6|Page
Week 2
A) Write a C program to find the both the largest and smallest number in a list of
integers.

Code:-
#include<stdio.h>
#include<conio.h>
void main() {
int a[50],i,num,large,small;
printf("Enter the number of elements :");
scanf("%d",&num);
printf("Input the array elements : ");
for(i=0;i<num;++i)
scanf("%d",&a[i]);
large=small=a[0];
for(i=1;i<num;++i) {
if(a[i]>large)
large=a[i];
if(a[i]<small)
small=a[i];
}
printf("small= %d",small);
printf("large= %d",large);
getch();
}

Output:- Enter the number of elements : 8


Input the array elements : 1 2 6 4 8 9 3 9
Small = 1
Large = 9

7|Page
B) Write a C program that uses functions to perform the following:
1) Addition of Two Matrices 2) Multiplication of Two Matrices

1) Code:- #include <stdio.h>


#include<conio.h>
void main() {
int r, c, a[100][100], b[100][100], sum[100][100], i, j;
printf("Enter the number of rows (between 1 and 100): ");
scanf("%d", &r);
printf("Enter the number of columns (between 1 and 100): ");
scanf("%d", &c);
printf("\nEnter elements of 1st matrix:\n");
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {
printf("Enter element a%d%d: ", i + 1, j + 1);
scanf("%d", &a[i][j]);
}
printf("Enter elements of 2nd matrix:\n");
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {
printf("Enter element b%d%d: ", i + 1, j + 1);
scanf("%d", &b[i][j]);
}
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {
sum[i][j] = a[i][j] + b[i][j]; }
printf("\nSum of two matrices: \n");
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {
printf("%d ", sum[i][j]);
if (j == c - 1) {
printf("\n\n"); } }
getch();
}

8|Page
Output:- Enter the number of rows (between 1 and 100): 2

Enter the number of columns (between 1 and 100): 3

Enter elements of 1st matrix:


Enter element a11: 2
Enter element a12: 3
Enter element a13: 4
Enter element a21: 5
Enter element a22: 2
Enter element a23: 3

Enter elements of 2nd matrix:


Enter element b11: -4
Enter element b12: 5
Enter element b13: 3
Enter element b21: 5
Enter element b22: 6
Enter element b23: 3

Sum of two matrices:


-2 8 7

10 8 6

9|Page
2) Code:- #include<stdio.h>
#include<conio.h>
void main() {
int a[10][10], b[10][10], c[10][10], n, i, j, k;
printf("Enter the value of N (N <= 10): ");
scanf("%d", & n);
printf("Enter the elements of Matrix-A: \n");
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
scanf("%d", & a[i][j]); } }
printf("Enter the elements of Matrix-B: \n");
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
scanf("%d", & b[i][j]); } }
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
c[i][j] = 0;
for (k = 0; k < n; k++) {
c[i][j] += a[i][k] * b[k][j]; } } }
printf("The product of the two matrices is: \n");
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
printf("%d\t", c[i][j]); }
printf("\n"); }
getch();
}

10 | P a g e
Output:-
Enter the value of N (N <= 10): 2

Enter the elements of Matrix-A:


22
22

Enter the elements of Matrix-B:


22
22

Product of the two matrices is :


8 8
8 8

11 | P a g e
Week 3
A) Write a C program to check weather a number is even or odd.

Code:-
#include <stdio.h>
#include<conio.h>
void main() {
int num;
printf("Enter an integer: ");
scanf("%d", &num);
if(num % 2 == 0)
printf("%d is even.", num);
else
printf("%d is odd.", num);
getch();
}

Output:-
Enter an integer: -7
-7 is odd.

12 | P a g e
B) Write a C program to check weather a number is a Vowel or
Consonant.

Code:-

#include <stdio.h>
#include<conio.h>
void main()
{
char ch;
printf("Enter any character: ");
scanf("%c", &ch);
if(ch=='a' || ch=='e' || ch=='i' || ch=='o' || ch=='u' ||
ch=='A' || ch=='E' || ch=='I' || ch=='O' || ch=='U')
{
printf("'%c' is Vowel.", ch);
}
else if((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))
{
printf("'%c' is Consonant.", ch);
}
else
{
printf("'%c' is not an alphabet.", ch);
}
getch();
}
Output:-
Input:- Enter any character: a
Output:- a is vowel

13 | P a g e
C) Write a C program to check weather a number is positive or negative.

Code:-
#include <stdio.h>
#include<conio.h>
void main()
{
int num;
printf("Input a number :");
scanf("%d", &num);
if (num >= 0){
printf("%d is a positive number \n", num);
}
else {
printf("%d is a negative number \n", num);
}
getch();
}
Output:-
Input a number : 15
15 is a positive number

14 | P a g e
Week 4
A) Write a C program to find the sum of natural numbers using Recursion.

Code:-
#include<stdio.h>
#include<conio.h>
int sum(int num)
{
if(num)
return(num + sum(num-1));
else
return 0;
}
int main()
{
int count;
printf("Enter a positive no\n");
scanf("%d", &count);
printf("Sum of 1st %d natural numbers is %d\n", count,
sum(count));
return 0;
}

Output:-
Enter a positive no 5
Sum of 1st 5 natural number is 15

15 | P a g e
B) Write a C program to Calculate the power using Recursion.

Code:-
#include <stdio.h>
#include<conio.h>
int power(int n1, int n2);
int main() {
int base, a, result;
printf("Enter base number: ");
scanf("%d", &base);
printf("Enter power number(positive integer): ");
scanf("%d", &a);
result = power(base, a);
printf("%d^%d = %d", base, a, result);
return 0;
}
int power(int base, int a) {
if (a != 0)
return (base * power(base, a - 1));
else
return 1;
}

Output:-
Enter base number: 3
Enter power number(positive integer): 4
3^4 = 81

16 | P a g e
C) Write a C program to find Transpose of a matrix.

Code:-
#include <stdio.h>
#include<conio.h>
void main() {
int a[10][10], transpose[10][10], r, c;
printf("Enter rows and columns: ");
scanf("%d %d", &r, &c);
printf("\nEnter matrix elements:\n");
for (int i = 0; i < r; ++i)
for (int j = 0; j < c; ++j) {
printf("Enter element a%d%d: ", i + 1, j + 1);
scanf("%d", &a[i][j]);
}
printf("\nEntered matrix: \n");
for (int i = 0; i < r; ++i)
for (int j = 0; j < c; ++j) {
printf("%d ", a[i][j]);
if (j == c - 1)
printf("\n");
}
for (int i = 0; i < r; ++i)
for (int j = 0; j < c; ++j) {
transpose[j][i] = a[i][j];
}
printf("\nTranspose of the matrix:\n");
for (int i = 0; i < c; ++i)
for (int j = 0; j < r; ++j) {
printf("%d ", transpose[i][j]);
if (j == r - 1)
printf("\n");
}
getch();
}

17 | P a g e
Output:-
Enter rows and columns: 2 3

Enter matrix elements:


Enter element a11: 1
Enter element a12: 4
Enter element a13: 0
Enter element a21: -5
Enter element a22: 2
Enter element a23: 7

Entered matrix:
1 4 0
-5 2 7

Transpose of the matrix:


1 -5
4 2
0 7

18 | P a g e
Week 5
Write a C program that users functions to perform the following:
5) Reading a Complex Number
6) Writing a Complex Number
7) Addition of two Complex Number
8) Multiplication of two Complex Number
(Note: Represent complex number using a structure)

Code:-
#include <stdio.h>
#include <conio.h>
struct complex
{
float real, imag;
}a, b, c;
struct complex read(void);
void write(struct complex);
struct complex add(struct complex, struct complex);
struct complex sub(struct complex, struct complex);
struct complex mul(struct complex, struct complex);
struct complex div(struct complex, struct complex);
void main ()
{
clrscr();
printf("Enter the 1st complex number\n ");
a = read();
write(a);
printf("Enter the 2nd complex number\n");
b = read();
write(b);
printf("Addition\n ");
c = add(a, b);
write(c);
printf("Substraction\n ");

19 | P a g e
c = sub(a, b);
write(c);
printf("Multiplication\n");
c = mul(a, b);
write(c);
printf("Division\n");
c = div(a, b);
write(c);
getch();
}
struct complex read(void)
{
struct complex t;
printf("Enter the real part\n");
scanf("%f", &t.real);
printf("Enter the imaginary part\n");
scanf("%f", &t.imag);
return t;
}
void write(struct complex a)
{
printf("Complex number is\n");
printf(" %.1f + i %.1f", a.real, a.imag);
printf("\n");
}
struct complex add(struct complex p, struct complex q)
{
struct complex t;
t.real = (p.real + q.real);
t.imag = (p.imag + q.imag);
return t;
}
struct complex sub(struct complex p, struct complex q)
{
struct complex t;
t.real = (p.real - q.real);

20 | P a g e
t.imag = (p.imag - q.imag);
return t;
}
struct complex mul(struct complex p, struct complex q)
{
struct complex t;
t.real=(p.real * q.real) - (p.imag * q.imag);
t.imag=(p.real * q.imag) + (p.imag * q.real);
return t;
}
struct complex div(struct complex p, struct complex q)
{
struct complex t;
t.real = ((p.imag * q.real) - (p.real * q.imag)) / ((q.real * q.real) +
(q.imag * q.imag));
t.imag = ((p.real * q.real) + (p.imag * q.imag)) / ((q.real * q.real) +
(q.imag * q.imag));
return(t);
}

Output:-
Enter the real part
2
Enter the imaginary part
4
Complex number is
2.0 + i4.0

Enter the real part


4
Enter the imaginary part
2
Complex number is
4.0 + i2.0

21 | P a g e
Addition
Complex number is
6.0 + i6.0

Subtraction
Complex number is
-2.0 + i2.0

Multiplication
Complex number is
0.0 + i20.0

Division
Complex number is
0.6 + i0.8

22 | P a g e
Week 6
A) Write a C program which copies one file to another.

Code:-

#include <stdio.h>
#include <stdlib.h>
void main()
{
FILE *fptr1, *fptr2;
char filename[100], c;
printf("Enter the filename to open for reading \n");
scanf("%s", filename);
fptr1 = fopen(filename, "r");
if (fptr1 == NULL)
{
printf("Cannot open file %s \n", filename);
exit(0); }
printf("Enter the filename to open for writing \n");
scanf("%s", filename);
fptr2 = fopen(filename, "w");
if (fptr2 == NULL) {
printf("Cannot open file %s \n", filename);
exit(0); }
c = fgetc(fptr1);
while (c != EOF) {
fputc(c, fptr2);
c = fgetc(fptr1); }
printf("\nContents copied to %s", filename);
fclose(fptr1);
fclose(fptr2);
getch();
}

23 | P a g e
Output:-
Enter the filename to open for reading
a.txt

Enter the filename to open for writing


b.txt

Contents Copied to b.txt

24 | P a g e
B) Write a C program to reverse the first n character in a file.
(Note: The File name and n are specified on the command line)

Code:- #include<stdio.h >


#include<conio.h >
#include<string.h >
#include<process.h >
void main(int argc, char *argv[]) {
FILE *fs, *fd;
char s[20], d[20];
int c = 0, count = 0, n; clrscr();
strcpy(s, argv[1]);
n = atoi(argv[2]);
fs = fopen(s, "r");
if(s == NULL)
printf("\n FILE ERROR");
printf("\n SOURCE FILE :\n");
while(!feof(fs)) {
printf("%c", fgetc(fs));
c++; }
fclose(fs);
fs = fopen(s, "r+");
count = 0;
while(count < n) {
d[count] = fgetc(fs);
count++; }
d[count] = '\0';
fseek(fs, 0L, 0);
fputs(strrev(d), fs);
fclose(fs);
fs = fopen(s,"r");
while(!feof(fs)) {
printf(“%c”, fgetc(fs));
c++; }
fclose(fs);
getch();
}

25 | P a g e
Week 7
A) Write a C program to generate Pascal’s Triangle.

Code:- #include < stdio.h >


#include<conio.h>
long factorial(int);
void main()
{
int i, n, c;
printf("Enter the number of rows you wish to see in pascal triangle\n");
scanf("%d", & n);
for (i = 0; i < n; i++) {
for (c = 0; c <= (n - i - 2); c++) printf(" ");
for (c = 0; c <= i; c++) printf("%ld ", factorial(i) / (factorial(c) *
factorial(i - c)));
printf("\n"); }
getch(); }
long factorial(int n) {
int c;
long result = 1;
for (c = 1; c <= n; c++) result = result * c;
return result; }

Output:-

Enter the number of rows you wish to see in pascal triangle

1
11
121
1331
14641

26 | P a g e
B) Write a C program to construct a pyramid of numbers.

Code:-
#include <stdio.h>
#include<conio.h>
void main()
{
int rows, space, i, j;
printf("Enter number of rows: ");
scanf("%d",&rows);
for(i=0; i<rows; i++)
{
for(space=1; space <= rows-i; space++)
printf(" ");
for(j=0-i; j <= i; j++) {
printf("%2d",abs(j));
}
printf("\n");
}}

Output:-
0
1 0 1
2 1 0 1 2
3 2 1 0 1 2 3
4 3 2 1 0 1 2 3 4

27 | P a g e

You might also like