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

PST Programs

these are 1st semester lab programs of bca course under banglore university

Uploaded by

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

PST Programs

these are 1st semester lab programs of bca course under banglore university

Uploaded by

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

Bangalore University

CA-C4P: Problem Solving Lab using C


1. Program to read the radius of a circle to find the area and circumference

#include<stdio.h>
#define PI 3.142
void main()
{
int r;
double area,circum;
printf("\n Enter radius of a circle : ");
scanf("%d",&r);
area=PI*r*r;
circum=2*PI*r;
printf("\n Area of a circle = %0.2lf",area);
printf("\n Circumference of a circle = %0.2lf",circum);
}

2. Program to read three numbers and find the biggest of three

#include<stdio.h>
#define MAX(a,b) (a>b ? a : b)
void main()
{
int a,b,c,L1,L2;
printf("\n Enter three numbers(a,b,c) :”);
scanf("%d %d %d",&a,&b,&c);
L1=MAX(a,b);
L2=MAX(c,L1);
printf("\n Biggest number = %d",L2);
}
3. Program to check whether the number is prime or not
#include<stdio.h>
void main()
{
int num,count=0,i;
printf("\n Enter a number : ");
scanf("%d",&num);
for(i=1;i<=num;i++)
{
if(num%i==0)
count++;
}
if(count==2)
printf("\n %d is a Prime number",num);
else
printf("\n %d is not a Prime number",num);
}

4. Program to read a number, find the sum of the digits,


reverse the number and check it for palindrome

#include<stdio.h>
void main()
{
int num,orgnum;
int sum=0,rev=0,rem;
printf("\n Enter a number : ");
scanf("%d",&num);
orgnum=num;
while(num>0)
{
rem=num%10;
sum=sum+rem;
rev=rev*10+rem;
num=num/10;
}
printf("\n Sum of digits = %d",sum);
printf("\n Reversed number = %d",rev);
if(orgnum==rev)
printf("\n Number is a Palindrome");
else
printf("\n Number is not a Palindrome");
}

5. Program to read numbers from the keyboard continuously till the user presses 999 and
to find the sum of only positive numbers

#include<stdio.h>
void main()
{
int num,sum=0;
do
{
printf("\n Enter a number : ");
scanf("%d",&num);
if(num > 0 && num!=999)
sum = sum + num;
printf("\n Sum = %d",sum);
}while(num!=999);
printf("\n You have pressed 999 : STOP");
}

6. Program to read the percentage of marks and to display appropriate


message (Demonstration of else-if ladder)
Percentage Grade
> =90% EXEMPLARY
80% - 89% OUTSTANDING
70% - 79% FIRST DIVISION WITH DISTINCTION
60% - 69% FIRST DIVISION
50% - 59% SECOND DIVISION
35% - 49% PASS CLASS
< 35% FAILS: RE-APPEAR

# include<stdio.h>
void main()
{
float per;
printf("\n Enter Percentage : ");
scanf("%f",&per);
if(per > =90.0)
printf("\n Grade = EXEMPLARY");
else if(per >=80.0 && per <90.0)
printf("\n Grade = OUTSTANDING");
else if(per >=70.0 && per < 80.0)
printf("\n Grade = FIRST DIVISION WITH DISTINCTION");
else if(per >=60.0 && per < 70.0)
printf("\n Grade = FIRST DIVISION");
else if(per >=50.0 && per < 60.0)
printf("\n Grade = SECOND DIVISION");
else if(per >=35.0 && per < 50.0)
printf("\n Grade = PASS CLASS");
else
printf("\n Grade = FAILS : RE-APPEAR");
}
7. Program to find the roots of quadratic equation

# include<stdio.h>
# include<stdlib.h>
# include<math.h>

void main()
{
int a,b,c,choice;
double disc,root1,root2,real,img;
printf("\n Enter a,b,c : ");
scanf("%d %d%d",&a,&b,&c);
disc=(b*b) - (4.0*a*c);
if(disc > 0)
choice=1;
else if(disc < 0)
choice=2;
else
choice=3;

switch(choice)
{
case 1:
{
printf("\n Real and Distinct Roots");
root1= ( -b + sqrt(disc) ) / (2.0 * a);
root2= ( -b - sqrt(disc) ) / (2.0 * a);
printf("\n Root1 = %0.2lf",root1);
printf("\n Root2 = %0.2lf",root2);
}
break;
case 2:
{
printf("\n Roots are complex and imaginary");real = -b/(2.0 * a);
img = sqrt(abs(disc))/(2.0 * a);
printf("\n Root1 = %0.2lf +i %0.2lf",real,img);
printf("\n Root2 = %0.2lf -i %0.2lf",real,img);
}
break;
case 3:
{
printf("\n Roots are Equal");
root1 = -b/(2.0 * a);
root2 = -b/(2.0 * a);
printf("\n Root1 = %0.2lf",root1);
printf("\n Root2 = %0.2lf",root2);
}
break;

default: printf("\n Invalid inputs");


}
}

8. Program to read marks scored by n students and to find the average of


marks (Demonstration of single dimensional array)
#include<stdio.h>
void main()
{
int n,i;
float marks[10];
float tot_marks=0.0,
avg_marks=0.0;
printf("\n Enter the count of students : ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\n Enter mark scored by student %d: ",i+1);
scanf("%f",&marks[i]);
tot_marks=tot_marks + marks[i];
}
avg_marks = tot_marks / n;
printf("\n Total marks = %0.2f",tot_marks);
printf("\n Average marks = %0.2f",avg_marks);
}
9. Program to remove duplicate element in single dimensional array

#include<stdio.h>
void main()
{
int a[20],n,i,j,ele;
printf("\n Enter array limit(n) : ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\n Enter a[%d] : ",i);
scanf("%d",&a[i]);
}
// remove duplicate
for(i=0;i<n-1;i++)
{
ele=a[i];
for(j=i+1;j<n;j++)
{
if(ele==a[j] && a[j]!=-111)
{
printf("\n %d Duplicate entry!!!",a[j]);
a[j]=-111; //set -111 for duplicate entry
}
}
}
printf("\n Final Array list\n");
for(i=0;i<n;i++)
{
if(a[i]!=-111)
printf("%5d",a[i]);
}

}
10. Program to perform addition and subtraction of matrices

#include<stdio.h>
void main()
{
int a[10][10], b[10][10], c[10][10], d[10][10];
int i,j,n;
printf("\n Enter order of square matrix(n) : ");
scanf("%d",&n);
printf("\n Enter the elements of matrix A \n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("\n Enter a[%d][%d] : ",i,j);
scanf("%d",&a[i][j]);
}
}

printf("\n Enter the elements of matrix B \n");


for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("\n Enter b[%d][%d] : ",i,j);
scanf("%d",&b[i][j]);
}
}

printf("\n Addition of matrices\n");


for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
c[i][j] = a[i][j] + b[i][j];
printf("%4d",c[i][j]);
}
printf("\n");
}

printf("\n Subtraction of matrices\n");


for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
d[i][j] = a[i][j] - b[i][j];
printf("%4d",d[i][j]);
}
printf("\n");
}
}

11. Program to find the factorial of a number

#include<stdio.h>
void main()
{
int num,i;
long int fact=1;
printf("\n Enter a number : ");
scanf("%d",&num);
for(i=1;i<=num;i++)
{
fact=fact*i;
}
printf("\n Factorial of %d = %ld",num,fact);
}
12. Program to generate fibonacci series
#include<stdio.h>
void main()
{
int n,i;
int F=0,S=1,NXT;
printf("\n Enter series limit(n) : ");
scanf("%d",&n);
printf("\n ******* FIBONACCI SERIES *******\n");
printf("%d \t %d",F,S);
for(i=3;i<=n;i++)
{
NXT=F+S;
printf("\t %d",NXT);
F = S;
S = NXT;
}
}

13. Program to find length of string without using built-in function

#include<stdio.h>
void main()
{
int len=0,i=0;
char *str;
printf("\ Enter a string : ");
scanf("%s",str);
while(str[i]!='\0')
{

len++;
i++;
}
printf("\n Length of string = %d",len);
}

14. Program to demonstrate string functions


#include<stdio.h>
#include<string.h>
void main()
{
char s[20],cpy[20],cmp[20],cat[20];
int value;
printf("\n Enter a String : ");
gets(s);
printf("\n 1. Length of the given String is : %d",strlen(s));
printf("\n The original string is: %s",s);
printf("\n Enter the string to be copied: ");
gets(cpy);
printf("\n 2. The copied string is : %s",strcpy(s,cpy));
printf("\n 3. Enter the String to be concatenated with %s: ",cpy);
gets(cat);
printf("\n Concatenation of two strings : %s ",strcat(cpy,cat));
printf("\n 4. Enter the string to be compared with %s : ",cpy);
gets(cmp);
value=strcmp(cpy,cmp);
if (value==0)
printf("\n Strings are same ");
else
printf("\n Strigs are not same ");
printf("\n 5. String in Lower case : %s",strlwr(s));
printf("\n 6. String in Upper case : %s",strupr(s));
printf("\n 7. Substring Search : %s",strchr(s,'L'));
printf("\n 8. Duplicate String : %s",strdup(s));
printf("\n 9. String Reverse : %s",strrev(s));
printf("\n 10. Set all characters to # : %s",strset(s,'#'));
}

Or
#include<stdio.h>
#include<string.h>
void main()
{
char str1[50];
char str2[50];
printf("\nEnter the strings: ");
scanf("%s%s",str1,str2);
printf("\n Entered String 1: %s \n Entered String 2: %s",str1,str2);
printf("\n 1. Length of %s: %d",str1,strlen(str1));
printf("\n 2. Comparison of strings: ");
if(strcmp(str1,str2)==0)
printf("Strings are same");
else
printf("Strings are not same");
printf("\n 3. String copy in the entered string 1: %s",strcpy(str1,"Hello "));
printf("\n 4. Concatenation of strings: %s",strcat(str1,str2));
printf("\n 5. String in lowercase: %s",strlwr(str1));
printf("\n 6. String in uppercase: %s",strupr(str1));
printf("\n 7. Substring search: %s",strchr(str1,'L'));
printf("\n 8. Duplicate string: %s",strdup(str1));
printf("\n 9. String reverse: %s",strrev(str1));
printf("\n 10. Set all characters to #: %s",strset(str1,'#'));
}

15. Program to read a string and to find the number of alphabets, digits,
vowels,consonants, spaces and special characters.
#include<stdio.h>
#include<ctype.h>
void main()
{
char str[100],ch;
int acount=0, dcount=0, vcount=0, ccount=0, scount=0,spcount=0,i=0;
printf("\n Enter a string : ");
gets(str);
while(str[i]!='\0')
{
if(isalpha(str[i]))
{
acount++;
ch=tolower(str[i]);
switch(ch)
{
case 'a':
case 'e':
case 'i':
case 'o':
case 'u': vcount++;
break; default
: ccount++;
}
}
else if(isdigit(str[i]))
dcount++;
else if(isspace(str[i]))
scount++;
else
spcount++;
i=i+1;
}
printf("\n No. of Alphabets = %d",acount);
printf("\n No. of Vowels = %d",vcount);
printf("\n No. of Consonants = %d",ccount);
printf("\n No. of Spaces = %d",scount);
printf("\n No. of Digits = %d",dcount);
printf("\n No. of Special symbols = %d",spcount);
}
Please note: The ctype. h header file declares functions used in character classification. Examples of such functions are
isalpha(), isdigit(), islower(), isupper() etc.

16.Program to swap two numbers using pointers


#include<stdio.h>
void main()
{
int n1,n2,*ptr1,*ptr2,temp;
printf("\n Enter two numbers : ");
scanf("%d %d",&n1, &n2);

printf("\n Before swapping n1 = %d n2 = %d\n",n1,n2);

ptr1=&n1;
ptr2=&n2;

temp=*ptr1;
*ptr1=*ptr2;
*ptr2=temp;

printf("\n After swapping n1 = %d n2 = %d\n",n1,n2);


}

17.Program to demonstrate student structure to read & display records of n


students
#include<stdio.h>
struct STUDENT
{
int regno;
char name[50];
char grade;
}Batch[50];

void main()
{
int n,i;
printf("\n Enter student count(n):");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\n Enter %d student(regno,name,grade) : ",i+1);
scanf("%d %s %c", &Batch[i].regno, &Batch[i].name, &Batch[i].grade);
}

printf("\n REGNO \t STUDENT NAME \t GRADE");


for(i=0;i<n;i++)
{
printf("\n %d \t %s \t %c", Batch [i].regno, Batch [i].name, Batch [i].grade);
}

18.Program to demonstrate the difference between structure & union


# include<stdio.h>
struct EMP1
{
int empid; //2 bytes
char name[50]; // 50 bytes
float salary; // 4 bytes (Total = 2 + 50 + 4 = 56)
};

union EMP2
{
int empid; // 2 bytes
char name[50]; // 50 bytes
float salary; // 4 bytes (highest = 50 bytes)
};

void main()
{
printf("\n Size of Structure EMP1 = %d",sizeof(struct EMP1));
printf("\n Size of Union EMP2 = %d",sizeof(union EMP2));
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy