0% found this document useful (0 votes)
11 views

Lab9 SNB

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Lab9 SNB

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

PROBLEM SOLVING AND COMPUTER PROGRAMMING

LAB 9

NAME: SANJEEV NAIK BANOTHU

ROLL NO: 221220052

1. Do the following using pointers


a. add two numbers
b. swap two numbers using a user defined function

SOURCE CODE

#include<stdio.h>

void swap(int *,int *);


int main(){
int a, b;
printf("ENTER THE TWO NUMBERS :");
scanf("%d %d", &a, &b);
printf("VALUE OF NUMBERS BEFORE SWAP:");
printf("a=%d b=%d\n", a, b);
swap(&a, &b);
printf("VALUE OF NUMBERS AFTER SWAP:");
printf("a=%d b=%d", a, b);
return 0;
}
void swap(int *x, int *y){
*x=*x+*y;
*y=*x-*y;
*x=*x-*y;
}

OUTPUT
2. Compute sum of the elements stored in an array using pointers and user defined function.

SOURCE CODE

#include<stdio.h>

int array_addition(int *, int);


int main(){
int n;
printf("enter length of array:");
scanf("%d",&n);
int a[n];
printf("enter the elements of the array:");
for(int i=0;i<n;i++){
scanf("%d",&a[i]);
}
int sum=array_addition(a, n);
printf("sum of %d numbers is %d", n, sum);
return 0;
}
int array_addition(int *p, int x){
int sum=0;
for(int i=0;i<x;i++){
sum=sum+*(p+i);
}
return sum;
}

OUTPUT
3. Write a program to create a structure named company which has name, address, phone and
no. of Employee as member variables. Read name of company, its address, phone and no. of
Employee. Finally display these members’ value.

SOURCE CODE

#include<stdio.h>

#include<string.h>
struct company{
char name[100];
char address[4][100];
char phone_number[10];
int no_of_employee;
};
int main(){
struct company c1;
gets(c1.name);
//enter address of the company
for(int i=0;i<4;i++){
gets(c1.address[i]);
}
gets(c1.phone_number);
scanf("%d",&c1.no_of_employee);
//displaying the details of the company
printf("name of the company :");
puts(c1.name);
printf("ADDRESS OF THE COMPANY \n");
printf("location:");
puts(c1.address[0]);
printf("city:");
puts(c1.address[1]);
printf("state :");
puts(c1.address[2]);
printf("pincode :");
puts(c1.address[3]);
printf("phone number of the comany :");
puts(c1.phone_number);
printf("no of employee %d ",c1.no_of_employee);
return 0;
}
OUTPUT

You might also like