Q. 1. Write a 'C' program to find a number is prime or not using function.
#include <stdio.h>
int prime(int);
void main()
{
int n;
printf("Enter a positive integer: ");
scanf("%d", &n);
prime(n);
}
int prime(n)
{
int i, flag = 0;
for (i = 2; i <= n / 2; ++i)
{
// condition for non-prime
if (n % i == 0) {
flag = 1;
break;
}
}
if (n == 1)
{
printf("1 is neither prime nor composite.");
}
else
{
if (flag == 0)
printf("%d is a prime number.", n);
else
printf("%d is not a prime number.", n);
}
return n;
}
Q.2. Write a 'C' program to calculate factorial using function.
#include <stdio.h>
int fact(int);
void main()
{
int no,factorial;
printf("Enter a number to calculate it's factorial\n");
scanf("%d",&no);
factorial=fact(no);
printf("Factorial of the num(%d) = %d\n",no,factorial);
}
int fact(int n)
{
int i,f=1;
for(i=1;i<=n;i++)
{
f=f*i;
}
return f;
}
#include <stdio.h>
#include <string.h>
char str2[75], str1[75]; ;
void main()
{
printf("Enter any string:\n");
gets(str1);
strcpy(str2, str1);
printf("original string s1='%s'\n", str1);
printf("copied string s2='%s'", str2);
}
0 Comments