Department of Electrical Engineering
Department of Electrical Engineering
Department of Electrical Engineering
Value-Returning Functions
Function Call
Pointer Data Type and Pointer Variables 794.
Declaring Pointer Variables.
Address of Operator (&).
Dereferencing Operator (*).
Operation on Pointers.
and itself. For example, 2, 3, 5, 7, and 11 are prime numbers. Write a program
that reads an integer and determines if it is a prime number. Suppose it is not a
prime number, print out the smallest divisor. Otherwise, print out the prime
number. Create a function to return the smallest divisor for an integer. Use this
function to determine and print all the prime numbers between 1 and 1000.
(Hint: use the modulus operator % to determine if a number is divisible by
another number. The smallest divisor of even number is 2. However, you only
need to test up to √n to verify if it is divisible)
Code
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
void temp(int*);
int main()
{
int a;
printf("Please Enter The No.: ");
scanf("%d",&a);
temp (&a);
printf("Prime Numbers From 1 to 1000 Are:\n ");
print();
return 0;
}
void temp(int *x)
{
int counter=0;
for(int i=2; i<*x ; i++)
{
if(*x%i==0)
{
printf("Number Is Not Prime\n");
printf("Smallest Divisor Is : %d \n",i);
counter=1;
break;
}
}
if (counter==0)
{
printf("Number Is Prime.\n");
}
}
void print()
{
int i, j, counter2;
for(i=2; i<=1000; i++)
{
counter2 = 1;
for(j=2; j<=i/2; j++)
{
if(i%j==0)
{
counter2 = 0;
break;
}
}
if(counter2==1)
{
printf("%d,",i);
}
}
}
Output
Lab Task 2: Write a program that reads the values for the width, length, and
height of a rectangular box using three double type variables in the main()
function. Pass values of these variables to a function, which calculates the
volume and surface area for six sides of the box. The box's volume and surface
area passed back from two other arguments are printed out in the function
main(). Check your program with the user input for the width, length, and height
of 2,3, 4 meters, respectively.
Code
#include<stdio.h>
#include<conio.h>
void fun(int w, int l, int h, int *v, int *s)
{
*v = w * l * h;
*s = 2 * (w*l + h*l + h*w);
}
int main()
{
int w,l,h;
int v,s;
printf("Enter the width: ");
scanf("%d",&w);
printf("Enter the length: ");
scanf("%d",&l);
printf("Enter the height: ");
scanf("%d",&h);
fun(w,l,h,&v,&s);
printf("Volume of the Rectangular Box = %d",v);
printf("\nSurface Area of the Rectangular Box = %d",s);
return 0;
}
Output
Lab Task 3: Implement the following equation in main without using any