0% found this document useful (0 votes)
21 views22 pages

C Practical

practical for c language

Uploaded by

nikhiljangra2202
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)
21 views22 pages

C Practical

practical for c language

Uploaded by

nikhiljangra2202
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/ 22

A

PRACTICAL FILE
ON
‘C’ PROGRAMMING
(Session 2023-24)

Submitted To: Submitted By:


Mr. Pankaj Dixit Name:Nikhil
Class :BCA 1st year
University roll no.: 231602014126
College roll no. :1230545132

Department of Management of Computer Science


Vaish College , Bhiwani
INDEX
S no. Particulars Signature
Page
no.
1. GROSS SALARY OF AN 3-4
EMPLOYEE

2. TO CHECK WHETHER A 5
NUMBER IS EVEN OR ODD

3. SWAPPING TWO NUM WITHOUT 6-7


THIRD VARIABLE

4. MULTIPLICATION TABLE UP 8-9


TO 10

5. INVERTED FULL PYRAMID OF “*” 10

6. MULTIPLE IF STATEMENTS 11-13

7. TO CHECK VOWEL OR 14-15


CONSONANT USING SWITCH
CASE

8. TO FIND SUM OF TWO MATRICES 16-18


OF SIZE 3X3

9. PROGRAM OF CALCULATOR 19-20

10. PRIME NUMBER PROGRAM IN C 21-22

Page no.2
 ‘C’ PROGRAM TO CALCULATE GROSS
SALARY OF AN EMPLOYEE

#include <stdio.h>
int main(){
float basic, gross, da, hra; /* Input basic salary of employee
*/

printf("Enter basic salary of an employee: ");


scanf("%f", &basic);

/* Calculate D.A and H.R.A according to specified conditions


*/

if(basic <= 10000)


{ da = basic * 0.9;
hra = basic * 0.3;
} else if(basic <= 20000) {
da = basic * 0.11;
hra = basic * 0.26;
} else{
da = basic * 0.96;
hra = basic * 0.5;
}
/* Calculate gross salary */

gross = basic + hra + da;


printf("GROSS SALARY OF EMPLOYEE = %.2f", gross);
return 0;

Page no.3
 OUTPUT
Enter basic salary of an employee:

9000 GROSS SALARY OF EMPLOYEE =

19800.00

/* Run Again */
Enter basic salary of an employee:

18000 GROSS SALARY OF EMPLOYEE =

24660.00

/*Run Again*/
Enter basic salary of an employee:

27000

GROSS SALARY OF EMPLOYEE = 66420.00


#include <stdio.h>
int main() {

int num;

printf("Enter an integer: ");

scanf("%d", &num);

// true if num is perfectly divisible by 2

if(num % 2 == 0)

printf("%d is even.", num);

else

printf("%d is odd.", num);

return 0;

 OUTPUT
Enter an integer num: 25

25 is odd.

/* Run Again */

Enter an integer num: 512

512 is even
 ‘C’ PROGRAM SWAPPING TWO NUMbers
WITHOUT THIRD VARIABLE
include <stdio.h>
#include <conio.h>
int main(int argc, char const *argv[])
{ int m,n;
printf("before swapping value:\n ");
//input integer value from user

printf("enter the value of m\n");

scanf("%d",&m);
printf("enter the value of n\n");
scanf("%d",&n);
m=m+n;
n=m- n;
m=m-n;
printf("after swapping value:\n");
printf("value of m=%d\n value of n=%d\n",m,n);
return 0;
}
 OUTPUT
Before Swapping value:
Enter the value of m
251
Enter the
value of n
563
After Swapping
value: Value
of m=563
Value of n=251
 MULTIPLICATION TABLE UP TO 10
#include <stdio.h>

clrscr();

int main(int argc, char const *argv[])


{ int a,b,c,d;
printf("Enter the number you want multiplication table of :\
n"); scanf("%d",&a);
printf("Enter the number you want multiplication table of :\
n"); scanf("%d",&b);
printf("Enter the number you want multiplication table of :\
n");
scanf("%d",&c);

printf("Enter the number you want multiplication table of :\


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

printf("Multi table of %d ,%d ,%d,%d is as following:\


n",a,b,c,d); for(int i=1; i<11; i++)
{
printf("%d x %d =%d\t",a,i,a*i);
printf("%d x %d =%d\t",b,i,b*i);
printf("%d x %d =%d\t",c,i,c*i);
printf("%d x %d =%d\n",d,i,d*i);
}
return 0;
}
 OUTPUT

Enter the number you want multiplication table of : 2


Enter the number you want multiplication table of : 3
Enter the number you want multiplication table of : 5
Enter the number you want multiplication table of : 25

Multiple table of 2 ,3 ,5,25is as following:


2 x 1 =2 3 x 1 =3 5 x 1 =5 25 x 1 =25

2 x 2 =4 3 x 2 =6 5 x 2 =10 25 x 2 =50

2 x 3 =6 3 x 3 =9 5 x 3 =15 25 x 3 =75

2 x 4 =8 3 x 4 =12 5 x 4 =20 25 x 4 =100

2 x 5 =10 3 x 5 =15 5 x 5 =25 25 x 5 =125

2 x 6 =12 3 x 6 =18 5 x 6 =30 25 x 6 =150

2 x 7 =14 3 x 7 =21 5 x 7 =35 25 x 7 =175

2 x 8 =16 3 x 8 =24 5 x 8 =40 25 x 8 =200

2 x 9 =18 3 x 9 =27 5 x 9 =45 25 x 9 =225

2 x 10 =20 3 x 10 =30 5 x 10 =50 25 x 10 =250


#include <stdio.h>

int main() {
int rows, i, j;

printf("Enter the number of rows: ");


scanf("%d", &rows);

for (i = rows; i >= 1; --i) {


for (j = 1; j <= i; ++j) {
printf("* ");
}
printf("\n");
}

return 0;
}

 OUTPUT

Enter the number of rows: 5

* * * * *
* * * *
* * *
* *
*
 MULTIPLE IF STATEMENTS
#include <stdio.h>

int main() {

int a, b, c;

printf("Enter three numbers (separated by spaces): ");

scanf("%d %d %d", &a, &b, &c);

if (a == b && b == c) {
printf("All three numbers are equal.\n");
} else if (a > b && a > c) {
printf("The greatest number is: %d\n", a);
} else if (b > a && b > c) {
printf("The greatest number is: %d\n", b);
} else if (c > a && c > b) {
printf("The greatest number is: %d\n", c);
} else {
printf("There are multiple numbers with the same
greatest value.\n");
}

return 0;
}

Page no.10
❖ OUTPUT
Enter three numbers?
58
64
52
b=64 is greater then a and c
/* Run Again */

Enter three

numbers? 58

58

58

All are equal

a=b=c

58=58=58

Page no.11
 C PROGRAM TO CHECK VOWEL OR
CONSONANT USING SWITCH CASE
#include <stdio.h>

int main() {
char ch;

printf("Enter a character: ");


scanf(" %c", &ch);

switch(ch) {
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
printf("%c is a vowel.\n", ch);
break;
default:
printf("%c is a consonant.\n", ch);
}

return 0;
}

Page no.12
❖ OUTPUT

Enter any character:


a
Character is vowel

/* Run Again */
Enter any character:
h
Character is a consonant
/* Run Again */
Enter any character:
G
Character is a consonant

Page no.13
 C PROGRAM TO FIND SUM OF
TWO MATRICES OF SIZE 3X3
#include <stdio.h>

int main() {
int matrix1[3][3], matrix2[3][3], sum[3][3];
int i, j;

// Input for matrix 1

printf("Enter elements of first matrix


(3x3):\n");
for(i = 0; i < 3; i++) {
for(j = 0; j < 3; j++) {
printf("Enter element [%d][%d]: ",
i+1, j+1);
scanf("%d", &matrix1[i][j]);
}
}

// Input for matrix 2


printf("\nEnter elements of second matrix
(3x3):\n");

for(i = 0; i < 3; i++) {


for(j = 0; j < 3; j++) {
printf("Enter element [%d][%d]: ",
i+1, j+1);
scanf("%d", &matrix2[i][j]);
}
}

// Adding matrices
for(i = 0; i < 3; i++) {
for(j = 0; j < 3; j++) {
sum[i][j] = matrix1[i][j] +
Page no.14
matrix2[i][j];
}
}

// Printing the sum matrix


printf("\nSum of the two matrices:\n");
for(i = 0; i < 3; i++) {
for(j = 0; j < 3; j++) {
printf("%d ", sum[i][j]);
}
printf("\n");
}

return 0;
}

❖ OUTPUT
Enter elements in matrix A of size
3x3: 2 5 6
854
691

Enter elements in matrix B of size 3x3:


894
547
643

Sum of matrices A+B


10 14 10
13 9 11
12 13 4

Page no.15
 CALCULATOR USING IF-ELSE-IF
CONDITION
#include <stdio.h>

int main() {
char operator;
double num1, num2, result;

// Input operator
printf("Enter an operator (+, -, *, /): ");
scanf(" %c", &operator);

// Input two numbers


printf("Enter two numbers: ");
scanf("%lf %lf", &num1, &num2);

// Perform calculation based on operator


if (operator == '+') {
result = num1 + num2;
} else if (operator == '-') {
result = num1 - num2;
} else if (operator == '*') {
result = num1 * num2;
} else if (operator == '/') {
if (num2 != 0) {
result = num1 / num2;
} else {
printf("Error! Division by zero.\n");
return 1; // Exit the program with an
error code
}
} else {
printf("Invalid operator!\n");
Page no.16
return 1;
// Exit the program with an error code
}

// Print the result


printf("Result: %.2lf\n", result);

return 0;
}

❖ OUTPUT
Choose your Operator (+,-,*,/,): *
Enter first no.- 15
Enter Second no.- 6
Multiplication of a and b is: 90

/* Run Again */

Choose your Operator (+,-,*,/,): +


Enter first no.- 253
Enter Second no.- 250
Sum of a and b is: 503

Page no.17
 PRIME NUMBER PROGRAM IN
C

#include <stdio.h>

int main() {
int num, i, flag = 0;

// Input a number from user


printf("Enter a positive integer: ");
scanf("%d", &num);

// Check if the number is prime


if (num <= 1) {
printf("%d is not a prime number.\n",
num);
} else {
for (i = 2; i <= num / 2; ++i) {
if (num % i == 0) {
flag = 1;
break;
}
}
if (flag == 0)
printf("%d is a prime number.\n",
num);
else
printf("%d is not a prime number.\n",
num);
}

return 0;
Page no.20
}

Page no.20
❖ OUTPUT
Enter the number to check prime
num: 25
Number is not prime

/* Run Again */
Enter the number to check prime
num: 39
Number is not prime

/* Run Again */
Enter the number to check prime
num: 29
Number is prime

Page no.21
Page no.22

You might also like