Sie sind auf Seite 1von 29

C PROGRAMMING EXERCISES

C Programming using Simple statements and expressions

EX NO:4a Area Of Circle


DATE :
AIM:
To write a C program to calculate area of a circle.
ALGORITHM:

Step-1 Start the program.


Step-2 Input the radius of the circle.
Step-3 Calculate the area of the circle using the formula
Area=3.14*r*r
Step-4 Print the area of the circle
Step-5 Stop

PROGRAM:

#include<stdio.h>
main()
{
floatr,area,;
printf(“\nEnter the radius of the circle”);
scanf(“%f”,&r);
area=3.14*r*r;
printf(“\nArea=%f”,area);
}

OUTPUT:

Enter the radius of the circle


4
Area = 50.24

EX.NO :4b Integer To Float Conversion


Date:
AIM:
To write a c program to convert a value of one data type into another data type.

ALGORITHM:
Step 1: Declare the necessary variables a and b as integer and float. Step 2: Read the input of the
integer a.
Step 3: Assign the value of the integer to a float variable and a double variable.
Step 3: Display the value of the float variable and the double variable.
PROGRAM:
#include<stdio.h>
main()
{
float b; int a;
printf("\nEnteran Integer\n"); scanf("%d",&a);
b=(float)a;
printf("\nThe Converted float value is %f",b);
}

OUTPUT:
Enter an Integer 45
The Converted float value is 45.00000

EX.NO :4c Temperature Conversion


DATE:
AIM:
To write a C program for temperature conversion from Celsius to Fahrenheit and vice versa.
ALGORITHM:

Step-1 Start the program.

Step-2 Input the celsius and fahrenheit values

Step-3 Calculate the temperature from Celsius to Fahrenheit using the formula

F= (1.8*c)+32

Step-4 Calculate the temperature from Fahrenheit to Celsius using the formula

C=5/9(F-32)

Step-5 Print the calculated values

Step-6 Stop

PROGRAM:

#include<stdio.h>
main()
{
floatcel, fah ,c ,f;
clrscr();
printf(“\nEnter the fahrenheit value:”);
scanf(“%f”,&f);
cel=(5.0/9.0)*(f-32);
printf(“Celsius=%d”,cel);
printf(“\nEnter the Celsius value:”);
scanf(“%f”,&c);
fah=(9.0/5.0)*c+32;
printf(“Fahrenheit=%d”,fah);
getch();
}

OUTPUT:

Enter the fahrenheit value:8


Celsius = -13.33
Enter the celsius value:10
Fahrenheit = 50

EX: NO: 4d AVERAGE OF FIVE MARKS


DATE:
AIM:
To write a c program to calculate the Average of given five Numbers.
ALGORITHM:
Step 1: Declare five integer variables to get the marks.
Step 2: Read the input of five marks and store them into integer variables.

Step 3: Calculate the sum of five numbers.

Step 4: Divide the sum by 5.

Step 5: Display the Average


PROGRAM:
#include<stdio.h>
main()
{
int m1,m2,m3,m4,m5,tot;
floatavg;
printf("Enter 5 Marks\n"); scanf("%d%d%d%d%d",&m1,&m2,&m3,&m4,&m5);
tot=m1+m2+m3+m4+m5;
avg=tot/5;
printf("\nThe Average is %f\n",avg);
}

OUTPUT:
Enter 5 Marks

98+87+92+78+75

The Average is 86%

RESULT:
Thus the C programming using simple statements and expression are executed and output was
verified
EX.NO :4e Expression Evaluation
DATE:
AIM:
To write a C program to evaluate the given expression
ALGORITHM:

Step-1 Start the program.

Step-2 Input the values for declared variables

Step-3 Substitute the values in expression and calculate the results.

Step-4 Print the results

Step-5 Stop

PROGRAM:

#include<stdio.h>
main()
{
inta,b,c;
floatx,y,z;
printf ("Enter the values for a,b,c \n");
scanf("%d,%d,%d", &a,&b,&c);
x = (a * b) – c;
y = (b/c) * a;
z = (a - b) / (c + d)
printf(" The value of x is “,x ”the value of y is “,y ”The value of z is “,z );
}

OUTPUT:

Enter the value for a,b,c.


a= 5; b = 6; c= 12;
x = 28.000;
y = 2.5000
z = 0.0555

RESULT:
Thus the C programming using simple statements and expression are executed and output was
verified
Scientific problem using decision making and looping

EX 5a Find the Given year is Lear Year or Not (using if else)


DATE:
AIM:
To write a C program to find a given year is leap year or not
ALGORITHM:

Step-1 Start the program.

Step-2 Enter the year value

Step-3 Divide the entered year by 4.If there is no remainder it is a leap year else not a leap year.

Step-4 Print the result

Step-5 Stop

PROGRAM:

#include<stdio.h>
main()
{
int year;
printf ("Enter the year \n");
scanf("%d", &year);
if (year%4==0)
printf("It is a Leap Year \n");
else
printf("It is Not a Leap Year\n");
}

OUTPUT:
Enter the year 2004

It is a Leap Year

Enter the year 1998

It is not a Leap Year

EX.NO 5b Largest of three numbers (using if elseif else)


DATE:
AIM:
To write a C program for finding largest of three given numbers.
ALGORITHM:

Step-1 Start the program.


Step-2 Input the three integer values A,B,C.

Step-3 Check

1. If the no A is greater than the other two no’s B and C

If so , Print A is largest

2. Elseif the second no B is greater than the third no C

If so, Print B is Largest

3. Else Print C is largest

Step-4 Print the result

Step-5 Stop

PROGRAM:

/*To Find the Largest of 3 Nos*/

#include<stdio.h>
#include<conio.h>
main()
{
int a,b,c;
clrscr();
printf(“Biggest of three No’s”);
printf(“Enter the values of A,B,C”);
scanf(“%d%d%d”,&a,&b,&c);
if((a>b)&&(b>c))
printf(“\n a=%d is greatest”,a);
elseif(b>c)
{
printf(“\n b=%d is greatest”,b);
}
else
{
printf(“\n c=%d is greatest”,c);
}
getch();
}

OUTPUT:

Enter the values of A,B,C:10 20 5

B = 20 is greatest
EX NO: 5 c Sum Of ‘N’ Natural Numbers using while loop
DATE:
AIM:

To write a c program to find the sum of “N‟ natural numbers for given range.
ALGORITHM:
Step 1: Initialize the sum as 0
Step 2: Read the range as input
Step 3: Initialize a counter with 1
Step 4: Overwrite the sum by adding counter value & sum
Step 5: Increment the counter value by 1
Step 6: Repeat the steps 4 & 5 until the counter is less than or equal to range Step 7: Print the sum

PROGRAM:
#include<stdio.h>
main()
{
int i,n,sum=0;
printf("Enter the range\n"); scanf("%d",&n);
i=1;
while(i<=n)
{
sum=sum+i;
i++;
}
printf("\nThe sum of first %d numbers is %d\n",n,sum);
}

OUTPUT:
Enter the range 16
The sum of first 16 numbers is 136

EX NO: 5d Sum of Digits using Do, While


DATE:

AIM:
To write a c program to find the sum of digits for a given number.
ALGORITHM:
Step 1: Declare a integer variable and initialize the sum as 0
Step 2: Read the input
Step 3: Take the remainder of given number while dividing 10
Step 4: Overwrite the sum by adding above remainder with available sum
Step 5: Overwrite the number by divide with 10
Step 6: Repeat the steps 3 to 5 until the number is greater than 0
Step 7: Print the sum

PROGRAM:
#include<stdio.h>
main()
{
int n,i,sum=0;
printf("Enter a Number\n"); scanf("%d",&n);
do
{ i=n%10; sum=sum+i; n=n/10;
}while(n>0);
printf("The Sum of digit is %d\n",sum);
}

OUTPUT:
Enter a Number 5891
The Sum of digit is 23
EX NO: 5e Arithmetic Calculator using switch case
DATE:
AIM:
To write a menu driven c program to implement an Arithmetic Calculator.

ALGORITHM:
Step 1: Get the two numbers
Step 2: Enter the choice
Step 3: Pass the choice into switch case
Step 4: In case 1, add the two numbers and print the result
Step 5: In case 2, subtract the two numbers and print the result
Step 6: In case 3, multiply the two numbers and print the result
Step 7: In case 4, divide the two numbers and print the result
PROGRAM:
#include<stdio.h>
main()
{
int a,b,ch,c;
printf("\nEnter the Number 1:\n"); scanf("%d",&a);
printf("\nEnter the Number 2:\n"); scanf("%d",&b);
printf("\n1.Add\n2.Subtract\n3.Multiply\n4.Divide\n"); printf("\nEnter the
Choice:\n");
scanf("%d",&ch);
switch(ch)
{
case 1: c=a+b;
printf("\n %d + %d = %d\n",a,b,c); break;
case 2: c=a-b;
printf("\n %d - %d = %d\n",a,b,c); break;
case 3: c=a*b;
printf("\n %d * %d = %d\n",a,b,c); break;
case 4: c=a/b;
printf("\n %d / %d = %d\n",a,b,c); break;
}
}

OUTPUT:
Enter the Number 1: 15
Enter the Number 2: 56
1.Add
2.Subtract
3.Multiply
4.Divide
Enter the Choice: 2
15 - 56 = -41

EX: NO: 5f Number Checking


DATE:
AIM:
To write a menu driven c program to check whether the given number is Palindrome, Armstrong
and Prime.

ALGORITHM:
Step 1: Get a number from the user
Step 2: Enter the choice
Step 3: Pass the choice into switch case
Step 4: In case 1,
a. Copy the given number into a variable
b. Initialize a counter to 1 and sum to 0
c. Extract the remainder of given number while dividing 10
d. Multiply the sum by 10
e. Overwrite the sum by adding above remainder with available sum
f. Overwrite the number by divide with 10
g. Repeat the steps a to f until the number is greater than 0
h. Compare the sum and copy of the number
i. If they are equal print as “Palindrome” else print “Not Palindrome”
Step 5: In case 2,
a. Copy the given number into a variable
b. Initialize a counter to 1 and sum to 0
c. Extract the remainder of given number while dividing 10
d. Calculate the value of remainder by assigning power 3
e. Overwrite the sum by adding above result with available sum
f. Overwrite the number by divide with 10
g. Repeat the steps a to e until the number is greater than 0
h. Compare the sum and copy of the number
i. If they are equal print as “Armstrong” else print “Not Armstrong”
Step 6: In case 3,
a. Initialize a flag value with 5
b. Initialize a counter to 2
c. Extract the remainder of given number by dividing with counter value
d. If the remainder is 0 changes the flag value to 0 and go to step g else go to next step.
e. Increment the counter value by 1
f. Repeat the steps a to e until counter is less than or equal to square root of the given
number
g. Check the flag value
h. If flag value is 0 then print as “Prime Number” else print as
“Not Prime”

PROGRAM:
#include<stdio.h>
#include<math.h>
main()
{
int a,i,sum=0,n,ch,m; printf("\nEnter a Number\n");
scanf("%d",&a);
printf("\n1.Palindrome\n2.Armstrong\n3.Prime\n"); printf("\nEnter the
Choice:\n");
scanf("%d",&ch);
switch(ch)
{
case 1: n=a; while(a>0) {
i=a%10;
sum=(sum*10)+i;
a=a/10;
}
if(n==sum)
printf("Given Number is Palindrome\n"); else
printf("Given Number is Not Palindrome\n"); break;
case 2: n=a; do
{ i=a%10; sum=sum+(i*i*i);
a=a/10;
}while(a>0);
if(n==sum)
printf("Given Number is Armstrong\n"); else
printf("Given Number is Not Armstrong\n"); break;
case 3: m=5; n=sqrt(a);
for(i=2;i<=n;i++)
{
if(a%i==0)
{
m=0;
break;
}
}
if(m==0)
printf("Given Number is Prime\n"); else
printf("Given Number is Not Prime\n"); break;
}
}

OUTPUT:

Enter a Number 121 1.Palindrome


2.Armstrong
3.Prime
Enter the Choice: 1
Given Number is Palindrome

RESULT:

Thus the program carried out using decision making and Looping was executed and output was verified.
SIMPLE PROGRAMMING FOR ONE DIMENSIONAL AND TWO
DIMENSIONAL ARRAYS

EX.NO :6a Generating Pascal triangle using Array

DATE

AIM:
To write a C program to generate pascal triangle using array.
ALGORITHM:

Step-1 Start the program

Step-2 Enter the no of lines from the user and accordingly create an array.

Step-3 Declare two variables one for outer rows and the other for inner rows.

Step-4 Check that if (j==0||i==j), display a[i][j]=1 or else display the consecutive integer

Step-5 Display the triangle.

Step-6 Stop.

PROGRAM:
#include<Stdio.h>
#include<conio.h>
void main()
{

int a[20][20], i ,j ,n, s=25, k;


printf("\n enter the number of lines:");
scanf("%d", &n);
for(i=0;i<n; i++) //outer loop for rows
for(k=s-2*i; k>=0;k--)
printf(" ");
for(j=0;j<=i;j++) //inner loop for columns
{
if(j==0||i==j)

a[i][j]=1;
else
a[i][j]=a[i-1][j-1]+a[i-1][j];
printf("%4d",a[i][j]);
printf("\n");
}
getch();
}
OUTPUT:
Enter the number of lines:6
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1

EX NO:6b Sum of array elements


DATE:
AIM:
To write a c program to find the sum of given array elements.

ALGORITHM:
Step 1: Declare an array with necessary size
Step 2: Get the value for total number of elements
Step 3: Initialize an index value to 0
Step 4: Read the input
Step 5: Increment the index value by 1
Step 6: Repeat steps 4 & 5 until counter less than total no. of elements
Step 7: Initialize an index value to 0 and sum to 0
Step 8: Obtain the sum by adding current index array value with available Sum
Step 9: Increment the index value by 1
Step 10: Repeat steps 8 & 9 until index value less than total no. of elements Step 11: Print the sum

PROGRAM:
#include<stdio.h>
main()
{
int i,n,a[10],sum=0;
printf("Enter total no. of Elements\n"); scanf("%d",&n);
printf("Enter Array elements one by one\n"); for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
sum=sum+a[i];
printf("The Sum of Array Elements is %d\n",sum);}
OUTPUT:
Enter total no. of Elements 8
Enter Array elements one by one 15 69 32 10 45 66 32 11
The Sum of Array Elements is 280

EX.NO 6c Multiplication of two matrices (Using For Loop)


DATE:

AIM:
To write a program to multiply two matrixes.

ALGORITHM:

Step-1 Start the program

Step-2 Enter the number of rows and columns of matrix A

Step-3 Enter the number of rows and columns of matrix B

Step-4 Enter the values of the A and B matrices

Step-5 Display the entered values of A and B in matrix format

Step-6 Set a loop up to row

Step-7 Set a inner loop up to column

Step-8 Set another inner loop up to column

Step-9 Multiply the A and B matrix and store the element in the C matrix

Step-10 Display the resultant matrix.

Step-11 Stop

PROGRAM:

#include<stdio.h>
main()
{
int a[15][15],b[15][15],c[15][15],i,j,k,r,s;
int m,n;
printf(“\nEnter the Rows and Columns of A matrix...”);
scanf(“%d %d”,&m,&n);
printf(“\nEnter the Rows and Columns of B matrix...”);
scanf(“%d %d”,&r,&s);
if(m!=r)
printf(“\nMatrix multiplication cannot be performed”);
else
{
printf(“\nEnter the elements of A matrix”);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
scanf(“\t%d”,&a[i][j]);
}
printf(“\nEnter the elements of B matrix”);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
scanf(“\t%d”,&b[i][j]);
}
printf(“\nThe elements of A matrix”);
for(i=0;i<m;i++)
{
printf(“\n”);
for(j=0;j<n;j++)
printf(“\t%d”,a[i][j]);
}
printf(“\n The elements of B matrix”);
for(i=0;i<m;i++)
{
printf(“\n”);
for(j=0;j<n;j++)
printf(“\t%d”,b[i][j]);
}
for(i=0;i<m;i++)
{
printf(“\n”);
for(j=0;j<n;j++)
{
c[i][j]=0;
for(k=0;k<m;k++)
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
}
}
printf(“The multiplication of two matrixes”);
for(i=0;i<m;i++)
{
printf(“\n”);
for(j=0;j<n;j++)
printf(“\t%d”,c[i][j]);
}
}
INPUT AND OUTPUT

Enter the Rows and Columns of A matrix... 3 3


Enter the Rows and Columns of B matrix... 3 3
Enter the elements of A matrix 2 4 6 8 9 10 11 12 13
Enter the elements of B matrix 1 3 5 7 9 11 13 15 17
The elements of A matrix
2 4 6
8 9 10
11 12 13
The elements of B matrix
1 3 5
7 9 11
13 15 17
The multiplication of two matrixes
108 132 156
201 255 309
264 336 408

RESULT:
Thus the simple programming for arrays was executed and output was verified.
SIMPLE PROGRAMMING USING STRING FUNCTION
EX NO: 7a STRING COMPARISION

DATE:

AIM:
To write a program to compares two strings using strcmp function

ALGORITHM:

Step-1 Start the program

Step-2 Enter the first string and second string

Step-3 get a two string and strore a,b

Step-4 compare first string and second string using strcmp function

Step-5 If the string is equal print the statement two strings are equal otherwise notequal

Step-6 Stop

PROGRAM:
#include <stdio.h>
#include <string.h>
int main()
{
char a[100], b[100];
printf("Enter the first string\n");
gets(a);
printf("Enter the second string\n");
gets(b);
if( strcmp(a,b) == 0 )
printf("Entered strings are equal.\n");
else
printf("Entered strings are not equal.\n");
return 0;
}

OUTPUT:
Enter the first string
SVCET
Enter the second string
SAI NIKETAN
Entered the strings are not equal
EX NO: 7b String Concatenation

DATE:

AIM:
To write a c program to find the length of given two strings and concatenate them

ALGORITHM:
Step 1: Create two character arrays with necessary size
Step 2: Read the Strings
Step 3: Calculate the string lengths using strlen function
Step 4: Print the string lengths
Step 5: Join the two strings using strcat function
Step 6: Print the concatenated string

PROGRAM:
#include<stdio.h>
#include<string.h>
main()
{
char s[20],s1[20]; printf("Enter a String1\n");
scanf("%s",s); printf("Enter a String2\n");
scanf("%s",s1); strcat(s,s1);
printf("The Concatenated String is %s\n",s);
}

OUTPUT:
Enter a String1 hai
Enter a String2 hello
The Concatenated String is haihello

RESULT:
Thus the C program using string function was executed and output was verified
EX NO: 8a Find the smallest element of array using User defined Functions
DATE:
AIM:
To write a c program to find the smallest element of given array of elements using functions.

ALGORITHM:
Step 1: Create a function small()
Step 2: Inside the function
a. Store the 0th index value into base
b. Initialize a index to 1
c. Compare the array index value with base
d. If the array index value is smaller than base store the array index value into base
e. Increment the index by 1
f. Repeat the steps c & e until index reaches total no. of elements
g. Return the base value
Step 3: Inside the main function
a. Create an integer array with necessary size
b. Get the total number of elements
c. Read the array elements one by one
d. Call the small() function by passing array and no. of elements as arguments

PROGRAM

#include<stdio.h> int small(int a[],int n)


{
int s,i; s=a[0]; for(i=0;i<n;i++)
{
if(a[i]<s)
s=a[i];
}
return s;
}
main()
{
int i,a[10],n,s;
printf("Enter total no. of elements\n"); scanf("%d",&n);
printf("Enter Array Elements one by one\n"); for(i=0;i<n;i++)
scanf("%d",&a[i]); printf("Array Elemets:\n");
for(i=0;i<n;i++) printf("%d\t",a[i]); printf("\n");
s=small(a,n);
printf("The Smallest element of given array is %d",s);
}

OUTPUT:
Enter total no. of elements 5
Enter Array Elements one by one 1 98 2 66 0
Array Elements: 1 98 2 66 0
The Smallest element of given array is 0

EX.NO 8b Swapping Two Numbers (Using Call By Value)


DATE:
AIM:
To write a C program to swap two numbers without using third variable using call by value.
ALGORITHM:
Step -1 Start the program
Step -2 Enter the two numbers
Step -3 Call the swap function
Step -4 Add the second number with the first number
Step -5 Subtract the second number from the first number and assign the value to the
Second number
Step -6 Subtract the second number from the first number and assign the value to the
First number
Step -7 Stop
PROGRAM:
/* Swapping 2 Nos without 3rd variable using call by value*/
#include<stdio.h>
#include<conio.h>
main()
{
int n1,n2;
void swap(int ,int);
clrscr();
printf("\n Enter the two numbers");
scanf("%d%d",&n1,&n2);
printf("\n Before swapping the values of n1 = %d and n2 = %d",n1,n2);
swap(n1,n2);
getch();
}
void swap(int a,int b)
{
a=a+b;
b=a-b;
a=a-b;
printf("\n After swapping the values of n1 = %d and n2 = %d",a,b);
}

OUTPUT:

Enter the two numbers


5
10
Before swapping the values of n1 =5 and n2 =10
After swapping the values of n1 =10 and n2 = 5

EX.NO 8c Swapping Two Numbers Using Call by Reference


DATE:
AIM:
To write a C program to swap two numbers using call by reference.
ALGORITHM:
Step -1 Start the program
Step -2 Enter the two numbers
Step -3 Call the swap function using the address of the numbers as arguments
Step -4 Assign the address of the first number to a temporary variable
Step -5 Assign the address of the second number to the first number
Step -6 Assign the value of the temporary variable to the first number
Step -7 Stop
PROGRAM :
/* Swapping 2 Nos using call by reference */
#include<stdio.h>
#include<conio.h>
main()
{
int n1,n2;
void swap(int *,int *);
clrscr();
printf("\n Enter the two numbers");
scanf("%d%d",&n1,&n2);
printf("\n Before swapping the value of n1= %d and n2= %d",n1,n2);
swap(&n1,&n2);
printf("\n After swapping the value of n1= %d and n2= %d",n1,n2);
getch();
}
void swap(int *a,int *b)
{
int t;
t = *a;

*a = *b;
*b = t;
}

OUTPUT:

Enter the two numbers


5
10
Before swapping the value of n1 = 5 and n2 = 10
After swapping the value of n1 = 10 and n2 =

RESULT:
Thus the C program with user defined function that includes parameter passing was executed and output
was verified
EX NO:9a Factorial Computation using recursive Function
DATE:
AIM:
To write a program to find the factorial of the given number using recursion

ALGORITHM:

Step-1 Start the program

Step-2 Enter the number

Step-3 Call the recursive function passing the number to the recursive function as an argument.

Step-4 If the entered number is equal to one then return one to main function.

Step-5 If the number is less greater then one then call recursive

Step-6 Print the factorial value of the number.

Step-7 Stop

PROGRAM:

/* Factorial using recursion*/


#include<stdio.h>
main()
{
int num,a;
printf(“Enter the number”);
scanf(“%d”,&num);
a=recur(num);
printf(“The factorial of the number %d is %d”,num,a);
}
recur(int no)
{
int fact=1;
if(no==1)
return(1);
else
fact=no*recur(no-1);
}

OUTPUT:

Enter the number 5

The factorial of the number 5 is 120


EX NO:9b Fibonacci series using Recursive function

DATE:

AIM:
To write a C Program to generate a Fibonacci series using recursive function.
ALGORITHM:
Step1: Start
Step2: Declare the variables int i, n and function longfibonacci(int).

Step3: Get the values of N.


Step4: Set a for loop to calculate the Fibonacci series values up to N calling the user
defined function FIBONACCI(I).
Step5: Inside fibonacci()
Step5.1: If N=0 then return 0, else If N=1 or 2 then return 1,else
Step5.2: Add fibonacci (n-1) value with return value of recursive
function fibonacci(n-2).
Step5.3: Return the value of longfibonacci().
Step5.4: Print fibonacci by receiving the value of longfibonacci().
Step6: Stop.

PROGRAM:

#include<stdio.h>
#include<conio.h>
long fibonacci(int);
void main()
{
int i, n;
clrscr();
printf("\nPRG TO FIND FIBONACCI SERIES USING
RECURSION\n"); printf("\n-----------------------------------------------n");
printf("\nEnter the number of terms required in the series: ");
scanf("%d",&n);
printf("\n\nThe Fibonacci Series up to %d terms are ",n);
for(i=0;i<n;i++)
{
printf("%ld ",fibonacci(i));
}
getch();
}
long fibonacci(int n)
{
if (n==0)
return 0;

else if(n==1 || n==2)


return 1;
else
return (fibonacci(n-1)+fibonacci(n-2));
}

OUTPUT:

PROGRAM TO FIND FIBONACCI SERIES USING RECURSION

----------------------------------------

Enter the number of terms required in the series: 5

The Fibonacci series upto 5 terms are 0 1 1 2 3

RESULT:

Thus the C program to compute factorial and Fibonacci series using recursive function
was executed and output was verified.
C PROGRAMMING USING STRUCTURES AND UNION
Ex.No 10a EMPLOYEE PAYROLL USING STRUCTURE
DATE:
AIM:
To write a c program to generate employee payroll using structures.
ALGORITHM:

Step-1 Start the program

Step-2 Create a Structure named Employee containing records Id,name, Basic Salary, Net Salary, HRA, DA
and Tax.

Step-3 Get the employee Id and retrieve his details.

Step-4 Using the basic salary, DA, HRA, Tax calculate tax and net salary

Step-5 Display the details each employee record containing name, number, basic salary, HRA, DA,
Net Salary and Tax

Step-6 Stop.

PROGRAM:

/* Calculate Employee salary using Structures*/


#include<stdio.h>
#include<conio.h>
struct employee
{
char name[15];
int empid;
float bsal;
float nsal;
float gross;
};
void main()
{
struct employee emp;
float hra,da,tax;
clrscr();
printf("\nEmployee Details");
printf("\nEnter the employee name");
scanf("%s",emp.name);
printf("\nEnter the employee id");
scanf("%d",&emp.empid);
printf("\nEnter the basic salary");
scanf("%f",&emp.bsal);
hra=((10*emp.bsal)/100);
da=((35*emp.bsal)/100);
tax=((15*emp.bsal)/100);
emp.gross=emp.bsal+hra+da;
emp.net=emp.gross-tax;
printf("\nEmployee name:%s",emp.name);
printf("\nEmployee no:%d",emp.empid);
printf("\nEmployee Basic salary:%f",emp.bsal);
printf("\nHRA:%f",hra);
printf("\nDA:%f",da);
printf("\nTax:%f",tax);
printf("\nNetSalary%f",emp.nsal);
printf("\nGross salary:%f",emp.gross);
getch();
}
OUTPUT:
Employee Details:
Enter the employee name : Robin
Enter the employee Id : 100
Enter the basic salary : 30000
Employee name : Robin
Employee Id : 100
Employee Basic salary : 30000.000000
HRA : 3000.000000
DA : 10,500.000000
Tax : 4500.000000
Gross salary : 39000.000000
EX NO: 10b PROGRAM FOR SIZE OF UNION
DATE:

AIM:
To write a c program to store the book information using union.

ALGORITHM:
Step 1: Create an union book
Step 2: Declare one integer and a character array inside the union for book name and book price
Step 3: Get the book name and price
Step 4: Print the book name and price
Step 5: Get the Book name alone
Step 6: Print the Book name
Step 7: Get the Book Price
Step 8: Print the Book price

PROGRAM:
#include<stdio.h> union book
{
int price;
char bname[20]; };
main()
{
union book b;
printf("Enter the Book Details:\n"); printf("Enter the
Book Name:\n"); scanf("%s",&b.bname);
printf("Enter the Book Price:\n");
scanf("%d",&b.price); printf("BOOK
DETAILS:\n"); printf("%s\t%d\n",b.bname,b.price);
printf("Enter the Book Name:\n");
scanf("%s",b.bname);
printf("Book Name=%s\n",b.bname);
}
OUTPUT
Enter the Book Details:
Enter the Book Name: English
Enter the Book Price: 150
BOOK DETAILS: 150
Enter the Book Name: English
Book Name=English

RESULT:
Thus the C program using structures and unions was executed and output was verified.

Das könnte Ihnen auch gefallen