Vous êtes sur la page 1sur 4

Laboratory Exercise #6:

1. Write a program that will input two positive integers and displays all prime numbers
between the numbers inputted. Call a function to check whether a number is prime
or not. Note: 2 nd number must be greater than the first number; otherwise, it will not
be accepted.
Sample Run:
Input begin range:
10
Input end range:
5
Input end range:
30
Prime numbers between 10 to 30 are: 11 13 17 19 23 29
2. Write a program that will input a decimal number and would call a particular function
to output the conversion of the number to binary or to octal based on the users
choice. Note: Continue to input and convert until the user inputs N(No).
Sample Run:
Enter a number:
Enter your choice [B Binary / O Octal]:
The Binary Conversion is:
Input another? [Y/N]:

Enter a number:
Enter your choice [B Binary / O Octal]:
The Octal Conversion is:
Input another? [Y/N]:

Laboratory Exercise #6:


1. Write a program that will input two positive integers and displays all prime numbers
between the numbers inputted. Call a function to check whether a number is prime
or not. Note: 2 nd number must be greater than the first number; otherwise, it will not
be accepted.
Sample Run:
Input begin range:
10
Input end range:
5
Input end range:
30
Prime numbers between 10 to 30 are: 11 13 17 19 23 29
2. Write a program that will input a decimal number and would call a particular function
to output the conversion of the number to binary or to octal based on the users
choice. Note: Continue to input and convert until the user inputs N(No).
Sample Run:
Enter a number:
12
Enter your choice [B Binary / O Octal]:
The Binary Conversion is: 1100
Input another? [Y/N]:

Enter a number:
16
Enter your choice [B Binary / O Octal]:
The Octal Conversion is: 20

Input another? [Y/N]:

Programming Solutions:
Problem #1
#include<stdio.h>
#include<conio.h>
int check_prime(int num);
main(){
int n1,n2,i,prime;
printf("Input begin range: ");
scanf("%d", &n1);
do{
printf("Input end range: ");
scanf("%d", &n2);
}while(n2<n1);
printf("Prime numbers between %d and %d are: ", n1, n2);
for(i=n1;i<n2;++i)
{
prime=check_prime(i);
if(prime==1)
printf("%d ",i);
}
getch();
}

int check_prime(int num)


{ int j,prime=1;
for(j=2;j<=num-1;++j){
if(num%j==0){
prime=0;
break;
}
}
return prime;
}

Problem #2
#include <stdio.h>
#include <conio.h>
int decimal_octal(int n);
int decimal_binary(int n);
main()
{
int n;
char choice, ans = 'Y';
do {
printf("\nEnter a number: ");
scanf("%d", &n);
printf("Enter your choice [B - Binary / O - Octal]: ");
scanf("\n%c",&choice);
if (choice=='O')
printf("The Octal Conversion is: %d\n", decimal_octal(n));
else if (choice=='B')
printf("The Binary Conversion is: %d\n", decimal_binary(n));
else
printf("Invalid Input!");
printf("\nInput another? [Y/N]: ");
scanf("\n%c", &ans);
}while(ans=='Y');
getch();
}
int decimal_octal(int n) /* Function to convert decimal to octal */
{
int rem, i=1, octal=0;
while (n!=0)
{
rem=n%8;
n/=8;
octal+=rem*i;
i*=10;
}
return octal;
}
int decimal_binary(int n) /* Function to convert decimal to binary.*/
{
int rem, i=1, binary=0;
while (n!=0)
{
rem=n%2;
n/=2;
binary+=rem*i;
i*=10;
}
return binary;
}

Vous aimerez peut-être aussi