[go: up one dir, main page]

0% found this document useful (0 votes)
14 views72 pages

CCP Word File

Uploaded by

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

CCP Word File

Uploaded by

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

Computer Concepts And Programming[CE143]23dcs088

Practical 1.1
AIM: Write a simple Program to print “Hello World”.
Algorithm: Step1:Start
Step2:Print “Hello World!”
Step3:Stop.

Flowchart:

Program:
#include<stdio.h>
int main()
{
Printf(“Hello World0”);\
Printf(“23dcs088 patel rudra”);
Return 0;
}
Output:

Conclusion:
In this program, printf () displays Hello, World! text on the screen. The return
0; statement is the "Exit status" of the program. In simple terms, the program ends with
this statement.

DEPSTAR(CSE) 1
Computer Concepts And Programming[CE143]23dcs088

Practical 1.2
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.
Algorithm:
Step 1: Start.
Step 2: Find the ASCII values for smiling face, diamond and heart.
Step 3: Print the given paragraph with proper spacing line break and punctuation.
Step 4: End.
Flowchart:

START

Print a pattern using 3 characters


having ascii value 1,2,and 3 print the
quote my michel singer

End

Program:
#include<stdio.h>
int main()
{
printf("\n\t\t %c %c %c",1,4,3);
printf("%c %c %c ",1,4,3);
printf("%c %c %c ",1,4,3);
printf("%c %c %c ",1,4,3);
printf("%c %c %c ",1,4,3);
printf("%c %c %c ",1,4,3);

DEPSTAR(CSE) 2
Computer Concepts And Programming[CE143]23dcs088

printf("%c %c %c ",1,4,3);
printf("%c %c %c ",1,4,3);
printf("%c %c %c ",1,4,3);
printf("%c %c %c ",1,4,3);
printf("%c %c %c ",1,4,3);
printf("%c %c %c ",1,4,3);
printf("%c",1);
printf("\n\t\t %c ""If you are resisting something, you are feeding it. \t\t\t %c",4,4);
printf("\n\t\t %c\tany energy you fight, you are feeding it %c",3,3);
printf("\n\t\t %c\t\tIf you are pushing somthing away, \t\t\t %c",1,1);
printf("\n\t\t %c\t\t\tYou are inviting it to stay.""by Micheal Singer. %c",4,4);
printf("\n\t\t %c %c %c ",1,4,3);
printf("%c %c %c ",1,4,3);
printf("%c %c %c ",1,4,3);
printf("%c %c %c ",1,4,3);
printf("%c %c %c ",1,4,3);
printf("%c %c %c ",1,4,3);
printf("%c %c %c ",1,4,3);
printf("%c %c %c ",1,4,3);
printf("%c %c %c ",1,4,3);
printf("%c %c %c ",1,4,3);
printf("%c %c %c ",1,4,3);
printf("%c %c %c ",1,4,3);
printf("%c\n\n\n\n",1);
Printf(“23dcs088 patel rudra”);
return 0;
}
Output:

DEPSTAR(CSE) 3
Computer Concepts And Programming[CE143]23dcs088

Question and Answer:


1. Have you learnt about ASCII values for different symbols other than smile, diamond and
heart? If yes, then mention any 5 ASCII symbols and their values in tabular format.
Sr. No. Symbol ASCII Value
1. ! 33
2. / 47
3. ] 93
4. ? 63
5. : 58

Conclusion: By this we came o know about different ASCII values and how to
print the shapes using different ASCII values.

DEPSTAR(CSE) 4
Computer Concepts And Programming[CE143]23dcs088

Practical 2.1
AIM: In a town, the percentage of men is 52. The percentage of total literacy is
48.If total percentage of literate men is 35 of the total population, write a
program to find the total number of illiterate men and women if the
population of the town is 80,000.
Algorithm:
Step1:Start

Step2: Calculate the total number of literate people in the town.

Step3: Calculate the number of literate men based on the percentage of literate men.
Step4: Calculate the number of literate women by subtracting the number of literate men
from the total number of literate people.
Step5: Calculate the number of illiterate people in the town by subtracting the total
number of literate people from the total population.
Step6: Calculate the number of illiterate men by subtracting the number of literate men
from the number of illiterate people.
Step7: Calculate the number of illiterate women by subtracting the number of illiterate
men from the total number of illiterate people.
Step8:Stop

DEPSTAR(CSE) 5
Computer Concepts And Programming[CE143]23dcs088

Flowchart:

Program:
#include<stdio.h>
int main()
{
long int tp=80000;
double nom,now,lm,lw,tl,ilm,ilw;
printf("1. Total poplution is: %ld",tp);
nom = tp*0.52;
printf("\n2. Num of men is: %.0f",nom);
now = tp*0.48;

DEPSTAR(CSE) 6
Computer Concepts And Programming[CE143]23dcs088

printf("\n3. Num of women is: %.0f",now);


lm=tp*0.35;
printf("\n4. Num of literate men is: %.0f",lm);
lw=tp*0.13;
printf("\n5. Num of literate men is: %.0f",lw);
tl=lm+lw;
printf("\n6. Num of total literate is: %.0f",tl);
ilm=nom-lm;
printf("\n7. Num of iliterate men is: %.0f",ilm);
ilw=now-lw;
printf("\n8. Num of literate women is: %.0f",ilw);
printf("23dcs088 patel rudra");
return 0;
}
Output:

Question And Answer:


1.Has this scenario helped you learn about integer and float datatype? If yes, then mention the
requirements of using integer and float data types.

Answer:

Yes, this scenario involves the use of integer and float data types in programming.

Integer Data Type


- Represents whole numbers without decimals.
- Example: 5, -10, 100.

DEPSTAR(CSE) 7
Computer Concepts And Programming[CE143]23dcs088

Float Data Type


- Represents numbers with decimals or in exponential notation.
- Example: 3.14, -0.5, 1.23e-4.
Use of integer and Floats
- Choose integers for whole numbers.
- Choose floats for numbers with decimals.
- Consider precision, memory usage, and arithmetic operations.

Sr no Get Outcome Value


1 Total population 80000
2 Number of Literate (Men + Women) 38400
3 Number of Men 41600
4 Number of literate men 28000
5 Number of illiterate men 13600
6 Number of Women 38400
7 Number of literate Women 10400
8 Number of illiterate Women 28000

Conclusion:

By this programme we learn about the arethimatic operations and learn


how to use it.

DEPSTAR(CSE) 8
Computer Concepts And Programming[CE143]23dcs088

Practical 2.2
AIM:
Write a program to calculate Net Salary. User has to input Basic Salary and Output should be:
Enter Basic Salary: 5000 (e.g. 5000)
Allowances:
DA = 70% of Basic Salary
HRA = 7% of Basic Salary
MA = 2% of Basic Salary
TA = 4% of Basic Salary
Deduction:
PF = 12% of Basic Salary
IT = any value (e.g. 500)
Gross Salary = Basic Salary + Allowances
Net Salary = Gross Salary – Deduction
Algorithm:
Step:1Start
Step2:Input:
- Enter Basic Salary
- IT
Step 3:Calculations:
- Calculate DA (Dearness Allowance):da = 0.70 * basic_salary
- Calculate HRA (House Rent Allowance): hra = 0.07 * basic_salary
- Calculate MA (Medical Allowance):ma = 0.02 * basic_salary
- Calculate TA (Travel Allowance): ta = 0.04 * basic_salary
- Calculate Total Allowances:total_allowances = da + hra + ma + ta
- Calculate PF (Provident Fund):pf = 0.12 * basic_salary
- Calculate Gross Salary: gross_salary = basic_salary + total_allowance
- Calculate Net Salary:net_salary = gross_salary - (pf + IT)
Step4: Output:

DEPSTAR(CSE) 9
Computer Concepts And Programming[CE143]23dcs088

- Display Net Salary:


Step5:Stop

Flowchart:

Program:
#include<stdio.h>
#include<conio.h>
int main()
{
float bs,da,hra,ma,ta,all,ded,pf,it,gs,ns;
printf("basic salary:");
scanf("%f",&bs);

DEPSTAR(CSE) 10
Computer Concepts And Programming[CE143]23dcs088

da=70*bs/100;
printf("da:%.0f\n",da);
hra=7*bs/100;
printf("hra:%.0f\n",hra);
ma=2*bs/100;
printf("ma:%.0f\n",ma);
ta=4*bs/100;
printf("ta:%.0f\n",ta);
all=da+hra+ma+ta;
gs=all+bs;
printf("gs:%.0f\n",gs);
pf=12*bs/100+500;
ded=pf;
ns=gs-ded;
printf("ns:%.0f\n",ns);
printf(“23dcs088 patel rudra”);
return 0;
}
Output:

Sr.No Input/Output Amount


.
1. Enter your basic salary 5000
2. DA for basic salary 3500
3. HRA for basic salary 350
4. MA for basic salary 100
5. TA for Basic salary 200

DEPSTAR(CSE) 11
Computer Concepts And Programming[CE143]23dcs088

6. PF for basic salary 1100


7. Gross salary 9150
8. Net salary 8050

Questions: 1. Have you learned about various data types that can be suitably used for
this problem? Do mention which data types can be used and why? Also mention the
difference between the outputs.
Answer:
Yes,
Float Data Type
- Suitable for representing monetary values with decimals (e.g., salary, allowances,
deductions).
- Provides precision needed for accurate calculations involving fractions.
- Used for values like percentages (e.g., 0.70 for 70%).
Integer Data Type
- Suitable for representing whole numbers (e.g., input values, rounded results).
- Used for values that are counted or non-fractional.
Difference between Outputs:
- Using `float` data types for monetary values maintains decimal precision in
calculations.
- Using `int` data types for whole numbers simplifies values without decimals.
- The outputs may differ in terms of decimal accuracy, reflecting the precision of the data
types used.

Conclusion:

By this we came to know about different types of math function used in c


language and how to use them in our program.

DEPSTAR(CSE) 12
Computer Concepts And Programming[CE143]23dcs088

Practical 3.1
AIM:

Write a program that takes the length of the pendulum as input and then
calculate the time period of the pendulum. Provided that, T=2π√L/G.
Define the value of π as 3.14 and take L as the length of the pendulum and
G as the acceleration of gravity either in m/s or as input from the keyboard.
Display the time period rounded to 2 decimal places.
Algorithm:
Step1:Start

Step2: Input
- Enter Length of Pendulum
- Enter Acceleration Due to Gravity
Step3: Calculations:
- Set the value of π to 3.14
- Calculate time period using the formula: time_period = 2 * pi * sqrt(length_pendulum /
acceleration_gravity)
Step4:. Output:
- Display Time Perio
Step5:Stop

Flowchart:

DEPSTAR(CSE) 13
Computer Concepts And Programming[CE143]23dcs088

Program:
#include<stdio.h>
#include<conio.h>
#include<math.h>
#define pi 3.14
int main()
{
float l,g,t;
printf("enter the length");
printf("enter the grevaty");
scanf("%f",&g);
t=2*pi*sqrt(l/g);
printf("%.2f\n",t);
Printf(“23dcs088 patel rudra”);
return 0;
}
Output:

DEPSTAR(CSE) 14
Computer Concepts And Programming[CE143]23dcs088

Sr.No Input Output


Length Gravity Time
calculated(sec)
1. 50 m 9.98 m/s2 14.19
2. 50 m 0 m/s2 error
3. 50 m 0.9993 m/s2 44.42
4. 50 m -1 m/s2 error

Questions: 1. Have you learned about, how math function is useful for calculating
square root? Which datatype is supported by all math functions? Also mention any 5
math functions with their purpose.
Answer:
Yes, I'm familiar with how math functions are used for various calculations. The data
type commonly supported by all math functions is the float data type.

Here are five commonly used math functions along with their purposes:

NO. MATH FUNCTION DESCRIPTION


1 math.sqrt(x) Computes the square
root of a given number
x.

2 math.sin(x) Calculates the sine of


an angle x(in radians).

DEPSTAR(CSE) 15
Computer Concepts And Programming[CE143]23dcs088

3 math.cos(x) Calculates the cosine of


an angle x(in radians).

4 math.exp(x) Computes the


exponential of `x`,
which is the value of
*e* raised to the power
of `x`.

5 math.log(x) Calculates the natural


logarithm of `x`.

Conclusion:

By this programme we learn how to use define function in programme and

how to use it

Practical 3.2(a)
AIM:

Let us understand the working of Pre-increment, Post-increment, Pre-


decrement and Post-decrement.
Consider a scenario where, Boys are playing in the park and
collecting and removing the yellow balls in/from the bucket based on
teacher’s instruction. Let’s say there are already 10 Yellow balls present in a
bucket. Following is the sequence of the instructions given by the teacher for
adding/removing the balls.
i.Rajiv: ++ Yellow
ii.Preet: --Yellow
iii.Raj: Yellow++
iv.Ritul: Yellow—
Algorithm:

DEPSTAR(CSE) 16
Computer Concepts And Programming[CE143]23dcs088

Step1:Start
Step2: Initialize yellowBalls as 10.
Step3: Rajiv adds a yellow ball using pre-increment (++yellowBalls).
Step4:Preet removes a yellow ball using pre-decrement (--yellowBalls).
Step5: Raj adds a yellow ball and then increments using post-increment
(yellowBalls++).
Step6: Ritul removes a yellow ball and then decrements using post-
decrement `yellowBalls--).
Step6:Stop.

Flowchart:

DEPSTAR(CSE) 17
Computer Concepts And Programming[CE143]23dcs088

Program:
#include<stdio.h>
int main()
{
int yballs=10;
printf("*****Count before execution*****");
printf("\n\nballs are:%d",yballs);
++yballs;
--yballs;
yballs++;

DEPSTAR(CSE) 18
Computer Concepts And Programming[CE143]23dcs088

yballs--;
printf("\n\n*****Count after execution*****");
printf("\n\nremaining balls are:%d",yballs);
Printf(“23dcs088 patel rudra”);
return 0;
}
Output:

Sr.No Instruction Yellow


1. Count Before Execution 10
2. Count after Execution 10

Conclusion:
Sr no Instruction Yellow
1 Count before execution 10
2 Count after execution 41

Practical 3.2(b)
AIM:

DEPSTAR(CSE) 19
Computer Concepts And Programming[CE143]23dcs088

Consider another scenario where boys and girls both are asked to
add/remove Yellow and Pink balls from the bucket respectively.
Currently there are 10 Yellow balls in the bucket and 20 Pink balls.
Teacher has given the sequence of instructions as below for
adding/removing the balls.
Calculate = ++Yellow + Yellow++ + --Yellow + ++Pink - --Pink - --
Pink
Get the count of Yellow and Pink balls after evaluating above given
scenario.
Algorithm:

Step1:Start.
Step2: Initialize yellowBalls as 10 and pinkBalls as 20.
Step3: Perform pre-increment on yellowBalls(++yellowBalls).
Step4: Perform post-increment on yellowBalls (yellowBalls++).
Step5: Perform pre-decrement on yellowBalls (--yellowBalls).
Step6:Perform pre-increment on pinkBalls (++pinkBalls).
Step7: Perform post-decrement on pinkBalls(--pinkBalls).
Step8: Perform post-decrement on pinkBalls (--pinkBalls).
Step9:Calculate`as ++Yellow + Yellow++ + --Yellow + ++Pink - --Pink - --
Pink`.
Step10: Display the count of Yellow and Pink balls after evaluating the
scenario.
Step11:Stop.

Flowchart:

DEPSTAR(CSE) 20
Computer Concepts And Programming[CE143]23dcs088

Program:
#include<stdio.h>
int main()
{
int yballs=10,pballs=20,calculate;
printf("*****Count before execution*****");
printf("\n\n yellow balls are:%d",yballs);
printf("\n pink balls:%d",pballs);
calculate=++yballs + yballs++ + --yballs + ++pballs - --pballs - --pballs;
printf("\n\n*****Count after execution*****");
printf("\n\n yellow balls are:%d",yballs);
printf("\n pink balls:%d",pballs);
printf("\n total calculation is:%d",calculate);

Printf(“23dcs088 patel rudra”);


return 0;
}

DEPSTAR(CSE) 21
Computer Concepts And Programming[CE143]23dcs088

Output:

Sr.No Instruction Pink Yellow


1. Count Before Execution 20 10
2. Count after Execution 19 11

Questions: Have you understood the working of Pre-increment, Post-increment, Pre-


decrement and Post-decrement?
Answer:
YES,I UNDERSTAND.

Conclusion:

By this we came to know how to use the increment and decrement


operations in a program.

Practical 3.3
DEPSTAR(CSE) 22
Computer Concepts And Programming[CE143]23dcs088

AIM:

Write a C program to swap two numbers (use two variables for collecting
value from user) without using third variable.
Algorithm:

Step1:Start
Step2: Initialize variables `num1`, `num2`, and `temp`.
Step3: Read input for `num1` and `num2`.
Step4: Display the values before swapping.
Step5: Assign the value of `num1` to `temp`.
Step6: Assign the value of `num2` to `num1`.
Step7:Assign the value of `temp` (which holds the original value
of `num1`) to `num2`.
Step8: Display the values after swapping.
Step9:Stop.
Flowchart:

Program:

DEPSTAR(CSE) 23
Computer Concepts And Programming[CE143]23dcs088

#include<stdio.h>
void main()
{
int num1,num2;
printf("enter number one: ");
scanf("%d",&num1);
printf("entre numbre two: ");
scanf("%d",&num2);
printf("before swapping num1 = %d,num2 = %d\n",num1,num2);
num1=num1+num2;
num2=num1-num2;
num1=num1-num2;
printf("after swapping num1 = %d,num2 = %d\n",num1,num2);
Printf(“23dcs088 patel rudra”);
return 0;
}
Output:

Sr.No Instruction Number 1 Number 2


1. Before Swapping 5 7
2. After Swapping 7 5

Questions And Aswer: 1. Have you learned about, how we can use
arithmetic operators for swapping the numbers?
YES
1.Using Addition and Subtraction

DEPSTAR(CSE) 24
Computer Concepts And Programming[CE143]23dcs088

- `a = a + b`
- `b = a - b`
- `a = a - b`
2. Using Multiplication and Division
- `a = a * b`
- `b = a / b`
- `a = a / b`
3. Using XOR (Bitwise Exclusive OR)
- `a = a ^ b`
- `b = a ^ b`
- `a = a ^ b`
Conclusion:

By this we came to know about how we can use arithmetic operators for
swapping the numbers.
Practical 4.1
AIM:
Write a program to convert the decimal number into octal and hexadecimal format. Print
hexadecimal and octal values for given inputs in expected outcomes.
Algorithm:

Step1:Start.
Step2:Declare a character array `characteristics`.
Step3:Read user input using `gets()`.
Step4:Display "My Characteristics:" using `puts()`.
Step5:Display the entered characteristics using another `puts()`
statement.
Step6:Stop.
DEPSTAR(CSE) 25
Computer Concepts And Programming[CE143]23dcs088

Flowchart:

Program:
#include<stdio.h>

int main()
{
int rollnum;
printf("enter your rool num:");
scanf("%d",&rollnum);

printf("%o %o %o %o %o\n",rollnum,143,0,1,-1);
printf("%x %x %x %x %x",rollnum,143,0,1,-1);
Printf(“23dcs088 patel rudra”);
return 0;

DEPSTAR(CSE) 26
Computer Concepts And Programming[CE143]23dcs088

}
Output:

QUESTION:

Write something about your characteristics not more than 50 words using
gets function and print out the same using puts function.
Answer:

- *gets(): It reads a line of text from standard input.


However, it's unsafe due to buffer overflow vulnerabilities
and is not recommended for use. `fgets()` is a safer
alternative.
- puts(): It writes a string to standard output and adds a
newline character. To print strings without the automatic
newline, you can use printf().
In modern programming, use `fgets()` and `printf()` for
safer input and output operations.
Sr.No Input Octal Hexal
1. Your roll number 27 17
2. 143 217 8f
3. 0 0 0
4. 1 1 1
5. -1 3777777777 fffffff
QUESTON
Write something about your characteristics not more than 50 words using gets function
and print out the same using puts function.

ANSWER :
- *gets(): It reads a line of text from standard input. However, it's unsafe due to buffer
overflow vulnerabilities and is not recommended for use. `fgets()` is a safer alternative.

DEPSTAR(CSE) 27
Computer Concepts And Programming[CE143]23dcs088

- puts(): It writes a string to standard output and adds a newline character. To print
strings without the automatic newline, you can use printf().
In modern programming, use `fgets()` and `printf()` for safer input and output
operations.

Conclusion:

By this programme we know that %o is for Octadecimal and %x for


Hexadecimal

PRACTICAL 5.1
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.

Algorithem:
Step1:Start.
Step2:Declare integer variable `quantity` and float variables `pricePerItem` and
`totalExpenses`.
Step3:Read quantity and price per item from the user.
Step4:Calculate total expenses as `quantity * pricePerItem`.
Step5: If `quantity` is greater than 1000, apply a 10% discount on totalExpenses.
Step6: Display the calculated `totalExpenses` using %.2f format specifier.
Step7:Stop.

Flowchart:

DEPSTAR(CSE) 28
Computer Concepts And Programming[CE143]23dcs088

Program:
#include<stdio.h>
int main()
{
int q;
float p,d,tp,tep;
printf("Enter Quantity");
scanf("%d",&q);
printf("Enter Price");
scanf("%f",&p);
if (q>1000)
{
tp=q*p;
d=tp*10/100;

DEPSTAR(CSE) 29
Computer Concepts And Programming[CE143]23dcs088

tep=tp-d;
printf("\n Total Price: %f",tp);
printf("\n Discount: %f",d);
printf("\n Total Expance: %f",tep);
}
else
{
tp=q*p;
printf("Total Price: %f",tp);
}
Printf(“23dcs088 patel rudra”);
return 0;
}

Output:

Sr No. Inputs Output


1 Quantity:5 price:2 10
2 Quantity:3 price:6.5 19.5

DEPSTAR(CSE) 30
Computer Concepts And Programming[CE143]23dcs088

PRACTICAL 5.2
AIM:
Three or more points are said to be collinear if they lie on a single straight line. If three points
(x1,y1) , (x2, y2) and (x3,y3) are entered through the keyboard find if these points are
collinear or not. (Hint: Calculate slope of line between each pair of points. For example slope
between first point and second point is s1=fabs(x2-x1)/fabs(y2-y1). If all the three slopes are
equal they fall on straight line ). Use fabs() function of math.h header file. Use If..Else
statement.

Algorithem:
Step1:Start
Step2: Declare variables: `x1`, `y1`, `x2`, `y2`, `x3`, `y3`, `slope1`, `slope2`, `slope3`.
Step3: Read three points `(x1, y1)`, `(x2, y2)`, and `(x3, y3)` from the user.
Step4: Calculate slopes between points:
- `slope1 = fabs((float)(y2 - y1) / (x2 - x1));`
- `slope2 = fabs((float)(y3 - y2) / (x3 - x2));`
- `slope3 = fabs((float)(y3 - y1) / (x3 - x1));`
Step5: If all three slopes (`slope1`, `slope2`, `slope3`) are equal, points are collinear.
Step6:Display the result accordingly using `if...else` statements.
Step7:Stop.

DEPSTAR(CSE) 31
Computer Concepts And Programming[CE143]23dcs088

Flowchart:

Program:
#include<stdio.h>
#include<math.h>
int main()
{
int x1,x2,x3,y1,y2,y3;

DEPSTAR(CSE) 32
Computer Concepts And Programming[CE143]23dcs088

float s1,s2,s3;
printf("\n Enter value of x1 :");
scanf("%d",&x1);
printf("\n Enter value of x2 :");
scanf("%d",&x2);
printf("\n Enter value of x3 :");
scanf("%d",&x3);
printf("\n Enter value of y1 :");
scanf("%d",&y1);
printf("\n Enter value of y2 :");
scanf("%d",&y2);
printf("\n Enter value of y3 :");
scanf("%d",&y3);
s1=fabs(y2-y1)/fabs(x2-x1);
s2=fabs(y3-y2)/fabs(x3-x2);
s3=fabs(y1-y3)/fabs(x1-x3);
if(s1==s2&&s2==s3&&s3==s1){
printf("Slope is collinear"); }
else{
printf("Slope is not collinear"); }
printf(“23dcs088 patel Rudra”);
return 0;
}

DEPSTAR(CSE) 33
Computer Concepts And Programming[CE143]23dcs088

Output:

Sr No. Inputs Outouts


1 (1,3),(6,4),(3,6) Non-collinear
2 (1,2),(2,4),(3,6) collinear
Conclusion:

DEPSTAR(CSE) 34
Computer Concepts And Programming[CE143]23dcs088

PRACTICAL 5.3
AIM:
Write a program to find whether the given Year is a Leap Year or not using Else…If Ladder.

Algorithem:
Step1:Start
Step2: Declare an integer variable `year`.
Step3: Read the year from the user.
Step4: Check if the year is divisible by 400 using (year % 400 ==
0 condition.
Step5:If not divisible by 400, check if it's divisible by 4 but not by
100 using (year % 4 == 0) && (year % 100 != 0) condition.
Step6: If either condition is true, the year is a leap year. Otherwise,
it's not a leap year.
Step7:Display the result using `printf()` with appropriate
messages.
Step8:Stop.

DEPSTAR(CSE) 35
Computer Concepts And Programming[CE143]23dcs088

Flowchart:

Program:
#include<stdio.h>
#include<conio.h>
int main()
{
int year;
printf("Enter year");
scanf("%d",&year);

DEPSTAR(CSE) 36
Computer Concepts And Programming[CE143]23dcs088

if ( year % 400==0){
printf("%d is leap year\n",year);
}
else if(year%100==0){
printf("%d is not a leap year.\n",year);
}
else if(year%4==0){
printf("%d is leap year\n",year);
}
Printf(“23dcs088 patel rudra”);
return 0;
}

Output:

Sr No. Inputs Outputs


1 1900 1900 is not leap year
2 2020 2020 is leap year
3 2000 2000 is leap year
Conclusion:
By this we learned the concept of nested if-else for leap year and not leap year

DEPSTAR(CSE) 37
Computer Concepts And Programming[CE143]23dcs088

PRACTICAL 5.4
AIM:
Write a C program to find all roots of a Quadratic equation using nested switch case. Take
three user inputs from keyboard for finding the discriminant (b2 – 4*a*c). Use the concept of
nested switch case for finding the roots of equation. Get the outputs for roots till 2 decimal
points only. Hint: Discriminant > 0 root1 = (-b + sqrt(discriminant)) / (2*a) root2 = (-b -
sqrt(discriminant)) / (2*a) Discriminant < 0 root1 = root2 = -b / (2*a) imaginary = sqrt (-
discriminant) / (2*a) (eg. Print it as: i20.3, i.e. i followed by value) Discriminant = 0 root1 =
root2 = -b / (2*a)

Algorithem:
Step1:Start
Step2: Declare float variables `a`, `b`, `c`, `discriminant`, `root1`, `root2`, and
`imaginaryPart`.
Step3: Read coefficients `a`, `b`, and `c` from the user.
Step4: Calculate `discriminant` using the formula.
Step5:Use a nested switch case for different cases of discriminant:
- If `discriminant > 0`, calculate and display two real roots.
- If `discriminant < 0`, calculate and display two imaginary roots.
- If `discriminant = 0`, calculate and display two equal real roots.
Step6:Display the results using `printf()` with appropriate messages.
Step7:Stop.

DEPSTAR(CSE) 38
Computer Concepts And Programming[CE143]23dcs088

Flowchart:

Program:
#include<stdio.h>
#include<math.h>
int main()
{
float a,b,c,d,imaginary,root1,root2;
int temp;
printf("enter the value of a");
scanf("%f",&a);
printf("enter the value of b");

DEPSTAR(CSE) 39
Computer Concepts And Programming[CE143]23dcs088

scanf("%f",&b);
printf("enter the value of c");
scanf("%f",&c);
d=(b*b)-(4*a*c);
printf("value of d is :%f",d);
if(d>0)
{
temp=1;
}
else if(d<0)
{
temp=2;
}
else if(d=0)
{
temp=3;
}
switch(temp)
{
case 1:root1=(-b+sqrt(d))/(2*a);
root2=(-b-sqrt(d))/(2*a);
printf("root1 :%f\n",root1);
printf("root2 :%f\n",root2);
break;
case 2:root1=root2=-b/(2*a);
imaginary=sqrt(-d)/(2*a);
printf("root1=root2 :%f\n",root1);
printf("imaginary :%f\n",imaginary);

DEPSTAR(CSE) 40
Computer Concepts And Programming[CE143]23dcs088

break;
case 3:root1=root2=-b/(2*a);
printf("root1 :%f",root1);
printf("root2 :%f",root2);
break; }
Printf(“23dcs088 patel rudra”);
return 0; }

Output:

Sr No. Inputs Root1 Root2 Imaginary


a b c
1 1 2 3 -1 -1 0
2 3 -7 -5 2.91 -0.57 0
3 9 12 4 0 0 0

Questions:
1. Have you learned about how to use normal switch case and nested switch case?
2. Is default case necessary for every switch case?

DEPSTAR(CSE) 41
Computer Concepts And Programming[CE143]23dcs088

3. What if break statement is not mentioned between two consecutive cases?

ANSWERS:
1. Yes, I'm familiar with how to use both normal switch case and nested switch case.
2. No, the default case is not necessary for every switch case.
3. If the break statement is not mentioned between two consecutive cases, the program will
continue to execute the code of subsequent cases even after a matching case is found.

Conclusion:
By this we came to learn about how to use switch case to determining the roots and value of
discriminent.

DEPSTAR(CSE) 42
Computer Concepts And Programming[CE143]23dcs088

PRACTICAL 5.5
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.
Algorithem:
Step-1 Start.
Step-2 Use getchar a and putchar.
Step-3 If a is digit, printf the input is number and alphanumeric.
Step-4 If a is alpha , then print the input is alpha. And if a is lower then convert it to
upper otherwise if a is upper then convert it to lower.
Step-5 If a is printable, then print entered number is printable. If a is punct then print
Is punctuation else if a is space then print is a white space character.
Step-6 If a is alnum, then print the input is alphanumeric.

Flowchart:
Start

Input a

a=getchar()

putchar(a)

Isalpha(a)

isdigit(a)

Print the input is


digit and
alphanumeric isprint(a)
Print the input
is alpha

DEPSTAR(CSE) 43
Computer Concepts And Programming[CE143]23dcs088

isalnum(a)
printf(“Entered
number is printable”)

islower(a)

ispunct(a) printf(“The input is


alphanumeric”)

printf(“Upper case\n
%c”,tolower(a))

isspace(a)
printf(“Is printable”)

printf(“Lower case\n
%c”,toupper(a))

Printf(“Is a white space


character”);

Stop

Program:
#include <stdio.h>
#include <ctype.h>

DEPSTAR(CSE) 44
Computer Concepts And Programming[CE143]23dcs088

int main()
{
char a;
printf("Enter any thing:");
a= getchar();
putchar(a);
if(isdigit(a)){
printf("The input is digit and alphanumeric\n");

}
else if(isalpha(a)){
printf("The input is alpha\n");
if(islower(a)){
printf("Lower case\n");
printf("%c",toupper(a));
}
else if(isupper(a)){
printf("Upper case\n");
printf("%c",tolower(a));
}
}
else if(isprint(a)){
printf("Entered number is printable");
if(ispunct(a)){
printf("Is punctuation");
}
else if(isspace(a)){
printf("Is a white space character");
}

DEPSTAR(CSE) 45
Computer Concepts And Programming[CE143]23dcs088

else if(isalnum(a)){
printf("The input is alphanumeric");
}
}
Printf(“23dcs088 patel rudra”);
return 0;
}

Output:

Sr No. Input a Character Output


1 10 10 is digit
2 a a is lower case character ,
Upper Case Character is A
3 D D is Upper case character ,
Lower Case Character is d.
4 “ “ is a punctuation mark

DEPSTAR(CSE) 46
Computer Concepts And Programming[CE143]23dcs088

5 \n \n is non printable character.


6 (enter white space) is a white space.

DEPSTAR(CSE) 47
Computer Concepts And Programming[CE143]23dcs088

PRACTICAL 6.1
AIM : Write a menu driven program which has following options:
1. Prime or not (Use for Loop)
2. Armstrong number or not (Use while loop)
3. Perfect number or not (Use do..while loop)
4. Exit
Use do...while statement so that the menu is displayed at least once. Also use Switch
statement.
Expected Outcome:
Draw flowchart, write algorithm and program for given scenario. Also attach screenshot of
output

ALGORITHM :
Step-1 Start
Step-2 Input a
Step-3 If a=1then perform step-4,if a=2 then perform step-5 and is a=3 then perform
step-5
Step-4 Input num. If num%i==0 then p++.And if p==2 then the entered number is a
prime number.
Step-5 Input nu. If nu%b==0 then result =result + b. And if result==nu then the
entered number is a perfect number.
Step-6 Input n. If n>0 then r=n%10, sum=sum+(r*r*r) and n=n/10. If c==sum then
the entered number is a Armstrong number.
Step-7 Stop.
Flowchart:
Start

Input a

a==1
DEPSTAR(CSE) 48
Computer Concepts And Programming[CE143]23dcs088

True False

Step-8
Input num a==2
False

Input nu

Step-9 a==3
num
%i==0

nu
%b==
Input n
p++

result=result+b

False
p==2 n>0

result False
True ==nu

Entered number is a
Prime number True
r=n%10 sum=sum+
Entered number is a (r*r*r) n=n/10
Perfect number
Entered number is
not a prime
number
c==sum
Entered number is
not a prime
number

Entered number is a
armstrong number

Entered number is
Stop not a armstrong
DEPSTAR(CSE) number 49
Computer Concepts And Programming[CE143]23dcs088

Program:
#include<stdio.h>
#include<conio.h>
int main(){
int a,num,p=0,i,nu,result=0,b,n,r,c,sum=0;
printf("Enter a Choice");
printf("1-Prime number\n 2-Perfect number\n 3-Armstrong number\n");
scanf("%d",&a);
switch(a)
{
case 1:
printf("Enter a number");
scanf("%d",&num);
for(i=1;i<=num;i++){
if(num%i==0){
p++;
}
}
if(p==2){
printf("Entered number is %d is a prime number",num);
}
else{
printf("Entered number is %d is not a prime number",num);
}
break;
case 2:
printf("Enter a number");
scanf("%d",&nu);
for(b=1;b<nu;b++){

DEPSTAR(CSE) 50
Computer Concepts And Programming[CE143]23dcs088

if(nu%b==0){
result= result + b;
}
}
if(result==nu){
printf("Perfect number");
}
else{
printf("Not");
}
break;
case 3:
printf("Enter a number");
scanf("%d",&n);
c=n;
while(n>0){
r=n%10;
sum=sum+(r*r*r);
n=n/10;
}
if(c==sum){
printf("Armstrong number");
}
else{
printf("Not a armstrong number");
}
break;
}
Printf(“23dcs088 patel rudra”);

DEPSTAR(CSE) 51
Computer Concepts And Programming[CE143]23dcs088

return 0;
}

Output:

DEPSTAR(CSE) 52
Computer Concepts And Programming[CE143]23dcs088

PRACTICAL 6.2
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.

Algorithem:
Step 1: Start
Step 2: Declare sum=21,u,c.
Step 3: If sum!=1
Input u.
Step 4: If u>=1 && u<=4
c = 5-u
sum = sum – (u+c)
goto step 3.
Step 5: else if sum == 1
Print computer wins the game.
Step 6: End.

DEPSTAR(CSE) 53
Computer Concepts And Programming[CE143]23dcs088

Flowchart:

Program:
#include<stdio.h>
int main()
{
int sum=21,u,c;
while(sum!=1)
{
printf("\n Choose the number: ");
scanf("%d",&u);
if(u>=1&&u<=4)

DEPSTAR(CSE) 54
Computer Concepts And Programming[CE143]23dcs088

{
c = 5-u;
printf(" Computer chooses the number: %d",c);
sum = sum - (u+c);
printf("\n Number of matchsticks left: %d",sum);
}
else
{
printf("\n Enter valid numbers");
break;
}
}
if(sum==1)
{
printf("\n Computer wins the game");
}
Printf(“23dcs088 patel rudra”);
return 0;
}

Output:

DEPSTAR(CSE) 55
Computer Concepts And Programming[CE143]23dcs088

Sr. No. Entered Number Entered number Stick left


by User by Computer
1. 1 4 16
2. 3 2 11
3. 2 3 6
4. 4 1 1
QUESTION-ANSWER:
What is the significance of using break and continue statement?
Ans: The break statement is used to terminate the loop immediately. The
continue statement is used to skip the current iteration of the loop.

DEPSTAR(CSE) 56
Computer Concepts And Programming[CE143]23dcs088

PRACTICAL 7.1
AIM : Twenty-five numbers are entered from the keyboard into an array. Write a C program
to find out how many numbers of them are positive, negative, and how many are even and
odd?

Algorithm:

Step 1 : Start
Step 2 : Declare variable i=0, j, a[25], negative=0, positive=0, odd=0,
even=0
Step 3 : Get the value of any 25 numbers from user using for loop
Step 4 : If i < 25 then go to step 5 otherwise go to
step 10
Step 5 : If a[i]<0 then negative++ otherwise go to step
6
Step 6 : If a[i]>0 then positive++ otherwise goto step
7
Step 7 : If a[i]%2==0 the even++ otherwise goto step
8
Step 8 : If a[i]%2!=0 the odd++ otherwise goto step 9
Step 9 : Print all value negative, positive, even, odd
Step 10 : End

DEPSTAR(CSE) 57
Computer Concepts And Programming[CE143]23dcs088

Flowchart:

Program:
#include<stdio.h>
int main()

DEPSTAR(CSE) 58
Computer Concepts And Programming[CE143]23dcs088

{
int i, j, a[25],positive=0,negative=0,even=0,odd=0,zero; printf("Enter 25 values : ");
for(i=0;i<=25;i++)
{
scanf("%d",&a[i]);
}
for (i = 0;i < 25;i++)
{
if(a[i] < 0)

{
negative++;
}
if(a[i] > 0)
{
positive++;
}
if (a[i] %2 == 0)
{
even++;
}
if (a[i] %2 != 0)
{
odd++;
}
if(a[i] == 0)
{
printf("\nZero is neither positive nor negative nor odd nor even.\n");

DEPSTAR(CSE) 59
Computer Concepts And Programming[CE143]23dcs088

}
}
printf("\nNegative values are %d.",negative);
printf("\nPositive values are %d.",positive);
printf("\nEven values are %d.",even);
printf("\nOdd values are %d.",odd);
printf("\n 23dcs088 PATEL RUDRA");
return 0;
}

Output:

DEPSTAR(CSE) 60
Computer Concepts And Programming[CE143]23dcs088

Questions:
1. Is it necessary to initialize a variable with zero everytime? If yes, then why? If No,
then when is it necessary to initialize the number with zero and why?
Ans.: Yes, it is necessary to initialize a variable with zero every time because if we don’t
initialize, the system would take a garbage value for that variable.

Sr. Parameter Counts


No.
1. Positive 19
Numbers:

DEPSTAR(CSE) 61
Computer Concepts And Programming[CE143]23dcs088

2. Negative 5
Numbers:
3. Even Numbers: 12

4. Odd Numbers: 13

Conclusion:

In this practical ,we learned how a array works.

PRACTICAL 7.2
AIM : Write a program for creating two arrays of different size and merge both arrays
into one by sorting those arrays in ascending order. [Merge by sorting]

Algorithm:
Step 1: start
Step 2: declare variables n1,n2.n3,temp.
Step 3: get the size values of two arrays (by using
scanf) Step 4: n3=n1+n2
Step 5: declare int a[n 1],b[n2],c[n3]
Step 6: print the first array using for loop and scanf (print

DEPSTAR(CSE) 62
Computer Concepts And Programming[CE143]23dcs088

a[i]) Step 7: declare and initialize k = n1


Step 8: print the second array using for loop and scanf (print b[i])
Step 9: declare and initialize z = 0 for starting the loop for the merged array.
Step 10: check if z<n3? If Yes, then print the merged array c[z] and go to
the next step. If
No, then go to step
12. Step 11: z++ and go to
step 10.
Step 12: declare and initialize p = 0 for starting the loop for the sorting array.
Step 13: check if p<n3 ?. If Yes, then go to next step. If No, then go to step
20.
Step 14: declare and initialize q = 0 for starting the secondary loop for the
sorting array.
Step 15: check if q<n3 ?. Yes, then go to next step. If No, then go to step 19.
Step 16: check if c[p]<c[a] ? If yes then go to next step. If No, then go to
step 18.
Step 17: temp = c[q]: c[q] = c[p]:c[p] =
temp; Step 18: q++ and go to step 15.
Step 19: p++ and go to step 13.
Step 20: print the value c[i] array using for loop, which is the merged and
sorted Array.
Step 21: End.

Flowchart:

DEPSTAR(CSE) 63
Computer Concepts And Programming[CE143]23dcs088

Program:
#include<stdio.h>
#include<conio.h>

int main()
{
int len_a, len_b, len_c;
int i, j, k;

DEPSTAR(CSE) 64
Computer Concepts And Programming[CE143]23dcs088

int a[20], b[20], c[40];

printf("Enter size of array a:");


scanf("%d", &len_a);
for(i=0; i<len_a; i++)
{
printf("Enter element for index %d :", i);
scanf("%d", &a[i]);
}

printf("\n");
printf("Enter size of array b:");
scanf("%d", &len_b);
for(j=0; j<len_b; j++)
{
printf("Enter element for index %d :", j);
scanf("%d", &b[j]);
}
len_c = len_a + len_b;
i=0, j=0, k=0;

while((i<len_a) && (j<len_b) && (k<len_c))


{
if(a[i] < b[j])
{
c[k]=a[i];
i++;
k++;
}

DEPSTAR(CSE) 65
Computer Concepts And Programming[CE143]23dcs088

else
{
c[k]=b[j];
j++;
k++;
}
}

while(i<len_a)
{
c[k]=a[i];
i++;
k++;
}
while(j<len_b)
{
c[k]=b[j];
j++;
k++;
}

printf("\nMerge and sorted Array c is:\n");


for(k=0; k<len_c; k++)
{
printf("%d \n", c[k]);
}

printf("23dcs088 PATEL RUDRA");

DEPSTAR(CSE) 66
Computer Concepts And Programming[CE143]23dcs088

Output:

Conclusion:
In this practical, we learned about the sorting and searching using array.

PRACTICAL 7.3
AIM :
Write a Program to multiply any two 3*3 Matrices. Test Data: Input the rows and
columns of first matrix: 3 3 Input the rows and columns of second matrix: 3 3

Algorithm:
Step 1 : Start
Step 2 : Drclare variables mat1[3][3],mat2[3][3],mul[3][3],i,j,k

DEPSTAR(CSE) 67
Computer Concepts And Programming[CE143]23dcs088

Step 3 : for i<3 if condition is true go to step 4 else go to step 6


Step 4 : for j<3,j++ take input of rows and columns else go to step 5
Step 5 : i++
Step 6 : for j<3 if condition is true go to step 7 else go to step 9
Step 7 : for j<3,j++ take input of rows and columns else go to step 8
Step 8 : i++
Step 9 : for i<3 if condition is true go to step 10 else go to step 14
Step 10 : for j<3 if condition is true go to step 11 else go to step 13
Step 11: for k<3 if condition is true mul[i][j]+=mat1[i][k]*mat2[i][j] else go to step 12
Step 12 : j++
Step 13 : i++
Step 14 : print the multiplication matrix
Step 15 : Stop

Flowchart:

DEPSTAR(CSE) 68
Computer Concepts And Programming[CE143]23dcs088

Output:

DEPSTAR(CSE) 69
Computer Concepts And Programming[CE143]23dcs088

Question Answer:
1. State the advantages of using Array Indexes. When is it suitable to take
array index?
Ans.: In arrays, the elements can be accessed randomly by using the index
number. Arrays allocate memory in contiguous memory locations for all its
elements. Hence there is no chance of extra memory being allocated in case of
arrays. This avoids memory overflow or shortage of memory in arrays.

DEPSTAR(CSE) 70
Computer Concepts And Programming[CE143]23dcs088

Conclusion:
In this practical, we learned about how to do arithmetic operations using array.

DEPSTAR(CSE) 71
Computer Concepts And Programming[CE143]23dcs088

DEPSTAR(CSE) 72

You might also like