You are on page 1of 25

EXP: 1

DATE:
NUMBER OR NOT USINGC
FIND OUT THE GIVEN NUMBER IS PERFECT
PROGRAM
ALGORITHM:

Step 1: Start

Step 2: [Take Input] Read: N

Step 3: [Initialize] Set: SOF =


0 and I =
1

Step 4: Repeat While I<N

Check If N%I ==0 Then

Compute: SOF = SOF +I

End of If Structure]

Compute: I=I+1|
End of While Loop]

Step 5: Check If N == SOF Then

Print: N is a Perfect Number.

Else
Print: N is not a Perfect number.

Step 6: Exit
PROGRAM:
#nclude<stdio.h>
int main()
int n.i=1,sum=0;

printf"Enter a number: ");


scanf"%d",&n);

whilei<n)i
if(n%i==0)
sum=sumti;
it+;

if(sum=n)
printf"od is a perfect number",i);
else
printf%d is not a perfect number",i);

return 0;

Output:
Enter a number: 6
6 is a perfect number
EXP: 2
DATE:
WRITE A CPROGRAM TOCHECK WHETHERTHE GIVEN NUMBER IS ARMSTRONG
OR NOT

ALGORITHM:
Step 1: Start

Step 2: read number

Step 3: set sum-0 and


duplicate=number
Step 4: reminder=number%10

Step 5: sum=sum+(reminder*reminder#reminder)

Step 6: number=number/10

Step 7: repeat steps 4 to 6 until number >0

Step 8: if sum = duplicate

Step 9: display number is Armstrong


Step 10: else

Step 11: display number is not Armstrong


Step 12: stop
PROGRAM:
#includestdio.h>
int main)
int num.r,sum=0,temp;

printf("Enter a number: ");


scanf("%d",&num);
temp-num,
while(num!=0){
r=num%l10;
num=num/10;
sum-sumt(r*r*r);
if(sumtemp)
printf("%d is an Armstrong number",temp);
else
printf("%d is not an
Armstrong number",temp);
return 0;

Output:
Enter a number: 153
153 is an Armstrong number

http://www.cquestions.com/2008/01/write-c-program-to-tind-pertect-number.html
EXP:3
DATE
WRITE A C PROGRAM TO FIND THE SUM OF
INTEGER INDIVIDUAL DIGITS OF A POSITIVE

ALGORITHM
Step 1: Start
Step 2: Read: Take Input N
Step 3: Initializing SUM] Set: SUM =0
Step 4: Repeat WhileN+0
Set: SUM N%10 -

SUM and
Set: N N/10
End of While Loop]
Step 5: Print: Sum of Digits of N is SUM
Step 6: Exit
PROGRAM
#include <stdio.h>
int sum(int n):
int main)

int num,add;
printf("Enter a positive integer:\n");
scanf"%d",&num);
add=sum(num);
printf("sum=%d".add);
int sum(int n)

if(n==0)
return n
else
return nt+sum(n-1);

Output:
Enter a positive integer: 5
15
DAT
WRUTE AC PROGRAM 1O GENERAIE THE FIRST N TERMs OF THE FIBONNACI
SERIES

ALGORIHM

STEP 1 Start

STEP 2Declare variables i, a,b, show

STEP 3: nitialize the variables, a=0, b=1, and show =0

STEP 4: Fnter the number of terms of Fibonacci series to be printed

STEP S Print tirst two terms of series


STEP 6: Use loop for the following steps
show=a+b
ab
>bshow
inerease value ofi each time by 1
print the v alue of show

STEP 7: End
PROGRAM:
#include<stdio.h>
int main(01
int k,r,
long int i=01j=1,f,

/Taking maximum numbers form user


printf("Enter the number range:");
scanf("%d",&r);
printf("FIBONACCI SERIES: ");
printf("%ld %ld",ij); //printing firts two values.

for(k=2;k<r;kt+){
f=itj
F

printf(" %ld"j);

returm 0;

Output:
Enter the number range: 15
FIBONACCI SERIES: 0112358 13 21 34 55 89 144 233 377
EXP:6
DATE:
WRITE AC PROGRAM TOGENETARE ALL THE PRIME NUMBERS BETWEEN 1 TO
N. WHERE N IS A VALUE SUPPLIED BY THE USER

ALGORITHM

Step 1: Start
Step 2: Declare variables n.i,flag.
Step 3: Initialize variables
flag-1
i-2
Step 4: Read n from user.
Step 5: Repeat the steps until is(n/2)
5.1 Ifremainder of n*i equals 0
flag-0
Go to step 6
5.2 i-i+1
Step 6: If flag=0
Display n is not prime
else
Display n is prime
Step 7: Stop
PROGRAM:

#includestdio.h
int isPrime(int,int),
int main()
int num,prime:
printf"Enter a positive number: ");
scanf%d",&num),
prime isPrimetnum.num/2);
if(primem1)
printf"od is a prine number",num);
else
printf"od is not a prime number",num);
return 0;:

int isPrime(int num,int i)


ifi-)
return 1
else
if(num%i-0)
return O;
else
isPrimelnum,i-1):

Output:
Enter a positive number: 13
13 is a prime number
EXP: 7
DATE:
WRITE AC PROGRAM TO FIND THE LARGEST AND SMALLEST NUMBER INA LIST
OF INTEGERS

ALGORITHM:

#include<stdio.h>
#include<conio.h>
void main()

int n,big,sml,i,totalNumber;
clrscr()
printf("n How many number you will enter : ");
scanf(%d",&totalNumber):
for (i-0;istotalNumber;it+)

printf("n Enter number %d: ",i+1);


scanf"%d",&n);
ifi-=0)

big=sml=n;

ifbigsn) big=n;
if(sml>n) sml=n;

printf("inBiggest number of all : %d",big);


printf("inSmallest number of all : %d",sml);
getch();
Output:
How many number you will enter: 5
Enter number 1: 12
Enter number 2: -6
Enter number 3 100

Enter number 4:3


Enter number 5 : 90

Biggest number of all: 100


Smallest number ofall :-6
EXP:8
DATE:
WRITE ACPROGRAM 1HAT USES FUNCTIONS TO PERFORM THE
FOLLOWING
A .ADDITION OF TWO MATRICS

B. MULTIPLICATION OF TWO MATRICS

ALGORITHM

Step1: Start
Step2: Read: m and n
Step3: Read: Take inputs for Matrix A[l:m, l:n) and Matrix B[I:m, l:n]
Step4: Repeat for i =l to m by 1:
Repeat for j :=1 to n by 1:
C[i. j]=A[i, j] + B[i, j]
End of inner for
loopP
|End of outer for loop]
Step5: Print: Matrix C
Step6: Exit
PROGRAM
*Addition of two matrix*
#include<stdio.h>
#includeconio.h>
void main)

int a[3][3].b[3][3].c[3][3]ij.m.n:
clrscr);
printf"Enter the rows and columns:"):
scanf("%d %d".&m.&n):
printf(" nEnter Matrix A values:");
for(i-0:i<m:i++)
forj-0j<nj-)
scanf("%d".&a[i]0]):

printf" nEnter Matrix B values:")


for(i=0;i<m;i)
forj=0j<nj)
scanf("%d",&b[i]j]):

printf("Addition of Matrices: n");


fori-0;i<m;i)
forj-0jnj+-)
cilbl=a[i]GI-b[i]G]:
printf" d",c[fi]0});
printf(" n");

getch()
OUT PUT

Entered order as 2, 2 i.e. two rows and two columns and matrices as
First Matrix A:
12
34
Second matrix B:-
45
-15
then output of the program (sum of First and Second matrix) will be
57
29
B.MUL TIPLICATION OF TWOMATRICS
ALGORITHM
Stepl: Start
Step2: Read: m, n. p and q
Step3 Read: Inputs for Matrices A||:m. I:n] and B[l:p,
l:q].
Step4: If n# p then:
Prnt: Multiplication is not possible.
Else:
Repeat tor i I to nm by 1:
Repeat for j I to q by I:
Cli.j0 [Initializingl
Repeat k: Ito n byI
Ci.jl=Ci.il A[i. k] x B[k. 3]
+

End of for loop|


[End of for loop]
End of for loop]
[End of If structure]
Step5: Print: C[1:m. 1:q
Step6: Exit.
PROGRAM

*Matrix Multiplication*/
#include<stdio.h>
#include<conio.h>
void main( )

int a[25][25].b[25][25].c[25][|25],.ij.k.r,s,m,n;
clrscr( );
printf("Enter the number of rows and columns of matrix A");
scanf("%d%d",&m,&n);
printf("Enter the number of rows and columns of matrix B")
scanf("%d%d",&r,&s);
if(n!=r)
printf("The matrix cannot be multiplied");
else

printf"Enter the elements of A matrix");


for(i-0;i<m;i++)
forj=0:j<n:j++)
scanf("%d",&a[i]lj]);
printf("Enter the elements of matrix B");
for(i=0;i<m;it+)
forj-0j<n;j++)
scanf("t%d",&b[i]i]);
printf("in the elements of the matrix A \n");
for(i-0;i<m;i++)
printf("n'");
forj-0;j<njt+)
printf(%dit",a[i]il);

printf("The elements of the matrix B:\n");


for(i-0;i<m;it+)
printf("n")
forj=0j<nj++)
printf("%dit",b[i]g]);
for(i-0;i<m,i++)
printf("n")
forj-0:j<nj++)
c[i]l-0;
for(k-0;k<m;k++)
c[i]bl-c[i]Gl+a[i][kj*b[k]U]:

printf("nThe multiplication of two matrix:n");


for(i=0;i<m;i++)
for(j-0;j<njt+)
printf(%d t",cfi]g]);
printf("n");

getch( );
Output:
Enter the number rows and columns of the first matrix

33

Enter the elements of the first matrix

120

011

201
Enter the number rows and columns of the second matrix

33

Enter the elements of the second matrix

1 12

211

121
Product of entered matrix:

534

3 32

34 5
EXP: 9
DATE:
Write a program to perform various string operations.

ALGORITHM:

Step 1: start

Step 2: read strings S1, $2, $3;

Step 3: read values X, LI, L2, L3

Step 4: X=strcomp (S1, $2)

Step 5: if X! =0

Print string is not equal

strcat (S1, S2)

else

Print strings are equal

strepy ($3,S1)
Ll strlenS1)

L2 strlen($2)
L3= strlen ($3)

Step 6: Stop
PROGRAM:

#include <stdio.h>
#include<conio.h>
#include<string.h>
#includectype.h>
#include<stdlib.h>

int length(char str[]):


void reverse(char str|]);
int palindrome(char str[]);
void copy(char str1|). char str2[0);
int compare(char strl[], char str2[]);
void concat(char strl[]. char str2[]);
void search(char strl[], char str2[]);
void count(char str1|[):

void main() {
char a 100], b[100];
int result, option,
do
printf("nl.Length of a string");
printf("n2.Reverse the Given String");
printf("n3.Check for Palindrome");
printf("in4.Copy");
printf("n5.String Comparison");
printf"n6.String Concatenation");
printf"n7.String Searching");
printf("n8.Counting of Words,Characters & Special Characters"):
printf("n9.Quit");
printf("in\nEnter Your Choice:");
scanf("%d", &option);

flushall):
switch (option)
case 1
printf("inEnter a String:");
gets(a);
result = length(a);

printf"inlLength of %s=%d", a, result);


printf("nPress a Character");
getch();
break
case 2:
printf("inEnter a String:");
gets(a);
reverse(a);
printf("nResult-%", a);
printf("nPress a Character");
getch();
break;
case 3:
printf("in Enter a String:");
gets(a);
result= palindrome(a);
if(result==0)
printf(" nNot a palindrome");
else
printf("nA palindrome");
printf("nPress a Character");
getch();
break
case 4:
printf"nEnter a String:");
gets(a);
copy(b, a);
printf("nResult=%s", b);
printf("nPress a Character");
getch();
break;

case 5:
printf("nEnter 1st string:");
gets(a);
printf("nEnter 2nd string:");
gets(b);
result=compare(a,b);
if(result== 0)
printf("inboth are same");
else if (result > 0)

printf("inlst>2nd");
else
printf("nlst<2nd");
printf("nPress a Character");
getch():
break;

case 6:
printf(" nEnter Ist string:");
gets(a):
printf(" nEnter 2nd string:");
gets(b);
concat(a, b);
printf("inresult=%s", a);
printf("nPress a Character");
getch();
break;

case 7:
printf(" nEnter Ist string:");
gets(a);
printf"inEnter 2nd string:");
gets(b);
search(a, b
printf("inPress a Character");
getch();
break;

case 8:
printf(" nEnter a string:");
gets(a);
count(a);
printf("nPress a Character");
getch();
break;

default:
printf("inlnvalid Choice:");
break;

while (option != 9);

int length(char str[])


int i = 0;

while (str[i] != \0')


itt
retum (i):

void reverse(char str[]) {


int i, j:
char temp:
i=j =0:
while (str[i]!=\0')
J--
while (i<j) {
temp = str[1];
str[i] = str[j]:
str[i] = temp:

i++
J-

int palindrome(char str[]) {


int i, j
i=j=0;
while (str[] !="\0')
j+
while (i <j) {
if (str[i] != str[i - 1])

return (00);
i++
J-
return ();

void copy(char str2[]. char str![])


int i = 0;

while (str l[i] !=\0)


str2[i] = strl [i];

str2[i] = "0';

int compare(char str1 [], char str2[]) {


int i;
i = 0;

while (strl[i] != \0') {


if(str1[i]> str2[i])
return (1);
if (str1[i] < str2[1)
return (-1);
i++
return (0);

void concat(char strl[0, char str2[0) {


int i, j
i = 0;

while (str1[i]!= \0)


itt;
for 0; str2[j] !="\0'; it+,j++)
strl[i) = str2j]:

str1[i] = \0';

void search(char str![], char str2[)


int i, j, lena, lenb;
for (lena =0; strl[lena] !="\0'; lena++);
for(lenb =0; str2[lenb]!= "\0'; lenbt+);
for (i = 0; i<= lena - lenb + 1; it++) {
for = 0, strl [i + j]== str2[j] && str2[j] != "\0': jtt);
if (str2j] == "0')
printf("nString found at location : %d", i + 1);

void count(char str[]) {


int words= 0, characters = 0, spchar = 0, i
for (i = 0; str[i] != "\0'; i++) {

if(isalnum(str|[i]) && (i==0}lisalnum(str|i - ID))


words++;
characters++;
if(lisalnum(str[i]) && lisspace(str|i]))
spchart+
printf("nNo of characters
=
%d", characters);

You might also like