1. Write a program in C to check whether a number is a prime number or not using the function.
SOURCE CODE:
#include<stdio.h>
void isPrime(int);
int main(){
int n;
printf("Enter number : ");
scanf("%d",&n);
isPrime(n);
return 0;
}
void isPrime(int n){
int i, c=0;
for(i=1;i<=n/2;i++){
if(n%i==0)
c++;
}
if(c==1)
printf("Given number %d is a prime number.\n",n);
else
printf("Given number %d is not a prime number.\n",n);
}
OUTPUT-1:
Enter number : 5
Given number 5 is a prime number.
OUTPUT-2:
Enter number : 25
Given number 25 is not a prime number.
2. Write a program in C to get the largest element of an array using the function.
SOURCE CODE:
#include <stdio.h>
int large(int[], int);
int main() {
int a[10], i, n;
printf("Enter number of elements to be insert : ");
scanf("%d", &n);
printf("Enter %d elements : ",n);
for (i = 0; i < n; i++)
scanf("%d", &a[i]);
printf("The largest element of the array = %d\n", large(a, n));
return 0;
}
int large(int ary[], int n){
int i, max;
max=ary[0];
for(i=1;i<n;i++){
if(ary[i]>max)
max=ary[i];
}
return max;
}
OUTPUT:
Enter number of elements to be insert : 4
Enter 4 elements : 8 3 4 2
The largest element of the array = 8
SOURCE CODE:
#include<stdio.h>
void isPrime(int);
int main(){
int n;
printf("Enter number : ");
scanf("%d",&n);
isPrime(n);
return 0;
}
void isPrime(int n){
int i, c=0;
for(i=1;i<=n/2;i++){
if(n%i==0)
c++;
}
if(c==1)
printf("Given number %d is a prime number.\n",n);
else
printf("Given number %d is not a prime number.\n",n);
}
OUTPUT-1:
Enter number : 5
Given number 5 is a prime number.
OUTPUT-2:
Enter number : 25
Given number 25 is not a prime number.
SOURCE CODE:
#include <stdio.h>
int large(int[], int);
int main() {
int a[10], i, n;
printf("Enter number of elements to be insert : ");
scanf("%d", &n);
printf("Enter %d elements : ",n);
for (i = 0; i < n; i++)
scanf("%d", &a[i]);
printf("The largest element of the array = %d\n", large(a, n));
return 0;
}
int large(int ary[], int n){
int i, max;
max=ary[0];
for(i=1;i<n;i++){
if(ary[i]>max)
max=ary[i];
}
return max;
}
OUTPUT:
Enter number of elements to be insert : 4
Enter 4 elements : 8 3 4 2
The largest element of the array = 8