0% found this document useful (0 votes)
27 views5 pages

Main (N, I, Flag

The document contains the code for a C program that takes a positive integer as input and determines if it is a prime number or not. It uses a for loop to check if the input number is divisible by any number between 2 and half of the input number. If it is divisible, a flag is set to 1, otherwise it is printed that the number is prime.

Uploaded by

Sravan Chintu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views5 pages

Main (N, I, Flag

The document contains the code for a C program that takes a positive integer as input and determines if it is a prime number or not. It uses a for loop to check if the input number is divisible by any number between 2 and half of the input number. If it is divisible, a flag is set to 1, otherwise it is printed that the number is prime.

Uploaded by

Sravan Chintu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

#include <stdio.

h>
int main()
{
int n, i, fl ag = 0;
printf("Enter a positive integer: ");
scanf("%d",&n);
for(i=2; i<=n/2; ++i)
{
// condition for nonprime number
if(n%i==0)
{
fl ag=1;
break;
}
}
if (fl ag==0)
printf("%d is a prime number.",n);
else
printf("%d is not a prime number.",n);
return 0;
}

1.

#include <stdio.h>

2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.

int fibo(int);
int main()
{
int num;
int result;
printf("Enter the nth number in fibonacci series: ");
scanf("%d", &num);
if (num < 0)
{
printf("Fibonacci of negative number is not possible.\n");
}
else
{
result = fibo(num);
printf("The %d number in fibonacci series is %d\n", num, result);
}
return 0;
}
int fibo(int num)
{
if (num == 0)
{
return 0;
}
else if (num == 1)
{
return 1;
}
else
{
return(fibo(num - 1) + fibo(num - 2));
}
}

ARRAY SORTING PROGRAM

#include <stdio.h>
void merge(int [], int, int [], int, int []);
int main() {
int a[100], b[100], m, n, c, sorted[200];
printf("Input number of elements in first array\n");
scanf("%d", &m);
printf("Input %d integers\n", m);
for (c = 0; c < m; c++) {
scanf("%d", &a[c]);
}
printf("Input number of elements in second array\n");
scanf("%d", &n);
printf("Input %d integers\n", n);
for (c = 0; c < n; c++) {
scanf("%d", &b[c]);
}
merge(a, m, b, n, sorted);
printf("Sorted array:\n");
for (c = 0; c < m + n; c++) {
printf("%d\n", sorted[c]);
}
}

return 0;

void merge(int a[], int m, int b[], int n, int sorted[]) {


int i, j, k;
j = k = 0;
for (i = 0; i < m + n;) {
if (j < m && k < n) {
if (a[j] < b[k]) {
sorted[i] = a[j];
j++;
}
else {
sorted[i] = b[k];
k++;
}
i++;
}
else if (j == m) {
for (; i < m + n;) {
sorted[i] = b[k];
k++;
i++;
}
}
else {
for (; i < m + n;) {

}
}

sorted[i] = a[j];
j++;
i++;

REMOVE DUPLICATE VALUES


#include<stdio.h>
#include<conio.h>
void main()
{
int a[20], i, j, k, n;
clrscr();

printf("\nEnter array size : ");


scanf("%d",&n);

printf("\nEnter %d array element : ", n);


for(i = 0; i < n; i++)
{
scanf("%d",&a[i]);
}

printf("\nOriginal array is : ");


for(i=0;i< n;i++)
{
printf(" %d",a[i]);
}

printf("\nNew array is : ");


for(i=0; i < n; i++)
{
for(j=i+1; j < n; )
{
if(a[j] == a[i])
{
for(k=j; k < n;k++)
{
a[k] = a[k+1];
}
n--;
}
else {
j++;
}
}
}

for(i=0; i < n; i++)


{
printf("%d ", a[i]);
}
getch();
}

You might also like