0% found this document useful (0 votes)
75 views

C 20 Programs 2023 List

The document provides the algorithms and programs for 10 different C programming lab exercises: 1. Sum and reverse of a number 2. Solution of a quadratic equation 3. Nth Fibonacci number 4. Factorial of a number 5. Creating a pyramid with '*' 6. Matrix transpose 7. Creating a pattern with a number N 8. Short form of a string (e.g. "Computer Science" to "CS") 9. Currency denomination of a given amount 10. Finding Armstrong numbers within a given range For each program, it provides the aim, algorithm, program code, and result.

Uploaded by

D11XH
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)
75 views

C 20 Programs 2023 List

The document provides the algorithms and programs for 10 different C programming lab exercises: 1. Sum and reverse of a number 2. Solution of a quadratic equation 3. Nth Fibonacci number 4. Factorial of a number 5. Creating a pyramid with '*' 6. Matrix transpose 7. Creating a pattern with a number N 8. Short form of a string (e.g. "Computer Science" to "CS") 9. Currency denomination of a given amount 10. Finding Armstrong numbers within a given range For each program, it provides the aim, algorithm, program code, and result.

Uploaded by

D11XH
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/ 48

C PROGRAMMING LAB 2023

PROGRAM 1:

SUM AND REVERSE OF A NUMBER

AIM:

Find the sum of digits and reverse of a number.

ALGORITHM:

STEP 1: Start.

STEP2: Declare n, r, sum, rev as integer.

STEP3: Initialize sum=0 and rev=0

STEP4: Read value for n.

STEP5: Repeat step 6 to STEP 10 until n<=0

STEP6: rn%10

STEP7: sum=sum+r

STEP8: rev=(rev*10) + r

STEP9: n=n/10

STEP10: Goto STEP 5

STEP11: Print the values of sum and rev.

STEP12: STOP.

PROGRAM:

#include<stdio.h>
#include<conio.h>
void main()
{
int n,r,sum=0,rev=0;
clrscr();
printf(" \nEnter the number: ");
scanf("%d", &n);
while(n>0)
{
r=n%10;
sum=sum+r;
rev=(rev*10)+r;
n=n/10;
}
printf("\n Sum of the digits of given number is: %d",sum);
printf("\n Reverse of the given number is: %d",rev);
getch();
}

RESULT:

The program has been completed and verified successfully


PROGRAM 2:

SOLUTION OF QUADRATIC EQUATION

AIM:

Find the solution of a quadratic equation

ALGORITHM:

STEP 1: Start.

STEP2 :Read a, b, c values


STEP 3:Compute d = b2 4ac
STEP 4:if d > 0 then
r1 = b+ sqrt (d)/(2*a)
r2 = b sqrt(d)/(2*a)
STEP 5:Otherwise if d = 0 then
STEP 6:compute r1 = -b/2a, r2=-b/2a
STEP 7:print r1,r2 values
STEP 8:Otherwise if d < 0 then
STEP 9: print roots are imaginary
STEP 10:Stop

PROGRAM

# include<stdio.h>

# include<conio.h>

# include<math.h>

Void main ( )

float a,b,c,r1,r2,d;

printf (“enter the values of a b c”);


scanf (“ %f %f %f”, &a, &b, &c);

d= b*b – 4*a*c;

if (d>0)

r1 = -b+sqrt (d) / (2*a);

r2 = -b-sqrt (d) / (2*a);

printf (“The real roots = %f %f”, r1, r2);

else if (d= =0){

r1 = -b/(2*a);

r2 = -b/(2*a);

printf (“roots are equal =%f %f”, r1, r2);

else

printf(“Roots are imaginary”);

getch ();

RESULT:

The program has been completed and verified successfully


PROGRAM 3:

N TH FIBONACCI NUMBERS

AIM:
Write a C program to print the N th fibonacci numbers.
ALGORITHM:
STEP1: Start
STEP2: Declare fib function
STEP3: Declare n and result
STEP4: Read value for n.
STEP5: Print the nth number of fibinocci series
STEP6: Check if (n == 0)

STEP7: Return 0
STEP8:Check if(n==1)
STEP9: Return 1
STEP10:Otherwise return (fib (n - 1) + fib (n - 2))
STEP 11:Stop

PROGRAM

#include<stdio.h>
#include<conio.h>
int fib (int);
void main ()
{
int n, result;
clrscr();
printf ("Enter the Nth Number: ");
scanf ("%d", &n);
result = fib (n);
printf ("The %dth number in Fibonacci series is %d\n", n, result);
getch();
}
int fib (int n)
{
if (n == 0)
{
return 0;
}
else if (n == 1)
{
return 1;
}
else
{
return (fib (n - 1) + fib (n - 2));
}
}
RESULT:

The program has been completed and verified successfully


PROGRAM 4:

FACTORIAL OF A NUMBER

AIM:

Find the factorial of a number.

ALGORITHM: MAIN

STEP1: Start
STEP2: Declare num as integer and result as long.
STEP3: Read the value for num.
STEP4: resultfact(num)
STEP5: Print the num and result.
STEP6: STOP.
ALGORITHM – FUNCTION FACT
STEP1: Start
STEP2: if num==0 or num==1 , then return 1 else GOTO STEP3
STEP3: return num* fact(num-1)
STEP4: Stop.
PROGRAM:

#include<stdio.h>
#include<conio.h>
long fact(int );
int main()
{
int num;
long result;
clrscr();
printf("\n\nFACTORIAL OF A NUMBER");
printf("\n......................................");
printf("\n\nEntehr the number: ");
scanf("%d",&num);
result=fact(num);
printf("\n Factorial of %d is : %li",num,result);
getch();
return 0;
}
long fact(int num)
{
if(num==0||num==1)
{
return 1;
}
else
{
return(num*fact(num-1));
}
}

RESULT:

The program has been completed and verified successfully


PROGRAM 5:

PYRAMID

AIM:

Create a pyramid using ‘*’.

ALGORITHM:

STEP1: Start
STEP2: Declare i, j, n and temp as integer variable.
STEP3: receive value for n.
STEP4: temp=n
STEP5: Initialize variables i and j with 1
STEP6: Repeat STEP 7 to STEP 16 until i>n
STEP7: Repeat STEP 8 to STEP 10 until j>temp
STEP9: Leave a space.
STEP10: Goto STEP7
STEP11: temp=temp-1
STEP12: repeat STEP 13 to STEP 14 until j>2*i-1
STEP13: print *
STEP14: Goto STEP 12.
STEP 15: Print a line break
STEP 16: Go to STEP 6
STEP17: Stop.

PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,n,temp;
clrscr();
printf("\n Pyramid:");
printf("\n \nEnter the number of rows in pyramid : ");
scanf("%d",&n);
temp=n;
for(i=1;i<=n;i++)
{
for(j=1;j<temp;j++)
{
printf(" ");
}
temp--;
for(j=1;j<=2*i-1;j++)
{
printf("*");
}
printf("\n");
}
getch();
}

RESULT:

The program has been completed and verified successfully


PROGRAM 6:

MATRIX TRANSPOSE

AIM:

Perform matrix transpose.

ALGORITHM:

STEP1: Start
STEP2: Declare i, j, col, row as integer variable and a[10][10] as integer array.
STEP3: Read value for row and col variables.
STEP4: i0, j0
STEP5: Repeat STEP6 to STEP9 until i>=row.
STEP6: Repeat STEP7 to STEP8 until j>=col.
STEP7:Receive value and store in a[i][j].
STEP8: GOTO STEP6.
STEP9:GOTO STEP5.
STEP10: Repeat STEP11 to STEP14 until i>=row.
STEP11: Repeat STEP12 to STEP13 until j>=col.
STEP12: Print value of array like a[j][i].
STEP13: GOTO STEP11.
STEP14:GOTO STEP10.
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10][10],i,j,row,col;
clrscr();
printf("\n TRANSPOSE OF A MATRIX");
printf("\n .......................");
printf("\n Enter the row count: ");
scanf("%d",&row);
printf("\n Enter the column count: ");
scanf("%d",&col);
printf("\n Enter the elements of matrix: ");

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

printf("\n The given matrix is:\n ");


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

printf("\n Transpose of given matrix is:\n ");


for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
printf("\t%d",a[j][i]);
}
printf("\n");
}
getch();
}

RESULT:

The program has been completed and verified successfully


PROGRAM 7:

PATTERN

AIM:

Create a pattern with the number N.

ALGORITHM:

STEP1: Start
STEP2: Declare i=0,n,j,a[10],s as integer
STEP3: Read the number n.
STEP4: While n>0 repeat step 5 to 7
STEP5: a[i]=n%10
STEP6: n=n/10
STEP7: i=i+1
STEP8: s=i-1
STEP9: j=0,repeat step 10 to 13 until j<=s.
STEP10: i=s-j repeat step 11 to 12 until i>0
STEP11: print a[i]
STEP12: ii-1
STEP13: jj+1
STEP14: Stop

PROGRAM:

#include<stdio.h>
#include<conio.h>

void main()

int i=0,n,j,a[10],s;

clrscr();

printf("{Enter the number");

scanf("%d",&n);

while(n>0)

a[i]=n%10;

n=n/10;

i++;

s=i-1;

for(j=0;j<=s;j++)

for(i=s-j;i>=0;i--)

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

printf("\n");

getch();
}

RESULT:

The program has been completed and verified successfully

PROGRAM 8:

SHORT FORM OF A STRING

AIM:

Display the short form of a string Eg: Computer Science: CS

ALGORITHM:

STEP1: Start
STEP2: Declare str[100],*ptr,I,l as character
STEP3: Enter a string
STEP4: Get string
STEP 5:Assign l=strlen(str)
STEP 6:ptr=str
STEP 7:ptrptr+0
STEP 8:i=1 repeat step 9 and 10 until i<l
STEP 9: if *(ptr=i-1)==’ ‘
STEP 10: get *(ptr+i) & i i+1
STEP 11: stop

PROGRAM:
#include<stdio.h>

#include<string.h>

#include<conio.h>

void main()

char str[100],*ptr,i,l;

clrscr();

printf("Enter any string\n");

gets(str);

l=strlen(str);

ptr=str;

printf("%c",*(ptr+0));

for(i=1;i<l;i++)

if(*(ptr+i-1)==' ')

printf(" %c ",*(ptr+i));

getch();

RESULT:

The program has been completed and verified successfully


PROGRAM 9:

CURRENCY DENOMINATION

AIM: Find the currency denomination of a given amount.

ALGORITHM:

STEP1: Start
STEP2: Declare amount as an integer variable.
STEP3: Receive value for variable amount.
STEP4: thousandamount/1000
STEP5: amountamount%1000
STEP6: Repeat STEP4 To STEP5 with variables fivehundred, hundred, fifty,
twenty, ten, five, two
and one.
STEP7: Print the values of all variables along with its description.
STEP8: Stop.

PROGRAM:

#include<stdio.h>
#include<conio.h>
void main()
{
int amount, thousand, fivehundred, hundred,fifty,twenty,ten,five,two,one;
clrscr();
printf(" Enter the amount : ");
scanf("%d",&amount);
thousand=amount/1000;
amount=amount%1000;
fivehundred=amount/500;
amount=amount%500;
hundred=amount/100;
amount=amount%100;
fifty=amount/50;
amount=amount%50;
twenty=amount/20;
amount=amount%20;
ten=amount/10;
amount=amount%10;
five=amount/5;
amount=amount%5;
two=amount/2;
amount=amount%2;
one=amount/1;
amount=amount%1;
printf("\n%d: thousand rupees",thousand);
printf("\n%d: five hundred rupees",fivehundred);
printf("\n%d: Hunfred rupees",hundred);
printf("\n%d: Fifty rupees",fifty);
printf("\n%d: Twenty rupees",twenty);
printf("\n%d: Ten rupees",ten);
printf("\n%d: Five rupees",five);
printf("\n%d: Two rupees",two);
printf("\n%d: One rupees",one);
getch();
}

RESULT:

The program has been completed and verified successfully


PROGRAM 10:

AMSTRONG NUMBERS

AIM:

Find the amstrong numbers within a given range.

ALGORITHM:

STEP1: Start.
STEP2: Declare i, n, lim1, lim2, sum and r as integer variables.
STEP3: receive starting and ending limit value and stored it in lim1 and lim2
respectively.
STEP4: repeat STEP 5 to STEP13 until i>lim2
STEP5: n=i
STEP6: sum 0
STEP7: repeat STEP 8 to STEP 12 until n<=0
STEP8: rn%10
STEP9: sum=sum+(r*r*r)
STEP10: n=n/10
STEP11: Goto STEP7.
STEP12: if i=sum , then print the value of i.
STEP13: Goto STEP4
STEP14: stop
PROGRAM:

#include<stdio.h>
#include<conio.h>
void main()
{
int i,n,lim1,lim2,sum,r;
clrscr();
printf("\n Enter the staring limit: ");
scanf("%d",&lim1);
printf("\n Enter the Ending limit: ");
scanf("%d",&lim2);
printf("\n\n The Amstrong number between %d and %d are",lim1,lim2);
for(i=lim1;i<=lim2;i++)
{
n=i;
sum=0;
while(n>0)
{
r=n%10;
sum=sum+(r*r*r);
n=n/10;
}
if(i==sum)
{
printf("\n %d",i);
}
}
getch();
}

RESULT:

The program has been completed and verified successfully


PROGRAM 11:

MATRIX MULTIPLICATION

AIM:

Matrix multiplication

ALGORITHM:

STEP1: Start

STEP 2: Enter the row and column of the first (a) matrix.

Step 3: Enter the row and column of the second (b) matrix.

Step 4: Enter the elements of the first (a) matrix.

Step 5: Enter the elements of the second (b) matrix.

Step 6: Print the elements of the first (a) matrix in matrix form.

Step 7: Print the elements of the second (b) matrix in matrix form.

Step 8: Set a loop up to row.

Step 9: Set an inner loop up to the column.

Step 10: Set another inner loop up to the column.


Step 11: Multiply the first (a) and second (b) matrix and store the element in the

third matrix (c)

Step 12: Print the final matrix.

Step 13: Stop 

PROGRAM:

#include <stdio.h>

void main()

int a[25][25],b[25][25],c[25][25],i,j,k,r,s;

int m,n;

printf("Enter the first matrix\n");

scanf("%d%d",&m,&n);

printf("Enter the second matrix\n");

scanf("%d%d",&r,&s);

if(m!=r)

printf("\n The matrix cannot multiplied");

else

printf("\n Enter the elements of first matrix ");

for(i= 0;i<m;i++)
{

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

scanf("\t%d",&a[i][j]);

printf("\n Enetr the elements of second matrix ");

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

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

scanf("\t%d",&b[i][j]);

printf("\n The element of first matrix is");

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

printf("\n");

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

printf("\t%d",a[i][j]);

printf("\n The element of second matrix is");

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

printf("\n");

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

printf("\t%d",b[i][j]);
}

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

printf("\n");

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

c[i][j]=0;

for(k=0;k<m;k++)

c[i][j]=c[i][j]+a[i][k]*b[k][j];

printf("\n Multiplication of two matrix is");

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

printf("\n");

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

printf("\t%d",c[i][j]);

getch();

RESULT:

The program has been completed and verified successfully


PROGRAM 12:

ODD AND EVEN NUMBERS IN SEPERATE FILE

AIM:

Write odd and even numbers into separate files.

ALGORITHM:

STEP1: Start
STEP2: Declare file pointers *all,*even,*odd with data structure file
STEP3: Declare number,i,records as integer
STEP4: open the file
STEP5: i=1 repeat step6 to step 9 until i<=records
STEP6: read the number
STEP7: If number==-1
STEP8: break
STEP9: print the number
STEP10: close the file all
STEP11: close file even and odd
STEP12: while(number=getw(odd))!=EOF
STEP13: print number
STEP14: Stop

PROGRAM:

#include<stdio.h>

#include<math.h>

void main()
{

FILE *all,*even,*odd;

int number,i,records;

clrscr();

printf("\n Enter the limit of the number :");

scanf("%d",&records);

printf(“/n Enter the numbers:”);

all=fopen("any number","w");

for(i=1;i<=records;i++)

scanf("%d",&number);

if(number==-1)

break;

putw(number,all);

fclose(all);

all=fopen("any number","r");

even=fopen("Even number","w");

odd=fopen("Odd number","w");

while((number=getw(all))!=EOF)

if(number%2==0)

putw(number,even);
else

putw(number,odd);

fclose(all);

fclose(even);

fclose(odd);

even=fopen("Even number","r");

odd=fopen("Odd number","r");

printf("The Even Numbers are:");

while((number=getw(even))!=EOF)

printf("%4d",number);

printf("\n The Odd numbers are:");

while((number=getw(odd))!=EOF)

printf("%4d",number);

fclose(even);

fclose(odd);

getch();

RESULT:

The program has been completed and verified successfully


PROGRAM 13:

BASE CONVERSION

AIM:

Base conversion of numbers.

ALGORITHM:

STEP 1:Start

STEP 2:Declare a[10],n,i

STEP 3:Call System()

STEP 4:Enter the number to convert

STEP 5:Read n

STEP 6:Check the condition for i goto step 7 and 8 until i>0

STEP 7: a[i]=n%2
STEP 8: n=n/2
STEP 9:Print Binary of Given Number

STEP 10: if i=i-1check i>=0 decrement i

STEP 11:Print a[i]

STEP 12:Stop

PROGRAM:

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
int a[10],n,i;
system ("cls");
clrscr();
printf("Enter the number to convert: ");
scanf("%d",&n);
for(i=0;n>0;i++)
{
a[i]=n%2;
n=n/2;
}
printf("\nBinary of Given Number is=");
for(i=i-1;i>=0;i--)
{
printf("%d",a[i]);
}
getch();
}

RESULT:

The program has been completed and verified successfully


PROGRAM 14:

ARRAY APPEND

AIM: Merge two numeric arrays in sorted order.

ALGORITHM:

STEP1: Start
STEP2: Declare a[50],b[50],c[100],m,n,I,j,k=0 as integer
STEP3: read the size of array A as m
STEP4: i=0 repeat step5 until i<m
STEP5: get a[i]
STEP6: read the size of array b as n .ii+1
STEP7: Enter elements
STEP8: i=0 repeat step 9 until i<n
STEP 9: get b[i]
STEP 10: i i++
STEP 11: i=0
STEP 12: j=0
STEP 13: while(i<m&&j<n)
STEP 14: if a[i]<b[j] do step 15 to step16
STEP 15: Assign c[k]=a[i]
STEP 16: i i+1
STEP 17: otherwise assign c[k]=b[j]
STEP 8: j j+1
STEP 19: if j>=m do step 20 to step 23
STEP 20: while (i<m)
STEP 21: c[k]=a[i]
STEP 22: i i+1
STEP 23: k k+1
STEP 24: i=0 repeat step 25 until i<m+n
STEP 25: get c[i]
STEP 26: stop

PROGRAM:
#include <stdio.h>
void main()
{
int a[50], b[50], c[100], m, n, i, j, k=0;
printf("\nEnter size of array A: ");
scanf("%d", &m);
printf("\nEnter sorted elements of array A:\n");
for(i=0; i<m; i++)
{
scanf("%d", &a[i]);
}
printf("\nEnter size of array B: ");
scanf("%d", &n);
printf("\nEnter sorted elements of array B:\n");
for(i=0; i<n; i++)
{
scanf("%d", &b[i]);
}
i=0;
j=0;
while(i<m && j<n)
{
if(a[i] < b[j])
{
c[k] = a[i];
i++;
}
else
{
c[k] = b[j];
j++;
}
k++;
}

if(i >= m)
{
while(j<n)
{
c[k] = b[j];
j++;
k++;
}
}

if(j >= n)
{
while(i<m)
{
c[k] = a[i];
i++;
k++;
}
}

printf("\nAfter merging:\n");
for(i=0; i<m+n; i++)
{
printf("\n%d", c[i]);
}
getch();
}
RESULT:

The program has been completed and verified successfully

PROGRAM 15:
LENGTH OF A STRING

AIM:

Find the length of a string using pointer

ALGORITHM:

. STEP 1: Start

STEP 2:Accept input(string) from user store it in some variable say str.

STEP 3:After that use while loop(any loop) to iterate string through last

element:

STEP 4:while(*ptr!='\0')

STEP 5: Inside the loop increment counter by 1(to calculate length of

string).

STEP 6: After that print value of count(length of string) on the output

screen.

STEP 7:Stop

PROGRAM:

#include<stdio.h>

void main()

char str[100];//declare string size 100


char *ptr=str;/*declare pointer which points to string(str)*/

int count=0;

clrscr();

printf("Enter the string:\n");

gets(str);

while(*ptr!='\0')

count++;

ptr++;

printf("Length of string= %d",count);

getch();

RESULT:

The program has been completed and verified successfully


PROGRAM 16:

TRIANGLE

AIM:

Fill upper triangle with 1,lower triangle with -1 and diagonal element with 0

ALGORITHM:

STEP1: Start
STEP2: Declare i,j,c,r as integer variable and a[10][10] as integer array
STEP3: Receive values for c and r
STEP4: i0 and j0
STEP5: repeat step 6 to 11 until i>=r
STEP6: repeat step 7 to 10 until j>=c
STEP7: if i==j, then a[i][j]0
STEP8: if i<j, then a[i][j]1
STEP9: if i>j, then a[i][j]-1
STEP10: goto step 6
STEP11: goto step 7
STEP12: print the matrix
STEP13: Stop

PROGRAM:

#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,a[10][10],r,c;
clrscr();
printf("\nEnter number of rows and columns");
scanf("%d%d",&r,&c);
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
if(i==j)
{
a[i][j]=0;
}
if(i<j)
{
a[i][j]=1;
}
if(i>j)
{
a[i][j]=-1;
}
}
}
printf("\nThe matrix is \n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("\t%d",a[i][j]);
}
printf("\n");}
getch();
}

RESULT:

The program has been completed and verified successfully

PROGRAM 17:

VOWELS IN A STRING

AIM:

Number of vowels in a String

ALGORITHM:

STEP1: Start
STEP2: Declare c,count with zero
STEP3: Declare a character array
STEP4: Input the Sting
STEP5: Read the string
STEP6: Check the String with vowels
STEP7: Increment the count value
STEP8: Increment the C value
STEP9: Stop
PROGRAM:

#include <stdio.h>
int main()
{
int c = 0, count = 0;
char s[1000];
printf("Input a string\n");
gets(s);

while (s[c] != '\0') {


if (s[c] == 'a' || s[c] == 'A' || s[c] == 'e' || s[c] == 'E' || s[c] == 'i' || s[c] == 'I' || s[c]
=='o' || s[c]=='O' || s[c] == 'u' || s[c] == 'U')
count++;
c++;
}
printf("Number of vowels in the string: %d", count);
return 0;
}

RESULT:

The program has been completed and verified successfully


PROGRAM 18:

LOCATION OF A NUMBER

AIM:

Find the location of a number in an array

ALGORITHM:

STEP 1:Start
STEP 2:Input size and elements in array from user.
STEP 3:Store it in some variable say size and arr.
STEP 4:Input number to search from user in some variable say toSearch.
STEP 5:Define a flag variable as found = 0. I have initialized found with 0
STEP 6:Run loop from 0 to size.
STEP 7: Loop structure should look like for(i=0; i<size; i++).
STEP 8:Inside loop check if current array element is equal to searched
number or not.
STEP 9: Which is if(arr[i] == toSearch) then set found = 1 flag and terminate
from loop.
STEP 10:Since element is found no need to continue further.
STEP 11:Outside loop if(found == 1) then element is found otherwise not.
STEP 12:Stop

PROGRAM:

#include <stdio.h>
#define MAX_SIZE 100
void main()
{
int arr[MAX_SIZE];
int size, i, toSearch, found;
clrscr();
printf("Enter size of array: ");
scanf("%d", &size);
printf("Enter elements in array: ");
for(i=0; i<size; i++)
{
scanf("%d", &arr[i]);
}

printf("\nEnter element to search: ");


scanf("%d", &toSearch);
found = 0;
for(i=0; i<size; i++)
{
if(arr[i] == toSearch)
{
found = 1;
break;
}
}
if(found == 1)
{
printf("\n%d is found at position %d", toSearch, i + 1);
}
else
{
printf("\n%d is not found in the array", toSearch);
}

getch();
}

RESULT:

The program has been completed and verified successfully

PROGRAM 19:

PRIME NUMBER

AIM:

Find the prime numbers in a range

ALGORITHM:

STEP 1: Start
STEP 2: Declare check prime function as integer
STEP 3: Declare i and count
STEP 4: Initialise i with 2
STEP 5: Iterate a “for” loop from 2 to num/2.
STEP 6:  If num is divisible by loop iterator, then increment i
STEP 7: If the count is equal to 0,
STEP 8: Number is prime
STEP 9: Else not prime
STEP 10:If number is equal to 1 then count is set to 1
STEP 11:Stop

PROGRAM:

#include<stdio.h>
int checkPrime(int number)
{
int i,count = 0;

for(i=2; i<=number/2; i++)


{
if(number%i == 0)
{
count=1;
break;
}
}

if(number == 1) count = 1;

return count;
}
void main()
{
int m,i,n;
clrscr();
printf("Enter min and max of the range: ");
scanf("%d %d",&m, &n);
printf("Prime numbers from %d to %d are: ", m, n);
for(i=m; i<=n; i++)
{
if(checkPrime(i) == 0)
printf("%d\t",i);
}
getch();
}

RESULT:

The program has been completed and verified successfully


PROGRAM 20:

POSITIVE,NEGATIVE AND ZEROS

AIM:

Find the positive,negative and zeros in a set of numbers

ALGORITHM:

STEP 1: Start
STEP 2: Declare limit,positive,negative and zero as integer
STEP 3: Enter the limit
STEP 4: check if its greater than 0, if its true increment the value of variable
positive by one
STEP 5:  If number is less than 0, then we increment the value of variable
negative by one
STEP 6:  If number is neither greater than 0 nor less than 0, then its a zero ,
increment the value of variable zero by one.
STEP 7: For each iteration of the while loop, we decrement the value of
variable limit by one.
STEP 8:Stop

PROGRAM:
#include < stdio.h >

void main()
{
int limit, num, positive = 0, negative = 0, zero = 0;
clrscr();
printf("Enter the limit\n");
scanf("%d", &limit);

printf("Enter %d numbers\n", limit);

while(limit)
{
scanf("%d", &num);

if(num > 0)
{
positive++;
}
else if(num < 0)
{
negative++;
}
else
{
zero++;
}

limit--;
}

printf("\nPositive Numbers: %d\n", positive);


printf("Negative Numbers: %d\n", negative);
printf("Number of zero: %d\n", zero);

getch();
}

RESULT:
The program has been completed and verified successfully

You might also like