44
C PROGRAMMING DATA TYPES, EXPRESSION EVALUATION AND CONDITION STATEMENTS AREA AND CIRCUMFERENCE OF CIRCLE AIM To write a program to find the area and circumference of circle. ALGORITHM Step 1: Start the program. Step 2: Get user input for radius of circle. Step 3: Calculate area of circle by using formula, area = 3.14*radius* radius. Step 4: Calculate circumference of circle by using formula, circum = 2*3.14*radius. Step 5: Display area and circumference. Step 6: Stop the program. PSEUDOCODE BEGIN INITIALIZE PI as 3.14, area, circum, r READ radius r CALCULATE area := PI*r*r CALCULATE circum :=2*PI*r PRINT area, circum END

c Programming

Embed Size (px)

Citation preview

Page 1: c Programming

C PROGRAMMING

DATA TYPES, EXPRESSION EVALUATION AND CONDITION STATEMENTS

AREA AND CIRCUMFERENCE OF CIRCLE

AIM To write a program to find the area and circumference of circle.

ALGORITHM

Step 1: Start the program.

Step 2: Get user input for radius of circle.

Step 3: Calculate area of circle by using formula, area = 3.14*radius* radius.

Step 4: Calculate circumference of circle by using formula, circum = 2*3.14*radius.

Step 5: Display area and circumference.

Step 6: Stop the program.

PSEUDOCODE

BEGIN

INITIALIZE PI as 3.14, area, circum, r

READ radius r

CALCULATE area := PI*r*r

CALCULATE circum :=2*PI*r

PRINT area, circum

END

Page 2: c Programming

FLOWCHART

Start

Read radius

Area = 3.14 * radius * radius Circum = 2 *3.14 * radius

Print area Print circum

Stop

PROGRAM

#include<stdio.h> #define Pi 3.14159 main() { float radius,area, circum; printf("Enter radius: "); scanf("%f",&radius); area= Pi * radius * radius; circum = 2 * Pi * radius; printf("\nArea=%0.2f",area); printf("\nCircumference=%0.2f \n",circum); }

OUTPUT: Enter radius: 5

Area=78.54 Circumference=31.42

RESULT Thus, program to calculate area and circumference of circle is executed and output is

verified

Page 3: c Programming

FACTORIAL OF A GIVEN NUMBER

AIM To write a program to find the factorial of a given number.

ALGORITHM

Step 1: Start the program.

Step 2: Get user input for the factorial number.

Step 3: Set loop to find factorial of given number using formula fact = fact*i.

Step 4: Display the factorial of the given number.

Step 5: Stop.

PSEUDOCODE

BEGIN

INITIALIZE i as 1, fact as 1

READ num

REPEAT

fact := fact*i

INCREMENT i

UNTIL i <= num

PRINT fact

END

Page 4: c Programming

FLOWCHART

Start

Initialize i=1, fact=1

Read number

fact= fact * i i++

Yes

Is i <= num

No

Print factorial

Stop

PROGRAM

#include <stdio.h>

int main() { int i=1, fact=1; int num;

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

while(i <= num) {

fact *= i; i++;

} printf("Factorial: %d\n", fact);

}

OUTPUT Enter a number: 5 Factorial: 120

RESULT

Page 5: c Programming

Thus, program to find factorial of given number is executed and output is verified.

Page 6: c Programming

GENERATING FIBONACCI SERIES

AIM To write a program to generate Fibonacci series up to a given number.

ALGORITHMStep 1: Start the program.

Step 2: Enter the limit

Step 3: Check whether the number is zero or not. If zero print zero, exit.

Step 4: Set loop up to given number.

Step 5: Calulate fib= fib + a, set a=b and b=c for every iteration in the loop.

Step 6: Print the Fibonacci number in every iteration of the loop.

Step 7: Stop the program.

PSEUDOCODE

BEGIN

DECLARE lim, a, b

INITIALIZE a=0, b=1, lim=1

fib = a + b

READ num

IF num == 0

PRINT 0

REPEAT

a = b

b= fib

fib = a + b

INCREMENT lim

PRINT fib

UNTIL lim < num

END

Page 7: c Programming

FLOWCHART

Start

a=0, b=1 fib = a+b

Read number

Is num == 0

a=b;b=fib;

fib = a+b; lim++;

Print fib

Is lim < num

No

Stop

PROGRAM

#include

<stdio.h> int

main() {

int fib=1;int a=0, b=1; int num, i=0; int lim =0; fib =a+b;

printf("Enter the limit:

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

Page 8: c Programming

YesPrint 0

No

Yes

Page 9: c Programming

printf("** Generating Fibonacci series...");if(num == 0)

printf("%d", num);

while(lim < num) {

a=b; b=fib; fib = a+b; lim++;printf(" %d", fib);

}printf("\n");

}

OUTPUT

Enter the limit: 5 ** Generating Fibonacci series... 2 3 5 8 13

RESULT Thus, program to generate Fibonacci series is generated and output is verified.

Page 10: c Programming

CHECK WHETHER A GIVEN NUMBER IS EVEN OR ODD AIM To write a program to find out whether a given number is even or odd.

ALGORITHMStep 1: Start the Program.

Step 2: Enter integer value.

Step 3: Determine rem = value modulo 2.

Step 4: Check if rem is 0.

Step 5: If rem is 0, print the given value is even.

Step 6: If rem is not 0, print the given value is odd.

Step 7: End.

PSEUDOCODE

BEGIN

DECLARE rem

READ val

DETERMINE rem = val%2

IF rem == 0

PRINT Value is Even

ELSE

PRINT Value is ODD

END

Page 11: c Programming

FLOWCHART

Start

Enter value

rem = val % 2

Is rem == 0

Print even Print odd

Stop

PROGRAM:

#include<stdio.h> main() { int num,rem; printf("Enter a number: "); scanf("%d",&num); rem=num%2; if(rem==0) printf(" \n The entered number is EVEN \n"); else printf(" \n The entered number is ODD \n"); }

OUTPUT Enter a number: 5 The entered number is ODD Enter a number: 2 The entered number is EVEN

RESULT Thus program to find whether a given number is even or odd is executed and output is

verified.

Page 12: c Programming

AREA OF TRIANGLE

AIM To write a C program to find the area of triangle

ALGORITHM Step 1: Start Step 2: Read the input values of a,b,c; Step 3: Compute s=(a+b+c)/2; Step 4: Compute d=(s*(s-a)*(s-b)*(s-c)) Step 5: Find the area of triangle with the formula area=sqrt(d) Step 6: Print area Step 7: Stop

PSEUDOCODE

DEFINE AS int a,b,c DEFINE AS float s,d,area Input a, b, c CALCULATE s= (a+b+c)/2 CALCULATE area=(s*(s-a)*(s-b)*(s-c)) PRINT area

PROGRAM

#include<stdio.h> #include<math.h> main() { float a,b,c; double s,d,area; printf("Enter 3 sides"); scanf("%f %f %f",&a,&b,&c); s=(a+b+c)/2; //d=4; d =(s*(s-a)*(s-b)*(s-c)); area=sqrt(d); printf("Area of triangle= %f sq units\n",area); }

Page 13: c Programming

FLOWCHART

Start

Read a,b,c

Read s,d,area

s=(a+b+c)/2

d=(s*(s-a)*(s-b)*(s-c))

Print area

stop

OUTPUT

Enter 3 sides 5 10 8 Area of triangle= 19.810035 sq units

RESULT Thus a C program to find the area of triangle was executed and output was verified.

Page 14: c Programming

FINDING DISCRIMINANT OF QUADRATIC EQUATION

AIM To write a C program to find the discriminant of a quadratic equation.

ALGORITHM Step 1: Start Step 2: Read a,b,c Step 3: Calculate sqroot=sqrt(b*b-4*a*c) Step 4: Calculate x1=((-b+sqroot)/2*a) Step 5: Calculate x2=((-b-sqroot)/2*a) Step 6: Display x1,x2 Step 7: Stop

PSEUDOCODE

BEGIN DEFINE AS int a,b,c DEFINE AS int sqroot, x1,x2 READ a, b, c CALCULATE sqroot = sqrt(b*b - 4*a*c) CALCULATE x1 = ((-b + sqroot) / 2*a) CALCULATE x2 = ((-b - sqroot) / 2*a) PRINT x1, x2 END

PROGRAM

#include<stdio.h> #include<math.h> main() { int a,b,c,sqroot,x1,x2; printf("Enter the values of a,b,c : "); scanf("%d%d%d",&a,&b,&c); sqroot = sqrt(b*b-4*a*c); x1=(-b + sqroot) / (2*a); x2=(-b - sqroot) / (2*a); printf(“First root is %d”, x1 printf("Second root is %d", x2); }

OUTPUT

RESULT Thus a C program to find the square root of a given number was executed and the output was verified.

Page 15: c Programming

C PROGRAM TO CALCULATE SIMPLE INTEREST

AIM To write a C program to calculate simple interest using while condition

ALGORITHM 1. Start 2. Read the values of amount,inrate 3. Read the values year,period 4. Check the condition whether year<=period 5. Calculate the value by using the formula

a. value=amount +inrate +amount 6. Assign the amount to value and increment the year by 1 7. Stop.

PSEUDOCODE

Define : int year,periodFloat amount,inrate,value

Input : amount,rate and period.Condition : while(year<=period) Calculate : value=amount+inrate+amount Output : display result

PROGRAM

main() { int year,period; float amount,inrate,value; printf("input amount,interest rate and period\n\n"); scanf("%f%f%d",&amount,&inrate,&period); printf("\n"); year=1; while(year<=period) { value=amount+inrate+amount; printf("%d%f\n",year,value); amount=value; year=year+1; } }

RESULT Thus a C program to calculate simple interest using while condition was executed and the

output was verified.

Page 16: c Programming

C PROGRAMMING

ARRAYS

SUM AND AVERAGE OF NUMBERS IN ARRAYAIM

To write a program to initialize an array of 5 numbers and print sum and average

ALGORITHM

Step 1: Start the program

Step 2: Declare and initialize array of size 5.

Step 3: Set loop up to size of array

Step 4: Find sum of array

Step 5: After execution of loop, find average.

Step 6: Print sum and average of array

Step 7: Stop the program.

PSEUDOCODE

BEGIN

DEFINE N AS 5

INITIALIZE integer array to size 5, i=0, and sum = 0

REPEAT

Sum = sum+arr[i]

UNTIL i < size

CALCULATE avg = sum/N

PRINT sum, avg

END

Page 17: c Programming

PROGRAM

#include<stdio.h> #define N 5

int main() {

int arr[] = {10,2,5,1,25}; int i, temp, j;

printf("\n\t Before Sorting: "); for(i=0; i<N; i++)

printf("%d ",arr[i]);

for(i=0; i<N; i++){ for(j=i+1; j<N; j++){

if(arr[i]>arr[j]) {

temp = arr[i]; arr[i] = arr[j]; arr[j] =

temp; } // end if }// end inner for

loop }// end outer for

printf("\n\n** Sorting the array in Descending order...\n");

printf("\n\tAfter Sorting: "); for(i=0; i<N; i++)

printf("%d ", arr[i]);

printf("\n\n\t** Max Number: %d", arr[0]); printf("\n\t** Min Number: %d\n", arr[N-1]); printf("\n");

return 0; } OUTPUT

Before Sorting: 10 2 5 1 25

** Sorting the array in Descending

order...

After Sorting: 1 2 5 10 25

** Max Number: 1 ** Min Number: 25

RESULT Thus, program to initialize an array of 5 numbers and print sum and average is executed

and output is verified.

Page 18: c Programming

DYNAMIC INITIALIZATION OF ARRAY DURING RUNTIMEAIM

To write a program to initialize array dynamically during runtime to calculate sum and

average for a given number of values specified by user.

ALGORITHM

Step 1: Start the program

Step 2: Enter the number of array

Step 3: Enter the elements of array

Step 4: Set loop up to size of array

Step 5: Find sum of array

Step 6: After execution of loop, find average of array.

Step 7:Print sum and average of array

Step 8: Stop the program.

PSEUDOCODE

BEGIN

INITIALIZE integer array to size 100, val, num, i=0, and sum = 0

PRINT number of values to enter

READ number into num

REPEAT

PRINT enter value i+1

READ value into arr[i]

DETERMINE sum = sum+ arr[i]

UNTIL i < num

CALCULATE avg = sum/num

PRINT sum, avg

END

Page 19: c Programming

PROGRAM #include <stdio.h>

int main() { int arr[100];

int sum=0, val, num; float avg; int i;

printf("How many values do you wanna enter?" ); scanf("%d",&num);

for(i=0; i<num; i++) { printf("\n Enter Value: "); scanf("%d", &val); arr[i] = val;

}

for(i=0; i<4; i++){ printf("\n Value %d: %d: ", i+1, arr[i]); sum +=arr[i];

} avg=sum/4;

printf("\n\t Sum = %d", sum); printf("\n\t Average = %.2f \n", avg);

return 0; }

OUTPUT

How many values do you wanna enter?5

Enter Value: 45 Enter Value: 98

Enter Value: 85

Enter Value: 79

Enter Value: 65

Value 1: 45:

Value 2: 98:

Value 3: 85:

Value 4: 79: Sum = 307 Average = 76.00

RESULT Thus, program to initialize array dynamically and print sum and average is executed and

output is verified.

Page 20: c Programming
Page 21: c Programming

SORT ARRAY ASCENDING AND DESCENDINGAIM

To write a program to sort a given array of variables in ascending and descending

order.

ALGORITHM

Step 1: Start the program

Step 2: Initialize size of array as N

Step 3: Initialize elements of array to N numbers

Step 4: Set loop to array size minus 1

Step 5: Set inner loop to array size

Step 6: Check whether next array element si greater than current element

Step 7: If greater exchange position.

Step 8: If not greater, then go to loop

Step 9: After execution of inner loop, inner loop is executed.

Step 10: Print ascending order of given array.

Step 11: Print descending order of given array

Step 12: Stop the program.

PSEUDOCODE

BEGIN

INITIALIZE integer array to size N

REPEAT

REPEAT

IF arr[i] > arr[j] THEN

SET temp = arr[i]

STORE arr[i] = arr[j]

SET arr[j] = temp

INCREMENT j

UNTIL j < N

INCREMENT i

UNITIL i<N-1

PRINT array in ascending order

PRINT array in descending order

PRINT max Num AS arr[N-1]

PRINT minNum AS arr[0]

END

Page 22: c Programming

PROGRAM #include<stdio.h> #define N 5

int main() {

int arr[] = {10,2,5,1,25}; int i, temp, j; printf("\n\t Before Sorting: "); for(i=0; i<N; i++)

printf("%d ",arr[i]);

for(i=0; i<N; i++){ for(j=i+1; j<N; j++){

if(arr[i]>arr[j]) {

temp = arr[i]; arr[i] = arr[j]; arr[j] =

temp; } // end if }// end inner for

loop }// end outer for

printf("\n\n** Sorting the array in Ascending order...\n"); for(i=0; i<N; i++)

printf("%d ", arr[i]); printf("\n\n** Sorting the array in Descending order...\n"); for(i=N-1; i>=0; i--)

printf("%d ", arr[i]); printf("\n\n\t** Max Number: %d", arr[0]);

printf("\n\t** Min Number: %d\n", arr[N-1]); printf("\n");

return 0; }

OUTPUT Before Sorting: 10 2 5 1 25

** Sorting the array in Ascending order... 1 2 5 10 25 ** Sorting the array in Descending order... 25 10 5 2 1

** Max Number: 1 ** Min Number: 25

RESULT Thus, program to sort a given array of variable size in ascending and descending order is

executed and output is verified.

Page 23: c Programming

MULTIPLICATION TABLE USING 2D ARRAYAIM

To write a program to display multiplication table using 2 dimensional array

ALGORITHM

Step 1: Start the program

Step 2: Define ROWS, COLUMNS

Step 3: Declare a 2D array, Product

Step 4: Set loop to size of COLUMNS

Step 5: Set inner loop to size of ROWS

Step 6: Calculate and print product

Step 7: Stop the program.

PSEUDOCODE

BEGIN

DEFINE ROWS AS 5, COLUMNS AS 5

INITIALIZE array Product to ROWS and COLUMNS

SET i=0, j=1

REPEAT

PRINT COLUMNS label

UNTIL i<= COLUMNS

REPEAT

PRINT ROWS label

SET row = i+1

REPEAT

SET column=j

CALCULATE product[i][j] = rows*columns

PRINT product[i][j]

UNTIL j<= COLUMNS

UNTIL i<= ROWS

END

Page 24: c Programming

PROGRAM

#include <stdio.h> #define ROWS 5 #define COLUMNS 5

int main() { int row, column; int product[ROWS][COLUMNS]; // result of multiplication int i,j; printf("\n\n** Multiplication Table **\n\n"); printf(" "); for(j=1; j<= COLUMNS; j++)

printf(" %4d", j);printf("\n");

printf("________________________________\n"); for(i=0; i<+ROWS; i++) {

row = i+1; printf("%2d |", row); for(j=1; j <= COLUMNS; j++) {

column = j; product[i][j] = row * column;printf(" %4d", product[i][j]);

} // end inner for - COLUMNSprintf("\n");

} // end outer for - ROWS return 0; }

OUTPUT

** Multiplication Table **

1 2 3 4 5________________________________1 | 1 2 3 4 52 | 2 4 6 8 103 | 3 6 9 12 154 | 4 8 12 16 205 | 5 10 15 20 25

RESULT Thus, program to display multiplication table using 2D array is executed and output is

verified.

Page 25: c Programming

C PROGRAMMING

STRUCTURES AND UNIONS

AIM To write a program using structures and unions store student details and find average

and grade for a set of students.

ALGORITHM

Step 1: Start the program

Step 2: Initialize the structure and union variable.

Step 3: Set a loop up to the number of students.

Step 4: Enter name, register number and marks for 3 tests and store in the structure variable.

Step 5: Calculate average and grade and store in the union variable.

Step 6: print the student name, register number, marks for 3 tests, average and grade for all the

students.

Step 7: Stop the program.

PSEUDOCODE

BEGIN

INITIALIZE structure student as stud[50] and union result as res[50]

READ number of students AS no

REPEAT

READ name and reg no, test 1, test 2, test 3 marks of student

STORE in structure variable

CALCULATE res[i].avg = (stud[i].test1 +stud[i].test2+stud[i].test3)/3

DETERMINE grade

IF res[i].avg>50 AND res[i].avg<60

SET grade AS D

ELSE IF res[i].avg>=60 AND res[i].avg<70

SET grade AS C

ELSE IF res[i].avg>=70 AND res[i].avg<80

SET grade AS B

ELSE IF res[i].avg>=80 AND res[i].avg<=100 SET grade AS A

ELSE

SET grade AS F

INCREMENT i by 1

UNTIL i<no

PRINT details of students. END

Page 26: c Programming

PROGRAM

#include <stdio.h>

main(){ struct student{

char name[25]; char regno[25]; int test1, test2,

test3; }stud[50]; union result{

char grade; float

avg; }res[50];

int i, no; int test1, test2, test3;

printf("Enter number of students:"); scanf("%d", &no);

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

printf("-- Enter details of student %d--\n", i+1); printf("Enter name: "); scanf("%s", &stud[i].name); printf("Enter regno: "); scanf("%s", &stud[i].regno);

/* Enter 3 marks and find average */ printf("Enter 3 test marks: "); scanf(" %d %d %d", &stud[i].test1, &stud[i].test2, &stud[i].test3); res[i].avg = (float) (stud[i].test1+stud[i].test2+stud[i].test3)/3;

if(res[i].avg>=50 && res[i].avg<60) res[i].grade = 'D';

else if(res[i].avg>=60 && res[i].avg<70) res[i].grade = 'C';

else if(res[i].avg>=70 && res[i].avg<80) res[i].grade = 'B';

else if(res[i].avg>=80 && res[i].avg <=100) res[i].grade = 'A';

else res[i].grade = 'F';

} // end for printf("\n** Printing from structure and union **\n"); printf("\n Name Reg No. Test 1 Test 2 Test 3 Average Grade\n");

for(i=0; i<no; i++) { printf("\n%s \t %s\t\t %d \t %d \t %d", stud[i].name, stud[i].regno, stud[i].test1,

stud[i].test2, stud[i].test3);/* printing from union */printf("\t %.2f \t %c", res[i].avg, res[i].grade);

Page 27: c Programming

}printf("\n");

} // end main function

OUTPUT Enter number of students:3 -- Enter details of student 1--Enter name: Max Enter regno: CS01 Enter 3 test marks: 45 68 95 -- Enter details of student 2--Enter name: Dan Enter regno: CS02 Enter 3 test marks: 45 86 54 -- Enter details of student 3--Enter name: Maria Enter regno: CS03 Enter 3 test marks: 77 85 56

** Printing from structure and union ** Name Reg No. Test 1 Test 2 Test 3 Average Grade

Max CS01 45 68 95 69.33 CDan CS02 45 86 54 61.67 CMaria CS03 77 85 56 72.67 B

RESULT Thus, program to store student details using structures and unions is executed and

output is verified.

Page 28: c Programming

C PROGRAMMING FUNCTIONS

AIM To write a program to use function for a simple calculator.

ALGORITHM

Step 1: Start the program Step 2: Read the value of x. Step 3: Display menu Step 4: Use switch case statements to call functions corresponding to the menu item. Step 5: Perform operation in each function and display result Step 6: Stop the program.

PSEDOCODE BEGIN READ value of x REPEAT DISPLAY menu GET choice IF choice is 1

CALL square function DETERMINE square of x = x*x PRINT value of square.

IF choice is 2 CALL cube function DETERMINE cube of x = pow(x, 3) PRINT value of cube

IF choice is 3 CALL root function DETERMINE root of x = sqrt(x) PRINT value of root

IF choice is 4 EXIT

UNTIL choice is 4 END

Page 29: c Programming

FLOWCHART

Start

Read x

Display Menu

Read choice

If choice =1

If choice =2

If choice =3

If choice =4

Stop

square = x*x

Print square

cube = pow(x,3)

Print cube

root = sqrt(x)

Print root

Page 30: c Programming

PROGRAM

#include <stdio.h> #include <math.h> /* function Declarations */ void square(int); void cube(int); void root(int);

main(){

int choice; int x; printf("Enter value of x:"); scanf("%d", &x);

printf("\n ** MENU **"); printf("\n[1] Square"); printf("\n[2] Cube"); printf("\n[3] Square root"); printf("\n[4] Exit");

input: printf("\n-- Enter choice: "); scanf("%d", &choice);

switch(choice) { case 1:

square(x);/* function call */ break;

case 2: cube(x); break;

case 3: root(x); break;

case 4: exit(1);

default: printf("Invalid Choice"); goto input; break;

}// end switch

goto input;

} // end main

Page 31: c Programming

void square(int x){ printf("x square = %d",

x*x); } // end function square.

void cube(int x){ int cubeVal; cubeVal = pow(x, 3); printf("x cube = %d",

cubeVal); } // end function cube void root(int x){

float sqVal;

if(x<0) printf("enter positive value!");

else{

sqVal = sqrt(x); printf("Square root of x: %.2f",

sqVal); } // end else }// end function root.

OUTPUT

Enter value of x: 5

** MENU ** [1] Square [2] Cube [3] Square root [4] Exit -- Enter choice: 1 x square = 25 -- Enter choice: 2 x cube = 125 -- Enter choice: 3 Square root of x: 2.24 -- Enter choice: 4

RESULT

Thus, program to use C functions for simple calculator is executed and output is verified.