0% found this document useful (0 votes)
49 views8 pages

Dslab 4

The document contains 4 programming experiments related to data structures: 1) A program to print the Fibonacci sequence up to a user-specified limit. 2) A program to swap two variables without using a third variable using call by value. 3) A function to print the prime factors of a positive integer entered by the user. 4) A program with a function to convert a decimal number to binary, octal, or hexadecimal according to the user's choice.

Uploaded by

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

Dslab 4

The document contains 4 programming experiments related to data structures: 1) A program to print the Fibonacci sequence up to a user-specified limit. 2) A program to swap two variables without using a third variable using call by value. 3) A function to print the prime factors of a positive integer entered by the user. 4) A program with a function to convert a decimal number to binary, octal, or hexadecimal according to the user's choice.

Uploaded by

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

Lab 4

Data structure

Ex 1
WAP for printing Fibonacci sequence. Take input from the user to print up to a certain limit.

#include <stdio.h>

int main()

int i, n;

int num1 = 0, num2 = 1;

int nextTerm = num1 + num2;

printf("Enter the number of terms: ");

scanf("%d", &n);

printf("Fibonacci Series: %d, %d ", num1, num2);

for (i = 3; i <= n; ++i) {

printf("%d, ", nextTerm);

num1= num2;

num2 = nextTerm;

nextTerm = num1 + num2;

return 0;

}
Ex 2
WAP to swap two variables without using a third variable, depict the same using call by
value concept.

#include<stdio.h>
int main()
{

int num1,num2;
printf("enter two no.s ::\n");
scanf("%d",&num1);
scanf("%d",&num2);
printf("\nbefore swap num1 =%d num2 =%d",num1,num2);
num1=num1+num2;
num2=num1-num2;
num1=num1-num2;
printf("\nAfter swap num1 =%d num2 =%d",num1,num2);
return 0;
}

Experiment3
A positive integer is entered through the keyboard. Write a Function to print the prime
factors of this number.

#include<stdio.h>
int prime(int x);
void main()
{
int num;
printf("Enter an integer: ");
scanf("%d", &num);
prime(num);
}
int prime(int x)
{
int a;
for(a=2;a<=x;a++)
{
if(x%a==0)
{
printf("%d ",a);
x = x/a;
a--;
}
}
}

Experiment 4
#include<stdio.h>
int convert(int, int);

int main()
{
int num, choice, base;
while(1)
{
printf("Select conversion: \n\n");
printf("1. Decimal to binary. \n");
printf("2. Decimal to octal. \n");
printf("3. Decimal to hexadecimal. \n");
printf("4. Exit. \n");

printf("\nEnter your choice: ");


scanf("%d", &choice);

switch(choice)
{
case 1:
base = 2;
break;
case 2:
base = 8;
break;
case 3:
base = 16;
break;
case 4:
printf("Exiting ...");
exit(1);
default:
printf("Invalid choice.\n\n");
continue;
}

printf("Enter a number: ");


scanf("%d", &num);

printf("Result = ");

convert(num, base);

printf("\n\n");
}

return 0;
}

int convert(int num, int base)


{
int rem;

if (num == 0)
{
return;
}

else
{
rem = num % base;
convert(num/base, base);
if(base == 16 && rem >= 10)
{
printf("%c", rem+55);
}

else
{
printf("%d", rem);
}
}
}

You might also like