You are on page 1of 47

C Programming

185151Computer Practice I

Ex. No. 3C PROGRAMMING


C is a powerful, portable and elegantly structured programming language. It combines features of a high-level language with the elements of an assembler and therefore, suitable for writing both system software and application packages. It is the most widely used generalpurpose language today. C has been used for implementing systems such as operating systems, compilers, linkers, word processors and utility packages. It is a robust language whose rich set of built-in functions and operators can be used to write any complex program. Programs written in C are fast and efficient.

Turbo C IDE Turbo C IDE facilitates editing, debugging and execution of applications written in C. C programs are saved with .c extension. Some of the shortcut keys are: Ctrl + F1 F2 F3 Copy Cut Paste Clear Help Save Open Ctrl + Ins Shift + Del Shift + Ins Ctrl + Del Alt + F9 Ctrl + F9 Alt + F5 F7 Alt + F3 Alt + X Compile Execute User Screen Trace Into Close Quit

cseannauniv.blogspot.com

S.K. Vijai Anand

C Programming

185151Computer Practice I

Ex. No. 3.1AREA OF A CIRCLE


Aim
To write a program to compute the area and circumference of a circle.

Algorithm
Step 1 : Step 2 : Step 3 : Step 4 : Step 5 : Step 6 : Step 7 : Start Define constant pi = 3.14 Read the value of radius Calculate area using formulae pi*radius2 Calculate circumference using formulae 2*pi*radius Print area and circumference Stop

Flowchart

Start

pi = 3.14

Read radius area = pi * radius2 circumference = 2 * pi * radius Print area, circumference

Stop

cseannauniv.blogspot.com

S.K. Vijai Anand

C Programming

185151Computer Practice I

Program
/* Area and circumference of a circle */ #include <stdio.h> #include <conio.h> #define pi 3.14 main() { int radius; float area, circum; clrscr(); printf("\nEnter radius of the circle : "); scanf("%d",&radius); area = pi * radius * radius; circum = 2 * pi * radius; printf("\nArea is %.2f and circumference is %.2f\n",area,circum); getch(); }

Output
Enter radius of the circle : 8 Area is 200.96 and circumference is 50.24

cseannauniv.blogspot.com

S.K. Vijai Anand

C Programming

185151Computer Practice I

Ex. No. 3.2BIGGEST OF TWO NUMBERS


Aim
To write a program to determine the bigger of two numbers using ternary operator.

Algorithm
Step 1 : Step 2 : Step 3 : Step 3.1 : Step 3.2 : Step 4 : Start Read values of a and b If a = b then print "Both are equal" Else if a > b then print "A is big" Else print "B is big" Stop

Flowchart

Start

Read a, b

a=b ?

Print "Both are equal"

N Print "B is big" N a>b ? Y Print "A is big"

Stop

cseannauniv.blogspot.com

S.K. Vijai Anand

C Programming

185151Computer Practice I

Program
/* Biggest of 2 Nos using Ternary Operator */ #include <stdio.h> #include <conio.h> main() { int a,b; clrscr(); printf("Enter A value : "); scanf("%d",&a); printf("Enter B value : "); scanf("%d",&b); (a==b) ? printf("\nBoth are equal") : a>b ? printf("\nA is greater") : printf("\nB is greater"); getch(); }

Output
Enter A value : -2 Enter B value : -5 A is greater

Enter A value : 4 Enter B value : 7 B is greater

Enter A value : 11 Enter B value : 11 Both are equal

cseannauniv.blogspot.com

S.K. Vijai Anand

C Programming

185151Computer Practice I

Ex. No. 3.3LEAP YEAR


Aim
To write a program to check whether the given year is a leap year.

Algorithm
Step 1 : Step 2 : Step 3 : Step 3.1 : Step 3.2 : Step 4 : Start Read the value of year If year divisible by 400 then print "Leap year" Else if year divisible by 4 and not divisible by 100 then print "Leap year" Else print "Not a leap year" Stop

Flowchart

Start

Read year

Y year%400=0 ? N Print "Not Leap" year%4 = 0 and year%100 0 ?

Print "Leap"

Print "Leap"

Stop

cseannauniv.blogspot.com

S.K. Vijai Anand

C Programming

185151Computer Practice I

Program
/* Leap Year */ #include <stdio.h> #include <conio.h> main() { int year; clrscr(); printf("\nEnter the year : "); scanf("%d",&year); if (year%400 == 0) printf("\n%d is else if (year%100 != printf("\n%d is else printf("\n%d is getch(); }

a Leap year",year); 0 && year%4 == 0) a Leap year",year); not a Leap year",year);

Output
Enter the year : 2000 2000 is a Leap year

Enter the year : 1800 1800 is not a Leap year

Enter the year : 2004 2004 is a Leap year

cseannauniv.blogspot.com

S.K. Vijai Anand

C Programming

185151Computer Practice I

Ex. No. 3.4SIMPLE CALCULATOR


Aim
To write a menu driven calculator program using switch statement.

Algorithm
Step 1 : Step 2 : Step 3 : Step 4 : Step 4.1 : Step 4.2 : Step 4.3 : Step 4.4 : Step 4.2 : Step 5 : Step 6 : Start Display calculator menu options Read the operator symbol and operands n1, n2 If operator = + then calculate result = n1 + n2 Else if operator = then calculate result = n1 n2 Else if operator = * then calculate result = n1 * n2 Else if operator = / then calculate result = n1 / n2 Else if operator = % then calculate result = n1 % n2 Else print "Invalid operator" and go to step 6 Print result Stop

cseannauniv.blogspot.com

S.K. Vijai Anand

C Programming

185151Computer Practice I

Flowchart

Start

Display Calculator menu

Read operator, n1, n2

operator ?

+ res=n1+n2

res=n1n2

* res=n1*n2

/ res=n1/n2

% res=n1%n2

Other

Print res Operator"

Print "Invalid operator"

Stop

cseannauniv.blogspot.com

S.K. Vijai Anand

C Programming

185151Computer Practice I

Program
/* Simple Calculator */ #include <stdio.h> #include <stdlib.h> #include <conio.h> main() { int n1, n2, result; char op; clrscr(); printf("\n Simple Calculator\n"); printf("\n + Summation"); printf("\n - Difference"); printf("\n * Product"); printf("\n / Quotient"); printf("\n % Remainder\n"); printf("\nEnter the operator : "); op = getchar(); printf("Enter operand1 and operand2 : "); scanf("%d%d",&n1,&n2); switch (op) { case '+': result = n1 +n2; break; case '-': result = n1 - n2; break; case '*': result = n1 * n2; break; case '/': result = n1 / n2; break; case '%': result = n1 % n2; break; default: printf("Invalid operator"); exit(-1); } printf("\n%d %c %d = %d", n1, op, n2, result); getch(); }

cseannauniv.blogspot.com

S.K. Vijai Anand

C Programming

185151Computer Practice I

Output
Simple Calculator + * / % Summation Difference Product Quotient Remainder

Enter the operator : Enter operand1 and operand2 : 2 4 2 - 4 = -2

Simple Calculator + * / % Summation Difference Product Quotient Remainder

Enter the operator : * Enter operand1 and operand2 : 3 2 3 * 2 = 6

Simple Calculator + * / % Summation Difference Product Quotient Remainder

Enter the operator : % Enter operand1 and operand2 : 5 2 5 % 2 = 1

cseannauniv.blogspot.com

S.K. Vijai Anand

C Programming

185151Computer Practice I

Ex. No. 3.5BINARY TO DECIMAL


Aim
To write a program to convert binary number to its decimal equivalent using while loop.

Algorithm
Step 1 : Step 2 : Step 3 : Start Read the binary value Initialize dec and i to 0 Repeat steps 47 until binary > 0 Step 4 : Step 5 : Step 6 : Step 7 : Step 8 : Step 9 : Extract the last digit using modulo 10 Calculate decimal = decimal + digit * 2i Recompute binary = binary / 10 Increment i by 1 Print decimal Stop

cseannauniv.blogspot.com

S.K. Vijai Anand

C Programming

185151Computer Practice I

Flowchart

Start

Read binary

decimal = i = 0

binary>0 ? Y

digit = binary % 10 decimal = decimal + digit * 2 i binary = binary / 10 i=i+1

Print decimal

Stop

cseannauniv.blogspot.com

S.K. Vijai Anand

C Programming

185151Computer Practice I

Program
/* Binary to Decimal */ #include <stdio.h> #include <math.h> #include <conio.h> main() { int dec=0,i=0, d; long bin; clrscr(); printf("Enter a binary number : "); scanf("%ld",&bin); while(bin) { d = bin % 10; dec = dec + pow(2,i) * d; bin = bin/10; i = i + 1; } printf("\nDecimal Equivalent is %d", dec); getch(); }

Output
Enter a binary number : 1101011 Decimal Equivalent is 107

cseannauniv.blogspot.com

S.K. Vijai Anand

C Programming

185151Computer Practice I

Ex. No. 3.6PRIME NUMBER


Aim
To write a program to check the given number is prime or not using for loop.

Algorithm
Step 1 : Step 2 : Step 3 : Start Read the value of n Initialize i to 2 and flg to 0 Repeat steps 4 and 5 until i Step 4 : Step 5 : Step 6 : Step 6.1 : Step 7 : n/2

If n is divisible by i then assign 1 to flg and go to Step 6 Increment i by 1 If flg = 0 then print "Prime" Else print "Not prime" Stop

cseannauniv.blogspot.com

S.K. Vijai Anand

C Programming

185151Computer Practice I

Flowchart

Start

Read n

i = 2, flg = 0

n/2 ? Y

n%i = 0 ? N i=i+1

flg = 1

Print "Not Prime"

flg = 0 ?

Print "Prime"

Stop

cseannauniv.blogspot.com

S.K. Vijai Anand

C Programming

185151Computer Practice I

Program
/* Prime number */ #include <stdio.h> #include <conio.h> main() { int i,n,flg=0; clrscr(); printf("\nEnter a number : "); scanf("%d",&n); for(i=2; i<=n/2; i++) { if (n%i == 0) { flg = 1; break; } } if (flg == 0) printf("\nThe given number is prime"); else printf("\nThe given number is not prime"); getch(); }

Output
Enter a number : 27 The given number is not prime

Enter a number : 43 The given number is prime

cseannauniv.blogspot.com

S.K. Vijai Anand

C Programming

185151Computer Practice I

Ex. No. 3.7ARRAY MINIMUM / MAXIMUM


Aim
To write a program to find the largest and smallest of an array

Algorithm
Step 1 : Step 2 : Step 3 : Step 4 : Step 5 : Start Read the number of array elements as n Set up a loop and read array elements Ai, i = 0,1,2,n1 Assume the first element A0 to be min and max Initialize i to 1 Repeat steps 68 until i < n Step 6 : Step 7 : Step 8 : Step 9 : Step 10 : If max < Ai then max = Ai If min > Ai then min = Ai Increment i by 1 Print max, min Stop

cseannauniv.blogspot.com

S.K. Vijai Anand

C Programming

185151Computer Practice I

Flowchart
Start

Read n

i = 0 to n1

Read Ai

min = max = A0

i = 1 to n1

max < Ai ? N

max = Ai

min > Ai ? N

min = Ai

Print min, max

Stop

cseannauniv.blogspot.com

S.K. Vijai Anand

C Programming

185151Computer Practice I

Program
/* Maximum and Minimum of an array */ #include <stdio.h> #include <conio.h> main() { int a[10]; int i,min,max,n; clrscr(); printf("Enter number of elements : "); scanf("%d",&n); printf("\nEnter Array Elements\n"); for(i=0; i<n; i++) scanf("%d",&a[i]); min = max = a[0]; for(i=1; i<n; i++) { if (max < a[i]) max = a[i]; if (min > a[i]) min = a[i]; } printf("\nMaximum value = %d \n Minimum value = %d",max,min); getch(); }

Output
Enter number of elements : 6 Enter Array Elements 3 8 -7 11 -9 0 Maximum value = 11 Minimum value = -9

cseannauniv.blogspot.com

S.K. Vijai Anand

C Programming

185151Computer Practice I

Ex. No. 3.8MATRIX MULTIPLICATION


Aim
To write a program to perform matrix multiplication based on their dimensions.

Algorithm
Step 1 : Step 2 : Step 3 : Step 4 : Step 5 : Step 6: Step 7 : Step 8 : Step 9 : Step 10 : Step 11 : Step 12 : Start Read the order of matrix A as m and n Read the order of matrix B as p and q If n p then print "Multiplication not possible" and Stop

Set up a loop and read matrix A elements Aij, i = 0 to m-1 and j = 0 to n-1 Set up a loop and read matrix B elements Bij, i = 0 to p-1 and j = 0 to q-1 Initialize i to zero Initialize j to zero Assign 0 to Cij Initialize k to zero Compute Cij = Cij + Aik * Bkj Increment k by 1 Repeat steps 11 and 12 until k < n

Step 13 :

Increment j by 1 Repeat steps 913 until j < q

Step 14 :

Increment i by 1 Repeat steps 814 until i < m

Step 15 : Step 16 :

Set up a loop and print product matrix Cij, i = 0 to m-1 and j = 0 to q-1 Stop

cseannauniv.blogspot.com

S.K. Vijai Anand

C Programming

185151Computer Practice I

Flow Chart
Start

Read m, n

Read p, q

n=p ? Y

Print min, max

X i = 0 to m1 j = 0 to n1 Read Aij

i = 0 to p1 j = 0 to q1 Read Bij

2
cseannauniv.blogspot.com S.K. Vijai Anand

C Programming

185151Computer Practice I

i = 0 to m1 j = 0 to q1 Cij = 0

k = 0 to n1 Cij = Cij + Aik * Bkj

i = 0 to m1 j = 0 to q1 Print Cij

i X

Stop

cseannauniv.blogspot.com

S.K. Vijai Anand

C Programming

185151Computer Practice I

Program
/* Matrix Multiplication */ #include <stdio.h> #include <conio.h> main() { int a[10][10], b[10][10], c[10][10]; int r1, c1, r2, c2; int i, j, k; clrscr(); printf("Enter scanf("%d%d", printf("Enter scanf("%d%d",

order of matrix A : "); &r1, &c1); order of matrix B : "); &r2, &c2);

if (c1 != r2) { printf("\nMatrix multiplication not possible"); getch(); exit(0); } printf("\nEnter matrix A elements\n"); for(i=0; i<r1; i++) { for(j=0; j<c1; j++) { scanf("%d",&a[i][j]); } } printf("\nEnter matrix B elements\n"); for(i=0; i<r2; i++) { for(j=0; j<c2; j++) { scanf("%d",&b[i][j]); } } for(i=0; i<r1; i++) { for(j=0; j<c2; j++) { c[i][j] = 0;

cseannauniv.blogspot.com

S.K. Vijai Anand

C Programming

185151Computer Practice I

for(k=0; k<c1; k++) { c[i][j] += a[i][k] * b[k][j]; } } } printf("\nProduct matrix C\n"); for(i=0; i<r1; i++) { for(j=0; j<c2; j++) { printf("%-4d",c[i][j]); } printf("\n"); } getch(); }

Output
Enter order of matrix A : 2 3 Enter order of matrix B : 3 2 Enter matrix A elements 1 1 1 1 1 1 Enter matrix B elements 2 2 2 2 2 2 Product matrix C 6 6 6 6

Enter order of matrix A : 3 3 Enter order of matrix B : 2 3 Matrix multiplication not possible

cseannauniv.blogspot.com

S.K. Vijai Anand

C Programming

185151Computer Practice I

Ex. No. 3.9ALPHABETICAL ORDER


Aim
To write a program to arrange names in their alphabetic order using bubble sort method.

Algorithm
Step 1 : Step 2 : Step 3 : Step 4 : Step 5 : Step 6: Step 7 : Start Read number of name as n Set up a loop and read the name list in name array Assign 0 to i Assign i+1 to j If namei > namej then swap the strings namei and namej Increment j by 1 Repeat steps 6 and 7 until j < n Step 8 : Increment i by 1 Repeat steps 58 until i < n-1 Step 9 : Step 10 : Set up a loop and print the sorted name array Stop

cseannauniv.blogspot.com

S.K. Vijai Anand

C Programming

185151Computer Practice I

Flow Chart
Start

Read n

i = 0 to n1 Read namei

i = 0 to n2 j = i+1 to n1

namei > namej ? N

Swap strings namei , namej

i = 0 to n1 Print namei

Stop

cseannauniv.blogspot.com

S.K. Vijai Anand

C Programming

185151Computer Practice I

Program
/* Sorting strings */ #include <stdio.h> #include <conio.h> #include <string.h> main() { char name[20][15], t[15], srch[15]; int i,j,n,upper,lower,mid; clrscr(); printf("Enter number of students : "); scanf("%d",&n); printf("\nEnter student names\n"); for(i=0; i<n; i++) scanf("%s",name[i]); /* Bubble Sort method */ for(i=0; i<n-1; i++) { for(j=i+1;j<n;j++) { if (strcmp(name[i],name[j]) > 0) { strcpy(t, name[i]); strcpy(name[i], name[j]); strcpy(name[j], t); } } } printf("\nAlphabetical Order\n"); for(i=0;i<n;i++) printf("%s\n",name[i]); getch(); }

cseannauniv.blogspot.com

S.K. Vijai Anand

C Programming

185151Computer Practice I

Output
Enter number of students : 10 Enter student names Raghu Praba Gopal Anand Venkat Kumar Saravana Naresh Christo Vasanth Alphabetical Order Anand Christo Gopal Kumar Naresh Praba Raghu Saravana Vasanth Venkat

cseannauniv.blogspot.com

S.K. Vijai Anand

C Programming

185151Computer Practice I

Ex. No. 3.10STRING REVERSE


Aim
To write a program to reverse the given string without using library functions.

Algorithm
Step 1 : Step 2 : Step 3 : Step 4 Step 5 : Step 6 : Step 7: Step 8 : Step 9 : Start Read string str Assign 0 to len If lenth character '\0' then go to step 5 else step 6

Increment len by 1 and go to step 4 Assign 0 to i and len-1 to j Swap the ith and jth character Increment i by 1 Decrement j by 1 Repeat steps 79 until i < len/2

Step 10 : Step 10 :

Print reversed string str Stop

cseannauniv.blogspot.com

S.K. Vijai Anand

C Programming

185151Computer Practice I

Flow Chart
Start

Read str

len = 0

lenth char '\0' ? Y len = len + 1

i=0 j = len - 1

i < len/2 ? Y T=i i=j j=T

i=i+1 j=j1

Print str

Stop

cseannauniv.blogspot.com

S.K. Vijai Anand

C Programming

185151Computer Practice I

Program
/* User-defined String reverse */ #include <stdio.h> #include <conio.h> main() { char *str; int i,j,len=0; char t; clrscr(); printf("Enter a String gets(str);

: ");

/* String Length */ while (str[len] != '\0') len++; /* String Reverse */ for(i=0, j=len-1; i<len/2; i++, j--) { t = str[i]; str[i] = str[j]; str[j] = t; } printf("\nReversed String : %s",str); getch(); }

Output
Enter a String : SMK Fomra Institute of Tech

Reversed String : hceT fo etutitsnI armoF KMS

cseannauniv.blogspot.com

S.K. Vijai Anand

C Programming

185151Computer Practice I

Ex. No. 3.11SINE SERIES


Aim
To write a program to generate sine series sin(x) = x function.

x3 3!

x5 5!

x7 7!

value using

Algorithm
Step 1 : Step 2 : Step 3 : Step 4 : Step 5 : Step 6: Start Read sine degree as deg Convert degree to radians using the formula x = deg * Call sinecalc function with parameter x Print computed value of sine series Stop / 180

sinecalc Function Step 1 : Assign x to term and sum Repeat steps 3 and 4 until term < 0.00001 Step 2 : Step 3 : Step 4 : Compute new term as term = term * x2 / (2i (2i+1)) Alternatively subtract and add term to sum Return sum

cseannauniv.blogspot.com

S.K. Vijai Anand

C Programming

185151Computer Practice I

Flow Chart
Start

Read deg, n

x = deg * 3.14 / 180

Call sinevalue = sinecalc(x)

Print sinevalue

Stop

sinecalc (x)

sgn = 1 sum = term = x

term> 0.00001

Y sgn = -sgn term = term * x2 / (2i (2i+1)) sum = sum + sgn * term

Return (sum)

cseannauniv.blogspot.com

S.K. Vijai Anand

C Programming

185151Computer Practice I

Program
/* Sine series */ #include <stdio.h> #include <math.h> #define pi 3.14 float sinecalc(float, int); main() { int deg,n; float x,sineval; clrscr(); printf("Enter Sine degree : "); scanf("%d",&deg); printf("Enter no. of terms : "); scanf("%d",&n); x = deg*pi/180; sineval = sinecalc(x); printf("\nGenerated series value : %.4f", sineval); printf("\nBuilt-in function value : %.4f", sin(x)); printf("\nComputational error\t : %.4f", sin(x)-sineval); getch(); } float sinecalc(float x) { int i=1,sgn=1; float sum,term; sum = term = x; while (1) { if (term < 0.00001) break; printf("\nThe term is %.4lf and sum is %.4lf", term, sum); sgn = -sgn; term *= x*x / (2*i * (2*i+1)); sum += sgn *term; i++; } return (sum); }

cseannauniv.blogspot.com

S.K. Vijai Anand

C Programming

185151Computer Practice I

Output
Enter Sine degree: 73 The The The The The term term term term term is is is is is 1.2734 0.3442 0.0279 0.0011 0.0000 and and and and and sum sum sum sum sum is is is is is 1.2734 0.9293 0.9572 0.9561 0.9561 0.9561 0.9561 0.0000

The value of Generated Sine series is Value using built-in function is Computational error is

cseannauniv.blogspot.com

S.K. Vijai Anand

C Programming

185151Computer Practice I

Ex. No. 3.12RECURSIVE FUNCTION


Aim
To write a program to find factorial value of a given number n! = n * (n-1)! using recursion.

Algorithm
Step 1 : Step 2 : Step 3 : Step 4 : Step 5: Start Read the value of n Call factorial function with parameter n Print factorial value Stop

factorial Function Step 1 : Step 1.1 : If n = 1 then return 1 Else return n * factorial(n-1)

cseannauniv.blogspot.com

S.K. Vijai Anand

C Programming

185151Computer Practice I

Flow Chart
Start

Read n

Call factvalue = factorial(n)

Print factvalue

Stop

factorial(n)

n=1 ?

Return (1)

Return (n * factorial(n-1))

cseannauniv.blogspot.com

S.K. Vijai Anand

C Programming

185151Computer Practice I

Program
/* Factorial--Recursion */ #include <stdio.h> #include <conio.h> long factorial(int); main() { int n; long int f; clrscr(); printf("Enter a number : "); scanf("%d",&n); f=factorial(n); printf("Factorial value : %ld",f); getch(); } long factorial(int n) { if (n<=1) return(1); else return (n * factorial(n-1)); }

Output
Enter a number : 6 Factorial value : 720

Enter a number : 12 Factorial value : 479001600

cseannauniv.blogspot.com

S.K. Vijai Anand

C Programming

185151Computer Practice I

Ex. No. 3.13PASS BY VALUE & REFERENCE


Aim
To write a program that demonstrates passing parameters to a function by value and reference.

Algorithm
Step 1 : Step 2 : Step 3 : Step 4 : Step 5 : Step 6 : Step 7 : Step 8 : Start Assign 10 to a and 20 to b Print a and b Call swapval function with values of a and b Print a and b Call swapref function with address of a and b Print a and b Stop

swapval Function Step 1 : Step 2 : Step 3 : Step 4 : t=a a=b b=t Return

swapref Function Step 1 : Step 2 : Step 3 : Step 4 : t = *a *a = *b *b = t Return

cseannauniv.blogspot.com

S.K. Vijai Anand

C Programming

185151Computer Practice I

Flow Chart
Start

a = 10 b = 20 Print a, b

Call swapval(a, b)

Print a, b

Call swapref(&a, &b)

Print a, b

Stop

swapval (a, b)

swapref (*a, *b)

t=a a=b b=t

t = *a *a = *b *b = t

Return

Return

cseannauniv.blogspot.com

S.K. Vijai Anand

C Programming

185151Computer Practice I

Program
/* Pass by value and reference */ #include <stdio.h> #include <conio.h> void swapval(int, int); void swapref(int *,int *); main() { int a = 10, b = 20; clrscr(); printf("\nValues before function calls\n"); printf("Value of A : %d\n",a); printf("Value of B : %d\n",b); swapval(a,b); printf("\nValues after Pass by Value\n"); printf("Value of A : %d\n",a); printf("Value of B : %d\n",b); swapref(&a,&b); printf("\nValues after Pass by Reference\n"); printf("Value of A : %d\n",a); printf("Value of B : %d",b); getch(); } /* Pass by value */ void swapval(int a,int b) { int t; t = a; a = b; b = t; } /* Pass by Reference*/ void swapref(int *x,int *y) { int t; t = *x; *x = *y; *y = t; }

cseannauniv.blogspot.com

S.K. Vijai Anand

C Programming

185151Computer Practice I

Output
Values before function calls Value of A : 10 Value of B : 20 Values after Pass by Value Value of A : 10 Value of B : 20 Values after Pass by Reference Value of A : 20 Value of B : 10

cseannauniv.blogspot.com

S.K. Vijai Anand

C Programming

185151Computer Practice I

Ex. No. 3.14PAYROLL APPLICATION


Aim
To write a program to print employee payroll of a concern using structure.

Algorithm
Step 1 : Step 2 : Step 3 : Step 4 : Start Define employee structure with fields empid, ename, basic, hra, da, it, gross and netpay Read number of employees n Set up a loop and read empid, ename, and basic for n employees in emp array Set up a loop and do steps 59 for n employees Step 5 : Step 6 : Step 7 : Step 8 : Step 9 : Step 10: Step 11 Step 12 : hra = 2% of basic da = 1% of basic gross = basic + hra + da it = 5% of basic netpay = gross - it Print company and column header Set up a loop and print empid, ename, basic, hra, da, it, gross and netpay for each employee in emp array Stop

cseannauniv.blogspot.com

S.K. Vijai Anand

C Programming

185151Computer Practice I

Flow Chart
Start

Define emp structure

Read n

i = 0 to n-1 Read empi.empid, empi.name, empi.basic

i = 0 to n-1 empi.hra = 0.02 * emp i.basic empi.da = 0.01 * empi.basic empi.it = 0.05 * empi.basic empi.gross = empi.basic + emp i.hra + empi.da empi.netpay = empi.gross - emp i.it

i = 0 to n-1 Print empi.empid, empi.name, empi.basic, empi.hra, empi.da, empi.gross, emp i.it, empi.netpay

Stop

cseannauniv.blogspot.com

S.K. Vijai Anand

C Programming

185151Computer Practice I

Program
/* Payroll Generation */ #include <stdio.h> #include <conio.h> struct employee { int empid; char ename[15]; int basic; float hra; float da; float it; float gross; float netpay; }; main() { struct employee emp[50]; int i, j, n; clrscr(); printf("\nEnter No. of Employees : "); scanf("%d", &n); for(i=0; i<n ;i++) { printf("\nEnter Employee Details\n"); printf("Enter Employee Id : "); scanf("%d", &emp[i].empid); printf("Enter Employee Name : "); scanf("%s", emp[i].ename); printf("Enter Basic Salary : "); scanf("%d", &emp[i].basic); } for(i=0; i<n; i++) { emp[i].hra = 0.02 * emp[i].basic; emp[i].da = 0.01 * emp[i].basic; emp[i].it = 0.05 * emp[i].basic; emp[i].gross = emp[i].basic+emp[i].hra+emp[i].da; emp[i].netpay = emp[i].gross - emp[i].it; }

cseannauniv.blogspot.com

S.K. Vijai Anand

C Programming

185151Computer Practice I

printf("\n\n\n\t\t\t\tXYZ & Co. Payroll\n\n"); for(i=0;i<80;i++) printf("*"); printf("\nEmpId\tName\t\tBasic\t HRA\t DA\t IT\t Gross\t\tNet Pay\n\n"); for(i=0;i<80;i++) printf("*"); for(i=0; i<n; i++) { printf("\n%d\t%-15s\t%d\t%.2f\t%.2f\t%.2f\t %.2f\t%.2f", emp[i].empid, emp[i].ename, emp[i].basic,emp[i].hra,emp[i].da, emp[i].it, emp[i].gross, emp[i].netpay); } printf("\n"); for(i=0;i<80;i++) printf("*"); getch(); }

Output
Enter No. of Employees : 2 Enter Enter Enter Enter Enter Enter Enter Enter Employee Details Employee Id : 436 Employee Name : Gopal Basic Salary : 10000 Employee Details Employee Id : 463 Employee Name : Rajesh Basic Salary : 22000

XYZ & Co. Payroll ******************************************************************************** EmpId Name Basic HRA DA IT Gross Net Pay

******************************************************************************** 436 463 Gopal Rajesh 10000 22000 200.00 440.00 100.00 220.00 500.00 10300.00 1100.00 22660.00 9800.00 21560.00

********************************************************************************

cseannauniv.blogspot.com

S.K. Vijai Anand

You might also like