# BCA I manual on C programming

```GOVERNMENT FIRST GRADE COLLEGE
RAICHUR.
DEPARTMENT OF COMPUTER SCIENCE
Certificate
Reg No:
Date:
This is to certify that has satisfactorily completed the course of
practical in “CACO2P-Practical I-C-Programming Lab“ as prescribed
by Raichur University, Raichur for BCA I semester, in the lab of
Department of computer science during the academic year 2021-22.
STAFF INCHARGE
(Smt.Ishrat Begum)
Examiners:
1.
2.
Programming in C For BCA Ist Sem
INDEX
SI.No
Program Name
Page No
PART – A
01
C Program to find area and circumference of circle
02
C Program to find greatest in 3 numbers
03
04
C program to demonstrate the library functions in
math.h
C program to check for prime
05
C program to generate prime numbers upto n
06
C Program to read a number ,find the sum of
digits,reverse the number and check for palindrome
07
C program to read numbers from keyboard
continuosly till the user presses 999 and to find the
sum of only positive numbers.
08
C program to Find the Percentage or marks of
students. This C program is an example to use
Control statements.
C program to find the roots of a quadratic equation
09
10
C Program to Delete Duplicate Elements from an
Array
11
Program to read marks scored by N students and
find the average of marks (single dimension Array)
Program to perform addition &amp; subtraction of
Matrices
PART -B
12
01
Program to find length of a string without using
built in functions.
02
03
04
Program to demonstrate STRING Functions
Program to demonstrate Pointers in C
Program to Check a number for prime by defining
isprime() function
Program to read, display &amp;to find the trace of a
square matrix.
05
06
using functions
07
Program to read,display and multiply two m x n
matrices using functions
08
Program to read and to find the number of
alphabets,digits,vowels,consonants,spaces
,special characters
09
Program to Reverse a string using pointers
GFGC RAICHUR
Remark
Programming in C For BCA Ist Sem
10
Programs to Swap Two Numbers using pointer
11
12
Part-A
1] Area and circumference of circle
Algorithm for area and circumference of circle
Step1: start
Step3: calculate
Step4: calculate
Step5: display area and circumference of circle
Step6: stop
GFGC RAICHUR
Programming in C For BCA Ist Sem
Flowchart for area and circumference of circle
start
Display area and
circumference of
circle
stop
GFGC RAICHUR
Programming in C For BCA Ist Sem
1. C Program to find area and circumference of circle
#include&lt;stdio.h&gt;
int main() {
float PI = 3.14, area, ci;
printf(&quot;\nArea of circle : %f &quot;, area);
ci = 2 * PI * rad;
printf(&quot;\nCircumference : %f &quot;, ci);
return (0);
}
Output:
Enter radius of a circle : 1
Area of circle : 3.14
Circumference : 6.28
GFGC RAICHUR
Programming in C For BCA Ist Sem
2] Find greatest number in 3 numbers
Algorithm for find greatest number in 3 numbers
Step1: start
Step2: input a,b,c
Step3: if((a&gt;b)&amp;&amp;(a&gt;c))
Print a is greatest
if ((b&gt;c&amp;&amp;(b&gt;a))
print b is greatest
if ((c&gt;a)&amp;&amp;(c&gt;b))
print c is greatest
Step4: stop
GFGC RAICHUR
Programming in C For BCA Ist Sem
Flowchart to find greatest in 3 numbers
start
Input a,b,c
no
If ((a&gt;b)&amp;&amp;(a&gt;c))
yes
If
((a&gt;b)&amp;&amp;(b&gt;a))
Print a is greatest
no
yes
If ((c&gt;a)&amp;&amp;(c&gt;b))
P&yen;&Agrave;pprint
yes
Print c is Preatest
stop
GFGC RAICHUR
Programming in C For BCA Ist Sem
2. C Program to find greatest in 3 numbers
#include&lt;stdio.h&gt;
int main() {
int a, b, c;
printf(&quot;\nEnter value of a, b &amp; c : &quot;);
scanf(&quot;%d %d %d&quot;, &amp;a, &amp;b, &amp;c);
if ((a &gt; b) &amp;&amp; (a &gt; c))
printf(&quot;\na is greatest&quot;);
if ((b &gt; c) &amp;&amp; (b &gt; a))
printf(&quot;\nb is greatest&quot;);
if ((c &gt; a) &amp;&amp; (c &gt; b))
printf(&quot;\nc is greatest&quot;);
return(0);
}
OUTPUT
Enter value for a,b &amp; c : 15 17 21
c is greatest
GFGC RAICHUR
Programming in C For BCA Ist Sem
3] Demonstrate the library functions in math.h
Algorithm for demonstrate the library functions in math.h
Step1: start
Step2: input dvaluex=5.0, dvaluey=5.0
Step3: calculate
result=pow(dvaluex,dvaluey)
Step4: display result
Step5: stop
GFGC RAICHUR
Programming in C For BCA Ist Sem
Flowchart for demonstrate the library functions in math.h
start
Input dvaluex=5.0
dvaluey=5.0
Calculate
result=pow(dvaluex,dvaluey)
Display result
stop
GFGC RAICHUR
Programming in C For BCA Ist Sem
3. C program to demonstrate the library functions in math.h
#include &lt;stdio.h&gt;
#include &lt;math.h&gt;
int main(void)
{
double dValuex = 5.0, dValuey = 5.0;
double dResult;
result = pow(dValuex, dValuey);
printf(&quot;%lf power %lf is: %lf\n&quot;, dValuex, dValuey, dResult);
return 0;
}
Output:
5.000000 power 5.000000 is: 3125.000000
GFGC RAICHUR
Programming in C For BCA Ist Sem
4] Check for prime number
Algorithm :- check for prime number
Step1: start
Step2: Read a “n” value to check prime or not
Step3: set i=1, count=0.
Step4: if &lt;=n if true go to step 5, else go to step 8.
Step5: check the condition n%i==0 if true evaluate step 6, false go to step 7.
Step6: set count =count+1.
Step7: i=i+1 go to step 4.
Step8: check count, if count =2 display prime, if not display it is not prime
Step9: stop
GFGC RAICHUR
Programming in C For BCA Ist Sem
Flowchart :- check for prime number
start
N=5
Set i=1
Count=0
no
i &lt;=n
yes
yes
yes
n% ==0
Count
==2
Display not
prime
Display n
Is prime
Count=count+1
no
i=i+1
stop
GFGC RAICHUR
Programming in C For BCA Ist Sem
C program to check for prime.
#include&lt;stdio.h&gt;
int main() {
int num, i, count = 0;
printf(&quot;Enter a number:&quot;);
scanf(&quot;%d&quot;, &amp;num);
for (i = 2; i &lt;= num / 2; i++)
{
if (num % i == 0)
{
count++;
break;
}
}
if (count == 0)
printf(&quot;%d is a prime number&quot;, num);
else
printf(&quot;%d is not a prime number&quot;, num);
return 0;
}
Output:
Enter a number : 4
4 is not a prime number
Enter a number : 2
2 is a prime number
GFGC RAICHUR
Programming in C For BCA Ist Sem
5. Algorithm and flowchart to generate prime number upto
n.
Step-1: start
Step-2: input j, k
Step-3: i=2
Step-4: if(i&lt;=n) if true
Then i++
Else goto step-5
Step-5: c=0 , j=1
if (j&lt;=i) if true
Then j++
Else go to step-6
Step-6: check the condition i%j==0
Print c++
Else goto step-7
Step-7: if(c==2) if true
Print i
Else goto step-8
Step-8: stop.
GFGC RAICHUR
Programming in C For BCA Ist Sem
Flowchart:start
Input j,k
I=2
No
If(j&lt;=n)
No
If(c==2)
Yes
C=o
J=1
yes
Print i
If(j&lt;=n)
yes
If(i%j==0)
Yes
C++
stop
GFGC RAICHUR
Programming in C For BCA Ist Sem
C program to generate prime numbers upto n
void main()
{
int i,j,n;
printf(&quot;Enter the number till which you want prime numbers\n&quot;);
scanf(&quot;%d&quot;,&amp;n);
printf(&quot;Prime numbers are:-\n&quot;);
for(i=2;i&lt;=n;i++)
{
int c=0;
for(j=1;j&lt;=i;j++)
{
if(i%j==0)
{
c++;
}
}
if(c==2)
{
printf(&quot;%d &quot;,i);
}
}
}
Output:
Enter the numbers till which you want prime numbers 25
Prime numbers are:23571113171923
GFGC RAICHUR
Programming in C For BCA Ist Sem
6] Algorithm and flowchart to read a number find the sum
of digits, reverse the number and check for palindrome.
Algorithm:
Step-1: start
Step-2: input n
Step-3: reversed=0
Original=n
Step-4: while(n!=0)
Then remainder=n%10
Reversed=reversed*10+remainder
n/=10
step-5: if(original==reversed)
print original is palindrome
else
print original is not palindrome
step-6: stop
GFGC RAICHUR
Programming in C For BCA Ist Sem
Flowchart:start
Input n
Reversed=0
Original=n
No
While
(n!=0)
yes
Remainder=n%10
Reversed=reversed*10+remainder
n/=10
No
If(Original==
reversed)
Print original is
not palindrome
yes
Print original is
palindrome
stop
GFGC RAICHUR
Programming in C For BCA Ist Sem
C Program to read a number ,find the sum of digits,reverse the
number and check for palindrome
#include &lt;stdio.h&gt;
int main() {
int n, reversed = 0, remainder, original;
printf(&quot;Enter an integer: &quot;);
scanf(&quot;%d&quot;, &amp;n);
original = n;
// reversed integer is stored in reversed variable
while (n != 0) {
remainder = n % 10;
reversed = reversed * 10 + remainder;
n /= 10;
}
// palindrome if orignal and reversed are equal
if (original == reversed)
printf(&quot;%d is a palindrome.&quot;, original);
else
printf(&quot;%d is not a palindrome.&quot;, original);
return 0;
}
Output
Enter an integer: 1001
1001 is a palindrome.
GFGC RAICHUR
Programming in C For BCA Ist Sem
7] Read numbers from keyboard countinuously till the
user presses 999 and to find the sum of only positive
numbers
Algorithm:
Step1: start
Step2: input num=1,pos=-1,
Neg=0, zero=0
Step3: while (num!=999) then
Pos+=num&gt;0
neg+=num&lt;0
zero+=num==0
print pos,neg,zero
GFGC RAICHUR
Programming in C For BCA Ist Sem
Flowchart:-
start
Num=1, pos=-1, neg=0,
zero=0
yes
While
num!=999
no
Pos+=num&gt;0
neg+=num&lt;0
zero+=num==0
Print pos, neg, zero
stop
GFGC RAICHUR
Programming in C For BCA Ist Sem
C program to read numbers from keyboard continuosly till the
user presses 999 and to find the sum of only positive numbers.
#include &lt;stdio.h&gt;
int main()
{
long num = 1, pos = -1, neg = 0, zero = 0;
while(num != 999) {
pos += num &gt; 0;
neg += num &lt; 0;
zero += num == 0;
scanf(&quot;%d&quot;, &amp;num);
}
printf(&quot;%d %d %d&quot;, pos, neg, zero);
return 0;
}
Output:
33
12
6
74
0
999
400
GFGC RAICHUR
Programming in C For BCA Ist Sem
8]Algorithm and flowchart to find the percentage or
marks of students.
Step-1: start
Step-2: input m1, m2, m3
Step-3: calculate
Total=m1+m2+m3
Per=total*100/300
Step-4: if(per&gt;=60&amp;&amp;per&lt;=100) if true
Then print you are 1st
Else if (per&gt;=50&amp;&amp;per&lt;=60) if true
Then print you are 2nd
Else if (per&gt;=40&amp;&amp;per&lt;=50) if true
Then print you are 3rd
Else
Print you are fail
Step-5: stop
GFGC RAICHUR
Programming in C For BCA Ist Sem
Flowchart:start
Input m1, m2,
m3
Calculate
Total=m1+m2+m3
Per=total*100/300
If(per&gt;=60
&amp;&amp;per&lt;=1
00
yes
Print you are
1st
If(per&gt;=60
&amp;&amp;per&lt;=1
00
else if
(per&gt;=40&amp;&amp;
per&lt;=50)
Print you are
2nd
Print you are
3rd
Print you
are fail
stop
GFGC RAICHUR
Programming in C For BCA Ist Sem
C program to Find the Percentage or marks of students. (Else –if
#include&lt;stdio.h&gt;
#include&lt;conio.h&gt;
void main()
{
int m1,m2,m3,total;
float per;
clrscr();
printf(&quot;Enter 3 Nos.&quot;);
scanf(&quot;%D%D%D&quot;,&amp;m1,&amp;m2,&amp;m3);
total=m1+m2+m3;
per=total*100/300;
if(per&gt;=60&amp;&amp;per&lt;=100)
printf(&quot;You are 1st :&quot;);
else if(per&gt;=50&amp;&amp;per&lt;=60)
printf(&quot;You are 2nd&quot;);
else if(per&gt;=40&amp;&amp;per&lt;=50)
printf(&quot;You are 3rd&quot;);
else
printf(&quot;You are Fail&quot;);
getch();
}
output:
Enter 3 Nos
40
50
60
You are 2nd
GFGC RAICHUR
Programming in C For BCA Ist Sem
9. Algorithm and flowchart to find the roots of a
Step-1:Start
Step-2:input a,b,c
Step-3:dis=b2-4a*c
Step-4:if(discM&lt;0) then write roots are imaginary
Stop
Endif
Step-5: if (disc==0)then write roots are real and equal
X1=-b/(2*a)
Write roots are x1,x1
Stop
Endif
Step-6: if (dis&gt;0)
Write roots are real and unequal
X1=(-b+sqrt (disc))/(2*a)
X2=(-b+sqrt (disc))/(2*a)
Write roots are X1,X2
Stop
Endif
Step-7: Stop
GFGC RAICHUR
Programming in C For BCA Ist Sem
Flowchart:Start
Count=c
Sum=0
Input a,b,c
Disc=b*b-4.0*a*c
Write roots
Are imaginary
?
Write roots are
real and equal
Dis c
Write roots are
real and equal
X1=(-b+sqrt(disc))/(2*a)
X2=(-b-sqrt(disc))/(2*a)
Stop
GFGC RAICHUR
X1=-b1(2*a)
Roots are
X1,X1
Programming in C For BCA Ist Sem
C program to find the roots of a quadratic equation
#include &lt;stdio.h&gt;
#include &lt;math.h&gt; /* Used for sqrt() */
int main()
{
float a, b, c;
float root1, root2, imaginary;
float discriminant;
printf(&quot;Enter values of a, b, c of quadratic equation (aX^2 + bX + c): &quot;);
scanf(&quot;%f%f%f&quot;, &amp;a, &amp;b, &amp;c);
discriminant = (b * b) - (4 * a * c);
switch(discriminant &gt; 0)
{
case 1:
/* If discriminant is positive */
root1 = (-b + sqrt(discriminant)) / (2 * a);
root2 = (-b - sqrt(discriminant)) / (2 * a);
printf(&quot;Two distinct and real roots exists: %.2f and %.2f&quot;,
root1, root2);
break;
case 0:
/* If discriminant is not positive */
switch(discriminant &lt; 0)
{
case 1:
GFGC RAICHUR
Programming in C For BCA Ist Sem
/* If discriminant is negative */
root1 = root2 = -b / (2 * a);
imaginary = sqrt(-discriminant) / (2 * a);
printf(&quot;Two distinct complex roots exists: %.2f + i%.2f and %.2f - i%.2f&quot;,
root1, imaginary, root2, imaginary);
break;
case 0:
/* If discriminant is zero */
root1 = root2 = -b / (2 * a);
printf(&quot;Two equal and real roots exists: %.2f and %.2f&quot;, root1,
root2);
break;
}
}
return 0;
}
Output
Enter values of a, b, c of quadratic equation (aX^2 + bX + c): 4 -2 -10
Two distinct and real roots exists: 1.85 and -1.35
GFGC RAICHUR
Programming in C For BCA Ist Sem
10]Algorithm and flowchart to read marks scored by
N students and find the average of marks.(single
dimension array).
GFGC RAICHUR
Programming in C For BCA Ist Sem
C Program to read marks scored by N students and
find the average of marks.(single dimension array).
#include &lt;stdio.h&gt;
int main()
{
int i, num;
float total = 0.0, average;
printf (&quot;Enter the number of students \n&quot;);
scanf(&quot;%d&quot;, &amp;num);
int array[num];
printf(&quot;Enter %d students marks \n&quot;, num);
for (i = 0; i &lt; num; i++)
{
scanf(&quot;%d&quot;, &amp;array[i]);
}
printf(&quot;Input Marks as array elements \n&quot;);
for (i = 0; i &lt; num; i++)
{
printf(&quot;%+3d\n&quot;, array[i]);
}
for (i = 0; i &lt; num; i++)
{
total+=array[i];
}
average = total / num;
printf(&quot;\n Sum of all marks = %.2f\n&quot;, total);
printf(&quot;\n Average of all input marks= %.2f\n&quot;, average);
}
Enter the number of students
5
Enter 5 students marks
10
20
30
40
50
GFGC RAICHUR
Programming in C For BCA Ist Sem
Input students marks as array elements
10
20
30
40
50
Sum of all numbers = 150
Average of all input numbers = 30
GFGC RAICHUR
Programming in C For BCA Ist Sem
11] Algorithm and flowchart to delete duplicate
elements from an array
Algorithm:
Step-1: start
Step-2: input size
Step-3: for(i=0;i&lt;size;i++)
Step-4: display arr[i]
Step-5: for(i=0;i&lt;size;i++)
Step-6: for(j=0;j&lt;size;j++)
Step-7: if(arr[i]==arr[j])
Step-8: for(k=j;k&lt;size;k++)
Step-9: arr[k]=arr[k+1]
Step-10: size—
j—
step-11: stop.
GFGC RAICHUR
Programming in C For BCA Ist Sem
Flowchart:
start
Input size
i=0;i&lt;size;i++
Arr[i]
i=0;i&lt;size;i++
j=0;j&lt;size;j++
A
GFGC RAICHUR
Programming in C For BCA Ist Sem
A
Arr[i]==arr[j]
k=j;k&lt;size;k++
Arr[k]=arr[k+1]
Size--
j--
1
stop
GFGC RAICHUR
Programming in C For BCA Ist Sem
11.C Program to Delete Duplicate Elements from an Array
#include &lt;stdio.h&gt;
int main()
{
int arr[10], i, j, k, Size;
printf(&quot;\n Please Enter Number of elements in an array : &quot;);
scanf(&quot;%d&quot;, &amp;Size);
printf(&quot;\n Please Enter %d elements of an Array \n&quot;, Size);
for (i = 0; i &lt; Size; i++)
{
scanf(&quot;%d&quot;, &amp;arr[i]);
}
for (i = 0; i &lt; Size; i++)
{
for(j = i + 1; j &lt; Size; j++)
{
if(arr[i] == arr[j])
{
for(k = j; k &lt; Size; k++)
{
arr[k] = arr[k + 1];
}
Size--;
j--;
}
}
}
GFGC RAICHUR
Programming in C For BCA Ist Sem
printf(&quot;\n Final Array after Deleteing Duplicate Array Elements is:\n&quot;);
for (i = 0; i &lt; Size; i++)
{
printf(&quot;%d\t&quot;, arr[i]);
}
return 0;
}
OUTPUT:
Please Enter Number of elements in an array : 5
Please Enter 5 elements of an Array
4
5
6
7
6
Final Array after Deleteing Duplicate Array Elements is:
4
5
6
7
GFGC RAICHUR
Programming in C For BCA Ist Sem
11]Algorithm to perform Addition &amp; subtraction of
two matrices
Algorithm:
Step-1: start
Step-2: input n, m, first[10][10], second[10][10]
Step-3: input elements of first matrix
for(c=0;c&lt;m;c++)
for(d=0;d&lt;n;d++)
Step-4: display first[c][d]
Step-5: input elements of second matrix
for(c=0;c&lt;m;c++)
for(d=0;d&lt;n;d++)
Step-5: display second[c][d]
for(c=0;c&lt;m;c++)
for(d=0;d&lt;n;d++)
Step-6: sum[c][d]=first[c][d]+second[c][d]
Step-7: display sum[c][d]
for(c=0;c&lt;m;c++)
for(d=0;d&lt;n;d++)
Step-8: diff[c][d]=first[c][d]-second[c][d]
Step-18: display diff[c][d]
Step-19 stop
GFGC RAICHUR
Programming in C For BCA Ist Sem
12.Program to perform addition and subtraction of matrices.
#include&lt;stdio.h&gt;
int main()
{
int n, m, c, d, first[10][10], second[10][10], sum[10][10], diff[10][10];
printf(&quot;\nEnter the number of rows and columns of the first matrix \n\n&quot;);
scanf(&quot;%d%d&quot;, &amp;m, &amp;n);
printf(&quot;\nEnter the %d elements of the first matrix \n\n&quot;, m*n);
for(c = 0; c &lt; m; c++)
for(d = 0; d &lt; n; d++)
scanf(&quot;%d&quot;, &amp;first[c][d]);
printf(&quot;\nEnter the %d elements of the second matrix \n\n&quot;, m*n);
for(c = 0; c &lt; m; c++)
for(d = 0; d &lt; n; d++)
scanf(&quot;%d&quot;, &amp;second[c][d]);
printf(&quot;\n\nThe first matrix is: \n\n&quot;);
for(c = 0; c &lt; m; c++) // to iterate the rows
{
for(d = 0; d &lt; n; d++) // to iterate the columns
{
printf(&quot;%d\t&quot;, first[c][d]);
}
printf(&quot;\n&quot;);
}
GFGC RAICHUR
Programming in C For BCA Ist Sem
printf(&quot;\n\nThe second matrix is: \n\n&quot;);
for(c = 0; c &lt; m; c++)
{
for(d = 0; d &lt; n; d++)
{
printf(&quot;%d\t&quot;, second[c][d]);
}
printf(&quot;\n&quot;);
}
for(c = 0; c &lt; m; c++)
for(d = 0; d &lt; n; d++)
sum[c][d] = first[c][d] + second[c][d];
printf(&quot;\n\nThe sum of the two entered matrices is: \n\n&quot;);
for(c = 0; c &lt; m; c++)
{
for(d = 0; d &lt; n; d++)
{
printf(&quot;%d\t&quot;, sum[c][d]);
}
printf(&quot;\n&quot;);
}
for(c = 0; c &lt; m; c++)
for(d = 0; d &lt; n; d++)
diff[c][d] = first[c][d] - second[c][d];
printf(&quot;\n\nThe difference(subtraction) of the two entered matrices is: \n\n&quot;);
for(c = 0; c &lt; m; c++)
{
for(d = 0; d &lt; n; d++)
GFGC RAICHUR
Programming in C For BCA Ist Sem
{
printf(&quot;%d\t&quot;, diff[c][d]);
}
printf(&quot;\n&quot;);
}
return 0;
}
Output:
4
5
The second matrix is:
4
3
2
1
The sum of the two entered matrices is:
6
6
6
6
The difference(subtraction) of the two entered matrices is:
-2
0
2
4
GFGC RAICHUR
Programming in C For BCA Ist Sem
PART-B
1. Algorithm and flowchart to find length of a string without using
built in functions.
Step-1: Start
Step-2: Declare i and initialize length=0
Step-3: Input a string
Step-4: i ―›0
Step-5: Repeat upto step-6 until str(i)!=10
Step-6: i++
Step-7: length=length+1
Step-5: output length of string
Step-6: stop
GFGC RAICHUR
Programming in C For BCA Ist Sem
Flowchart:
Start
Declare i,length=0
gets(string)
for(i=0;string[i]!=10;i++)
length++
Output length
Stop
GFGC RAICHUR
Programming in C For BCA Ist Sem
1. Program:
#include&lt;stdio.h&gt;
Void main()
{
char string[50];
int i,length=0
printf(“Enter a string\n”);
gets(string);
for(i=0;string[i] != ‘\0’; i++)
{
length++;
}
Printf(“The length of a string is the number of characters in it\n ”);
Primtf(“So, the length of %S=%d\n”, string, length);
}
OUTPUT:
Enter a string
renuka is good girl
:the length of a string is the number of characters in it so,
The length of renuka is a good girl = 21
GFGC RAICHUR
Programming in C For BCA Ist Sem
2) Algorithm and flowchart to demonstrate string functions.
Step-1: Start
Step-2: input string1[25], string2[25]
Step-3: Calcute
L=Strlen(string1)
Step-4: if (Strcmp(string1, String2)==0&amp;
True then print strings are equal
Else
Print strings are not equal
Step-5: strcpy(string1,String2)
Step-6: Stop
GFGC RAICHUR
Programming in C For BCA Ist Sem
Flowchart
Start
Input String1[25]
String[25]
If
(Strcmp(String1
String2)==0)
Print Strigs are
not equal
Print (String1,string2)==0)
Strcpy(String1,String2)
Stop
GFGC RAICHUR
Programming in C For BCA Ist Sem
2. Program to demonstrate STRING Functions.
#include&lt;stdio.h&gt;
#include&lt;conio.h&gt;
Void main(){
Char string1[25],string2[25];
int l;
Clrscr();
Printf(“***** performing string length ******\n”);
Printf(“enter only one string \n”);
Scanf(“%s”,string1);
l = strlen(string1);
printf(“the string length is %d\n\n”,l);
printf(“**** performing string concatenation ****\n”);
printf(“enter two strings\n”);
scanf(“%s%s”,string1,string2);
printf(“the concatenated string is %s\n\n”,strcat(string1,string2));
printf(“***** performing string compare *****\n”);
printf(“enter two strings \n”);
scanf(“%s%s”,string1,string2);
if(strcmp(string1,string2) = = 0)
printf(“strings are equal\n”);
else
printf(“strings are not equal\n”);
printf(“*** performing string copy ****\n”);
printf(“enter the two strings\n”);
scanf(“%d%d”,string1,string2);
printf(“the first string is %s and second string is %s\n”,string1,string2);
strcpy(string1,string2);
printf(“the first string is %s and second string is %s\n”,string1,string2);
getch();
}
Output:
Enter only one string A
Enter string length 1
Enter two string BC
Enter string length 2
GFGC RAICHUR
Programming in C For BCA Ist Sem
3. Check a number for prime by defining isprime() function
Algorithm:Step1: start
Step2: input num [positive numbers]
Step3:[call function]
If[is prime(num)]
Step a: print “prime number”
else
Step b: print “not prime number”;
Return 0;
Step4: exit
Called function
Step1: isprime(int num)
Step2:For (i=2,i&lt;(num/2);i++)
Step a:if (num%i==&Oslash;)
return 0;
else
return 1;
Step3: Exit
GFGC RAICHUR
Programming in C For BCA Ist Sem
Start
Flow chart:
Input num
If
(is prime(num))?
Not a prime num
Is a prime num
stop
Isprime(intnum)
For (7=2;7&lt;=(num/2);7++)
Is
Num%7==0
Return
0
Return
1
i
End function
GFGC RAICHUR
Programming in C For BCA Ist Sem
3. Program to check a number for prime by defining isprime() function
#include&lt;stdio.h&gt;
#include&lt;math.h&gt;
#include&lt;stdlib.h&gt;
int isPrime(int num);
int main()
{
int num;
clrscr();
printf(&quot;Enter a positive number\n&quot;);
scanf(&quot;%d&quot;,&amp;num);
if(isPrime(num))
printf(&quot;%d is a Prime Number&quot;,num);
else
printf(&quot;%d is NOT a Prime Number&quot;,num);
return 0;
}
int isPrime(int num)
{
int i;
for(i = 2; i &lt;=(num/2); ++i) {
if(num%i==0) {
return 0;
}
}
getch();
return 1;
}
Output:
Enter a positive number = 40
40 is not a prime number
GFGC RAICHUR
Programming in C For BCA Ist Sem
5) Program to read, display &amp;to find the trace of a square
matrix.
Step-1: Start
Step-2: input a[10][10],m
Step-3: for(i=0;i&lt;m; i++)
for(j=0;j&lt;m;J++)
Sum=0
Step-4: for(i=0;i&lt;m;i++)
Sum=Sum+a[i][i]
Step-5: Stop
GFGC RAICHUR
Programming in C For BCA Ist Sem
Flowchart
Start
Input a[10][10],m
I=0;i&lt;m;i++
J=0;j&lt;m;j++
Sum=0
I=0;i&lt;m;i++
Sum=Sum+a[i][j]
Stop
GFGC RAICHUR
Programming in C For BCA Ist Sem
5. Program to read ,display and to find the trace of a Square Matrix.
# include&lt;stdio.h&gt;
main( )
{
int a[10][10], m,i,j, sum;
printf (&quot;\n Enter order of the square matrix :&quot;) ;
scanf (&quot;%d&quot;, &amp;m);
printf (&quot;\n Enter the matrix \n&quot;);
for( i=0; i&lt;m;i++)
for ( j=0; j&lt;m; j++)
scanf (&quot;%d&quot;, &amp;a[i ][ j ]);
/* loop to find trace of the matrix */
sum = 0;
for ( i=0; i&lt;m; i++)
sum = sum + a[i ][ i ];
printf (&quot;\n trace of the matrix = %d&quot;, sum);
}
OUTPUT:
Enter order of the square matrix : 2 2
Enter the matrix
5 6
3 4
trace of the matrix = 9
GFGC RAICHUR
Programming in C For BCA Ist Sem
6.Program to read,display and to add two m , n matrices using
functions
#include &lt;stdio.h&gt;
int rows, columns;
void matrixAddition(int mat1[][10], int mat2[][10], int mat3[][10]) {
int i, j;
for (i = 0; i &lt; rows; i++) {
for (j = 0; j &lt; columns; j++) {
mat3[i][j] = mat1[i][j] + mat2[i][j];
}
}
return;
}
int main() {
int matrix1[10][10], matrix2[10][10];
int matrix3[10][10], i, j;
printf(&quot;Enter the no of rows and columns(&lt;=10):&quot;);
scanf(&quot;%d%d&quot;, &amp;rows, &amp;columns);
if (rows &gt; 10 || columns &gt; 10) {
printf(&quot;No of rows/columns is greater than 10\n&quot;);
return 0;
}
/* input first matrix */
printf(&quot;Enter the input for first matrix:&quot;);
for (i = 0; i &lt; rows; i++) {
for (j = 0; j &lt; columns; j++) {
scanf(&quot;%d&quot;, &amp;matrix1[i][j]);
}
}
/* input second matrix */
printf(&quot;Enter the input for second matrix:&quot;);
for (i = 0; i &lt; rows; i++) {
for (j = 0; j &lt; columns; j++) {
GFGC RAICHUR
Programming in C For BCA Ist Sem
scanf(&quot;%d&quot;, &amp;matrix2[i][j]);
}
}
/* print the results */
for (i = 0; i &lt; rows; i++) {
for (j = 0; j &lt; columns; j++) {
printf(&quot;%5d&quot;, matrix3[i][j]);
}
printf(&quot;\n&quot;);
}
return 0;
}
Output:
Enter the no of rows and columns(&lt;=10):
3 3
Enter the input for first matrix:
10 20 30
40 54 60
70 80 90
Enter the input for second matrix:
100 110 120
130 140 150
160 170 180
110 130 150
170 194 210
230 250 270
GFGC RAICHUR
Programming in C For BCA Ist Sem
7.Program to read,display and multiply two m x n matrices using
functions
#include&lt;stdio.h&gt;
void multiply(int mat1[12][12],int mat2[12][12],int ,int ,int );
void main()
{
int mat1[12][12],mat2[12][12];
int i,j,k,m,n,p;
printf(&quot;Enter the number of rows and columns for 1st matrix\n&quot;);
scanf(&quot;%d%d&quot;,&amp;m,&amp;n);
printf(&quot;Enter the elements of the 1st matrix\n&quot;);
for(i=0;i&lt;m;i++)
{
for(j=0;j&lt;n;j++)
{
scanf(&quot;%d&quot;,&amp;mat1[i][j]);
}
}
printf(&quot;Enter the number of columns for 2nd matrix\n&quot;);
scanf(&quot;%d&quot;,&amp;p);
printf(&quot;Enter the elements of the 2nd matrix\n&quot;);
for(i=0;i&lt;n;i++)
{
for(j=0;j&lt;p;j++)
{
scanf(&quot;%d&quot;,&amp;mat2[i][j]);
}
}
printf(&quot;The 1st matrix\n&quot;);
for(i=0;i&lt;m;i++)
{
for(j=0;j&lt;n;j++)
{
printf(&quot;%d\t&quot;,mat1[i][j]);
}
printf(&quot;\n&quot;);
}
printf(&quot;The 2nd matrix\n&quot;);
GFGC RAICHUR
Programming in C For BCA Ist Sem
for(i=0;i&lt;n;i++)
{
for(j=0;j&lt;p;j++)
{
printf(&quot;%d\t&quot;,mat2[i][j]);
}
printf(&quot;\n&quot;);
}
multiply(mat1,mat2,m,n,p);
}
void multiply(int mat1[12][12],int mat2[12][12],int m,int n,int p)
{
int mul[12][12],i,j,k;
for(i=0;i&lt;m;i++)
{
for(j=0;j&lt;p;j++)
{
mul[i][j]=0;
for(k=0;k&lt;n;k++)
{
mul[i][j]=mul[i][j]+mat1[i][k]*mat2[k][j];
}
}
}
printf(&quot;The resultant matrix formed on multiplying the two matrices\n&quot;);
for(i=0;i&lt;m;i++)
{
for(j=0;j&lt;p;j++)
{
printf(&quot;%d\t&quot;,mul[i][j]);
}
printf(&quot;\n&quot;);
}
}
GFGC RAICHUR
Programming in C For BCA Ist Sem
8.program to read a string and to find the number of alphabets
,digits,vowels
#include &lt;stdio.h&gt;
int main() {
char line[150];
int vowels, consonant, digit, space;
vowels = consonant = digit = space = 0;
printf(&quot;Enter a line of string: &quot;);
fgets(line, sizeof(line), stdin);
for (int i = 0; line[i] != '\0'; ++i) {
line[i] = tolower(line[i]);
if (line[i] == 'a' || line[i] == 'e' || line[i] == 'i' ||
line[i] == 'o' || line[i] == 'u') {
++vowels;
}
else if ((line[i] &gt;= 'a' &amp;&amp; line[i] &lt;= 'z')) {
++consonant;
}
else if (line[i] &gt;= '0' &amp;&amp; line[i] &lt;= '9') {
++digit;
}
else if (line[i] == ' ') {
++space;
}
}
printf(&quot;Vowels: %d&quot;, vowels);
printf(&quot;\nConsonants: %d&quot;, consonant);
printf(&quot;\nDigits: %d&quot;, digit);
printf(&quot;\nWhite spaces: %d&quot;, space);
GFGC RAICHUR
Programming in C For BCA Ist Sem
return 0;
}
Output
Enter a line of string: C++ 20 is the latest version of C++ yet.
Vowels: 9
Consonants: 16
Digits: 2
White spaces: 8
GFGC RAICHUR
Programming in C For BCA Ist Sem
9.Program to Reverse a String Using Pointers
#include &lt;stdio.h&gt;
int main()
{
char str[100];
char rev[100];
char *sptr = str; // sptr stores the base address of the str
char *rptr = rev; // rptr stores the base address of the reverse
int i = -1;
printf(&quot;\n\nEnter a string: &quot;);
scanf(&quot;%s&quot;, str);
while(*sptr)
{
sptr++;
i++; // i is the index of the end location
}
while(i &gt;= 0)
{
sptr--;
*rptr = *sptr; // storing the value in sptr in rptr
rptr++; // pointing to next location
i--; // decrementing the index
}
*rptr = '\0';
rptr = rev;
while(*rptr)
{
*sptr = *rptr;
sptr++;
rptr++;
}
printf(&quot;\n\nReverse of the string is: %s &quot;, str);
GFGC RAICHUR
Programming in C For BCA Ist Sem
return 0;
}
Output
10.Program to swap two numbers using pointers
#include &lt;stdio.h&gt;
int main()
{
int x, y, *a, *b, temp;
printf(&quot;Enter the value of x and y\n&quot;);
scanf(&quot;%d%d&quot;, &amp;x, &amp;y);
printf(&quot;Before Swapping\nx = %d\ny = %d\n&quot;, x, y);
a = &amp;x;
b = &amp;y;
temp = *b;
*b = *a;
*a = temp;
printf(&quot;After Swapping\nx = %d\ny = %d\n&quot;, x, y);
return 0;
}
Output
GFGC RAICHUR
Programming in C For BCA Ist Sem
11.Program to demonstrate student structure to read &amp; display
records of n students
#include&lt;stdio.h&gt;
struct student
{
int age,roll_no,fees;
char name[25];
}stud[100];
void main()
{
int i,n;
printf(&quot;Enter the no of students\n&quot;);
scanf(&quot;%d&quot;,&amp;n);
printf(&quot;enter student info as roll_no , name , age , fees\n&quot;);
for(i=0;i&lt;n;i++)
{
scanf(&quot;%d %s %d
%d&quot;,&amp;stud[i].roll_no,stud[i].name,&amp;stud[i].age,&amp;stud[i].fees);
}
printf(&quot;\nROLL_NO\t\tNAME\t\tAGE\t\tFEES\n&quot;);
for(i=0;i&lt;n;i++)
{
printf(&quot;%d\t\t %s\t\t%d\t\t%d\t\t\n&quot;,stud[i].roll_no,stud[i].name,stud[i].age,st
ud[i].fees);
}
}
Out put
GFGC RAICHUR
Programming in C For BCA Ist Sem
12. Program to demonstrate the difference between structures
&amp; union
#include &lt;stdio.h&gt;
#include &lt;string.h&gt;
// declaring structure
struct struct_example
{
int integer;
float decimal;
char name[20];
};
// declaring union
union union_example
{
int integer;
float decimal;
char name[20];
};
void main()
{
// creating variable for structure
// and initializing values difference
// six
struct struct_example s={18,38,&quot;geeksforgeeks&quot;};
// creating variable for union
GFGC RAICHUR
Programming in C For BCA Ist Sem
// and initializing values
union union_example u={18,38,&quot;geeksforgeeks&quot;};
printf(&quot;structure data:\n integer: %d\n&quot;
&quot;decimal: %.2f\n name: %s\n&quot;,
s.integer, s.decimal, s.name);
printf(&quot;\nunion data:\n integer: %d\n&quot;
&quot;decimal: %.2f\n name: %s\n&quot;,
u.integer, u.decimal, u.name);
// difference two and three
printf(&quot;\nsizeof structure : %d\n&quot;, sizeof(s));
printf(&quot;sizeof union : %d\n&quot;, sizeof(u));
// difference five
printf(&quot;\n Accessing all members at a time:&quot;);
s.integer = 183;
s.decimal = 90;
strcpy(s.name, &quot;geeksforgeeks&quot;);
printf(&quot;structure data:\n integer: %d\n &quot;
&quot;decimal: %.2f\n name: %s\n&quot;,
s.integer, s.decimal, s.name);
u.integer = 183;
u.decimal = 90;
strcpy(u.name, &quot;geeksforgeeks&quot;);
printf(&quot;\nunion data:\n integer: %d\n &quot;
&quot;decimal: %.2f\n name: %s\n&quot;,
u.integer, u.decimal, u.name);
printf(&quot;\n Accessing one member at time:&quot;);
printf(&quot;\nstructure data:&quot;);
s.integer = 240;
printf(&quot;\ninteger: %d&quot;, s.integer);
GFGC RAICHUR
Programming in C For BCA Ist Sem
s.decimal = 120;
printf(&quot;\ndecimal: %f&quot;, s.decimal);
strcpy(s.name, &quot;C programming&quot;);
printf(&quot;\nname: %s\n&quot;, s.name);
printf(&quot;\n union data:&quot;);
u.integer = 240;
printf(&quot;\ninteger: %d&quot;, u.integer);
u.decimal = 120;
printf(&quot;\ndecimal: %f&quot;, u.decimal);
strcpy(u.name, &quot;C programming&quot;);
printf(&quot;\nname: %s\n&quot;, u.name);
//difference four
printf(&quot;\nAltering a member value:\n&quot;);
s.integer = 1218;
printf(&quot;structure data:\n integer: %d\n &quot;
&quot; decimal: %.2f\n name: %s\n&quot;,
s.integer, s.decimal, s.name);
u.integer = 1218;
printf(&quot;union data:\n integer: %d\n&quot;
&quot; decimal: %.2f\n name: %s\n&quot;,
u.integer, u.decimal, u.name);
}
GFGC RAICHUR
Programming in C For BCA Ist Sem
GFGC RAICHUR
Programming in C For BCA Ist Sem
GFGC RAICHUR
Programming in C For BCA Ist Sem
GFGC RAICHUR
Programming in C For BCA Ist Sem
GFGC RAICHUR
Programming in C For BCA Ist Sem
GFGC RAICHUR
Programming in C For BCA Ist Sem
GFGC RAICHUR
Programming in C For BCA Ist Sem
GFGC RAICHUR
Programming in C For BCA Ist Sem
GFGC RAICHUR
Programming in C For BCA Ist Sem
GFGC RAICHUR
```