Sei sulla pagina 1di 28

ASSIGNMENT 1:

TITLE:
WRITE A PROGRAM TO FIND OUT IF THE
GIVEN NUMBER IS PALINDROME OR NOT.
ALGORITHM:
step 1 : input n
step 2 : s = 0, a=n
step 3 : while(n>0)
begin
rem=n%10
s=s*10+rem
n=n/10
end
step 4 : if(s==a)
print 'it is a palindrome'
else
print 'it is not a palindrome'
step 5 : stop

/* FIND OUT IF THE GIVEN NUMBER IS


PALINDROME*/
#include<stdio.h>
#include<conio.h>
1

void main()
{
int copy, number, sum=0, remainder;
clrscr();
printf("FIND OUT IF THE GIVEN NUMBER IS
PALINDROME\n\n\n");
printf ("please enter a number:=\n");
scanf("%d",&number);
copy=number;
while(number>0)
{
remainder=number%10;
sum=sum*10+remainder;
number=number/10;
}
if (copy==sum)
printf ("the number %d is palindrome",copy);
else
printf ("the number %d is not palindrome",copy);

getch();
2

}
OUTPUT:
please enter a number:= 121
the number 121 is palindrome.
please enter a number:= 123
the number 123 is not palindrome.

ASSIGNMENT 2:
TITLE:
WRITE A PROGRAM IN C TO FIND OUT SUM OF
DIGIT OF THE GIVEN NUMBER USING WHILE
LOOP.
3

ALGORITHM:
Step 1: Input N
Step 2: Sum = 0
Step 3: While (N != 0)
Rem = N % 10;
Sum = Sum + Rem;
N = N / 10;
Step 4: Print Sum

/* FIND OUT SUM OF THE DIGIT OF THE GIVEN NUMBER*/


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

void main()
{
int copy, number, sum=0, remainder;
clrscr();
printf("FIND OUT SUM OF THE DIGIT OF THE GIVEN
NUMBER\n\n\n");
printf ("Please enter a number:=\n");
scanf("%d",&number);
copy=number;
while(number>0)
{
remainder=number%10;
sum=sum+remainder;
number=number/10;
}
printf ("The sum of the digit of number %d is
%d",copy,sum);
getch();
}

OUTPUT:
5

Please enter a number:= 123


The sum of the digit of number 123 is 6
Please enter a number:= 435
The sum of the digit of number 435 is 12.

ASSIGNMENT 3:
6

TITLE:
WRITE A MENU DRIVEN C PROGRAM FOR
IMPLEMENTING CALCULATOR.

ALGORITHM:
Step-1 start
Step-2 display menu
Step-3 read the two variable a and b
Step-4 enter the option code
Step-5 evaluate option code with case statements
Step-5.1 case =1 c=a+b print c. goto step-7
Step 5.2 case=2 c=a-b print c. goto step-7
Step 5.3 case=3 c=a*b print c. goto step-7
Step 5.4 case=4 c=a/b print c. goto step-7
Step 5.5 case=5 c=0 exit
Step-6
entered case option is invalid code the print invalid
operation code
Step-7 stop

/* MENU DRIVEN PROGRAM FOR IMPLEMENTING A


CALCULATOR*/
7

#include<stdio.h>
#include<conio.h>
void main()
{
int number1, number2,
add,subtraction,multiplication,choice,ch,division;
clrscr();
printf ("MENU DRIVEN PROGRAM FOR IMPLEMENTING A
CALCULATOR\n");
printf("Please enter two numbers:\n");
scanf("%d %d",&number1,&number2);
do
{
printf("Please enter the choice:");
printf("\t 1.For addition\n");
printf("\t 2.For subtraction\n");
printf("\t 3.For multiplication\n");
printf("\t 4.For division\n");
scanf("%d",&choice);
switch(choice)
{
8

case 1:
add=number1+number2;
printf("\n the addition of %d + %d =
%d\n",number1,number2,add);
break;
case 2:
if (number1 > number2)
{
subtraction=number1-number2;
printf("\n the subtraction of %d - %d =
%d\n",number1,number2,subtraction);
}
else
{
subtraction=number2-number1;
printf("\n the subtraction of %d - %d =
%d\n",number2,number1,subtraction);
}
break;
case 3:
multiplication= number1 * number2;

printf("\n the multiplication of %d x %d =


%d\n",number1,number2,multiplication);
break;
case 4:
if (number1 > number2)
{
division=number1 / number2;
printf("\n the division of %d / %d =
%d\n",number1,number2,division);
}
else
{
division=number2/number1;
printf("\n the division of %d / %d =
%d\n",number2,number1,division);
}
break;
default:
printf("wrong choice");
}
printf("press 1 to calculate again");

10

scanf("%d",&ch);
}
while (ch==1);
getch();
}
OUTPUT:
MENU DRIVEN PROGRAM FOR IMPLEMENTING A
CALCULATOR
Please enter two numbers:
65
Please enter the choice:
1.For addition
2.For subtraction
3.For multiplication
4.For division
1
the addition of 6+ 5 = 11
press 1 to calculate again
1
Please enter the choice:
1.For addition
11

2.For subtraction
3.For multiplication
4.For division
2
the subtraction of 6 - 5 = 1
press 1 to calculate again
1
Please enter the choice:
1.For addition
2.For subtraction
3.For multiplication
4.For division
3

the multiplication of 6 x 5 = 30
press 1 to calculate again
1
Please enter the choice:
1.For addition
2.For subtraction
3.For multiplication
12

4.For division
4

the division of 6 / 5 = 1

ASSIGNMENT 4:
13

TITLE:
WRITE A PROGRAM WHICH REPRESENT
FIBONACCI SERIES.

/* FIBONACCI SERIES*/
#include<stdio.h>
#include<conio.h>
int fib( int term);
void main()
{
int x=0, y=1, term1;
clrscr();
printf("Please enter the term upto which step
you want to see:\n");
scanf ("%d",&term1);
printf("The fibonacci series =");
printf("%d\t%d",x,y);
fib(term1);
getch();
}
int fib( int term)
{
int a=0, b=1, fibonacci1,i;
for (i=1;i<=term-2;i++)
{
fibonacci1=a+b;
a=b;
b=fibonacci1;
printf("\t%d",fibonacci1);
14

}
}
OUTPUT:
Please enter the term upto which step you want
to see: 5
The fibonacci series = 0 1 1 2 3

ASSIGNMENT 5:
TITLE:
15

TO FIND THE FACORIAL OF A NUMBER USING


RECURSION.
ALGORITHM:

step 1. Start
step 2. Read the number n
step 3. [Initialize]
i=1, fact=1
step 4. Repeat step 4 through 6 until i=n
step 5. fact=fact*i
step 6. i=i+1
step 7. Print fact
step 8. Stop

/*FIND THE FACTORIAL OF A NUMBER*/


#include<stdio.h>
#include<conio.h>
int fact(int number);
void main()
16

{
int number1,result;
clrscr();
printf("please enter the number");
scanf("%d",&number1);
result=fact(number1);
printf("the factorial of %d is
%d",number1,result);
getch();
}
int fact(int number)
{
if (number==1)
return 1;
else
return number*fact(number-1);
}
OUTPUT:
please enter the number
6
the factorial of 6 is 720.

ASSIGNMENT 6:
TITLE:
17

WRITE A PROGRAM TO IMPLEMENT CALL BY


REFERENCE.
/* ADDITION USING CALL BY REFERENCE*/
#include<stdio.h>
#include<conio.h>
int add( int i, int j);
void main()
{
int number1, number2,sum;
clrscr();
printf("please enter two numbers:\n");
scanf("%d%d",&number1,&number2);
sum=add(number1,number2);
printf("the sum of %d and %d is
%d\n",number1,number2,sum);
getch();
}
int add( int i, int j)
{
int sum1;
sum1=i + j;
return(sum1);
}
OUTPUT:
please enter two numbers: 3 7
the sum of 3 and 7 is 10.

ASSIGNMENT 7:
TITLE:
18

WRITE A PROGRAM IN C TO IMPLEMENT


MATRIX ADDITION.

ALGORITHM:
write a program to perform matrix addition.
ALGORITHM: Step-1 Start the program.
Step-2
Enter the row and column of the matrix.
Step-3
Enter the elements of the a matrix.
Step-4
Enter the elements of the b matrix.
Step-5
Print the a matrix in the matrix form.
Step-6
Print the b matrix in the matrix form.
Step-7
Set a loop up to the row.
Step-8
Set a inner loop up to the column
Step-9
Add the elements of a and b in column w
ise and store the result in c matrix.
Step-10
After the execution of the two loops. Print the
value oh c matrix.
Step-11
Stop

/*MATRIX ADDITION*/
#include<stdio.h>
#include<conio.h>
19

#include<math.h>
void main()
{
int a[3][3],b[3][3],c[3][3],i,j;
clrscr();
printf("please enter the value for first 3x3
matrix\n");
for(i=0;i<=2;i++)
{
printf("\n");
for (j=0;j<=2;j++)
scanf("\t%d",&a[i][j]);
}
printf("please enter the value for Second 3x3
matrix\n");
for(i=0;i<=2;i++)
{
for (j=0;j<=2;j++)
scanf("\t%d",&b[i][j]);
}
for(i=0;i<=2;i++)
{
for (j=0;j<=2;j++)
c[i][j]=a[i][j]+b[i][j];
}
printf("sum of the matrix is");
for(i=0;i<=2;i++)
{
printf("\n");
for (j=0;j<=2;j++)
printf("\t%d",a[i][j]);
}
printf("\n+");
{
for(i=0;i<=2;i++)
20

{
printf("\n");
for (j=0;j<=2;j++)
printf("\t%d",b[i][j]);
}
}
for(i=0;i<=2;i++)
{
printf("\n=");
for (j=0;j<=2;j++)
printf("\t%d",c[i][j]);
}
getch();
}

OUTPUT: please enter the value for first 3x3


matrix 1 2 3
1 2 3
1 2 3
please enter the value for Second 3x3 matrix
1 2 3
1 2 3
1 2 3

sum of the matrix is


1 2 3
21

1 2 3
1 2 3
+
1 2 3
1 2 3
1 2 3
=
2 4 6
2 4 6
2 4 6

22

ASSIGNMENT 8:
TITLE:
WRITE A PROGRAM IN C WHICH IMPLEMENT STRCPY,
STRCAT AND STRLEN.

/* IMPLEMENTATION OF STRCPY,STRCAT AND STRLEN IN


STRING*/
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char str1[10]="hello";
char str2[10]="world";
char str3[20];
int len;
clrscr();
printf("str1 %s\n",str1);
printf("str2 %s\n",str2);
strcpy (str3,str1);
printf("After using strcpy command the string in
str3 is HELLO
\n",str3);
strcat(str1,str2);
printf("strcat (str1,str2)%s\n",str1);
len=strlen(str1);
printf("string=%s length=%d\n",str1,len);
getch();
}
23

OUTPUT:
STR1 HELLO
STR2 WORLD
After using strcpy command the string in str3 is
HELLO.
strcat (str1,str2) IS HELLOWORLD.
String HELLOWORLD length=10

24

ASSIGNMENT 9
TOPIC:

ILLUSTRATE THE METHOD SENDING AND


ENTIRE THE STRUCTURE AS A PARAMETER TO
A FUNCTION.
/*PASSING THE COPY OF ENTIRE STRUCTURE*/
struct stores
{
char name[20];
float price;
int quantity;
};
struct stores update(struct stores product, float p,
int q);
float mul (struct stores stock);
void main()
{
float p_increment, value;
int q_increment;
struct stores item = ("xyz",25.75, 12);
printf("\ninput increment values:");
printf(" price increment and quantity increment:\n");
scanf("%f%d",&p_increment,&q_increment);
item= update(item, p_increment, q_increment);
printf("update values of item\n\n");
printf ("name : %s\n",item.name);
printf("price : %f\n",item.price);
printf("quantity : %d\n",item.quantity);
value=mul(item);
printf("\n value of the item= %f\n",value);
}

25

struct stores update(struct stores product, float p,


int q);
{
product.price+= p;
product.quantity+=q;
return(product1);
}
float mul(struct stores stock)
{
return(stock.price*stock.quantity);
}

OUTPUT:
Input increment values: price increment and quantity
increment 10 12
Updated values of item
Name : xyz
Price :35.750000
Quantity :24
Value of the item = 858.000000

26

ASSIGNMENT 10
TOPIC:
PROGRAM TO TEST THE FILE HANDLING
FEATURES ON THIS SYSTEM.
/* PROGRAM TO TEST THE FILE HANDLING FEATURES ON THIS
SYSTEM*/
#include<stdio.h>
void main()
{
FILE *fp;
char ch;
printf("data input\n\n");
fp=fopen("input","r");
while((ch =getchar()) !=EOF)
putc(ch,fp);
fclose(fp);
printf("\n data output\n\n");
fp=fopen("input","r");
while ((ch=getc(fp)) !=EOF)
printf("%c",ch);
fclose(fp);
}

OUTPUT:
Data input
27

This is a program to test the file handling features on


this system
Data output:
This is a program to test the file handling features on
this system

28