PST Programs
PST Programs
#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);
}
#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);
}
#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");
}
# 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;
#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]);
}
}
#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;
}
}
#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);
}
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.
ptr1=&n1;
ptr2=&n2;
temp=*ptr1;
*ptr1=*ptr2;
*ptr2=temp;
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);
}
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));
}