SOLUTION FOR PROGRAMMING-LAB 5 ASSIGNMENT

1.

#include <stdio.h>

#include<math.h>

float zval(float x);

main()

{

float x;

printf(“Give the value of x:\n”);

scanf(“%f”,x);

z = zval(x);

printf(“The value of z is: %f\n”, z);

}

float zval(float x)

{

float a= 2, b=1, c=1, z;

z = sqrt(a*x*x + b*x + c);

return(z);

}

2.

#include <stdio.h>

long int fibonacci(int count);

main()

{

int count, n;

printf("How many Fibonacci Numbers?\n");

scanf("%d", &n);

for (count = 1; count <=n; ++count)

printf("\n%ld", fibonacci(count));

}

long int fibonacci(int count)

{

long int f ;

if (count < 3) f = 1;

else f = fibonacci(count -1) + fibonacci(count -2);

return (f);

}

long int fibonacci(int count)

{

long int f1 = 1, f2 = 1;

long int f;

f = (count < 3) ? 1 : f1 + f2;

f2 = f1;

f1 = f;

return (f);

}

3.

#include <stdio.h>

main()

{

int num[] = {24,34,12,44,56,17};

int i, *j;

j = &num[0]; /* assign address of zeroeth elements */

for( i = 0; i <=5; i++)

{

printf("\n Address = %u", j);

printf("Element = %d", *j);

j++; /* increament pointer to point to next location */

}

}

4. Selection Sort

void selectionsort(int numbers[], int array_size)

{

int i, j;

int min, temp;

for (i = 0; i < array_size-1; i++)

{

min = i;

for (j = i+1; j < array_size; j++)

{

if (numbers[j] < numbers[min])

min = j;

}

temp = numbers[i];

numbers[i] = numbers[min];

numbers[min] = temp;

}

}

5.

#include <stdio.h>

main ()

{

struct student

{

char name[25];

char rollno[10];

char department[10];

char course[10];

int year;

};

struct student s1[450];

int i;

void studentnames(int Year)

{

for (i=0; i<450; i++)

{

if (s1[i].year == Year)

printf(“Name: %s\n”, s1[i].name);

}

}

void studentdata(char givenrollno[10])

{

for (i=0; i<450; i++)

{

if (strcmp(s1[i].rollno, “givenrollno”) == 0)

{

printf(“Name: %s\n”, s1[i].name);

printf(“Roll No. : %s\n”, s1[i].rollno);

printf(“Department: %s\n”, s1[i].department);

printf(“Course: %s\n”, s1[i].course);

printf(“Year: %d\n”, s1[i].year);

}

}

}

}

6.

#include<stdio.h>

main()

{

char name[20]; char phone[10];

struct address

{

char houseno[10];

char street[10];

char city[10];

};

struct address customeraddress;

FILE *fptr;

fptr = fopen("customer", "w");

printf("Give the Name of the Customer:\n");

scanf("%[^\n]", name);

getchar();

fprintf(fptr, "%s\n", name);

printf("Give the Phone Number of the Customer:\n");

scanf("%[^\n]", phone);

getchar();

fprintf(fptr, "%s\n", phone);

printf("Give the House Number of the Customer:\n");

scanf("%[^\n]", customeraddress.houseno);

getchar();

fprintf(fptr, "%s\n", customeraddress.houseno);

printf("Give the Street Name of the Customer:\n");

scanf("%[^\n]", customeraddress.street);

getchar();

fprintf(fptr, "%s\n", customeraddress.street);

printf("Give the City Name of the customer:\n");

scanf("%[^\n]", customeraddress.city);

getchar();

fprintf(fptr, "%s\n", customeraddress.city);

fclose(fptr);

}

7.

#include<stdio.h>

main()

{

char name[20], phone[10];

struct address

{

char houseno[10];

char street[10];

char city[10];

};

struct address customeraddress;

FILE *fptr;

fptr = fopen("customer", "r");

fscanf(fptr, "%[^\n]s", name);

getc(fptr);

printf("Name of the Customer:%s\n", name);

fscanf(fptr, "%[^\n]s", phone);

getc(fptr);

printf("Phone Number of the Customer:%s\n", phone);

fscanf(fptr, "%[^\n]s", customeraddress.houseno);

getc(fptr);

printf("House Number of the Customer:%s\n", customeraddress.houseno);

fscanf(fptr, "%[^\n]s", customeraddress.street);

getc(fptr);

printf("Street Name of the Customer:%s\n", customeraddress.street);

fscanf(fptr, "%[^\n]s", customeraddress.city);

getc(fptr);

printf("City Name of the customer:%s\n", customeraddress.city);

fclose(fptr);

}