0% found this document useful (0 votes)
129 views16 pages

PST Manual

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
129 views16 pages

PST Manual

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

APS COLLEGE OF ARTS AND SCIENCE

NR.Colony, Bangalore-560019

Department of Computer Science


Problem Solving Techniques Using C
LIST OF PROGRAMS

1 Program to read the radius of the circle and to find area and circumference
2 Program to read the numbers and find the biggest of three.
3 Program to check whether the number is prime or not.
4 Program to find the root of quadratic equation.
5 Program to read a number, find the sum of the digits, reverse the number and check it for
palindrome.
6 Program to read numbers from keyboard continuously till the user presses 999 and find the sum of
only positive numbers.
7 Program to read percentage of marks and to display appropriate message using else if ladder.
8 Program to simulate a simple calculator using switch case
9 Program to read marks scored by n students and find the average of mark (Single dimensional array)
10 Program to remove duplicate elements in a single dimensional array.

11 Program to find the factorial of a number.


12 Program to generate Fibonacci series.
13 Program to demonstrate string functions.
14 Program to find the length of the string without using built-in function.
15 Program to read, display and add two n x m matrices using function.
16 Program to read a string and to find the number of alphabets, digits, vowels, consonants, space and
special characters.
17 Program to swap two numbers using pointers.
18 Program to demonstrate student structure to read & display records of n students.
19 Program to demonstrate the difference between structure and union.
20 Program to design the following pattern using nested for loop:
*
* *
** *
* * * *
* * * * *
1. Program to read the radius of the circle and to find area and circumference.

#include<stdio.h>
#include<conio.h>
void main()
{
int r;
float area,circum;
clrscr();
printf("Enter radius of a circle : ");
scanf("%d",&r);
area=3.14*r*r;
circum=2*3.14*r;
printf("Area of a circle = %f\n",area);
printf("Circumference of a circle = %f",circum);
getch();
}

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

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
printf("Enter 3 numbers to compare:\n ");
scanf("%d%d%d",&a,&b,&c);
if((a>b)&&(a>c))
printf("a is biggest");
else if(b>c)
printf("b is biggest");
else
printf("c is biggest");
getch();
}
3. Program to check whether the number is prime or not.

#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,c=0;
clrscr();
printf("Enter a number \n");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
if(n%i==0)
c++;
}
if(c==2)
printf("%d is a prime number",n);
else
printf("%d is not a prime number",n);
getch();
}

4. Program to find the root of quadratic equation.

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
double a,b,c,dis,r1,r2,real,img;
clrscr();
printf("Enter values of a,b and c:\n");
scanf("%lf%lf%lf",&a,&b,&c);
dis=b*b-4*a*c;
if(dis>0)
{
r1=-b+sqrt(dis)/(2*a);
r2=-b-sqrt(dis)/(2*a);
printf("Roots are real and different\n");
printf("Roots=%lf \t Root2=%lf",r1,r2);
}
else if(dis==0)
{
r1=r2=-b/(2*a);
printf("Roots are real and equal\n");
printf("Roots=%lf \t Root2=%lf",r1,r2);
}
else
{
real=-b/2*a;
img=sqrt(-dis)/(2*a);
printf("Roots are real and imaginary\n");
printf("Root1=%lf+%lfi \t Root2=%lf-%lfi",real,img,real,img);
}
getch();
}

5. Program to read a number, find the sum of the digits, reverse the number and check it for
palindrome.

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

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

#include<stdio.h>
#include<conio.h>
void main()
{
int n,sum=0;
clrscr();
printf("\nEnter any number:\n");
printf("To STOP press 999\n");
while(1)
{
scanf("%d",&n);
if(n==999)
break;
if(n>0)
sum = sum + n;
}
printf("Sum of only positive numbers = %d",sum);
getch();
}
7. Program to read percentage of marks and to display appropriate message. If a percentage
is 70 and above- Distinction, 60-69 – First Class, 50-59 – Second Class, 40-49 Pass, below 40 –
Fail.(Demonstrate of if-else ladder)

#include<stdio.h>
#include<conio.h>
void main()
{
int per;
clrscr();
printf("\nEnter the percentage:\n");
scanf("%d",&per);
if(per>=70)
printf("Congration!, You have secured Distinction");
else if(per>=60)
printf("You have secured First class");
else if(per>=50)
printf("You have secured Second class");
else if(per>=40)
printf("You have secured Pass class");
else
printf("You have Failed");
getch();
}

8. Program to simulate a simple calculator with addition, subtraction, multiplication, division


and it should display the error message for division of zero using switch case.

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
char op;
clrscr();
printf("\nEnter an operator (+, -, *, /):\n");
scanf("%c",&op);
printf("Enter 2 operands: ");
scanf("%d%d",&a, &b);
switch(op)
{
case '+':printf("Addition = %d",a+b);
break;
case '-':printf("Subtraction= %d",a-b);
break;
case '*':printf("Multiplication= %d",a*b);
break;
case '/':if(b!=0)
printf("Division= %d",a/b);
else
printf("Error:Number cannot divisible by zero");
break;
default: printf("Invalid operator");
}
getch();
}

9. Program to read marks scored by n students and find the average of mark (Demonstration
of single dimensional array)

#include<stdio.h>
#include<conio.h>
void main()
{
int n,i;
float marks[10], sum=0.0,avg=0.0;
clrscr();
printf("\n Enter no of students:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter student %d marks:\n",i+1);
scanf("%f",&marks[i]);
sum=sum + marks[i];
}
avg = sum / n;
printf("Total marks = %f\n",sum);
printf("Average marks = %f",avg);
getch();
}

10. Program to remove duplicate elements in a single dimensional array.

#include<stdio.h>
#include<conio.h>
void main()
{
int a[20],n,i,j,k;
clrscr();
printf("Enter n:\n");
scanf("%d",&n);
printf("Enter array elements:\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
for(j=i+1;j<n;j++)
if(a[i]==a[j])
{
for(k=j;k<n;k++)
a[k]=a[k+1];
j--;
n--;
}
printf("After deleting duplication:\n");
for(i=0;i<n;i++)
printf("%d\t",a[i]);
getch();
}
11. Program to find the factorial of a number
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,fact=1;
clrscr();
printf("\nEnter a number:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
fact=fact*i;
}
printf("Factorial of %d = %d",n,fact);
getch();
}

12. Program to generate Fibonacci series.


#include<stdio.h>
#include<conio.h>
void main()
{
int a=0,b=1,c,n,i;
clrscr();
printf("\nEnter no of terms: ");
scanf("%d",&n);
printf("Fibonacci series:%d,%d",a,b);
for(i=3;i<=n;i++)
{
c=a+b;
printf(",%d",c);
a = b;
b = c;
}
getch();
}
13. Program to demonstrate string functions. (String Length, String Copy, String
Concatenate, String Comparison)

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char s1[10]="APS",s2[10]="College";
clrscr();
printf("\nString 1 = %s\n",s1);
printf("String 2 = %s\n",s2);
printf("Length of string 1=%d\n",strlen(s1));
printf("Concatination of string 1 and 2:%s\n",strcat(s1,s2));
printf("Copying a new string to
s2:%s\n",strcpy(s2,"BCA"));
printf("After copying s2:%s\n",s2);
if(strcmp(s1,s2)==0)
printf("Strings are equal");
else
printf("Strings are not equal");
getch();
}

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

#include<stdio.h>
#include<conio.h>
void main()
{
char str[20];
int len=0;
clrscr();
printf("\nEnter a string : ");
gets(str);
while(str[len]!='\0')
{
len++;
}
printf("Length of string = %d",len);
getch();
}

15. Program to read, display and add two n x m matrices using function.

#include<stdio.h>
#include<conio.h>
void display(int x[20][20], int row, int col)
{
int i,j;
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
printf("%d\t",x[i][j]);
}
printf("\n");
}
}
void main()
{
int row,col,i,j,a[20][20],b[20][20],sum[20][20];
clrscr();
printf("Enter no of rows and columns:\n");
scanf("%d%d",&row,&col);
printf("Enter %d elements of first
matrix:\n",row*col);
for(i=0;i<row;i++)
for(j=0;j<col;j++)
scanf("%d",&a[i][j]);
printf("Enter %d elements of second matrix:\n",row*col);
for(i=0;i<row;i++)
for(j=0;j<col;j++)
scanf("%d",&b[i][j]);
for(i=0;i<row;i++)
for(j=0;j<col;j++)
{
sum[i][j]=a[i][j]+b[i][j];
}
printf("Addition of two matrix:\n");
display(sum,row,col);
getch();
}

16. Program to read a string and to find the number of alphabets, digits, vowels, consonants,
space and special characters.

#include<stdio.h>
#include<conio.h>
#include<ctype.h>
void main()
{
char ch[50];
int i,alpha_c=0,digit_c=0,vowel_c=0,con_c=0,space_c=0,spl_c=0;
clrscr();
printf("Enter a string\n");
gets(ch);
for(i=0;ch[i]!='\0';i++)
{
if(isalpha(ch[i]))
{
alpha_c++;
if(ch[i]=='a'||ch[i]=='e'||ch[i]=='i'||ch[i]=='o'||ch[i]=='
u')
vowel_c++;
else
con_c++;
}
else if(isdigit(ch[i]))
digit_c++;
else if(isspace(ch[i]))
space_c++;
else
spl_c++;
}
printf("No. of Alphabets = %d\n",alpha_c);
printf("No. of Vowels = %d\n",vowel_c);
printf("No. of Consonants = %d\n",con_c);
printf("No. of Digits = %d\n",digit_c);
printf("No. of Spaces = %d\n",space_c);
printf("No. of Special symbols = %d",spl_c);
getch();
}

17. Program to swap two numbers using pointers

#include<stdio.h>
#include<conio.h>
void swapno(int *a,int *b)
{
int temp;
temp=*a;
*a=*b;
*b=temp;
}
void main()
{
int n1,n2;
clrscr();
printf("Enter 2 numbers:\n");
scanf("%d%d",&n1,&n2);
printf("Before swapping n1=%d n2=%d\n",n1,n2);
swapno(&n1,&n2);
printf("After swapping n1=%d n2=%d",n1,n2);
getch();
}

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

#include<stdio.h>
#include<conio.h>
struct student
{
int roll;
char name[30];
};
void main()
{
struct student s[50];
int i,n;
clrscr();
printf("Enter no of students:\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter rollno and name of %d student:",i+1);
scanf("%d%s",s[i].roll,s[i].name);
}
printf("\nStudent details:\n");
for(i=0;i<n;i++)
{
printf("\n %d \t %s", s[i].roll, s[i].name);
}
getch();
}
19. Program to demonstrate the difference between structure and union for the following
Student name, Student roll no.

#include<stdio.h>
#include<conio.h>
struct bca
{
int rno;
char name[20];
};
union bsc
{
int rno;
char name[20];
};
void main()
{
struct bca s={1001,"ramu"};
union bsc u={2001};
clrscr();
printf("\nDifference on size\n");
printf("size of structure=%d\n",sizeof(s));
printf("size of union=%d\n",sizeof(u));
printf("\n");
printf("Difference on accessing all members at a time\n");
printf("Structure members:Regno=%d Name=%s\n",s.rno,s.name);
printf("Union members:Regno=%d Name=%s\n",u.rno,u.name);
getch();
}

20. Program to design the following pattern using nested for loop:
*
* *
* * *
* * * *
* * * * *
#include <stdio.h>
#include <conio.h>
void main()
{
int n,i,j,k=0;
clrscr();
printf("Enter no of the rows:");
scanf("%d",&n);
for(i=1;i<=n; i++)
{
for(j=1;j<=n-i; j++)
{
printf (" ");
}
for(k=1;k<=(2*i-1); k++)
{
printf ("* ");
}
printf ("\n");
}
getch();
}

You might also like