Prog1 LabAct9
Prog1 LabAct9
Proceso
BSIT 1-2
COMSCI 1101 LAB B
Prog1 Week 6 Laboratory Activity No. 9
1. Write a program that will ask for a number from the user. Create a function test
that will determine if the number is positive or negative. (Apply 2 kinds of Functions)
PSEUDOCODE
START
INPUT userNum
IF userNum >= 0
PRINT Positive
PRINT Negative
END IF
STOP
CORRECT CODE
Function 1:
#include<stdio.h>
#include<conio.h>
void main()
{
int num;
clrscr();
printf("Enter a Number:");
scanf("%d",&num);
if(num>=0)
printf("%d is a Positive Number.\n",num);
else
printf("%d is a Negative Number.\n",num);
getch();
}
Function 2:
#include<stdio.h>
#include<conio.h>
int main()
{
int num;
clrscr();
printf("Enter a Number:");
scanf("%d",&num);
if(num>0)
{
printf("You entered a Positive Number.");
}
if(num<0)
{
printf("You entered a Negative Number.");
}
if(num==0)
{
printf("The Number you entered is Zero.");
}
getch();
return 0;
}
PROGRAM OUTPUTS
2. Create a function NumberDays that return the number of days in a month. Write a
program that will ask for the integer equivalent of a month from the user and display
the number of days for that month using NumberDays. (Apply 2 kinds of Functions)
PSEUDOCODE
START
INITIALIZE NumberDays m
SET a[12]=31,28,31,30,31,30,31,31,30,31,30,31
INPUT userNum
IF m>12 || m<1
PRINT INVALID
ELSE IF m==2
PRINT “EITHER 28 OR 29.”
ELSE
PRINT "NO. OF DAYS IN MONTH %d is %d.”
END IF ELSE
STOP
CORRECT CODE
Function 1:
#include<stdio.h>
#include<conio.h>
int a[12]={31,28,31,30,31,30,31,31,30,31,30,31};
int m;
clrscr();
printf("Enter an Integer:");
scanf("%d",&m);
NumberDays(m);
return 0;
}
void NumberDays(int m)
{
if (m>12 || m<1)
{
printf("Your input is invalid.");
}
else if(m==2)
{
printf("The Number of Days in Month 2 is either 28 or 29.");
}
else
printf("The Number of Days in Month %d is %d.",m,a[m-1]);
getch();
}
Function 2:
#include<stdio.h>
#include<conio.h>
int m;
int a[12]={31,28,31,30,31,30,31,31,30,31,30,31};
clrscr();
printf("Enter an Integer:");
scanf("%d",&m);
NumberDays(a,&m);
return 0;
}
void NumberDays(int *a, int *m)
{
if(*m>12 || *m<1)
{
printf("Your input is invalid.");
}
else if(*m==2)
{
printf("The Number of Days in Month 2 is either 28 or 29.");
}
else
printf("The Number of Days in Month %d is %d.",*m,*(a+(*m-
1)));
getch();
PROGRAM OUTPUTS
3. Write a program that will create a user-defined function that name PrimeNumber
that check whether the integer entered by the user is prime or not. (Apply 4 kinds
of Functions)
PSEUDOCODE
START
INITIALIZE Variables
INPUT userNum
STOP
CORRECT CODE
Function 1:
#include<stdio.h>
#include<conio.h>
if(flag==1)
printf("%d is not a Prime Number.",n);
else
printf("%d is a Prime Number.",n);
getch();
return 0;
}
int primenumber(int n)
{
int i;
for(i=2; i<=n/2; ++i)
{
if (n%i==0)
return 1;
}
return 0;
}
Function 2:
#include<stdio.h>
#include<conio.h>
void primenumber(int n);
int main()
{
int n;
clrscr();
printf("Enter a Positive Integer:");
scanf("%d",&n);
primenumber(n);
getch();
return 0;
}
void primenumber(int n)
{
int i, flag=0;
{
if(n%i==0)
{
flag=1;
break;
}
}
if(flag==1)
printf("%d is not a Prime Number.",n);
else
printf("%d is a Prime Number.",n);
Function 3:
#include<stdio.h>
#include<conio.h>
int primenumber();
int main()
{
int n, i, flag=0;
clrscr();
n=primenumber();
for
(i=1; i<=n/2; ++i)
{
if (n%i==0)
{
flag=1;
break;
}
}
if (flag==1)
printf("%d is not a Prime Number.",n);
else
printf("%d is a Prime Number.", n);
getch();
return 0;
}
int primenumber()
{
int n;
printf("Enter a Positive Integer:");
scanf("%d",&n);
return n;
Function 4:
#include<stdio.h>
#include<conio.h>
void primenumber(void)
{
int n, i, flag=0;
clrscr();
printf("Enter a Positive Integer:");
scanf("%d",&n);
}
}
if (n==1)
{
printf("1 is neither Prime nor Composite Number.");
}
else
{
if (flag==0)
printf("%d is a Prime Number.",n);
else
printf("%d is not a Prime Number.",n);
}
}
void main(void)
{
clrscr();
primenumber();
getch();
}
PROGRAM OUTPUTS