PART B
1. Program to find the length of a string without using built in function
2. Program to demonstrate string functions.
3. Program to demonstrate pointers in C
4. Program to check a number for prime by defining isprime( ) function
5. Program to read, display and to find the trace of a square matrix
6. Program to read, display and add two m x n matrices using functions
7. Program to read, display and multiply two m x n matrices using functions
8. Program to read a string and to find the number of alphabets, digits, vowels,
consonants, spaces and special characters.
9. Program to Reverse a String using Pointer
10. Program to Swap Two Numbers using Pointers
11. Program to demonstrate student structure to read & display records of n students.
12. Program to demonstrate the difference between structure & union.
Note: Student has to execute a minimum of 10 programs in each part to complete the
Lab course
Assessment Criteria Marks
Program – 1 from Part B Flowchart / Algorithm 02
Writing the Program 05
Execution and Formatting 08
Program – 2 from Part B Flowchart / Algorithm 02
Writing the Program 05
Execution and Formatting 08
Viva Voice based on C Programming 05
Practical Record 05
Total 40
Programming in C I Sem BCA
PART B
1. Program to find the length of a string without using built in function
#include<stdio.h>
#include<conio.h>
void main()
{
char string[50];
int i, length = 0;
clrscr();
printf ("Enter a string \n");
gets (string);
/* keep going through each character of the string till its end */
for (i = 0; string[i] != '\0'; i++)
{
length++;
}
printf ("The length of a string is the number of characters in it \n");
printf ("So, the length of %s = %d\n", string, length);
getch();
}
Programming in C I Sem BCA
2. Program to demonstrate string functions
#include<stdio.h>
#include<string.h>
#include<conio.h>
void main()
{
char name[20]; /* String Declaration*/
clrscr();
printf ("Enter your name:\n");
scanf ("%s", name);
printf ("\n%s", name); /*Displaying String*/
getch();
}
Programming in C I Sem BCA
3. Program to demonstrate pointers in C
#include<stdio.h>
#include<conio.h>
void main()
{
int num = 10;
clrscr();
printf ("Value of variable num is: %d", num);
/* To print the address of a variable we use %p format specifier and ampersand
(&) sign just before the variable name like &num.*/
printf ("\nAddress of variable num is: %p", &num);
getch();
}
Programming in C I Sem BCA
4. Program to check a number for prime by defining isprime( ) function
#include<stdio.h>
#include<conio.h>
void main()
{
int num, res=0;
clrscr();
printf ("\nEnter a Number:\n");
scanf ("%d", &num);
res = isprime(num);
if (res == 0)
printf ("\n%d is a Prime Number", num);
else
printf ("\n%d is not a Prime Number", num);
getch();
}
int isprime(int n)
{
int i;
for (i = 2; i <= n/2; i++)
{
If (n % i != 0)
continue;
else
return 1;
}
return 0;
}
Programming in C I Sem BCA
5. Program to read, display and to find the trace of a square matrix
#include<stdio.h>
#include<conio.h>
void main()
{
int a[5][5], r, c, i, j, sum=0;
clrscr();
printf ("Enter a number of rows and columns:-\n");
scanf ("%d %d", &r, &c);
if (r == c)
{
printf ("\nEnter %d*%d elements in matrix:\n", r, c);
for (i=0; i<r; i++)
{
for (j=0; j<c; j++)
scanf ("%d", &a[i][j]);
}
for (i=0; i<r; i++)
sum = sum + a[i][i];
printf ("\nTrace of the matrix = %d", sum);
}
else
printf ("Not a square matrix. It is not possible to find trace.");
getch();
}
Programming in C I Sem BCA
6. Program to read, display and add two m x n matrices using functions
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10][10], b[10][10], sum[10][10], i, j, m, n;
clrscr();
printf ("Enter the number and rows of matrix\n");
scanf ("%d %d", &m, &n);
//first matrix
printf ("Enter the elements for A Matrix\n");
for (i=0; i<m ;i++)
{
for (j=0; j<n; j++)
{
scanf ("%d", &a[i][j]);
}
}
//second matrix
printf ("Enter the elements for B Matrix\n");
for (i=0; i<m; i++)
{
for (j=0; j<n; j++)
{
scanf ("%d", &b[i][j]);
}
}
Programming in C I Sem BCA
//sum of two matrix elements
printf ("Sum of Matrix A & B are\n");
for (i=0; i<m; i++)
{
for (j=0; j<n; j++)
{
sum[i][j] = a[i][j] + b[i][j];
printf ("%d\t", sum[i][j]);
}
printf ("\n");
}
getch();
}
Programming in C I Sem BCA
7. Program to read, display and multiply two m x n matrices using functions
#include<stdio.h>
#include<conio.h>
void main()
{
int m, n, p, q, c, d, k, sum = 0;
int first[10][10], second[10][10], multiply[10][10];
clrscr();
printf ("Enter number of rows and columns of first matrix\n");
scanf ("%d %d", &m, &n);
printf ("Enter elements of first matrix\n");
for (c = 0; c < m; c++)
for (d = 0; d < n; d++)
scanf ("%d", &first[c][d]);
printf ("Enter number of rows and columns of second matrix\n");
scanf ("%d%d", &p, &q);
if (n != p)
printf ("The multiplication isn't possible.\n");
else
{
printf ("Enter elements of second matrix\n");
for (c = 0; c < p; c++)
for (d = 0; d < q; d++)
scanf ("%d", &second[c][d]);
Programming in C I Sem BCA
for (c = 0; c < m; c++)
{
for (d = 0; d < q; d++)
{
for (k = 0; k < p; k++)
{
sum = sum + first[c][k] * second[k][d];
}
multiply[c][d] = sum;
sum = 0;
}
}
printf ("Product of the matrices:\n");
for (c = 0; c < m; c++)
{
for (d = 0; d < q; d++)
printf ("%d\t", multiply[c][d]);
printf ("\n");
}
}
getch();
}
Programming in C I Sem BCA
8. Program to read a string and to find the number of alphabets, digits, vowels,
consonants, spaces and special characters.
#include <stdio.h>
#include <conio.h>
void main()
{
char str[200];
int i, vowels=0,consonants=0,digits=0,spaces=0,alphabets=0, specialCharacters=0;
clrscr();
printf ("Enter a string\n");
gets(str);
for(i=0; str[i]!='\0'; i++)
{
if (str[i] == 'a' || str[i] == 'e' || str[i] == 'i' || str[i] == 'o' || str[i] == 'u' || str[i]== 'A' || str[i]==
'E' || str[i]== 'I' || str[i]== 'O' || str[i]== 'U')
{
vowels++;
}
else if (( str[i] >= 'a' && str[i] <= 'z') || (str[i] >= 'A' && str[i] <= 'Z'))
{
consonants++;
}
else if (str[i] >= '0' && str[i] <= '9')
{
digits++;
}
Programming in C I Sem BCA
else if (str[i] == ' ')
{
spaces++;
}
else
{
specialCharacters++;
}
if ((str[i] >= 65 && str[i] <= 90 || str[i] >=97 && str[i] <= 122))
{
alphabets++;
}
}
printf ("\nVowels = %d", vowels);
printf ("\nConsonants = %d", consonants);
printf ("\nDigits = %d", digits);
printf ("\nWhite spaces = %d", spaces);
printf ("\nSpecial characters = %d", specialCharacters);
printf ("\nAlphabets = %d", alphabets);
getch();
}
Programming in C I Sem BCA
9. Program to Reverse a String using Pointer
#include <stdio.h>
#include <conio.h>
void main()
{
char *s;
int len, i;
clrscr();
printf ("\nEnter a String: ");
gets(s);
len = strlen(s);
printf ("\n The Reverse Of The String Is:");
for(i = len; i >= 0; i--)
printf ("%c",*(s+i));
getch();
}
Programming in C I Sem BCA
10. Program to Swap Two Numbers using Pointers
#include<stdio.h>
#include<conio.h>
void main()
{
int x, y, *a, *b, temp;
clrscr();
printf ("Enter the value of x and y\n");
scanf ("%d %d", &x, &y);
printf ("Before Swapping\n x = %d\n y = %d\n", x, y);
a = &x;
b = &y;
temp = *b;
*b = *a;
*a = temp;
printf("After Swapping\n x = %d\n y = %d\n", x, y);
getch();
}
Programming in C I Sem BCA
11.Program to demonstrate student structure to read & display records of n
students.
#include<stdio.h>
#include<conio.h>
struct student
{
char firstName[50];
int roll;
float marks;
} s[5];
void main()
{
int i;
clrscr();
printf ("Enter information of students:\n");
// storing information
for (i = 0; i < 5; ++i)
{
s[i].roll = i + 1;
printf ("\nFor roll number %d,\n", s[i].roll);
printf ("Enter first name: ");
scanf ("%s", s[i].firstName);
printf ("Enter marks: ");
scanf ("%f", &s[i].marks);
}
Programming in C I Sem BCA
printf ("Displaying Information:\n\n");
// displaying information
for (i = 0; i < 5; ++i)
{
printf ("\nRoll number: %d\n", i + 1);
printf ("First name: ");
puts (s[i].firstName);
printf ("Marks: %.1f", s[i].marks);
printf ("\n");
}
getch();
}
Programming in C I Sem BCA