BCA I Sem Lab Manual

You might also like

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

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 HEAD OF THE DEPARTMENT

(Smt.Ishrat Begum) (Dr.Basava Prasad)

Examiners:
1.
2.
Programming in C For BCA Ist Sem

INDEX
SI.No Program Name Page No Remark

PART – A
01 C Program to find area and circumference of circle
02 C  Program to find greatest in 3 numbers
03 C program to demonstrate the library functions in
math.h
04 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.
09 C program to find the roots of a quadratic equation

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)
12 Program to perform addition & subtraction of
Matrices
PART -B
01 Program to find length of a string without using built
in functions.

02 Program to demonstrate STRING Functions


03 Program to demonstrate Pointers in C
04 Program to Check a number for prime by defining
isprime() function

GFGC RAICHUR
Programming in C For BCA Ist Sem

05 Program to read, display &to find the trace of a square


matrix.

06 Program to read,display and add two m x n matrices


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

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

Step2: input radius

Step3: calculate
Area=pi*radius*radius
Step4: calculate
Circumference=2*pi*radius
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

Input radius

Area=pi*radius*radius

Circumference=2*pi*radius

GFGC RAICHUR
Programming in C For BCA Ist Sem

Display area and


circumference of
circle

stop

1. C Program to find area and circumference of circle


#include<stdio.h>

int main() {
int rad;
float PI = 3.14, area, ci;
printf("\nEnter radius of circle: ");
scanf("%d", &rad);
area = PI * rad * rad;
printf("\nArea of circle : %f ", area);
ci = 2 * PI * rad;
printf("\nCircumference : %f ", 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>b)&&(a>c))
Print a is greatest
if ((b>c&&(b>a))
print b is greatest
if ((c>a)&&(c>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>b)&&(a>c))

yes

If
no
Print a is greatest ((a>b)&&(b>a))

yes

If ((c>a)&&(c>b))

P¥Àpprint
yes

Print c is Preatest

GFGC RAICHUR
Programming in C For BCA Ist Sem

stop

2. C  Program to find greatest in 3 numbers


#include<stdio.h>
int main() {
int a, b, c;
printf("\nEnter value of a, b & c : ");
scanf("%d %d %d", &a, &b, &c);
if ((a > b) && (a > c))
printf("\na is greatest");
if ((b > c) && (b > a))
printf("\nb is greatest");
if ((c > a) && (c > b))
printf("\nc is greatest");
return(0);
}

OUTPUT
Enter value for a,b & 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 <stdio.h>
#include <math.h> 
int main(void)
{
  double dValuex = 5.0, dValuey = 5.0;
  double dResult;
  result = pow(dValuex, dValuey);
  printf("%lf power %lf is: %lf\n", 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 <=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
Read n

Set i=1
Count=0

no yes
i <=n

yes
yes Count n% ==0
==2

Display not
Display n Count=count+1
Is prime prime no

i=i+1
stop

GFGC RAICHUR
Programming in C For BCA Ist Sem

C program to check for prime.


#include<stdio.h>
int main() {
int num, i, count = 0;
printf("Enter a number:");
scanf("%d", &num);
for (i = 2; i <= num / 2; i++)
{
if (num % i == 0)
{
count++;
break;
}
}
if (count == 0)
printf("%d is a prime number", num);
else
printf("%d is not a prime number", 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<=n) if true
Then i++
Else goto step-5
Step-5: c=0 , j=1
if (j<=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<=n)

No Yes
If(c==2)
C=o

J=1
yes

Print i

If(j<=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("Enter the number till which you want prime numbers\n");
scanf("%d",&n);
printf("Prime numbers are:-\n");
for(i=2;i<=n;i++)
{
int c=0;
for(j=1;j<=i;j++)
{
if(i%j==0)
{
c++;
}
}

if(c==2)
{
printf("%d ",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+remainde
r

No
If(Original==
reversed)

Print original is not


yes palindrome

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 <stdio.h>
int main() {
int n, reversed = 0, remainder, original;
printf("Enter an integer: ");
scanf("%d", &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("%d is a palindrome.", original);
else
printf("%d is not a palindrome.", 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>0
neg+=num<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>0
neg+=num<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 <stdio.h>
int main()
{
long num = 1, pos = -1, neg = 0, zero = 0;
while(num != 999) {
pos += num > 0;
neg += num < 0;
zero += num == 0;
scanf("%d", &num);
}
printf("%d %d %d", 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>=60&&per<=100) if true
Then print you are 1st
Else if (per>=50&&per<=60) if true
Then print you are 2nd
Else if (per>=40&&per<=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>=60
&&per<=1
00

If(per>=60
yes
&&per<=1
Print you are 1st 00

else if
(per>=40&&
Print you are per<=50)
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


ladder)

#include<stdio.h>
#include<conio.h>
void main()
{
         int m1,m2,m3,total;
         float per;
         clrscr();
         printf("Enter 3 Nos.");
         scanf("%D%D%D",&m1,&m2,&m3);
         total=m1+m2+m3;
         per=total*100/300;
         if(per>=60&&per<=100)
                 printf("You are 1st :");
        else if(per>=50&&per<=60)
                 printf("You are 2nd");
        else if(per>=40&&per<=50)
                 printf("You are 3rd");
        else
                 printf("You are Fail");
        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


quadratic equation.
Step-1:Start
Step-2:input a,b,c
Step-3:dis=b2-4a*c
Step-4:if(discM<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>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 Write roots are


?
Are imaginary real and equal
Dis c

Write roots are X1=-b1(2*a)


real and equal

X1=(-b+sqrt(disc))/(2*a) Roots are


X1,X1
X2=(-b-sqrt(disc))/(2*a)

Stop

GFGC RAICHUR
Programming in C For BCA Ist Sem

C program to find the roots of a quadratic equation


#include <stdio.h>
#include <math.h> /* Used for sqrt() */
int main()
{
float a, b, c;
float root1, root2, imaginary;
float discriminant;
printf("Enter values of a, b, c of quadratic equation (aX^2 + bX + c): ");
scanf("%f%f%f", &a, &b, &c);
discriminant = (b * b) - (4 * a * c);
switch(discriminant > 0)
{
case 1:
/* If discriminant is positive */
root1 = (-b + sqrt(discriminant)) / (2 * a);
root2 = (-b - sqrt(discriminant)) / (2 * a);

printf("Two distinct and real roots exists: %.2f and %.2f",


root1, root2);
break;

case 0:
/* If discriminant is not positive */
switch(discriminant < 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("Two distinct complex roots exists: %.2f + i%.2f and %.2f - i%.2f",
root1, imaginary, root2, imaginary);
break;
case 0:
/* If discriminant is zero */
root1 = root2 = -b / (2 * a);
printf("Two equal and real roots exists: %.2f and %.2f", 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 <stdio.h> 
int main()
{
int i, num;
float total = 0.0, average;
printf ("Enter the number of students \n");
scanf("%d", &num);
int array[num];
printf("Enter %d students marks \n", num);
  for (i = 0; i < num; i++)
{
scanf("%d", &array[i]);

printf("Input Marks as array elements \n");
for (i = 0; i < num; i++)
{
printf("%+3d\n", array[i]);
}
for (i = 0; i < num; i++)
{
total+=array[i];
}
average = total / num;
printf("\n Sum of all marks = %.2f\n", total);
printf("\n Average of all input marks= %.2f\n", 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<size;i++)
Step-4: display arr[i]
Step-5: for(i=0;i<size;i++)
Step-6: for(j=0;j<size;j++)
Step-7: if(arr[i]==arr[j])
Step-8: for(k=j;k<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<size;i++

Arr[i]

i=0;i<size;i++

j=0;j<size;j++

GFGC RAICHUR
Programming in C For BCA Ist Sem

Arr[i]==arr[j]

k=j;k<size;k++

Arr[k]=arr[k+1]

Size--

j--

stop

GFGC RAICHUR
Programming in C For BCA Ist Sem

11.C Program to Delete Duplicate Elements from an Array


#include <stdio.h>
int main()
{
int arr[10], i, j, k, Size;
printf("\n Please Enter Number of elements in an array : ");
scanf("%d", &Size);
printf("\n Please Enter %d elements of an Array \n", Size);
for (i = 0; i < Size; i++)
{
scanf("%d", &arr[i]);
}
for (i = 0; i < Size; i++)
{
for(j = i + 1; j < Size; j++)
{
if(arr[i] == arr[j])
{
for(k = j; k < Size; k++)
{
arr[k] = arr[k + 1];
}
Size--;
j--;
}
}
}

GFGC RAICHUR
Programming in C For BCA Ist Sem

printf("\n Final Array after Deleteing Duplicate Array Elements is:\n");


for (i = 0; i < Size; i++)
{
printf("%d\t", 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 & 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<m;c++)
for(d=0;d<n;d++)
Step-4: display first[c][d]
Step-5: input elements of second matrix
for(c=0;c<m;c++)
for(d=0;d<n;d++)
Step-5: display second[c][d]
for(c=0;c<m;c++)
for(d=0;d<n;d++)
Step-6: sum[c][d]=first[c][d]+second[c][d]
Step-7: display sum[c][d]
for(c=0;c<m;c++)
for(d=0;d<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<stdio.h>
int main()
{

int n, m, c, d, first[10][10], second[10][10], sum[10][10], diff[10][10];


printf("\nEnter the number of rows and columns of the first matrix \n\n");
scanf("%d%d", &m, &n);
printf("\nEnter the %d elements of the first matrix \n\n", m*n);
for(c = 0; c < m; c++)
for(d = 0; d < n; d++)
scanf("%d", &first[c][d]);

printf("\nEnter the %d elements of the second matrix \n\n", m*n);


for(c = 0; c < m; c++)
for(d = 0; d < n; d++)
scanf("%d", &second[c][d]);
printf("\n\nThe first matrix is: \n\n");
for(c = 0; c < m; c++) // to iterate the rows
{
for(d = 0; d < n; d++) // to iterate the columns
{
printf("%d\t", first[c][d]);
}
printf("\n");
}
printf("\n\nThe second matrix is: \n\n");

GFGC RAICHUR
Programming in C For BCA Ist Sem

for(c = 0; c < m; c++)


{
for(d = 0; d < n; d++)
{
printf("%d\t", second[c][d]);
}
printf("\n");
}
for(c = 0; c < m; c++)
for(d = 0; d < n; d++)
sum[c][d] = first[c][d] + second[c][d];
printf("\n\nThe sum of the two entered matrices is: \n\n");
for(c = 0; c < m; c++)
{
for(d = 0; d < n; d++)
{
printf("%d\t", sum[c][d]);
}
printf("\n");
}
for(c = 0; c < m; c++)
for(d = 0; d < n; d++)
diff[c][d] = first[c][d] - second[c][d];
printf("\n\nThe difference(subtraction) of the two entered matrices is: \n\n");
for(c = 0; c < m; c++)
{
for(d = 0; d < n; d++)
{

GFGC RAICHUR
Programming in C For BCA Ist Sem

printf("%d\t", diff[c][d]);
}
printf("\n");
}
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<stdio.h>
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&
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<stdio.h>
#include<conio.h>
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<(num/2);i++)
Step a:if (num%i==Ø)
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))?

Is a prime num Not a prime num

stop

Isprime(intnum)

For (7=2;7<=(num/2);7++)

Is

Num%7==0

Return 0 Return 1

End function

GFGC RAICHUR
Programming in C For BCA Ist Sem
3. Program to check a number for prime by defining isprime() function

#include<stdio.h>
#include<math.h>
#include<stdlib.h>
int isPrime(int num);
int main()
{
int num;
clrscr();
printf("Enter a positive number\n");
scanf("%d",&num);
if(isPrime(num))
printf("%d is a Prime Number",num);
else
printf("%d is NOT a Prime Number",num);
return 0;
}
int isPrime(int num)
{
int i;
for(i = 2; i <=(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 &to find the trace of a square


matrix.

Step-1: Start
Step-2: input a[10][10],m
Step-3: for(i=0;i<m; i++)
for(j=0;j<m;J++)
Sum=0
Step-4: for(i=0;i<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<m;i++

J=0;j<m;j++

Sum=0

I=0;i<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<stdio.h>
main( )
{
int a[10][10], m,i,j, sum;
printf ("\n Enter order of the square matrix :") ;
scanf ("%d", &m);
printf ("\n Enter the matrix \n");
for( i=0; i<m;i++)
for ( j=0; j<m; j++)
scanf ("%d", &a[i ][ j ]);
/* loop to find trace of the matrix */
sum = 0;
for ( i=0; i<m; i++)
sum = sum + a[i ][ i ];
printf ("\n trace of the matrix = %d", 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 <stdio.h>

  int rows, columns;

  void matrixAddition(int mat1[][10], int mat2[][10], int mat3[][10]) {


        int i, j;

        for (i = 0; i < rows; i++) {


                for (j = 0; j < 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("Enter the no of rows and columns(<=10):");


        scanf("%d%d", &rows, &columns);

        if (rows > 10 || columns > 10) {


                printf("No of rows/columns is greater than 10\n");
                return 0;
    }

        /* input first matrix */


        printf("Enter the input for first matrix:");
        for (i = 0; i < rows; i++) {
                for (j = 0; j < columns; j++) {
                        scanf("%d", &matrix1[i][j]);
        }
    }

        /* input second matrix */


        printf("Enter the input for second matrix:");
        for (i = 0; i < rows; i++) {
                for (j = 0; j < columns; j++) {

GFGC RAICHUR
Programming in C For BCA Ist Sem
                        scanf("%d", &matrix2[i][j]);
        }
    }

        /* matrix addtion */
        matrixAddition(matrix1, matrix2, matrix3);

        /* print the results */


        printf("\nResult of Matrix Addition:\n");
        for (i = 0; i < rows; i++) {
                for (j = 0; j < columns; j++) {
                        printf("%5d", matrix3[i][j]);
        }
                printf("\n");
    }
        return 0;
 }

Output:
  Enter the no of rows and columns(<=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

  Result of Matrix Addition:


  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<stdio.h>
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("Enter the number of rows and columns for 1st matrix\n");
scanf("%d%d",&m,&n);
printf("Enter the elements of the 1st matrix\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&mat1[i][j]);
}
}

printf("Enter the number of columns for 2nd matrix\n");


scanf("%d",&p);
printf("Enter the elements of the 2nd matrix\n");
for(i=0;i<n;i++)
{
for(j=0;j<p;j++)
{
scanf("%d",&mat2[i][j]);
}
}

printf("The 1st matrix\n");


for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%d\t",mat1[i][j]);
}
printf("\n");
}
printf("The 2nd matrix\n");

GFGC RAICHUR
Programming in C For BCA Ist Sem
for(i=0;i<n;i++)
{
for(j=0;j<p;j++)
{
printf("%d\t",mat2[i][j]);
}
printf("\n");
}
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<m;i++)
{
for(j=0;j<p;j++)
{
mul[i][j]=0;
for(k=0;k<n;k++)
{
mul[i][j]=mul[i][j]+mat1[i][k]*mat2[k][j];
}
}
}

printf("The resultant matrix formed on multiplying the two matrices\n");


for(i=0;i<m;i++)
{
for(j=0;j<p;j++)
{
printf("%d\t",mul[i][j]);
}
printf("\n");
}
}

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 <stdio.h>
int main() {

char line[150];
int vowels, consonant, digit, space;

vowels = consonant = digit = space = 0;

printf("Enter a line of string: ");


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] >= 'a' && line[i] <= 'z')) {


++consonant;
}

else if (line[i] >= '0' && line[i] <= '9') {


++digit;
}

else if (line[i] == ' ') {


++space;
}
}
printf("Vowels: %d", vowels);
printf("\nConsonants: %d", consonant);
printf("\nDigits: %d", digit);
printf("\nWhite spaces: %d", 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 <stdio.h>

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("\n\nEnter a string: ");


scanf("%s", str);

while(*sptr)
{
sptr++;
i++; // i is the index of the end location
}

while(i >= 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("\n\nReverse of the string is: %s ", str);

GFGC RAICHUR
Programming in C For BCA Ist Sem
return 0;
}

Output

10.Program to swap two numbers using pointers

#include <stdio.h>

int main()
{
int x, y, *a, *b, temp;
printf("Enter the value of x and y\n");
scanf("%d%d", &x, &y);
printf("Before Swapping\nx = %d\ny = %d\n", x, y);
a = &x;
b = &y;
temp = *b;
*b = *a;
*a = temp;
printf("After Swapping\nx = %d\ny = %d\n", x, y);
return 0;
}

Output

GFGC RAICHUR
Programming in C For BCA Ist Sem

11.Program to demonstrate student structure to read & display


records of n students

#include<stdio.h>
struct student
{
    int age,roll_no,fees;
    char name[25];
}stud[100];
 
void main()
{
    int i,n;
    printf("Enter the no of students\n");
    scanf("%d",&n);
    printf("enter student info as roll_no , name , age , fees\n");
    for(i=0;i<n;i++)
    {
        scanf("%d %s %d
%d",&stud[i].roll_no,stud[i].name,&stud[i].age,&stud[i].fees);
    }
    printf("\nROLL_NO\t\tNAME\t\tAGE\t\tFEES\n");
    for(i=0;i<n;i++)
    {
        printf("%d\t\t    %s\t\t%d\t\t%d\t\t\
n",stud[i].roll_no,stud[i].name,stud[i].age,stud[i].fees);
    }
}

Out put

GFGC RAICHUR
Programming in C For BCA Ist Sem

12. Program to demonstrate the difference between structures


& union

#include <stdio.h>
#include <string.h>
  
// 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,"geeksforgeeks"};
  

GFGC RAICHUR
Programming in C For BCA Ist Sem

    // creating variable for union


    // and initializing values
    union union_example u={18,38,"geeksforgeeks"};
  
          
    printf("structure data:\n integer: %d\n"
                "decimal: %.2f\n name: %s\n",
                s.integer, s.decimal, s.name);
    printf("\nunion data:\n integer: %d\n"
                 "decimal: %.2f\n name: %s\n",
                u.integer, u.decimal, u.name);
  
  
    // difference two and three
    printf("\nsizeof structure : %d\n", sizeof(s));
    printf("sizeof union : %d\n", sizeof(u));
      
    // difference five
    printf("\n Accessing all members at a time:");
    s.integer = 183;
    s.decimal = 90;
    strcpy(s.name, "geeksforgeeks");
      
    printf("structure data:\n integer: %d\n "
                "decimal: %.2f\n name: %s\n",
            s.integer, s.decimal, s.name);
      
    u.integer = 183;
    u.decimal = 90;
    strcpy(u.name, "geeksforgeeks");
      
    printf("\nunion data:\n integer: %d\n "
                "decimal: %.2f\n name: %s\n",
            u.integer, u.decimal, u.name);
      
    printf("\n Accessing one member at time:");
      
    printf("\nstructure data:");
    s.integer = 240;

GFGC RAICHUR
Programming in C For BCA Ist Sem

    printf("\ninteger: %d", s.integer);


      
    s.decimal = 120;
    printf("\ndecimal: %f", s.decimal);
      
    strcpy(s.name, "C programming");
    printf("\nname: %s\n", s.name);
      
    printf("\n union data:");
    u.integer = 240;
    printf("\ninteger: %d", u.integer);
      
    u.decimal = 120;
    printf("\ndecimal: %f", u.decimal);
      
    strcpy(u.name, "C programming");
    printf("\nname: %s\n", u.name);
      
    //difference four
    printf("\nAltering a member value:\n");
    s.integer = 1218;
    printf("structure data:\n integer: %d\n "
                " decimal: %.2f\n name: %s\n",
                s.integer, s.decimal, s.name);
      
    u.integer = 1218;
    printf("union data:\n integer: %d\n"
           " decimal: %.2f\n name: %s\n",
            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

You might also like