Practical File CSE
Practical File CSE
Practical File CSE
: 18DCS040
PRACTICAL 1
Aim:
Write a C program that will output this passage by Michael Singer. Make sure
your output looks exactly as shown here (including spacing, line breaks,
punctuation, and the title and author). Use Required Escape Sequence and
ASCII Value.
Program Code:
#include<stdio.h>
#include<conio.h>
void main()
int a=4,b=1,c=3;
clrscr();
printf("\nDate : 24-07-2018\n");
printf("%c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c
%c %c %c %c %c %c %c %c %c %c %c %c %c %c %c
%c",b,a,c,b,a,c,b,a,c,b,a,c,b,a,c,b,a,c,b,a,c,b,a,c,b,a,c,b,a,c,b,a,c,b,a,c,b);
printf("\n%c",a);
printf("\"If you are resisting something, you are feeding it. ");
printf("\t\t\t%c",a);
printf("\n%c",c);
printf("\t\t\t%c",c);
printf("\n%c",b);
DEPSTAR(CSE) Page 1
Computer Concepts and Programming[CE141] ID No. : 18DCS040
printf("\t\t\t%c",b);
printf("\n%c",a);
printf("\t%c",a);
printf("\n%c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c
%c %c %c %c %c %c %c %c %c %c %c %c %c %c %c
%c",b,a,c,b,a,c,b,a,c,b,a,c,b,a,c,b,a,c,b,a,c,b,a,c,b,a,c,b,a,c,b,a,c,b,a,c,b);
getch();
Output:
Conclusion:
In this program we learnt the use of escape sequence and the ASCII values of various symbols.
DEPSTAR(CSE) Page 2
Computer Concepts and Programming[CE141] ID No. : 18DCS040
PRACTICAL 2
Aim:
Ramesh‟s basic salary is input through the keyboard. His dearness allowance
is 40% of basic salary, and house rent allowance is 20% of basic salary. Write
a program to calculate his gross salary.
Program Code:
#include<stdio.h>
#include<conio.h>
void main()
int basic_salary;
float da,hra,gross_salary;
clrscr();
printf("\nDate : 24-07-2018\n");
scanf("%d",&basic_salary);
da=0.4*basic_salary;
hra=0.2*basic_salary;
gross_salary=basic_salary+hra+da;
getch();
DEPSTAR(CSE) Page 3
Computer Concepts and Programming[CE141] ID No. : 18DCS040
Output:
Conclusion:
In this program we learnt the use basic arithmetic operator multiplication(*).
DEPSTAR(CSE) Page 4
Computer Concepts and Programming[CE141] ID No. : 18DCS040
PRACTICAL 3
Aim:
Write a program to calculate area of two circle. (πr2). Use Preprocessor
directive named macro expansion for the symbol π (Symbolic Constant)
without argument and with argument. Use typedef to rename the float
datatype.
Program Code:
#include<stdio.h>
#include<conio.h>
#define PI 3.1415
void main()
int radius;
float area,area_wa;
clrscr();
printf("\nDate : 24-07-2018\n");
scanf("%d",&radius);
area=PI*radius*radius;
area_wa=AREA;
getch();
DEPSTAR(CSE) Page 5
Computer Concepts and Programming[CE141] ID No. : 18DCS040
Output:
Conclusion:
In this program we learnt the use of symbolic constants.
DEPSTAR(CSE) Page 6
Computer Concepts and Programming[CE141] ID No. : 18DCS040
PRACTICAL 4 (a)
Aim:
Write a program to do following:
(a) Input an amount and convert it into rupees and paisa. (For Ex. 25.67
Rs = 25 Rs and 67 Paisa). (Implicit type Conversion)
Program Code:
#include<stdio.h>
#include<conio.h>
void main()
int Rs,Ps;
float amount;
clrscr();
printf("\nDate : 31-07-2018\n");
scanf("%f", &amount);
Rs=(int)amount;
Ps=(amount-Rs)*100;
getch();
DEPSTAR(CSE) Page 7
Computer Concepts and Programming[CE141] ID No. : 18DCS040
Output:
Conclusion:
In this program we learnt the concept of implicit type coversion.
DEPSTAR(CSE) Page 8
Computer Concepts and Programming[CE141] ID No. : 18DCS040
PRACTICAL 4(b)
Aim:
Write a program to do following:
(b) Input No of female and No of male and calculate the ratio of females
to males in a town. No of female and No of male are in int and ratio is
in float. (For Ex. No_of_Female = 10 & No_of_Male = 7 then ratio =
1.43). (Explicit type Conversion)
Program Code:
#include<stdio.h>
#include<conio.h>
void main()
int F,M;
float Ratio;
clrscr();
printf("\nDate : 31-07-2018\n");
scanf("%d",&F);
scanf("%d",&M);
Ratio=(float) F/M;
getch();
DEPSTAR(CSE) Page 9
Computer Concepts and Programming[CE141] ID No. : 18DCS040
Output:
Conclusion:
In this program we learnt the cocept of explicit type coversion.
DEPSTAR(CSE) Page 10
Computer Concepts and Programming[CE141] ID No. : 18DCS040
PRACTICAL 5
Aim:
While purchasing certain items, a discount of 10% is offered if the quantity
purchased is more than 1000.If quantity and price per item are input through
the keyboard, write a program to calculate the total expenses. Use Simple If
statement.
Program Code:
#include<stdio.h>
#include<conio.h>
void main()
int quantity;
float price,discount=0,total_expense,amount;
clrscr();
printf("\nDate : 31-07-2018");
scanf("%d",&quantity);
scanf("%f",&price);
amount=quantity*price;
if(quantity>1000)
discount=0.1*amount;
total_expense=amount-discount;
DEPSTAR(CSE) Page 11
Computer Concepts and Programming[CE141] ID No. : 18DCS040
getch();
Output:
Conclusion:
In this program we learnt the syntax and working of simple if statement.
DEPSTAR(CSE) Page 12
Computer Concepts and Programming[CE141] ID No. : 18DCS040
PRACTICAL 6
Aim:
Given three points (x1, y1), (x2, y2) and (x3, y3), write a program to check if
all the three points fall on one straight line. Use fabs() function of <
maths.h> header file. Use if…else statement.
Program Code:
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
int x1,x2,x3,y1,y2,y3;
float s1,s2;
clrscr();
printf("\nDate : 31-07-2018\n");
scanf("%d",&x1);
scanf("%d",&y1);
scanf("%d",&x2);
scanf("%d",&y2);
DEPSTAR(CSE) Page 13
Computer Concepts and Programming[CE141] ID No. : 18DCS040
scanf("%d",&x3);
scanf("%d",&y3);
s1=fabs((y2-y1)/(x2-x1));
s2=fabs((y3-y2)/(x3-x2));
if(s1==s2)
else
getch();
Output:
Conclusion:
In this program we learnt the use of if…..else statement and fabs() which is available in math.h
header file.
DEPSTAR(CSE) Page 14
Computer Concepts and Programming[CE141] ID No. : 18DCS040
PRACTICAL 7
Aim:
If the three sides of a triangle are entered through the keyboard, write a
program to check whether the triangle is valid or not. The
triangle is valid if the sum of two sides is greater than the largest of
the three sides. Use nested if…else statement.
Program code:
#include<stdio.h>
#include<conio.h>
void main()
int side1,side2,side3;
clrscr();
printf("\nDate : 07-08-2018\n");
scanf("\n%d %d %d",&side1,&side2,&side3);
if((side1+side2)>side3)
if((side2+side3>side1))
if((side3+side1)>side2)
DEPSTAR(CSE) Page 15
Computer Concepts and Programming[CE141] ID No. : 18DCS040
else
getch();
Output:
(i)
(ii)
Conclusion:
In this program we learnt the use of nested if…else statement.
DEPSTAR(CSE) Page 16
Computer Concepts and Programming[CE141] ID No. : 18DCS040
PRACTICAL 8
Aim:
An Insurance company follows following rules to calculate premium.
(1) If a person‟s health is excellent and the person is between 25 and 35 years
of age and lives in a city and is a male then the premium is Rs. 4 per
thousand and his policy amount cannot exceed Rs. 2 lakhs.
(2) If a person satisfies all the above conditions except that the gender is
female then the premium is Rs. 3 per thousand and her policy amount
cannot exceed Rs. 1 lakh.
(3) If a person‟s health is poor and the person is between 25 and 35 years of
age and lives in a village and is a male
then the premium is Rs. 6 per thousand and his policy cannot exceed Rs.
10,000.
(4) In all other cases the person is not insured.
Write a program to output whether the person should be insured or not,
his/her premium rate and maximum amount for which he/she can be
insured. Use Else…if Ladder.
Program code:
#include<stdio.h>
#include<conio.h>
void main()
int age;
char gender,place,health;
clrscr();
printf("\nDate : 07-08-2018\n");
scanf("%d",&age);
DEPSTAR(CSE) Page 17
Computer Concepts and Programming[CE141] ID No. : 18DCS040
scanf(" %c",&place);
scanf(" %c",&health);
scanf(" %c",&gender);
if((age>25)&&(age<35)&&(gender=='m')&&(place=='c')&&(health=='e'))
else if((age>25)&&(age<35)&&(gender=='f')&&(place=='c')&&(health=='e'))
else if((age>25)&&(age<35)&&(gender=='m')&&(place=='v')&&(health=='p'))
else
getch();
DEPSTAR(CSE) Page 18
Computer Concepts and Programming[CE141] ID No. : 18DCS040
Output:
(i)
(ii)
Conclusion:
In this code we learnt the use of else…if ladder and also the use of logical operator (&&).
DEPSTAR(CSE) Page 19
Computer Concepts and Programming[CE141] ID No. : 18DCS040
PRACTICAL 9
Aim:
Write a program to input a character using getchar() and print the character
using putchar() and check the character category. Also convert uppercase
alphabet to lower case and vice versa. (Use Character Test Functions :
isalnum(), isalpha(), isdigit(), islower(), isprint(), ispunct(), isspace(),
isupper()) and (toupper() & tolower()) of <ctype.h> header file.
Program Code:
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
void main()
char a;
clrscr();
printf("\nDate : 07-08-2018\n");
a=getchar();
if((isalnum(a))&&(isprint(a)))
if(isalpha(a))
DEPSTAR(CSE) Page 20
Computer Concepts and Programming[CE141] ID No. : 18DCS040
if(isupper(a))
a=tolower(a);
putchar(a);
else if(islower(a))
a=toupper(a);
putchar(a);
if(isdigit(a))
putchar(a);
else if((ispunct(a))&&(isprint(a)))
DEPSTAR(CSE) Page 21
Computer Concepts and Programming[CE141] ID No. : 18DCS040
putchar(a);
else if((isspace(a))&&(isprint(a)))
putchar(a);
getch();
Output:
(i)
(ii)
DEPSTAR(CSE) Page 22
Computer Concepts and Programming[CE141] ID No. : 18DCS040
(iii)
(iv)
(v)
Conclusion
In this program we learnt the use of various charaacter functions from header file ctypr.h and
also the use of two input and output functions getchar() and putchar().
DEPSTAR(CSE) Page 23
Computer Concepts and Programming[CE141] ID No. : 18DCS040
PRACTICAL 10
Aim:
Write a program to find the grace marks for a student using Switch
Statement. The user should enter the class obtained by the student and the
number of subjects he has failed in.
1. If the student gets first class and the number of subjects he failed in is
greater than 3, then he does not get any grace. If the number of
subjects he failed in is less than or equal to 3 then the grace is of 5
marks per subject.
2. If the student gets second class and the number of subjects he failed
in is greater than 2, then he does not get any grace. If the number of
subjects he failed in is less than or equal to 2 then the grace is of 4
marks per subject.
3. If the student gets third class and the number of subjects he failed in
is greater than 1, then he does not get any grace. If the number of
subjects he failed in is equal to 1 then the grace is of 5 marks per
subject.
Program Code:
#include<stdio.h>
#include<conio.h>
void main()
int fail_sub;
int Class;
clrscr();
printf("\nDate : 07-08-2018\n");
scanf("%d",&Class);
DEPSTAR(CSE) Page 24
Computer Concepts and Programming[CE141] ID No. : 18DCS040
scanf("%d",&fail_sub);
switch(Class)
else if(fail_sub<=3)
break;
else if(fail_sub<=2)
break;
DEPSTAR(CSE) Page 25
Computer Concepts and Programming[CE141] ID No. : 18DCS040
else if(fail_sub>=1)
break;
getch();
Output:
(i)
(ii)
Conclusion:
In this program we learnt the use of switch case.
DEPSTAR(CSE) Page 26
Computer Concepts and Programming[CE141] ID No. : 18DCS040
PRACTICAL 11
Aim:
Write a program to calculate following series using if and goto statement.
Compare the results using for loop.
1^2+2^2+…….n^2.
Program Code:
#include<stdio.h>
#include<conio.h>
void main()
int n,a=1,i;
clrscr();
printf("\nDate : 13-08-2018");
scanf("%d",&n);
flag:
sum1=sum1+(a*a);
a++;
if(a<=n)
goto flag;
DEPSTAR(CSE) Page 27
Computer Concepts and Programming[CE141] ID No. : 18DCS040
for(i=1;i<=n;i++)
sum2=sum2+(i*i);
getch();
Output:
Conclusion:
In this program we learnt the use of goto statement which is a jump statement.
DEPSTAR(CSE) Page 28
Computer Concepts and Programming[CE141] ID No. : 18DCS040
PRACTICAL 12
Aim:
An “Armstrong number” is an n-digit number that is equal to the sum of the
nth powers of its individual digits. For example, 153 is an Armstrong number
because it has 3 digits and 13+ 53+33 =153. Similarly 1634 is an Armstrong
number because it has 4 digits and 14+ 64+34 + 44 = 1634. Write a program
to find whether the entered number is Armstrong or not using While Loop.
Program Code:
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
long num,num2,rem,i=0,ans=0;
clrscr();
printf("\nDate : 13-08-2018\n");
scanf("%ld",&num);
num2=num;
while(num2!=0)
i++;
num2=num2/10;
DEPSTAR(CSE) Page 29
Computer Concepts and Programming[CE141] ID No. : 18DCS040
num2=num;
while(num2!=0)
rem=num2%10;
ans=ans+pow(rem,i);
num2=num2/10;
if(num==ans)
else
getch();
Output:
(i)
DEPSTAR(CSE) Page 30
Computer Concepts and Programming[CE141] ID No. : 18DCS040
(ii)
Conclusion:
In this program we learnt the concept of looping structuires and the while loop.
DEPSTAR(CSE) Page 31
Computer Concepts and Programming[CE141] ID No. : 18DCS040
PRACTICAL 13
Aim:
Write a menu driven program which has following options:
1. Prime or not
2. Perfect number or not
3. Factorial of a number
4. Exit
Use do...while statement so that the menu is displayed at least once. Also use
Switch statement.
Program Code:
#include<stdio.h>
#include<conio.h>
void main()
int choice,num;
clrscr();
printf("\nDate : 20-08-2018\n");
while(1)
printf("\nMENU!");
DEPSTAR(CSE) Page 32
Computer Concepts and Programming[CE141] ID No. : 18DCS040
scanf("%d",&choice);
switch(choice)
case 1: {
int c=0,i;
scanf("%d",&num);
for(i=1;i<=num;i++)
if(num%i==0)
c++;
if(c==2)
printf("\nNumber is prime.");
else
printf("\nNot prime.");
getch();
clrscr();
break;
case 2: {
int i=0,sum=0;
printf("\n\nPerfectNumberor not");
DEPSTAR(CSE) Page 33
Computer Concepts and Programming[CE141] ID No. : 18DCS040
scanf("%d",&num);
for(i=1;i<num;i++)
if(num%i==0)
sum=sum+i;
if(sum==num)
getch();
clrscr();
break;
case 3: {
int fact=1,i;
printf("\n\nFactorial of number");
scanf("%d",&num);
for(i=1;i<=num;i++)
fact=fact*i;
getch();
clrscr();
DEPSTAR(CSE) Page 34
Computer Concepts and Programming[CE141] ID No. : 18DCS040
break;
case 4: {
getch();
exit(0);
break;
getch();
clrscr();
Output:
(i)
DEPSTAR(CSE) Page 35
Computer Concepts and Programming[CE141] ID No. : 18DCS040
(ii)
(iii)
DEPSTAR(CSE) Page 36
Computer Concepts and Programming[CE141] ID No. : 18DCS040
(iv)
Conclusion:
In this program we made use of while loops and switch case to find various outputs according to
the user’s choice.
DEPSTAR(CSE) Page 37
Computer Concepts and Programming[CE141] ID No. : 18DCS040
PRACTICAL 14
Aim:
Write a program to print the following pattern using Nested for loop
Pattern 1:
*
**
***
****
*****
Program code:
#include<stdio.h>
#include<conio.h>
void main()
int i,j,num;
clrscr();
printf("\nDate : 20-08-2018\n");
scanf("%d",&num);
for(i=0;i<num;i++)
for(j=0;j<=i;j++)
printf("* ");
DEPSTAR(CSE) Page 38
Computer Concepts and Programming[CE141] ID No. : 18DCS040
printf("\n");
getch();
Output:
Conclusion:
In this program we learnt the use of nested for loops to pront various patterns.
DEPSTAR(CSE) Page 39
Computer Concepts and Programming[CE141] ID No. : 18DCS040
Pattern 2:
1
35
7 9 11
13 15 17 19
21 23 25 27 29
Souce Code:
#include<stdio.h>
#include<conio.h>
void main()
int i,j,k,c=1;
clrscr();
printf("\nDate : 20-08-2018\n\n");
for(i=1;i<=5;i++)
for(j=4;j>=i;j--)
printf(" ");
for(k=1;k<=i;k++)
DEPSTAR(CSE) Page 40
Computer Concepts and Programming[CE141] ID No. : 18DCS040
printf("%3d",c);
c+=2;
printf("\n");
getch();
Output:
Conclusion:
In this program we learnt to print various patterns using nested for loop.
DEPSTAR(CSE) Page 41
Computer Concepts and Programming[CE141] ID No. : 18DCS040
Pattern 3:
1
1 2
3 5 8
13 21 34 55
Source Code:
#include<stdio.h>
#include<conio.h>
void main()
int i,j,k,a=0,b=1,c;
clrscr();
printf("\nDate : 20-08-2018\n\n");
for(i=1;i<5;i++)
for(j=4;j>=i;j--)
printf(" ");
for(k=1;k<=i;k++)
c=a+b;
printf("%3d ",c);
b=a;
DEPSTAR(CSE) Page 42
Computer Concepts and Programming[CE141] ID No. : 18DCS040
a=c;
printf("\n");
getch();
Output:
Conclusion:
In this program we learnt to print various patterns using nested for loops.
DEPSTAR(CSE) Page 43
Computer Concepts and Programming[CE141] ID No. : 18DCS040
Pattern 4:
AAAAA
BBBB
CCC
DD
F
Source Code:
#include<stdio.h>
#include<conio.h>
void main()
int i,j,k;
char ch='A';
clrscr();
printf("\nDate : 20-08-2018\n\n");
for(i=1;i<=5;i++)
for(j=1;j<i;j++)
printf(" ");
for(k=5;k>=i;k--)
printf("%c",ch);
DEPSTAR(CSE) Page 44
Computer Concepts and Programming[CE141] ID No. : 18DCS040
ch++;
printf("\n");
getch();
Output:
Conclusion:
In this program we learnt to print various patterns using nested for loop.
DEPSTAR(CSE) Page 45
Computer Concepts and Programming[CE141] ID No. : 18DCS040
HOMEWORK 1
Aim:
Write a program for a match-stick game between the computer and a user. Your Program
should ensure that the computer always wins. Rules for the games are as follows:
There are 21 match-sticks. The computer asks the player to pick 1, 2, 3, or 4 match-sticks.
After the person picks, the computer does its picking. Whoever is forced to pick up the last
match-stick loses the game. Use while loop, break and Continue Statements.
Program code:
#include<stdio.h>
#include<conio.h>
void main()
int n,n1=21,i;
clrscr();
printf("\nDate : 20-08-2018\n");
printf("RULES:-\n");
printf("1) There are 21 Matchsticks. Each player has to pick any number of matchsticks from 1
to 4\n");
printf("3) The player who picks the last matchstick will loose\n");
while (n1>0)
DEPSTAR(CSE) Page 46
Computer Concepts and Programming[CE141] ID No. : 18DCS040
scanf("%d",&n);
i=5-n;
n1=n1-n-i;
if (n1!=1)
continue;
if (n1==1)
break;
printf("\nComputer Wins!");
getch();
DEPSTAR(CSE) Page 47
Computer Concepts and Programming[CE141] ID No. : 18DCS040
Output:
Conclusion:
In this program we had made a game of 21 matchsticks in which the computer always wins the
game by picking matchsticks five minus the matchsticks user picked.
DEPSTAR(CSE) Page 48
Computer Concepts and Programming[CE141] ID No. : 18DCS040
PRACTICAL 15
Aim:
Write a program to that will scan an array from the user and print maximum
and minimum number on the screen. Also find the index of given specific
element from the user.
Program Code:
#include<stdio.h>
#include<conio.h>
void main()
int a[100],n,max=0,min,i;
clrscr();
printf("\nDate : 27-08-2018\n");
scanf("%d",&n);
for (i=0;i<n;i++)
scanf("%d",&a[i]);
for (i=0;i<n;i++)
if (max<=a[i])
max=a[i];
if (min>=a[i])
DEPSTAR(CSE) Page 49
Computer Concepts and Programming[CE141] ID No. : 18DCS040
min=a[i];
printf("\nMax:%d",max);
printf("\nMin:%d",min);
printf("\n\nEnter the value for which you need to display the index : ");
scanf("%d",&val);
for(i=0;i<10;i++)
if(arr[i]==val)
count=i;
break;
getch();
DEPSTAR(CSE) Page 50
Computer Concepts and Programming[CE141] ID No. : 18DCS040
Output:
Conclusion:
In this program we learnt the concepts of array and found the maximum,minimun and the index
of a number in an array.
DEPSTAR(CSE) Page 51
Computer Concepts and Programming[CE141] ID No. : 18DCS040
PRACTICAL 16
Aim:
Write a program to sort elements of an array in ascending order using
Bubble Sort.
Program Code:
#include<stdio.h>
#include<conio.h>
void main()
int A[5],i,j,temp=0;
clrscr();
printf("\nDate : 27-08-2018\n");
for(i=0;i<5;i++)
scanf("%d",&A[i]);
for(i=0;i<4;i++)
for(j=0;j<4;j++)
if(A[j]>A[j+1])
temp=A[j];
A[j]=A[j+1];
DEPSTAR(CSE) Page 52
Computer Concepts and Programming[CE141] ID No. : 18DCS040
A[j+1]=temp;
for(i=0;i<5;i++)
printf("%d\n",A[i]);
getch();
Output:
Conclusion:
In this program we used the method of Bubble Sort to sort the entered array in ascending order.
DEPSTAR(CSE) Page 53
Computer Concepts and Programming[CE141] ID No. : 18DCS040
PRACTICAL 17
Aim:
Write a program that reads an M x N matrix A and prints its elements in spiral order. You
should start from the element in the 0th row and 0th column in the matrix and proceed in a
spiral order as shown below.
1→ 2 → 3 → 4
5→6→7 8
↑ ↓ ↓
9 10←11 12
↑ ↓
13←14←15← 16
Program code:
#include<stdio.h>
#include<conio.h>
void main()
int a[100][100],n,i,j,m,k=0,l=0;
clrscr();
printf("\nDate : 03-09-2018\n");
scanf("%d%d",&m,&n);
DEPSTAR(CSE) Page 54
Computer Concepts and Programming[CE141] ID No. : 18DCS040
for (i=0;i<m;i++)
for (j=0;j<n;j++)
scanf("%d",&a[i][j]);
for (i=0;i<m;i++)
for (j=0;j<n;j++)
printf(" %d ",a[i][j]);
printf("\n");
printf("\n\n\n");
k++;
DEPSTAR(CSE) Page 55
Computer Concepts and Programming[CE141] ID No. : 18DCS040
n--;
if ( k < m)
m--;
if (l < n)
l++;
getch();
DEPSTAR(CSE) Page 56
Computer Concepts and Programming[CE141] ID No. : 18DCS040
Output:
Conclusion:
In this practical we have inputed a matrix and gave output by printing the matrix in spiral form
by using four for loops.
DEPSTAR(CSE) Page 57