Вы находитесь на странице: 1из 96

1.

BRANCHING AND SELECTION


1) WRITE A C-PROGRAM TO FIND THE BIGGEST OF THE
GIVEN THREE NUMBERS
ALGORITHM
Step 1:

start

Step 2: Declare variables x,y,z and max as a integer type


Step 3: Read x,y and z values
Step 4: if(x<y)then
if(y<z)then
Max=z
otherwise
Max=y
Step 5: otherwise if(x<z)then
Max=z
otherwise
Max=x
Step 6: Write max value
Step 7: Stop

Mrs. B. Lakshmi, Asst.Professor, Dept. of Computer Applications, VRSEC

PROGRAM
/* Program to find the biggest of the given three numbers */
#include<stdio.h>

/* printf(), scanf() */

#include<conio.h>

/* getch(), clrscr() */

void main()
{
int x,y,z,max;
clrscr();

/* clears the screen */

printf("Enter x value:");
scanf("%d",&x);

/* reads x from the keyboard */

printf("Enter y value:");
scanf("%d",&y);

/* reads y from the keyboard */

printf("Enter z value:");
scanf("%d",&z);

/* reads z from the keyboard */

if(x<y)
{
if(y<z)
max=z;
else
max=y;
}
else

Mrs. B. Lakshmi, Asst.Professor, Dept. of Computer Applications, VRSEC

{
if(x<z)
max=z;
else
max=x;
}
printf("Biggest Of The Given Three Numbers is %d ",max);
getch();

/* reads a char from keyboard but it doesnt print it on

the screen */
}

Mrs. B. Lakshmi, Asst.Professor, Dept. of Computer Applications, VRSEC

2.WRITE A C-PROGRAM TO PRINT ELECTRICITY BILLS


WITH SLABS
ALGORITHM
Step 1: start
Step 2:

Declare variables mno, cr, pr, n as a integer type and

bill as float type


Step 3: Read mno, cr, pr values
Step 4: Computing number of units consumed
n=cr-pr;
Step 5: Calculate Amount based on Units
if(n>=0&&n<=200) then
bill=n*0.50
else if(n>200&&n<=400) then
bill=100+n*0.65
else if(n>400&&n<=600) then
bill=230+n*0.80
else if(n>600) then
bill=390+n*1.0
else then
write Invalid number of units
Step 6: Write mno, bill value
Step 7: Stop

Mrs. B. Lakshmi, Asst.Professor, Dept. of Computer Applications, VRSEC

PROGRAM
/* Program to print Electricity bill with slabs */
#include<stdio.h>

/* printf(), scanf() */

#include<conio.h>

/* getch(), clrscr() */

void main()
{
int n,cr,pr,mno;
char utype;
float bill=0.0;
clrscr();

/* clears the screen */

printf("\n\t****************************************");
printf("\n\t*****ELECTRICITY

BILL

CALCULATION*******");
printf("\n\t****************************************");
printf("\nEnter meter number :");
scanf("%d",&mno);
/*Reading current and previous readings*/
printf("\nEnter current reading:");
scanf("%d",&cr);
printf("\nEnter previous reading:");
scanf("%d",&pr);
/*Computing number of units consumed*/
n=cr-pr;

Mrs. B. Lakshmi, Asst.Professor, Dept. of Computer Applications, VRSEC

if(n>=0&&n<=200)
bill=n*0.50;
else if(n>200&&n<=400)
bill=100+n*0.65;
else if(n>400&&n<=600)
bill=230+n*0.80;
else if(n>600)
bill=390+n*1.0;
else
printf("\nInvalid number of units");
if(bill>0)
{
printf("\nThe meter number= %d",mno);
printf("\nThe number of units consumed=%d",n);
printf("\nBill amount for %d units is %f\n",n,bill);
}
getch();

/* reads a char from keyboard but it doesnt print it on

the screen */
}

Mrs. B. Lakshmi, Asst.Professor, Dept. of Computer Applications, VRSEC

3.WRITE A C-PROGRAM TO PRINT STUDENT GRADES


ALGORITHM
Step 1: start
Step 2: Declare variables n1, n2, n3, tot as a integer type
Step 3: Read n1, n2, n3 values
Step 4: Computing Total Marks
tot = n1+n2+n3
write tot
tot=tot/300
Step 5: Computing Grade
if(tot >= 80) then
wirte You got A grade
else if ( tot >=60) then
write You got B grade
else if ( tot >=40) then
write You got C grade}
else if ( tot < 40) then
write You Failed in this exam
Step 6: Stop

Mrs. B. Lakshmi, Asst.Professor, Dept. of Computer Applications, VRSEC

PROGRAM
/* Program to print Student grades */
#include<stdio.h>

/* printf(), scanf() */

#include<conio.h>

/* getch(), clrscr() */

void main()
{
int n1,n2,n3,tot;
printf("Enter your mark\n ");
printf("Subject 1:");
scanf("%d",&n1);
printf("Subject 2:");
scanf("%d",&n2);
printf("Subject 3:");
scanf("%d",&n3);
if(n1<0 || n2<0 || n3<0)
{
printf("\n Invalid Marks");
exit(0);
}
if(n1<35 || n2<35 || n3<35)
{
printf("\n You Failed in this exam");
exit(0);

Mrs. B. Lakshmi, Asst.Professor, Dept. of Computer Applications, VRSEC

}
tot=n1+n2+n3;
printf("\n Total Marks %d", tot);

// printing outputs

if(tot >= 80){


printf("\n You got A grade"); }

// printing outputs

else if ( tot >=60){

// Note the space between

else & if
printf("\n You got B grade");}
else if ( tot >=40){
printf("\nYou got C grade");}
else if ( tot < 40){
printf("\n You Failed in this exam");}
getch();
}

Mrs. B. Lakshmi, Asst.Professor, Dept. of Computer Applications, VRSEC

2. CONTROL STATEMENTS
1) WRITE

C-PROGRAM

TO

IMPLEMENT

THE

CALCULATOR APPLICATION (USING SWITCH)


ALGORITHM
Step 1: start
Step 2: Declare variables sign as a char data type and a, b as
integer data types
Step 3: Read sign value
sign=getchar()
Step 4: switch(sign)
Step 5: case '+':

read a, b values
Write a+b

Step 6: case '-': read a, b values


Write a-b
Step 7: case '*':

read a, b values
Write a*b

Step 8: case '/': read a, b values


Write a/b
Step 9: case '%':

read a, b values

Write a%b
Step 10: default:

Invalid operation"

Step 11: Repeat the Steps 3,4,5,6,7,8,9,10 until

Mrs. B. Lakshmi, Asst.Professor, Dept. of Computer Applications, VRSEC

while(sign != 'Q' && sign != 'q')


Step 12: Stop

PROGRAM
/*Program to implement the calculator application (using
switch)*/
#include<stdio.h>
#include<conio.h>

/* printf(), scanf() */
/* getch(), clrscr() */

void main()
{
int a,b;
char sign;
clrscr();
printf("\nPlease enter an operation +, -, *, /, %, <'Q' to quit>: ");
flush(stdin);
scanf("%c",&sign);
while (sign != 'Q' && sign != 'q')
{
switch(sign)
{
case '+': printf("\nEnter 2 numbers : ");
scanf("%d%d",&a,&b);

Mrs. B. Lakshmi, Asst.Professor, Dept. of Computer Applications, VRSEC

printf("Addition

of

%d

and

%d

is

%d

is

%d

is

%d\n",a,b,a+b);
break;
case '-':

printf("\nEnter 2 numbers : ");


scanf("%d%d",&a,&b);
printf("Subtraction

of

%d

and

%d\n",a,b,a-b);
break;
case '*':

printf("\nEnter 2 numbers : ");


scanf("%d%d",&a,&b);
printf("Multiplication

of

%d

and

%d\n",a,b,a*b);
break;
case '/':

printf("\nEnter 2 numbers : ");


scanf("%d%d",&a,&b);
printf("Division of %d and %d is %d\n",a,b,a/b);
break;

case '%': printf("\nEnter 2 numbers : ");


scanf("%d%d",&a,&b);
printf("Modulus

of

%d

and

%d

%d\n",a,b,a%b);
break;
default:

printf("\nInvalid Option\n");

Mrs. B. Lakshmi, Asst.Professor, Dept. of Computer Applications, VRSEC

is

break;
}
printf("\nPlease enter an operation +, -, *, /, %, <'Q' to
quit>: ");
fflush(stdin);
scanf("%c",&sign);
}
getch();
}

Mrs. B. Lakshmi, Asst.Professor, Dept. of Computer Applications, VRSEC

WRITE A C-PROGRAM TO PRINT TABLE FOR A GIVEN


NUMBER
ALGORITHM
Step 1: start
Step 2: Declare variables n, i as Integer data types
Step 3: Read n value
Step 4: set i:=1
Step 5: repeat steps 6,7 until i<=10
Step 6: write n x i = i * n
Step 7: set i:=i+1
Step 8: Stop

PROGRAM
/* Program to print table for a given number*/
#include<stdio.h>

/* printf(), scanf() */

#include<conio.h>

/* getch(), clrscr() */

void main()
{
int i,n;
clrscr();
printf("\nEnter Number to Generate Table: ");
scanf("%d",&n);
printf("\n %d Table ",n);

Mrs. B. Lakshmi, Asst.Professor, Dept. of Computer Applications, VRSEC

printf("\n----------------------");
for(i=1;i<10;i++)
{
printf("\n%d X %d = %d",n,i,n*i);
}
getch();
}

Mrs. B. Lakshmi, Asst.Professor, Dept. of Computer Applications, VRSEC

WRITE A C-PROGRAM TO PRINT 1 TO N NATURAL


NUMBERS
ALGORITHM
Step 1: start
Step 2: Declare variables n, i as Integer data types
Step 3: Read n value
Step 4: set i:=1
Step 5: repeat steps 6,7 until i<=n
Step 6: write n
Step 7: set i:=i+1
Step 8: Stop

PROGRAM
/* Program to print 1 to n natural numbers */
#include<stdio.h>
#include<conio.h>
void main()
{
int i,n;
clrscr();
printf("\nEnter Number to Print 1 to N natural Numbers: ");
scanf("%d",&n);
printf("\n1 to %d Natural Numbers ",n);

Mrs. B. Lakshmi, Asst.Professor, Dept. of Computer Applications, VRSEC

printf("\n----------------------------------\n");
for(i=1;i<n;i++)
{
printf("%d\t",i);
}
getch();
}

Mrs. B. Lakshmi, Asst.Professor, Dept. of Computer Applications, VRSEC

LOOPING CONSTRUCTS-I
1) WRITE A C-PROGRAM TO CHECK THE GIVEN NUMBER
IS ARMSTRONG OR NOT
ALGORITHM
Step 1: start
Step 2: Declare variables n, r, temp ,count=0 as Integer data
types
Step 3: Read n value Assign temp = n
Step 4: Assign temp = n
Step 5: Compute
r=n%10
count=count+(r*r*r)
n=n/10
Step 6: Repeat Step 5 while n>0
Step 7: if count equals to temp then
Write Given number is Armstrong
Else
Write Given number is Not Armstrong
Step 8: Stop
PROGRAM
/* Program to Check the Given Number Is Armstrong Or Not*/
#include<stdio.h>
#include<conio.h>

Mrs. B. Lakshmi, Asst.Professor, Dept. of Computer Applications, VRSEC

void main()
{
int n,count=0,r,temp;
clrscr();
printf("Enter a number : ");
scanf("%d",&n);
temp=n;
while(n>0)
{
r=n%10;
count=count+(r*r*r);
n=n/10;
}
if(count==temp)
printf("\nGiven Number is Armstrong");
else
printf("\nGiven Number is not Armstrong");
getch();
}

Mrs. B. Lakshmi, Asst.Professor, Dept. of Computer Applications, VRSEC

WRITE A C-PROGRAM TO CHECK THE GIVEN NUMBER


IS PALINDROME OR NOT
ALGORITHM
Step 1: start
Step 2: Declare variables n, r, temp ,count=0 as Integer data
types
Step 3: Read n value
Step 4: Assign temp = n
Step 5: Compute
r=n%10;
count=(count*10)+r;
n=n/10;
Step 6: Repeat Step 5 while n>0
Step 7: if count equals to temp then
Write Given number is Palindrome
Else
Write Given number is Not Palindrome
Step 8: Stop

Mrs. B. Lakshmi, Asst.Professor, Dept. of Computer Applications, VRSEC

PROGRAM
/*Program to check the given number is palindrome or not*/
#include<stdio.h>
#include<conio.h>
void main()
{
int n,count=0,r,temp,opt;
clrscr();
printf("Enter a number : ");
scanf("%d",&n)
temp=n;
while(n>0)
{
r=n%10;
count=(count*10)+r;
n=n/10;
}
if(count==temp)
printf("\nGiven Number is Palindrome");
else
printf("\nGiven Number is not Palindrome");
getch();
}

Mrs. B. Lakshmi, Asst.Professor, Dept. of Computer Applications, VRSEC

WRITE A C-PROGRAM TO CHECK THE GIVEN NUMBER


IS PERFECT OR NOT
ALGORITHM
Step 1: Start
Step 2: read n
Step 3: set i:=i+1,sum:=0
Step 4: repeat steps 5,6 until i n/2
Step 5: if n mod i=0
set sum:=sum+i
Step 6: set i:=i+1
Step 7: if sum=n
write n is perfect
else
write n is not perfect
Step 8: stop

Mrs. B. Lakshmi, Asst.Professor, Dept. of Computer Applications, VRSEC

PROGRAM
/*Program to check the given number is perfect or not*/
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,count=0,r,temp,opt;
clrscr();
printf("Enter a number : ")
scanf("%d",&n);
for(i=1;i<=n/2;i++)
{
if(n%i==0)
count=count+i;
}
if(n==count)
printf("Given number is Perfect number");
else
printf("Given number is not Perfect number");
getch();
}

Mrs. B. Lakshmi, Asst.Professor, Dept. of Computer Applications, VRSEC

WRITE A C-PROGRAM TO CHECK THE GIVEN NUMBER


IS STRONG OR NOT
ALGORITHM
Step 1: start
Step 2: Declare variables n, i, f, r, count=0, temp as Integer data
types
Step 3: Read n value Assign temp = n
Step 4: set i=1 and f=1 r=n%10;
Step 5: compute f=f*i and i=i+1
Step 6: Repeat step 5 until i<=r
Step 7: count=count+f;
n=n/10;
Step 8: Repeat Step 4,5,6,7 while n>0
Step 9: if count equals to temp then
Write Given number is Strong
Else
Write Given number is not Strong
Step 10: Stop
PROGRAM
/*Program to check the given number is strong or not*/
#include<stdio.h>
#include<conio.h>
void main()

Mrs. B. Lakshmi, Asst.Professor, Dept. of Computer Applications, VRSEC

{
int n,i,f,r,count=0,temp;
printf("Enter a number: ");
scanf("%d",&n);
while(n>0)
{
i=1;f=1;
r=n%10;
while(i<=r){
f=f*i;
i++;
}
count=count+f;
n=n/10;
}
if(count==temp)
printf("%d is a strong number",temp);
else
printf("%d is not a strong number",temp);
getch();
}

Mrs. B. Lakshmi, Asst.Professor, Dept. of Computer Applications, VRSEC

WRITE A C-PROGRAM TO PRINT THE REVERSE OF THE


GIVEN NUMBER
ALGORITHM
Step 1: start
Step 2: Declare variables n, r, temp ,count=0 as Integer data
types
Step 3: Read n value
Step 4: Compute
r=n%10;
count=(count*10)+r;
n=n/10;
Step 5: Repeat Step 5 while n>0
Step 6: write count
Step 7: Stop

Mrs. B. Lakshmi, Asst.Professor, Dept. of Computer Applications, VRSEC

PROGRAM
/* Program to print the reverse of the given number */
#include<stdio.h>
#include<conio.h>
void main()
{
int n,count=0,r,temp,opt;
clrscr();
printf("Enter a number : ");
scanf("%d",&n);
temp=n
while(n>0)
{
r=n%10;
count=(count*10)+r;
n=n/10;
}
printf("\nReverse of %d is %d",temp,count);
getch();
}

Mrs. B. Lakshmi, Asst.Professor, Dept. of Computer Applications, VRSEC

WRITE A C-PROGRAM TO CONVERT THE GIVEN


DECIMAL NUMBER TO BINARY NUMBER
ALGORITHM
Step 1: Start
Step 2: read a decimal number n
Step 3: repeat steps 4,5,6,7 until n>0
Step 4: set r:=n%2
Step 5: set num:=(num10)+r
Step 6: set cnt:=cnt+1
Step 7: set n:=n/2
Step 8: set n:=num
Step 9: repeat steps until cnt>0
Step 10: set r:=n mod10
Step 11: set bin:=(bin10)+r
Step 12: set cnt=cnt-1
Step 13: set n:=n/10
Step 14: write The binary number is bin
Step 15: Stop

Mrs. B. Lakshmi, Asst.Professor, Dept. of Computer Applications, VRSEC

PROGRAM
/* Program to convert the given decimal number to binary
number */
#include<stdio.h>
#include<conio.h>
void main()
{
long int n,num=0,bin=0,cnt=0,m;
int r;
clrscr();
printf("\nEnter a number:");
scanf("%ld",&n);
m=n;
for(;n>0;n=n/2)
{
r=n%2;
num=(num*10)+r
cnt++;
}
for(n=num;cnt>0;n=n/10)
{
r=n%10;
bin=(bin*10)+r;

Mrs. B. Lakshmi, Asst.Professor, Dept. of Computer Applications, VRSEC

cnt--;
}
printf("\nThe binary number of given decimal number %ld
is %ld",m,bin);
getch();
}

Mrs. B. Lakshmi, Asst.Professor, Dept. of Computer Applications, VRSEC

LOOPING CONSTRUCTS-II
1) WRITE A C-PROGRAM TO PRINT SUM OF N NATURAL
NUMBERS
ALGORITHM
Step 1: start
Step 2: read n
Step 3: set i:=1,sum=0
Step 4: repeat steps 5,6,7 until in
Step 5: read num
Step 6: set sum:=sum+num
Step 7: set i:=i+1
Step 8: write sum
Step 9: stop

Mrs. B. Lakshmi, Asst.Professor, Dept. of Computer Applications, VRSEC

PROGRAM
/* Program to print sum of n natural numbers */
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,sum=0,num;
clrscr();
printf("\nEnter value for n:");
scanf("%d",&n);
i=1;
do
{
printf("\nEnter a number:");
scanf("%d",&num);
sum=sum+num;
i=i+1;
}while(i<=n);
printf("\n The sum of %d numbers=%d",n,sum);
getch();
}

Mrs. B. Lakshmi, Asst.Professor, Dept. of Computer Applications, VRSEC

WRITE A C-PROGRAM TO GENERATE FIBONACCI


SERIES UP TO N NUMBERS
ALGORITHM
Step 1: start
Step 2: read n
Step 3: set a:=0,b:=1
Step 4: write a and b
Step 5: set i:=1 repeat steps 6,7,8,9 until i<=(n-2)
Step 6: set c:=a+b
Step 7: write c
Step 8: set a:=b,b:=c
Step 9: set i:=i+1
Step 10: stop
PROGRAM
/* Program to generate Fibonacci series up to n numbers */
#include<stdio.h>
#include<conio.h>
void main()
{
int i,n,a=0,b=1,c;
clrscr();
printf("\nEnter number of fibonacci numbers do you want:");
scanf("%d",&n);

Mrs. B. Lakshmi, Asst.Professor, Dept. of Computer Applications, VRSEC

printf("Fibonacci numbers upto %d\n",n);


printf("------------------------------\n");
printf("%d\t%f\t",a,b);
for(i=1;i<=n-2;i++)
{
c=a+b;
printf("%d\t",c);
a=b;
b=c;
}
getch();
}

Mrs. B. Lakshmi, Asst.Professor, Dept. of Computer Applications, VRSEC

WRITE A C-PROGRAM TO PRINT PRIME NUMBERS


BETWEEN 1 TO N NUMBERS
ALGORITHM:
Step 1: Start
Step 2: read n
Step 3: set i:=1
Step 4: repeat steps 5,6,7,8 until in
Step 5: set cnt:=0,j=1
Step 6: repeat steps i),ii) until ji
i).if i mod j=0
set cnt:=cnt+1
ii). set j:=j+1
Step 7: if cnt=2
write i
Step 8: set i:=i+1
Step 9: Stop
PROGRAM
/* Program to generate Fibonacci series up to n numbers */
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,j,cnt;

Mrs. B. Lakshmi, Asst.Professor, Dept. of Computer Applications, VRSEC

clrscr();
printf("\nEnter value for n:");
scanf("%d",&n);
printf("\nThe Prime Numbers from 1 to %d\n\n",n);
for(i=1;i<=n;i++)
{
cnt=0;
for(j=1;j<=i;j++)
{
if(i%j==0)
cnt++;
}
if(cnt==2)
printf("%d\t",i);
}
getch();
}

Mrs. B. Lakshmi, Asst.Professor, Dept. of Computer Applications, VRSEC

WRITE A C-PROGRAM TO PRINT * IN EQUILATERAL


TRIANGLE
ALGORITHM:
Step 1: Start
Step 2: read n
Step 3: set i:=1,k:=1,j:=1
Step 4: repeat steps 5,6,7,8 until in
Step 5: repeat steps i),ii) until jn-i
i) write ;
ii) Set j=j+1;
Step 6: repeat steps a),b) until k<=(2*i)-1
a) Write
b) Set k=k+1;
Step 7: set i:=i+1
Step 8: Stop
PROGRAM
/* C program to print Equilateral Triangle using * */
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,k,n;
clrscr();

Mrs. B. Lakshmi, Asst.Professor, Dept. of Computer Applications, VRSEC

printf("Enter number of rows of the triangle \n");


scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=n-i;j++)
{
printf(" ")
}
for(k=1;k<=(2*i)-1;k++)
{
printf("*");
}
printf("\n");
}
getch();
}

Mrs. B. Lakshmi, Asst.Professor, Dept. of Computer Applications, VRSEC

WRITE A C-PROGRAM TO PRINT SUM OF FIRST N


NATURAL NUMBERS
ALGORITHM
Step 1: Start
Step 2: declare n, i, sum=0
Step 3: read n
Step 4: set i:=1
Step 5: calculate:
sum = sum + i
i++
Step 6: repeat Step 5 until i<=n
Step 7: write sum
Step 8: Stop

Mrs. B. Lakshmi, Asst.Professor, Dept. of Computer Applications, VRSEC

PROGRAM
/* Program to find the sum of first N Natural Numbers */
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,sum=0;
clrscr();
printf("\nEnter N value : ");
scanf("%d", &n);
i=1;
do
{
sum = sum + i
i = i++;
}
while(i<=n);
printf("\nSum of first %d Natural Numbers : %d",n,sum);
getch();
}

Mrs. B. Lakshmi, Asst.Professor, Dept. of Computer Applications, VRSEC

3. ARRAYS
1) WRITE

C-PROGRAM

TO

REMOVE

DUPLICATE

ELEMENTS IN THE GIVEN ARRAY


ALGORITHM
Step 1: Start
Step 2: Declare i,j,k,n,a[20],temp as Integer type
Step 3: read n
Step 4: set i:=0
Step 5: read a[i] , set i:=i+1
Step 6: repeat Step 5 until i<n
Step 7: set i:=0
Step 8: repeat step 9,10,11 until i<n
Step 9: set j:=i+1
Step 10: repeat step 11 until j<n
Step 11: if(a[i]==a[j]) then
a) Set k=j and repeat step b),c) until k<n
b) set a[k]=a[k+1]
c) set k=k+1
d) n:=n-1
else
set j:=j+1
Step 12: set i:=0

Mrs. B. Lakshmi, Asst.Professor, Dept. of Computer Applications, VRSEC

Step 13: write a[i] , set i:=i+1


Step 14: repeat Step 13 until i<n
Step 15: Stop

PROGRAM
/* program to remove duplicate elements in the given array */
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,k,n,a[20],temp;
clrscr();
printf("\nEnter the Size of Array:");
scanf("%d",n);
printf("\nEnter the Elements of array:");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("\nElements Before Removing Duplicate Elements :");
for(i=0;i<n;i++)
printf("%3d",a[i]);
for(i=0;i<n;i++)
{
for(j=i+1;j<n;)

Mrs. B. Lakshmi, Asst.Professor, Dept. of Computer Applications, VRSEC

{
if(a[i]==a[j])
{
for(k=j;k<n;k++)
a[k]=a[k+1];
n=n-1;
}
else
j++;
}
}
printf("\n\nRemaining Elements are:");
for(i=0;i<n;i++)
printf("%3d",a[i]);
getch();
}

Mrs. B. Lakshmi, Asst.Professor, Dept. of Computer Applications, VRSEC

WRITE A C-PROGRAM TO INSERT AN ELEMENT AT THE


SPECIFIED LOCATION OF THE ARRAY
ALGORITHM
Step 1: Start
Step 2: Declare i,j,n,a[20],ins,pos,flag=0 as Integer type
Step 3: read n
Step 4: set i:=0
Step 5: read a[i] , set i:=i+1
Step 6: repeat Step 5 until i<n
Step 7: read pos , ins
Step 8: set i:=0
Step 9: repeat step 10 until i<n
Step 10: if(i==pos) then
e) flag=1
f) Set j=n-1 and repeat step c),d) until j>=i
g) set a[j+1]=a[j]
h) set j=j-1
i) a[i]=ins
Step 11: if(flag==0) then
set n=n+1
Else
Array index out of size
Step 12: set i=0

Mrs. B. Lakshmi, Asst.Professor, Dept. of Computer Applications, VRSEC

Step 13: write a[i] , set i:=i+1


Step 14: repeat Step 13 until i<n
Step 15: Stop
PROGRAM
/* Program to insert an element at the specified location of the
array */
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,n,a[20],ins,pos,flag=0;
clrscr();
printf("\nEnter the Size of Array:");
scanf("%d",&n);
printf("\nEnter the Elements of array:");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("\nElements Before Inserting an Element is:");
for(i=0;i<n;i++)
printf("%3d",a[i]);
printf("\n\nEnter

which

Position

want

to

Element:");
scanf("%d",&pos);

Mrs. B. Lakshmi, Asst.Professor, Dept. of Computer Applications, VRSEC

insert

printf("\nEnter which Element is u want to Insert:");


scanf("%d",&ins);
for(i=0;i<n;i++)
{
if(i==pos-1)
{
flag=1;
for(j=n-1;j>=i;j--)
a[j+1]=a[j];
a[i]=ins;
}
}
if(flag==1)
{
n=n+1;
}
else
printf("\n Array index out of size");
printf("\n\nElements after inserting an Element is:");
for(i=0;i<n;i++)
printf("%3d",a[i]);
getch();
}

Mrs. B. Lakshmi, Asst.Professor, Dept. of Computer Applications, VRSEC

WRITE A C-PROGRAM TO ARRANGE THE GIVEN


ELEMENTS IN THE ASCENDING ORDER
ALGORITHM
Step 1: start
Step 2: Declare i,j,n,a[20],temp as Integer type
Step 3: read n
Step 4: set i:=0
Step 5: read a[i] , set i:=i+1
Step 6: repeat Step 5 until i<n
Step 8: set i:=0
Step 9: repeat step 10,11 until i<n-1
Step 10: set j=i+1
Step 11: repeat step 12 until j<n
Step 12: if(a[i]>a[j]) then
temp=a[i];
a[i]=a[j];
a[j]=temp;
Step 13: set i=0
Step 14: write a[i] , set i:=i+1
Step 15: repeat Step 13 until i<n
Step 16: Stop

Mrs. B. Lakshmi, Asst.Professor, Dept. of Computer Applications, VRSEC

PROGRAM
/*Program to arrange the given elements in the ascending order
*/
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,n,a[20],temp;
clrscr();
printf("\nEnter the Size of Array:");
scanf("%d",&n);
printf("\nEnter the Elements of array:");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("\nElements Before Sorting :");
for(i=0;i<n;i++)
printf("%3d",a[i]);
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]>a[j])
{

Mrs. B. Lakshmi, Asst.Professor, Dept. of Computer Applications, VRSEC

temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
printf("\n\nAfter Sorting Elements are:");
for(i=0;i<n;i++)
printf("%3d",a[i]);
getch();
}

Mrs. B. Lakshmi, Asst.Professor, Dept. of Computer Applications, VRSEC

STRINGS
1) WRITE A C-PROGRAM TO PERFORM THE FOLLOWING
OPERATIONS WITH AND WITHOUT USING STRING
HANDLING FUNCTIONS
I.

LENGTH OF THE STRING


ALGORITHM
Step 1: start
Step 2: Declare and initialize a[10] as Character array and j=0,
l=0, i=0, opt as integers
Step 3: read a
Step 4: repeat step 5, 6 until loop breaks
Step 5: read opt
Step 6: if opt==1 then
Write strlen(a)
Else if opt==2 then
(a)

Repeat step (b) while a[i]!=/0

(b)

Set j=j+1and i=i+1

(c)Write j
Else
Break;
Step 7: stop

Mrs. B. Lakshmi, Asst.Professor, Dept. of Computer Applications, VRSEC

PROGRAM
/* Program to find length of the string */
#include<stdio.h>
#include<conio.h>
void main()
{
char a[10];
int j=0,l=0,i=0,opt;
clrscr();
printf("\n Enter String:");
scanf("%s",a)
while(1)
{
printf("\n\n1.Using String Handling
Function\n2.Without using String Handling
Functions\n3.exit\nEnter your Option:");
scanf("%d",&opt);
if(opt==1)
{
l=strlen(a);
printf("\n length of the string using Strlen()
:%d",l);
}

Mrs. B. Lakshmi, Asst.Professor, Dept. of Computer Applications, VRSEC

else if(opt==2)
{
while(a[i]!='\0')
{
j++;
i++;
}
printf("\n Length of the given string is %d",l);
}
else
break;
}
getch();
}

Mrs. B. Lakshmi, Asst.Professor, Dept. of Computer Applications, VRSEC

REVERSE OF THE GIVEN STRING


ALGORITHM
Step 1: start
Step 2: declare and initialize a[10], b[10]as Character array and
l=0, i=0, opt as integers
Step 3: read a
Step 4: copy a into b
Step 5: repeat step 6,7 until loop breaks
Step 6: read opt
Step 7: if opt==1 then
Write strrev(b)
Else if opt==2 then
(d)

i=strlen(a)

(e)repeat step (c) until i>0


(f) write a[i]
(g)

i=i+1

Else
Break;
Step 8: stop

Mrs. B. Lakshmi, Asst.Professor, Dept. of Computer Applications, VRSEC

PROGRAM
/* Program to reverse the string */
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char a[10],b[10];
int l=0,i=0,opt;
clrscr();
printf("\n Enter String:");
scanf("%S",a);
strcpy(b,a);
while(1)
{
printf("\n\n1.Using String Handling
Function\n2.Without using String Handling
Functions\n3.exit\nEnter your Option:");
scanf("%d",&opt);
if(opt==1)
{
printf("\n Reverse of the string using Strrev()
:%s",strrev(b));

Mrs. B. Lakshmi, Asst.Professor, Dept. of Computer Applications, VRSEC

}
else if(opt==2)
{
l=strlen(a);
printf("\n Reverse of the given string :");
for(i=l;i>=0;i--)
{
printf("%c",a[i]);
}
}
else
break;
}
getch();
}

Mrs. B. Lakshmi, Asst.Professor, Dept. of Computer Applications, VRSEC

CONCATENATE THE TWO STRINGS


ALGORITHM
Step 1: start
Step 2: declare and initialize a[10], b[10], c[10] as Character
array and l=0, i=0, opt as integers
Step 3: read a,b
Step 4: repeat step 5,6 until loop breaks
Step 5: read opt
Step 6: if opt==1 then
Strcpy(c,a)
strcat(c,b)
write c
Else if opt==2 then
(h)

l=strlen(a)

(i) repeat step (c) until b[i]!=\0


(j) set c[l+i]=b[i]
(k)

c[l+i]=\0

Else
Break;
Step 7: stop

Mrs. B. Lakshmi, Asst.Professor, Dept. of Computer Applications, VRSEC

PROGRAM
/* Program to concatenate two strings */
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char a[10],b[10],c[10];
int l=0,i=0,opt;
clrscr();
printf("\n Enter String1:");
scanf("%s",a);
printf("\n Enter String2:")
scanf("%s",b);
while(1)
{
printf("\n\n1.Using String Handling
Function\n2.Without using String Handling
Functions\n3.exit\nEnter your Option:");
scanf("%d",&opt);
if(opt==1)
{
strcpy(c,a);

Mrs. B. Lakshmi, Asst.Professor, Dept. of Computer Applications, VRSEC

strcat(c,b);
printf("\n\n Concatenated String:%s",c);
}
else if(opt==2)
{
l=strlen(a);
strcpy(c,a);
for(i=0;b[i]!='\0';i++)
{
c[i+l]=b[i];
}
c[i+l]='\0';
printf("\n\n Concatenated String: %s",c);
}
else
break;
}
getch();
}

Mrs. B. Lakshmi, Asst.Professor, Dept. of Computer Applications, VRSEC

COMPARE THE TWO STRINGS


ALGORITHM
Step 1: start
Step 2: declare and initialize a[10], b[10], c[10] as Character
array and l=0, i=0, opt as integers
Step 3: read a,b
Step 4: repeat step 5,6 until loop breaks
Step 5: read opt
Step 6: if opt==1 then
If(Strcmp(a,b)==0) then
Write Strings are equal
Else
write Strings are not equal
Else if opt==2 then
(l) Set i=0
(m) repeat step (c) until a[i]!=b[i]
(n)

if( a[i] ==\0 || b[i]=\0) then break loop

else set i++


(o)

if( a[i] ==\0 && b[i]=\0) then

write strings are equal


Else Strings are not equal
Else
Break;

Mrs. B. Lakshmi, Asst.Professor, Dept. of Computer Applications, VRSEC

Step 7: stop
PROGRAM
/* Program to Compare to Strings */
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char a[10],b[10];
int i=0,opt;
clrscr();
printf("\n Enter String1:")
scanf("%s",a);
printf("\n Enter String2:");
scanf("%s",b);
while(1)
{
printf("\n\n1.Using String Handling Function\n2.Without using
String Handling Functions\n3.exit\nEnter your Option:");
scanf("%d",&opt);
if(opt==1)
{
if( strcmp(a,b) == 0 )

Mrs. B. Lakshmi, Asst.Professor, Dept. of Computer Applications, VRSEC

printf("\nEntered strings are equal.\n");


else
printf("\nEntered strings are not equal.\n");
}
else if(opt==2)
{

while( a[i] == b[i] )


{
if( a[i] == '\0' || b[i] == '\0' )
break;
i++;
}
if( a[i] == '\0' && b[i] == '\0' )
printf("\nEntered strings are equal.\n");
else
printf("\nEntered strings are not equal.\n");
}
else
break;
}
getch();}

Mrs. B. Lakshmi, Asst.Professor, Dept. of Computer Applications, VRSEC

CONCEPT OF USER DEFINED FUNCTIONS


1) WRITE A C-PROGRAM TO SWAP THE TWO NUMBERS
USING CALL BY VALUE AND CALL BY REFERENCE
ALGORITHM
Step 1: start
Step 2: read x,y
Step 3: write x,y
Step 4: call swapvalue()
Step 5: write x,y
Step 6: call swapreference()
Step 7: write x,y
Step 8: stop
void swapvalue(int,int)
Step 1: declare temp
Step 2: temp = b;
b = a;
a = temp;
step 3: write a,b
step 4: stop
void swapreference(int*, int*)
Step 1: declare temp
Step 2: temp = *b;

Mrs. B. Lakshmi, Asst.Professor, Dept. of Computer Applications, VRSEC

*b = *a;
*a = temp;
step 3: write *a,*b
step 4: stop
PROGRAM
/* Program to swap the two numbers using call by value and call
by reference */
#include<stdio.h>
#include<conio.h>
void swapreference(int*, int*);
void swapvalue(int,int);
void main()
{
int x, y;
printf("Enter the value of x and y\n")
scanf("%d%d",&x,&y);
printf("\n----Swap using Call By Value----");
printf("\n\nBefore Swapping\nx = %d\ny = %d\n", x, y);
swapvalue(x, y);
printf("\nAfter Swapping\nx = %d\ny = %d\n", x, y);
printf("\n----Swap using Call By Reference----");
printf("\n\nBefore Swapping\nx = %d\ny = %d\n", x, y);
swapreference(&x, &y);

Mrs. B. Lakshmi, Asst.Professor, Dept. of Computer Applications, VRSEC

printf("\nAfter Swapping\nx = %d\ny = %d\n", x, y);


getch();
}
void swapreference(int *a, int *b)
{
int temp;
temp = *b;
*b = *a;
*a = temp;
printf("\nInside called Function\n");
printf ( "a = %d\nb = %d\n",*a,*b) ;
}
void swapvalue( int a, int b )

// x and y are formal

parameters
{
int t ;
t=a;
a=b;
b=t;
printf("\nInside called Function\n");
printf ( "a = %d\nb = %d\n", a, b) ;
}

Mrs. B. Lakshmi, Asst.Professor, Dept. of Computer Applications, VRSEC

WRITE A C-PROGRAM TO FIND THE FACTORIAL OF


THE GIVEN NUMBER USING RECURSION
ALGORITHM
Step 1: start
Step 2: Declare n, ft as integer variables
Step 3:read n
Step 4: call fact
ft=fact(n);
Step 5: write n,ft
Step 6: stop
int fact(int)
Step 1: if (a==1) then
Return 1;
Else then
Call fact return (a*fact(a-1))
Step 2: stop

PROGRAM
/* Program to find the factorial of the given number using
recursion */
#include<stdio.h>
#include<conio.h>
void main()

Mrs. B. Lakshmi, Asst.Professor, Dept. of Computer Applications, VRSEC

{
int n,ft;
int fact(int);
clrscr();
printf("\nEnter the value of n:");
scanf("%d",&n);
ft=fact(n);
printf(\n\n\tFactorial of %d = %d\n",n,ft);
getch();
}
int fact(int a)
{
if(a==1)
return 1;
else
return a*fact(a-1);

Mrs. B. Lakshmi, Asst.Professor, Dept. of Computer Applications, VRSEC

WRITE A C-PROGRAM TO FIND NCR USING FUNCTIONS


ALGORITHM
Step 1: start
Step 2: Declare n, r as integer variables and ncr as long
Step 3: Read n,r
Step 4: call find_ncr
ncr=find_ncr(n,r);
Step 5: write ncr
Step 6: stop
long find_ncr(int, int)
Step 1: declare result as long variable
Step 2: call fact
result=fact(n)/fact(r) *fact(n-r);
Step 3: return result
Step 4: stop
long fact(int)
Step 1: if (a==1) then
Return 1;
Else then
Call fact return (a*fact(a-1))
Step 2: stop

PROGRAM

Mrs. B. Lakshmi, Asst.Professor, Dept. of Computer Applications, VRSEC

/* Program to find ncr using functions */


#include<stdio.h>
#include<conio.h>
long fact(int);
long find_ncr(int, int);
void main()
{
int n, r;
long ncr;
clrscr();
printf("Enter the value of n and r\n);
scanf("%d%d",&n,&r);
ncr = find_ncr(n, r);
printf("\n\n\t%dC%d = %ld\n", n, r, ncr);
getch();
}
long find_ncr(int n, int r)
{
long result;
result = fact(n)/(fact(r)*fact(n-r));
return result;
}
long fact(int n)

Mrs. B. Lakshmi, Asst.Professor, Dept. of Computer Applications, VRSEC

{
if(n==1)
return 1;
else
return n*fact(n-1);
}

Mrs. B. Lakshmi, Asst.Professor, Dept. of Computer Applications, VRSEC

WRITE A C-PROGRAM TO FIND MEAN AND STANDARD


DEVIATION OF A GIVEN SET OF NUMBERS (Define
functions for mean and standard deviation)
ALGORITHM
Step 1: start
Step 2: Declare n as integer variables and m, x[10] as float
Step 3: read n
Step 4: set i=0;
Step 5: read x[i], set i:=i+1;
Step 6: repeat step 5 unti i<n
Step 7: call mean
m=mean(x,n);
Step 8: call sd(m,x,n);
Step 9: stop
float mean(float x[10],int n)
Step 1: declare sum=0 as float variable
Step 2: set i=0;
Step 3: Sum=sum+x[i], set i:=i+1;
Step 4: repeat step 3 unti i<n
Step 5: write sum
Step 6: return sum
Step 7: stop

Mrs. B. Lakshmi, Asst.Professor, Dept. of Computer Applications, VRSEC

void sd(float m,float x[10],int n)


Step 1: declare sum=0 as float variable
Step 2: set i=0;
Step 3: Sum=sum+pow((x[i]-m),2), set i:=i+1;
Step 4: repeat step 3 unti i<n
Step 5: sum=sum/float(n);
sum=sqrt(sum);
step 6: stop

PROGRAM
/* Program to find mean and standard deviation of a given set of
numbers */
#include<stdio.h>
#include <math.h>
#include<conio.h>
#define MAXSIZE 10
float mean(float x[10],int n);
void sd(float m,float x[10],int n);
void main()
{
float x[MAXSIZE],m;
int i, n;
clrscr();

Mrs. B. Lakshmi, Asst.Professor, Dept. of Computer Applications, VRSEC

printf("\nEnter the value of N :");


scanf("%d", &n);
printf("Enter %d real numbers \n", n);
for (i = 0; i < n; i--)
{
scanf("%f", &x[i]);
}
m=mean(x,n);
sd(m,x,n);
getch();
}
float mean(float x[],int n)
{
int i;

float sum=0
for (i=0;i<n;i++)
sum=sum+x[i];
sum=sum/(float)n;
printf("MEAN IS %5.2f\n",sum);
return sum;
}
void sd(float m,float x[10],int n)

Mrs. B. Lakshmi, Asst.Professor, Dept. of Computer Applications, VRSEC

{
float average, variance, std_deviation,sum=0;
/* Compute variance and standard deviation */
int i=0;
for (i = 0; i < n; i++)
{
sum = sum + pow((x[i] - m), 2);
}
variance = sum/ (float)n;
std_deviation = sqrt(variance);
//printf("variance of all elements = %.2f\n", variance);
printf("Standard deviation = %.2f\n", std_deviation);
}

Mrs. B. Lakshmi, Asst.Professor, Dept. of Computer Applications, VRSEC

FILE HANDLING OPERATIONS


WRITE

C-PROGRAM

TO

COUNT

NUMBER

OF

CHARACTERS, SPACES, WORDS AND LINES IN GIVEN


FILE
ALGORITHM
Step 1: start
Step 2: declare noc, now, nol as integers , fr as FILE Pointers and
fname[20], ch as character variables
Step 3: set noc=0,now=0,nol=0
Step 4: read fname
Step 5: open fname in read mode using fr pointer
fr=fopen(fname,r);
Step 6: if(fr==null) then
Write error
Exit(0);
Step 7: ch=fgetc(fr)
Step 8: repeat step 9 while (ch!=EOF)
Step 9: noc=noc+1;
If(ch== ) then
now=now+1;
if(ch==\n) then
nol=nol+1;
now=now+1;

Mrs. B. Lakshmi, Asst.Professor, Dept. of Computer Applications, VRSEC

ch=fgetc(fr)
Step 10: close file
fclose(fr);
Step 11: write now,noc,nol
Step 12: Stop
PROGRAM
/* Program to count number of characters, spaces, words and
lines in given file */
#include<stdio.h>
#include<conio.h>
void main()
{
int noc=0,now=0,nol=0;
FILE *fw,*fr;
char fname[20],ch;
clrscr();
printf("\n Enter the source file name:");
gets(fname);
fr=fopen(fname,"r");
if(fr==NULL)
{
printf("\n error \n");
exit(0);

Mrs. B. Lakshmi, Asst.Professor, Dept. of Computer Applications, VRSEC

}
ch=fgetc(fr);
while(ch!=EOF)
{
noc+;
if(ch==' ');
now++;
if(ch=='\n')
{
nol++;
now++;
}
ch=fgetc(fr);
}
fclose(fr);
printf("\n total no of character=%d",noc);
printf("\n total no of words=%d",now);
printf("\n total no of lines=%d",nol);
getch();
}

Mrs. B. Lakshmi, Asst.Professor, Dept. of Computer Applications, VRSEC

WRITE A C-PROGRAM TO COPY THE CONTENT OF ONE


FILE INTO ANOTHER FILE USING COMMAND LINE
ARGUMENTS
ALGORITHM
Step 1: start
Step 2: read argv[] from command prompt
Step 3: declare fs, ft as FILE pointers and ch as integer
Step 4: if(argc!=3) then
Write Invalid no of arguments
Return 1
Step 5: Open file in read mode
fs=fopen(argv[1],r)
Step 6: if(fs==null) then
Write Cant find source file
Return 1
Step 7: Open file in write mode
ft=fopen(argv[2],w)
Step 8: if(ft==null) then
Write Cant find target file
Close source file: fclose(fs)
Return 1
Step 9: Repeat step 12 until loop breaks
step 10: ch=fgetc(fs)

Mrs. B. Lakshmi, Asst.Professor, Dept. of Computer Applications, VRSEC

if(feof(fs)) then break


fputc(ch,ft)
Step 11: fclose(fs)
fclose(ft)
Return 0
Step 12: Stop

PROGRAM
/* Program to copy the content of one file into another file using
command line arguments */
#include<stdio.h>
#include<conio.h>
int main(int argc,char *argv[])
{
FILE fs,*ft;
int ch;
if(argc!=3)
{
printf("Invalide numbers of arguments.");
return 1;
}
fs=fopen(argv[1],"r");
if(fs==NULL)

Mrs. B. Lakshmi, Asst.Professor, Dept. of Computer Applications, VRSEC

{
printf("Can't find the source file.");
return 1;
}
ft=fopen(argv[2],"w");
if(ft==NULL)
{
printf("Can't open target file.");
fclose(fs);
return 1;
}

while(1)
{
ch=fgetc(fs);
if (feof(fs)) break;
fputc(ch,ft);
}
fclose(fs);
fclose(ft);
return 0;
}

Mrs. B. Lakshmi, Asst.Professor, Dept. of Computer Applications, VRSEC

POINTERS
1) WRITE A C-PROGRAM TO IMPLEMENT ARITHMETIC
OPERATIONS USING POINTERS
ALGORITHM
Step 1: start
Step 2: declare: n1, n2 as integer data type and *x,*y as integer
type pointers
Step 3: read 2 numbers: n1, n2
Step 4: Set x=&n1 and y=&n2
Write *x+*y
Write *x-*y
Write *x x *y
Write *x/*y
Write *x%*y
Step 5: stop

PROGRAM
/* Program to implement arithmetic operations using pointers */
#include <stdio.h>

/* printf() , scanf() functions */

#include <conio.h>

/* clrscr(), getch() */

void main()
{

Mrs. B. Lakshmi, Asst.Professor, Dept. of Computer Applications, VRSEC

int n1,n2,*x,*y;
clrscr();

/* 2 integer variables */
/* clears the screen */

printf("Enter 2 numbers : "); /* prints "Enter 2 numbers :


" on the screen */
printf("\n n1= ");
scanf("%d",&n1);

/* reads an integer value to n1 */

printf("\n n2= ");


scanf("%d",&n2);
x=&n1;
y=&n2;

/* reads an integer value to n2 */

printf("\n Addition : %d + %d = %d ",*x,*y, x+y);


printf("\n Subtraction : %d - %d = %d ",*x,*y,*x-*y);
printf("\n Multiplication : %d * %d = %d ",*x,*y,*x**y);
printf("\n Division : %d / %d = %d ",*x,*y,*x/(*y));
printf("\n Modulus : %d %% %d = %d ",*x,*y,*x%*y);
getch();
}

Mrs. B. Lakshmi, Asst.Professor, Dept. of Computer Applications, VRSEC

2) WRITE A C-PROGRAM TO SORT NAMES IN ASCENDING


ORDER USING FUNCTIONS
ALGORITHM
Step 1: start
Step 2: read str
Step 3: set i=0
Step 4: repeat step 5 until i<n
Step 5: read x[i]
Step 6: call reorder(,x)
Step 7: set i=0
Step 8: repeat step 9 until i<n
Step 9: write x[i]
Step 10: stop

void reorder(int n,char *x[])


Step 1: set i=0
Step 2: repeat step 3, 4 until i<n
Step 3: set j=i+1
Step 4: repeat step 5 until j<n
Step 5: if(strcmp(x[i],x[j]>0) then
strcpy(t,x[j])
strcpy(x[j],x[i])

Mrs. B. Lakshmi, Asst.Professor, Dept. of Computer Applications, VRSEC

strcpy(x[i],t)
Step 6: stop
PROGRAM
/* Program to sort names in ascending order using functions */
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char *x[20];
int i,n=0;
void reorder(int n,char *x[]);
clrscr();
printf("Enter no. of String : ")
scanf("%f",&n);
printf("\n");
for(i=0;i<n;i++)
{
printf("Enter the Strings %d : ",i+1);
/* allocating memory to array element */
x[i]=(char *)malloc(20*sizeof(char));
scanf("%s",x[i]);
}

Mrs. B. Lakshmi, Asst.Professor, Dept. of Computer Applications, VRSEC

reorder(n,x);
printf("\nreorder list is : \n");
for(i=0;i<n;i++)
{
printf("%d %s\n",i+1,x[i]);
}
getch();
}
void reorder(int n,char *x[])
{
int i,j;
char t[20];
for(i=0;i<n-1;i++)
for(j=i+1;j<n;j++)
if(strcmp(x[i],x[j])>0)
{
strcpy(t,x[j]);
strcpy(x[j],x[i]);
strcpy(x[i],t);
}
return;
}

Mrs. B. Lakshmi, Asst.Professor, Dept. of Computer Applications, VRSEC

1. USER DEFINED DATA TYPES


1) WRITE A C-PROGRAM TO CREATE A NEW DATA TYPE
CALLED AS STUDENT (READ AND PRINT DETAILS OF
STUDENT)
ALGORITHM
Step 1: start
Step 2: declare Structure
Step 3: read sno, name, gen and marks using structure variable s
Step 4: write sno, name, gen and marks
Step 5: stop
PROGRAM
/* Program to create a new data type called as student */
#include<stdio.h>
#include<conio.h>
#include<string.h>
struct Student
{
int sno;
char name[20],gen;
float marks;
};
void main()

Mrs. B. Lakshmi, Asst.Professor, Dept. of Computer Applications, VRSEC

{
struct Student s;
clrscr();
printf("\nEnter Student number : ");
scanf("%d",&s.sno);
printf("\nEnter Student name

: ");

fflush(stdin);
gets(s.name);
printf(\nEnter gender

: ");

scanf("%c",&s.gen);
printf("\nEnter Marks

: ");

scanf("%f",&s.marks);
clrscr()
printf("\t\t\t STUDENT DETAILS \n\n");
printf("\nStudent Number
printf("\nStudnet Name

: %d",s.sno);
: %s",s.name);

printf("\nGender

: %c",s.gen);

printf("\nMarks

: %.2f",s.marks);

getch();
}

Mrs. B. Lakshmi, Asst.Professor, Dept. of Computer Applications, VRSEC

WRITE A C-PROGRAM TO PRINT DAYS IN A WEEK USINF


ENUMERATION
ALGORITHM
Step 1: start
Step 2: Declare enumerated operator days
Step 3: read num
Step 4: weekday=num
Step 5: call display(weekday)
Step 6: stop
Void Display(enum days weekday)
Step 1: switch(weekday)
Step 2: case '1':

Write Monday

Step 3: case '2':

Write Tuesday

Step 4: case '3':

Write Wednesday

Step 5: case '4':

Write Thursday

Step 6: case '5':

Write Friday

Step 7: case '6':

Write Saturday

Step 8: case '7':

Write Sunday

Step 9: default:

Invalid operation"

Step 10: Repeat the Steps 3,4,5,6,7,8,9,10 until


while(sign != 'Q' && sign != 'q')
Step 11: Sto
PROGRAM

Mrs. B. Lakshmi, Asst.Professor, Dept. of Computer Applications, VRSEC

/* Program to print days in a week usinf enumeration */


#include<stdio.h>
#include <conio.h>
enum days {
Monday=1, Tuesday, Wednesday,
Thursday, Friday, Saturday, Sunday
} weekday;
void Display(enum days);
void main(void)
{
int num;
printf("Enter an integer for the day of the week. "
"Mon=1,...,Sun=7\n");
scanf("%d", &num);
weekday=num;
Display(weekday)
getch();
}
void Display(enum days weekday)
{
switch (weekday)
{
case Monday:

Mrs. B. Lakshmi, Asst.Professor, Dept. of Computer Applications, VRSEC

printf("Today is a Monday.\n");
break;
case Tuesday
printf("Today is a Tuesday.\n");
break;
case Wednesday:
printf("Today is a Wednesday.\n");
break;
case Thursday:
printf("Today is a Thursday.\n");
break;
case Friday:
printf("Today is a Friday.\n");
break;
case Saturday:
printf("Today is a Saturday.\n");
break;
case Sunday:
printf("Today is a Sunday.\n");
break;
default:
printf("Invalid Option\n");
}

Mrs. B. Lakshmi, Asst.Professor, Dept. of Computer Applications, VRSEC

2. STRUCTURES
1) WRITE A C-PROGRAM TO READ NAME OF THE
STUDENT, ROLLNUMBER AND MARKS OBTAINED IN
SUBJECTS FROM KEYBOARD AND PRINT NAME OF
STUDENT, ROLLNUMBER, MARKS IN 3 SUBJECTS AND
TOTAL MARKS BY USING STRUCTURES CONCEPT
ALGORITHM
Step 1: start
Step 2: declare Structure
Step 3: read sno, name, marks using structure variable s
Step 4: calculate total =s.m1+s.m2+s.m3
Step 5: write sno, name, marks and total
Step 6: stop
PROGRAM
/* Program to read name of the student, rollnumber and marks
obtained in subjects from keyboard and print name of student,
rollnumber, marks in 3 subjects and total marks by using
structures concept */
#include<stdio.h>
#include <conio.h>
#include <string.h>
struct Student

Mrs. B. Lakshmi, Asst.Professor, Dept. of Computer Applications, VRSEC

{
int sno;
char name[20];
float m1,m2,m3;
};
void main()
{
struct Student s;
clrscr();
printf("\nEnter Student number : ");
scanf("%d",&s.sno);
printf("\nEnter Student name

: ");

fflush(stdin);
gets(s.name);
printf("\nEnter Marks in Subject 1: ");
scanf("%f",&s.m1);
printf("\nEnter Marks in Subject 2: ");
scanf("%f",&s.m2);
printf("\nEnter Marks in Subject 3: ");
scanf("%f",&s.m3);
clrscr();
printf("\t\t\t STUDENT DETAILS \n\n");
printf("\nStudent Number

: %d",s.sno);

Mrs. B. Lakshmi, Asst.Professor, Dept. of Computer Applications, VRSEC

printf("\nStudnet Name

: %s",s.name);

printf("\nMarks in Subject1

: %.2f",s.m1);

printf("\nMarks in Subject2

: %.2f",s.m2);

printf("\nMarks in Subject3

: %.2f",s.m3);

printf("\nTotal Marks

: %.2f",s.m1+s.m2+s.m3);

getch();
}

Mrs. B. Lakshmi, Asst.Professor, Dept. of Computer Applications, VRSEC

WRITE A C-PROGRAM TO READ NAME OF THE


STUDENT, ROLLNUMBER AND MARKS OBTAINED IN
SUBJECTS FROM KEYBOARD AND PRINT NAME OF
STUDENT, ROLLNUMBER, MARKS IN 3 SUBJECTS AND
TOTAL MARKS BY USING ARRAYS OF STRUCTURES
CONCEPT
ALGORITHM
Step 1: start
Step 2: declare Structure
Step 3: read n
Step 4: set i=0
Step 5: repeat step 6 until i<n
Step 6: read sno, name, marks using structure variable s[i]
Step 7: set i=0
Step 8: repeat step 9,10 until i<n
Step 9: calculate total =s[i].m1+s[i].m2+s[i].m3
Step 10: write sno, name, marks and total of s[i]
Step 11: stop

Mrs. B. Lakshmi, Asst.Professor, Dept. of Computer Applications, VRSEC

PROGRAM
/* Program to read name of the student, rollnumber and marks
obtained in subjects from keyboard and print name of student,
rollnumber, marks in 3 subjects and total marks by using arrays
of structures concept */
#include<stdio.h>
#include <conio.h>
#include <string.h>
struct Student
{
int sno;
char name[20];
float m1,m2,m3;
};
void main()
{
struct Student s[10];
int n,i;
clrscr();
printf("Enter Number of Students:");
scanf("%d",&n);
for(i=0;i<n;i++)
{

Mrs. B. Lakshmi, Asst.Professor, Dept. of Computer Applications, VRSEC

printf("Enter Details of Student %d",i+1);


printf("\nEnter Student number : ");
scanf("%d",&s[i].sno);
printf("\nEnter Student name

: ");

fflush(stdin);
gets(s[i].name);
printf("\nEnter Marks in Subject 1: ");
scanf("%f",&s[i].m1);
printf("\nEnter Marks in Subject 2: ");
scanf("%f",&s[i].m2);
printf("\nEnter Marks in Subject 3: ");
scanf("%f",&s[i].m3);
}
clrscr();
for(i=0;i<n;i++)
{
printf("\n\n STUDENT %d \n",i+1);
printf("\nStudent Number
printf("\nStudnet Name

: %d",s[i].sno);
: %s",s[i].name);

printf("\nMarks in Subject1

: %.2f",s[i].m1);

printf("\nMarks in Subject2

: %.2f",s[i].m2);

printf("\nMarks in Subject3

: %.2f",s[i].m3);

Mrs. B. Lakshmi, Asst.Professor, Dept. of Computer Applications, VRSEC

printf("\nTotal Marks

%.2f",s[i].m1+s[i].m2+s[i].m3);
}
getch();
}

Mrs. B. Lakshmi, Asst.Professor, Dept. of Computer Applications, VRSEC

Вам также может понравиться