Nothing Special   »   [go: up one dir, main page]

Practical File CSE

Download as pdf or txt
Download as pdf or txt
You are on page 1of 57

Computer Concepts and Programming[CE141] ID No.

: 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("Name : Yash Makadiya");

printf("\nID No.: 18DCS040");

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("\tAny energy you fight you are feeding it.");

printf("\t\t\t%c",c);

printf("\n%c",b);

DEPSTAR(CSE) Page 1
Computer Concepts and Programming[CE141] ID No. : 18DCS040

printf("\t\tIf you are pushing something away,");

printf("\t\t\t%c",b);

printf("\n%c",a);

printf("\t\t\tYou are inviting it to stay.\"by Michael Singer.");

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("Name : Yash Makadiya");

printf("\nID No.: 18DCS040");

printf("\nDate : 24-07-2018\n");

printf("\nEnter Basic Salary : ");

scanf("%d",&basic_salary);

da=0.4*basic_salary;

hra=0.2*basic_salary;

gross_salary=basic_salary+hra+da;

printf("\n\nThe gross salary is : %f",gross_salary);

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

#define AREA PI*radius*radius

void main()

int radius;

float area,area_wa;

clrscr();

printf("Name : Yash Makadiya");

printf("\nID No.: 18DCS040");

printf("\nDate : 24-07-2018\n");

printf("\nPlease enter the radius : ");

scanf("%d",&radius);

area=PI*radius*radius;

printf("\nThe area of circle is : %f",area);

area_wa=AREA;

printf("\nthe area of circle with directive using arguements: %f",area_wa);

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("Name : Yash Makadiya");

printf("\nID No.: 18DCS040");

printf("\nDate : 31-07-2018\n");

printf("\nEnter the amount = ");

scanf("%f", &amount);

Rs=(int)amount;

Ps=(amount-Rs)*100;

printf("%d Rupees %d Paisa",Rs,Ps);

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("Name : Yash Makadiya");

printf("\nID No.: 18DCS040");

printf("\nDate : 31-07-2018\n");

printf("\nEnter the No. of Female:",F);

scanf("%d",&F);

printf("Enter the No. of Male:",M);

scanf("%d",&M);

Ratio=(float) F/M;

printf("Ratio of Female to Male =%.2f",Ratio);

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("Name : Yash Makadiya");

printf("\nID No.: 18DCS040");

printf("\nDate : 31-07-2018");

printf("\nEnter the quantity: ");

scanf("%d",&quantity);

printf("Enter the price : ");

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

printf("The total expense is :%f ",total_expense);

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("Name : Yash Makadiya");

printf("\nID No.: 18DCS040");

printf("\nDate : 31-07-2018\n");

printf("\nEnter x1: ");

scanf("%d",&x1);

printf("Enter y1: ");

scanf("%d",&y1);

printf("Enter x2: ");

scanf("%d",&x2);

printf("Enter y2: ");

scanf("%d",&y2);

DEPSTAR(CSE) Page 13
Computer Concepts and Programming[CE141] ID No. : 18DCS040

printf("Enter x3: ");

scanf("%d",&x3);

printf("Enter y3: ");

scanf("%d",&y3);

s1=fabs((y2-y1)/(x2-x1));

s2=fabs((y3-y2)/(x3-x2));

if(s1==s2)

printf("The points lie on same line.");

else

printf("The points do not lie on same line.");

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("Name : Yash Makadiya");

printf("\nID No.: 18DCS040");

printf("\nDate : 07-08-2018\n");

printf("\nEnter three sides of triangle:\n");

scanf("\n%d %d %d",&side1,&side2,&side3);

if((side1+side2)>side3)

if((side2+side3>side1))

if((side3+side1)>side2)

printf("The triangle is valid.");

DEPSTAR(CSE) Page 15
Computer Concepts and Programming[CE141] ID No. : 18DCS040

else

printf("The triangle is not valid.");

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("Name : Yash Makadiya");

printf("\nID No.: 18DCS040");

printf("\nDate : 07-08-2018\n");

printf("\nEnter age : ");

scanf("%d",&age);

printf("\nEnter place(city/village) :");

DEPSTAR(CSE) Page 17
Computer Concepts and Programming[CE141] ID No. : 18DCS040

scanf(" %c",&place);

printf("\nEnter health(excellent/poor) :");

scanf(" %c",&health);

printf("\nEnter gender :");

scanf(" %c",&gender);

if((age>25)&&(age<35)&&(gender=='m')&&(place=='c')&&(health=='e'))

printf("\nThe person can be insured.The premium is 4Rs per thousand and


maximum amount is 200000");

else if((age>25)&&(age<35)&&(gender=='f')&&(place=='c')&&(health=='e'))

printf("\nThe person can be insured.The premium is 3Rs per thousand and


maximum amount is 100000");

else if((age>25)&&(age<35)&&(gender=='m')&&(place=='v')&&(health=='p'))

printf("\nThe person can be insured.The premium is 1Rs per thousand and


maximum amount is 10000");

else

printf("\nPerson cannot be insured.");

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("Name : Yash Makadiya");

printf("\nID No.: 18DCS040");

printf("\nDate : 07-08-2018\n");

printf("\nEnter your character: ");

a=getchar();

if((isalnum(a))&&(isprint(a)))

printf("\nThe input is printable\n");

if(isalpha(a))

DEPSTAR(CSE) Page 20
Computer Concepts and Programming[CE141] ID No. : 18DCS040

printf("\nThe character is an alphabet\n");

if(isupper(a))

printf("\nThe character is in upper case\n");

printf("\nThe character in lower case is ");

a=tolower(a);

putchar(a);

else if(islower(a))

printf("\nThe character is in lower case\n");

printf("\nThe character in lower case is ");

a=toupper(a);

putchar(a);

if(isdigit(a))

printf("\nThe character is digit ");

putchar(a);

else if((ispunct(a))&&(isprint(a)))

printf("\nThe input is a punctuation ");

DEPSTAR(CSE) Page 21
Computer Concepts and Programming[CE141] ID No. : 18DCS040

putchar(a);

else if((isspace(a))&&(isprint(a)))

printf("\nThe input is a blank space and not printable.");

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("Name : Yash Makadiya");

printf("\nID No.: 18DCS040");

printf("\nDate : 07-08-2018\n");

printf("\nEnter class : ");

scanf("%d",&Class);

printf("\nEnter number of subjects stdent is failing : ");

DEPSTAR(CSE) Page 24
Computer Concepts and Programming[CE141] ID No. : 18DCS040

scanf("%d",&fail_sub);

switch(Class)

case '1': if(fail_sub>3)

printf("\nNo grace marks given");

else if(fail_sub<=3)

printf("\n5 grace marks per subject are given.");

break;

case '2': if(fail_sub>2)

printf("\nNo grace marks given.");

else if(fail_sub<=2)

printf("\n4 grace marks per subject are given.");

break;

case '3': if(fail_sub>1)

printf("\nNo grace marks given.");

DEPSTAR(CSE) Page 25
Computer Concepts and Programming[CE141] ID No. : 18DCS040

else if(fail_sub>=1)

printf("5 grace marks per subject are given.");

break;

default: printf("Please enter correctly.");

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;

long int sum1=0,sum2=0;

clrscr();

printf("Name : Yash Makadiya");

printf("\nID No.: 18DCS040");

printf("\nDate : 13-08-2018");

printf("\nEnter number of terms.");

scanf("%d",&n);

flag:

sum1=sum1+(a*a);

a++;

if(a<=n)

goto flag;

printf("\nThe sum(using goto label) is : %d ",sum1);

DEPSTAR(CSE) Page 27
Computer Concepts and Programming[CE141] ID No. : 18DCS040

for(i=1;i<=n;i++)

sum2=sum2+(i*i);

printf("\nThe sum(using for loop) is : %d",sum2);

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("Name : Yash Makadiya");

printf("\nID No.: 18DCS040");

printf("\nDate : 13-08-2018\n");

printf("\nEnter a number : ");

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)

printf("\nThe number is armstrong number.");

else

printf("\nThe number is not armstrong number.");

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("Name : Yash Makadiya");

printf("\nID No.: 18DCS040");

printf("\nDate : 20-08-2018\n");

while(1)

printf("\nMENU!");

printf("\n1.To check whether number is prime or not.");

printf("\n2.To check wheteher number is perfect or not.");

printf("\n3.To calculate factorial of the number.");

printf("\n4.To exit the program.");

printf("\nEnter your choice:");

DEPSTAR(CSE) Page 32
Computer Concepts and Programming[CE141] ID No. : 18DCS040

scanf("%d",&choice);

switch(choice)

case 1: {

int c=0,i;

printf("\n\nPrime Number or not");

printf("\nEnter your number:");

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");

printf("\nEnter your number:");

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)

printf("\nThe number is perfect number.");

getch();

clrscr();

break;

case 3: {

int fact=1,i;

printf("\n\nFactorial of number");

printf("\nEnter your number:");

scanf("%d",&num);

for(i=1;i<=num;i++)

fact=fact*i;

printf("\nThe factorial is : %d ",fact);

getch();

clrscr();

DEPSTAR(CSE) Page 34
Computer Concepts and Programming[CE141] ID No. : 18DCS040

break;

case 4: {

printf("Thank you!Exiting the program(Press any key to exit)");

getch();

exit(0);

break;

default: printf("\nNot valid choice.");

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("Name : Yash Makadiya");

printf("\nID No.: 18DCS040");

printf("\nDate : 20-08-2018\n");

printf("\nEnter number: ");

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("Name : Yash Makadiya");

printf("\nID No.: 18DCS040");

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("Name : Yash Makadiya");

printf("\nID No.: 18DCS040");

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("Name : Yash Makadiya");

printf("\nID No.: 18DCS040");

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("Name : Yash Makadiya");

printf("\nID No.: 18DCS040");

printf("\nDate : 20-08-2018\n");

printf("****** 21 MATCHSTICKS ******\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("2) The first turn is of the user\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

printf("Enter the number of matchsticks you want to pick\n");

scanf("%d",&n);

i=5-n;

printf("The number of matchsticks you picked: %d\n",n);

printf("The number of matchsticks computer picked : %d\n",i);

n1=n1-n-i;

printf("Number of matchsticks remaining : %d\n",n1);

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("Name : Yash Makadiya");

printf("\nID No.: 18DCS040");

printf("\nDate : 27-08-2018\n");

printf("Enter the size of array\n");

scanf("%d",&n);

printf("Enter elements of array:\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\nTo find the index");

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;

printf("The index is %d",i);

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("Name : Yash Makadiya");

printf("\nID No.: 18DCS040");

printf("\nDate : 27-08-2018\n");

printf("\nEnter elements :\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;

printf("\nThe sorted elements are : \n");

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

Output for the above matrix:1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10

Program code:
#include<stdio.h>

#include<conio.h>

void main()

int a[100][100],n,i,j,m,k=0,l=0;

clrscr();

printf("Name : Yash Makadiya");

printf("\nID No.: 18DCS040");

printf("\nDate : 03-09-2018\n");

printf("Enter the size of array\n");

scanf("%d%d",&m,&n);

DEPSTAR(CSE) Page 54
Computer Concepts and Programming[CE141] ID No. : 18DCS040

printf("Enter elements of array:\n");

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");

while (k<m && l<n)

for (i = l; i < n; ++i)

printf("%d ", a[k][i]);

k++;

DEPSTAR(CSE) Page 55
Computer Concepts and Programming[CE141] ID No. : 18DCS040

for (i = k; i < m; ++i)

printf("%d ", a[i][n-1]);

n--;

if ( k < m)

for (i = n-1; i >= l; --i)

printf("%d ", a[m-1][i]);

m--;

if (l < n)

for (i = m-1; i >= k; --i)

printf("%d ", a[i][l]);

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

You might also like