solved programs class-: b.tech-i year subject -: computer ... · developed by pawan agarwal,...

35
Developed by Pawan Agarwal, Computer Engg. Dept., Hindustan College of Sc. & Tech. Solved Programs Class-: B.Tech-I year Subject -: Computer Programming /. Write a program to print the word "Hello" on the screen. //Program to print Hello #include<stdio.h> #include<conio.h> void main() { clrscr(); printf("Hello"); getch(); } 2. Write a program to add two numbers given by the user //program to add two numbers given by the user #include<stdio.h> #include<conio.h> void main() { int a,b,c; clrscr(); printf("Enter the two numbers"); scanf("%d%d",a,b); c=a+b; printf("The sum of two numbers is %d",c); getch(); } 3. Write a program to swap two numbers. //program to swap two numbers #include<stdio.h> #include<conio.h> void main() { int a,b,c; clrscr(); a=10; b=20; printf("value of a =%d and b=%d before swapping",a,b); c=a; a=b; b=c; printf("value of a =%d and b=%d after swapping",a,b); getch(); } 1

Upload: hatram

Post on 04-Apr-2018

213 views

Category:

Documents


1 download

TRANSCRIPT

Developed by Pawan Agarwal, Computer Engg. Dept., Hindustan College of Sc. & Tech.

Solved Programs Class-: B.Tech-I year

Subject -: Computer Programming

/. Write a program to print the word "Hello" on the screen.

//Program to print Hello #include<stdio.h> #include<conio.h>

void main() {

clrscr(); printf("Hello"); getch();

}

2. Write a program to add two numbers given by the user

//program to add two numbers given by the user #include<stdio.h> #include<conio.h> void main() {

int a,b,c; clrscr(); printf("Enter the two numbers"); scanf("%d%d",a,b); c=a+b; printf("The sum of two numbers is %d",c); getch();

}

3. Write a program to swap two numbers.

//program to swap two numbers #include<stdio.h> #include<conio.h> void main() {

int a,b,c; clrscr(); a=10; b=20; printf("value of a =%d and b=%d before swapping",a,b); c=a; a=b; b=c; printf("value of a =%d and b=%d after swapping",a,b); getch(); }

1

Developed by Pawan Agarwal, Computer Engg. Dept., Hindustan College of Sc. & Tech.

4. Write a program to find the Simple interest.

II Calculation of simple interest by p, n and r #include<stdio.h> #include<conio.h> void main () {

int p, n, count; float r, si; clrscr(); printf ( "Enter values of p, n, and r " ); scanf ("%d %d %f', &p, &n, &r); si = p * n * r / 1 0 0 ; printf ("Simple Interest = Rs.%f\n", si) ;

getch(); }

5. Write a program to add your date of birth number.fEx. 28-09-1980, 2+8+0+9+1+9+8+0=37 = 10 =1)

II addition of the date of birth #include<stdio.h> #include<conio.h> void main() {

int month,year,day; int a,b,c; clrscr(); printf("enter the day, month and yearin two digits"); scanf("%d%d%d",&day,&month,&year); a=month/10; b=month%10; c=year/10; d=year%10; e=day/10; f=day%10; g=a+b+c+d+e+f; printf(" Sum of the date of birth is %d",g); getch();

}

2

Developed by Pawan Agarwal, Computer Engg. Dept., Hindustan College of Sc. & Tech.

6. Write a Prosram to find the greatest of three numbers

II find the addition of three numbers #include<stdio.h> #include<conio.h> void main() { int a,b,c; clrscr(); printf("Enter the first number : "); scanf("%d",&a); printf("Enter the second number : "); scanf("%d",&b); printf(" Enter the third number : "); scanf("%d",&c); if(a>b && a>c) printf("nt The greatest number is : %d ",a); if(b>a && b>c) printf("nt The greatest number is : %d ",b); if(c>a && Ob) printf("The greatest number is : %d ",c); getch(); }

7. Write Program to convert the Decimal to binary and binary to decimal

II program to convert decimal to binary andbinary to decimal #include<stdio.h> #include<conio.h> #include<math.h> void main() {

int number, result,ch,x,y; int a,b,c,sum,rs; x=10;y=10; clrscr();

printf("press 1-to convert decimal to binary and 2- for binary to decimal\n"); scanf("%d",&ch); switch(ch) {

case 1: printf("Enter the decimal numberW); scanf("%d",&number); while(number>= 1) { gotoxy(x,y);

if(number==l) {

printf("%d",number); break;

} else {

result=number%2; number=number/2; printf("%d",result); x-;

3

Developed by Pawan Agarwal, Computer Engg. Dept., Hindustan College of Sc. & Tech.

} } break;

case 2: printf("Enter the binary number\n"); scanf("%d",&number); sum=0,x=0; while(number>0) {

if(number<=9) {

rs=number*pow(2,x); sum=sum+rs; break;

} c=number%10; rs=c*pow(2,x); sum=sum+rs; number=number/10; x++;

} printf("answer is =%d\n",sum); break;

default: printf("You have entered wrong number\n"); }

getch(); }

8. Write a program to display the Division based on the percentage entered by the user

II program to division based on percentage entered by user #include<stdio.h> #include<conio.h> #include<math.h>

void main() {

int per; printf("enter the Percentage of the student"); scanf("%d",&per); if(per>=75) {

printf("Distinction'); } else {

if(per>=60) {

printf("First Division"); }

4

Developed by Pawan Agarwal, Computer Engg. Dept., Hindustan College of Sc. & Tech.

else {

if(per>=40) {

printf("Second division"); } else {

printf("Fail"); } } } }

9. Write a program to evaluate the addition, subtraction, multiplication and division functions based on the input given by the user (by switch statement)

II program to evaluate add, sub,mul,div based on user choicce #include<stdio.h> #include<conio.h> void main() {

int a,b,c,ch; printf("press 1- addition, 2-subtraction, 3- multiply, 4-div, 5-exit"); scanf("%d",&ch); printf("enter two numbers"); scanf("%d%d",&a,&b); switch(ch) {

case 1: c=a+b; break;

case 2: c=a-b; break;

case 3: c=a*b; break;

case 4: c=a/b; break; case 5: exit(l); default:

printf("You have entered wrong choice") exit(l);

}

printf("Answer is %d",c); }

5

Developed by Pawan Agarwal, Computer Engg. Dept., Hindustan College of Sc. & Tech.

10. Write a program to print tables from 1 to 10

II program to print tables from 1 to 10 #include<stdio.h> #include<conio.h> void main() {

int a,b,c; for(a=l;a<ll;a++) {

for(b=l;b<ll;b++) {

c=a*b; printf("\t%d",c);

} printf("\n"); } }

//. Write a program to find whether the given number is prime or not

II program to find whether the given number is prime or not #include<stdio.h> #include<conio.h>

void main() {

int a,c=0,i,n; clrscr(); printf("enter the number to be checked\n"); scanf("%d",&n); for(i=l;i<=n;i++) {

a=n%i; if(a==0) {

c=c+l; } }

if(c==2) {

printf("the given number is prime"); } else

{ printf("the given number is not prime");

} getch();

6

Developed by Pawan Agarwal, Computer Engg. Dept., Hindustan College of Sc. & Tech.

12. Write a program to find the factorial of a given number

II program to find whether the given number is prime or not #include<stdio.h> #include<conio.h> void main() {

int i=l,f=l,num; clrscr(); printf("\nEnter a number:"); scanf("%d",&num); while(i<=num) {

f=f*i; i++;

} printf("\nFactorial of %d is:%d",num,f); getch(); }

13. Write a program to print the following figure

* ** *** ****

// program to print the pattern #include<stdio.h> #include<conio.h>

void main() {

int i,j,value; printf("Enter how many lines you want to print"); scanf("%d",&value); for(i= 1 ;i<=value;i++) {

forG=l;j<=i;j++) {

printf("*"); } printf("\n");

} }

7

Developed by Pawan Agarwal, Computer Engg. Dept., Hindustan College of Sc. & Tech.

14. Write a program to print the following figure 1 12 123 1234 12345

// program to print the pattern #include<stdio.h> #include<conio.h>

void main() {

int i,j,value; clrscr(); printf("Enter how many lines you want to print"); scanf("%d",&value); for(i= 1 ;i<=value;i++) {

for(]=l;j<=i;j++) {

printf("%d",j); } printf("\n");

} getch();

}

15. Write a program to print the following figure

*

// program to print the pattern #include<stdio.h> #include<conio.h> void main() {

int i,j,l,k, value; clrscr(); printf("Enter how many lines you want to print"); scanf("%d",&value); 1=1; for(i= 1 ;i<=value;i++) {

for(k=l;k<39-i;k++) {

printf(" "); } for(]=l;]<=l;]++) {

8

Developed by Pawan Agarwal, Computer Engg. Dept., Hindustan College of Sc. & Tech.

printer*"); } printf("\n"); 1=1+2;

} getch();

}

16. Write a program to find the length of the string

II program to find the length of the string #include<stdio.h> #include<conio.h> void stringlen(char array[]); void main() {

char string[20]; clrscr(); printf("Enter the string\n"); scanf("%s",string); stringlen(string); getch(); }

void stringlen(char array[]) {

int a=0; while(array[a]!='\0') {

a++; } printf( "Length of the string is %d",a);

}

17. Write a program to copy one string into another

II program to find the length of the string #include<stdio.h> #include<conio.h>

void stringcopy(char array[]);

void main() {

char string[20]; clrscr(); printf("Enter the string\n"); scanf("%s",string); stringcopy (string); getch();

}

9

Developed by Pawan Agarwal, Computer Engg. Dept., Hindustan College of Sc. & Tech.

void stringcopy(char array []) {

int a=0; char array 1 [20]; while(array[a]!='\0') {

array 1 [a]=array[a]; a++;

} array l[a]='\0';

printf("contents of second array is %s",arrayl);

18. Write a program to concate two strings

II program to find concate the two strings #include<stdio.h> #include<conio.h>

void stringconcat(char source []); void main() {

char string[20]; clrscr(); printf("Enter the source string content\n"); scanf("%s",string); stringconcat(string); getch(); }

void stringconcat(char source []) {

int a=0,b=0; char target[40]="Hello" ; while(target[a]!='\0') {

a++; } target[a]=''; a=a+l; while(source[b]!='\0') {

target[a]=source[b]; b++; a++; }

printf("contents of Target string is %s",target);

10

Developed by Pawan Agarwal, Computer Engg. Dept., Hindustan College of Sc. & Tech.

19. Write a program to reverse the string

II program to reverse the contents of string #include<stdio.h> #include<conio.h>

void stringrev(char source []); void main() {

char string[20]; clrscr();

printf("Enter the source string content\n"); scanf("%s",string); stringrev( string); getch(); }

void stringrev(char source []) {

int a=0,b=0; chartarget[20]; while(source[a] !='\0') {

a++; } a=a-l; while(a>=0) {

target[b]=source[a]; b++; a-;

} target[b]='\0';

printf("Reverse of Source string is %s",target); }

20. Write a program to find whether the given string is palindrome or not

II program to check whether the string is palindrome or not #include<stdio.h> #include<conio.h>

int stringpal(char source[]); void main() {

char string[20]; clrscr(); printf("Enter the source string content\n"); scanf("%s",string);

11

Developed by Pawan Agarwal, Computer Engg. Dept., Hindustan College of Sc. & Tech.

stringpal(string); getch(); }

int stringpal(char source[]) {

int ij; chartarget[20]; for (i=0;source[i]!='\0';i++) {

}

for (j=0;source[j]!-\0';j++,i~) {

if (source[i] !=source[j]) {

printf("string is not palindrome"); return(O); } }

printf("string is palindrome"); return(O);

21. Write a program to find the greatest of the 10 numbers in the integer array

II program to find the greatest of 10 number in the integer array #include<stdio.h> #include<conio.h>

void greatestarray(int arr[]); void main() {

int array [10]; int a; clrscr(); for(a=0;a<10;a++) {

printf("Enter the number\n"); scanf("%d",&array[a]); }

greatestarray(array); getch();

12

Developed by Pawan Agarwal, Computer Engg. Dept., Hindustan College of Sc. & Tech.

void greatestarray(int arr[]) {

int temp,a; temp=arr[0]; for(a=l;a<10;a++) {

if(temp<arr[a]) {

temp=arr[a]; } } printf("Greatest Element is

%d", temp);

}

22. Write a program to demonstrate the value passing from calling to called function and called to calling function

//program to demonstrate the value passing from calling to called and called to calling #include<stdio.h> #include<conio.h>

int addarray(int arr[]); void main() {

int array [10]; int a; clrscr(); for(a=0;a<10;a++) {

printf("Enter the number\n"); scanf("%d",&array[a]);

}

a=addarray (array); printf("\nSum of the array is %d", a);

getch();

}

int addarray(int arr[]) {

int sum=0,a; for(a=0;a<10;a++) {

sum=sum+arr[a]; } return sum;

}

13

Developed by Pawan Agarwal, Computer Engg. Dept., Hindustan College of Sc. & Tech.

23. Write a program to demonstrate the difference between the call by value and call by reference.

//program to demonstrate the call by value and call by reference #include<stdio.h> #include<conio.h>

void callbyvalue(int b); void callbyref(int *b); void main() {

int a; clrscr(); a=15; printf("Initial value of a=%d\n",a); callbyvalue(a); printf(" Value of a after callbyvalue= %d\n",a); callbyref(&a); printf(" Value of a after the call by reference= %d", a); getch();

}

void callbyvalue(int b) {

b=10; //value will not be reflected in the main function

}

void callbyref(int *b) {

*b=10; }

24. Write a Program to shift one position of the element of the array fafOJ will go into afl I and so on).

//program to shift one elment #include<stdio.h> #include<conio.h>

void shiftarray(int arr[]);

void main() {

int a; int array [10]; clrscr(); for(a=0;a<10;a++) {

printf("Enter the element of position array[%d]\n",a); scanf("%d",&array[a]);

14

Developed by Pawan Agarwal, Computer Engg. Dept., Hindustan College of Sc. & Tech.

} shiftarray(array); getch();

}

void shiftarray(int arr[]) {

int i,j,temp; temp=arr[9]; for(i=9;i>0;i~) {

arr[i]=arr[i-l]; } arr[i]=temp; for(i=0;i<10;i++) {

printf("Element position = %d and Element= %d\n",i,arr[i]); }

}

25. Write a program to print Fibonacci series.

//program to print fibonacci series #include<stdio.h> #include<conio.h> void main () {

int n,i,nl,n2,r; clrscr(); printf("Enter the number of Fibonacci numbers you want: "); scanf("%d",&n); nl = 1; n2= 1; if(n==l)

printf("l"); if(n > 1) printf("l 1"); for(i=3; i<=n ; i++) {

r = nl + n2; nl = n2 ; n2 = r; printf(" %d",n2);

}

15

{

} else {

} getch();

Developed by Pawan Agarwal, Computer Engg. Dept., Hindustan College of Sc. & Tech.

26. Write a program to convert decimal to octal and octal to decimal.

//program to change octal to decimal and decimal to octal #include<stdio.h> #include<conio.h>

void main () {

int a,ch; clrscr(); printf("Press 1-To convert octal to decimal and 2- To convert decimal to octal\n"); scanf("%d",&ch); switch(ch) {

case 1: printf("Enter the Octal number\n"); scanf("%o",&a); printf("Decimal of %o is %d",a,a); break;

case 2: printf("Enter the decimal number\n"); scanf("%d",&a); printf("Octal of %d is %o",a,a); break;

default: printf("You have entered wrong choice");

} getch();

}

27. Write a program to swap the values of two variables using pointers.

//program to swap two variables using pointers #include<stdio.h> #include<conio.h>

void swap(int *a,int *b); void main () {

int c,d; clrscr(); c=10,d=20; printf( "Value of c swap(&c,&d); printf( "Value of c getch();

}

=%d and d=%d before swapping\n",c,d);

=%d and d=%d after swapping \n",c,d);

16

Developed by Pawan Agarwal, Computer Engg. Dept., Hindustan College of Sc. & Tech.

void swap(int *a, int *b) {

int temp; temp=*a; *a=*b ; *b=temp; }

28. Write a C program to copy the contents of one file to another.

//program to copy one file into another #include<stdio.h> #include<conio.h>

void main() {

FILE *fs,*ft; char ch; char source[10], target[10]; clrscr(); printf("Enter the source filename"); scanf("%s",source); printf("Enter the Target filename"); scanf("%s",target); fs = fopen(source,"r"); if(fs==NULL) {

printf("Cannot open source e file ! Press key to exit."); getch(); exit();

} ft = fopen(target,"w"); if(ft==NULL) {

printf("Cannot copy file ! Press key to exit."); fclose(fs); getch(); exit();

} while(l) {

ch = getc(fs); if(ch==EOF) {

break; } else {

putc(ch,ft); }

} printf("File copied succesfully!"); fclose(fs);

17

Developed by Pawan Agarwal, Computer Engg. Dept., Hindustan College of Sc. & Tech.

fclose(ft); getch();

}

29. write a program to search from an given array.

//program to search an item from a given array #include<stdio.h> #include<conio.h>

int main() {

int list[10]; int i,item; clrscr(); for(i=0;i<10;i++) {

printf("Enter the elemenfAn"); scanf("%d",&list[i]);

} printf("Enter the Element to be searched\n"); scanf("%d",&item); for(i=0;i<10;i++) {

if(item==list[i]) {

printf("Search is successful^"); getch(); return 1; } } printf("Search is unsuccessful^"); getch(); return 1;

30. write a program to sort an array.

//program to sort an array using bubble sort #include<stdio.h> #include<conio.h>

void main() {

int a, temp, array[5],x,y; int n=5; clrscr(); for(a=0;a<n;a++) {

printf("Enter the elements");

18

Developed by Pawan Agarwal, Computer Engg. Dept., Hindustan College of Sc. & Tech.

scanf("%d",&array[a]); }

for( x=l; x<=n; x++) {

for(y=0; y<n-x; y++) {

if (array [y] >array [y+1 ]) {

temp = array [y+1]; array[y+l] = array[y]; array [y] = temp; } }

} for(x=0;x<n;x++) {

printf("%d",array[x]); } getch();

//program to sort an array using selection sort #include<stdio.h> #include<conio.h>

void main() {

int a, temp,pos,min,array[5],x,y; int n=5; clrscr(); for(a=0;a<n;a++) {

printf("Enter the elements"); scanf("%d",&array[a]); }

for( x=0; x<n; x++) {

min=array[x]; pos=x; for(y=x+l; y<n; y++) {

if(min>array[y]) {

min=array[y]; pos=y; } }

19

Developed by Pawan Agarwal, Computer Engg. Dept., Hindustan College of Sc. & Tech.

temp=array[x]; array [x]=min; array [pos]=temp;

} for(x=0;x<n;x++) {

printf("%d",array[x]); } getch();

}

31. write a program to prepare a link list

//program to create a link list #include<stdio.h> #include<conio.h> #include<malloc. h>

struct node {

int item; struct node *next;

}*start=NULL; void create_list(int data); void display(); void main() {

int ch,n; clrscr(); while(l) {

printf("\n\n\nl. Create List\n"); printf("2. Display the list\n"); printf("3. Exit\n"); printf("\nEnter any choice:\n"); scanf("%d",&ch); switch(ch) {

case 1: printf("Enter the data item\n"); scanf("%d",&n); create_list(n); break;

case 2: display(); break; case 3: exit(l);

default:printf("Wrong choice");

}; /*end of switch*/

20

Developed by Pawan Agarwal, Computer Engg. Dept., Hindustan College of Sc. & Tech.

} /*end of while*/ }

/*end of main*/

void create_list(int data) {

struct node *q,*tmp; tmp=malloc(sizeof(struct node)); tmp ->item=data; tmp->next=NULL; if(start==NULL) /* if list is empty*/ start=tmp; else {

q=start; while(q->next! =NULL) {

q=q->next; // q->next=tmp;

} q->next=tmp;

}

void display() {

struct node *q; if(start==NULL) {

printf("List is empty\n"); return;

}/*endofif()*/ q=start; printf("\n\nList is :\n"); while(q!=NULL) { printf("\t%d",q->item); q=q->next; } /*end of whileQ*/

} /*end of display()*/

32. Write a program to append the contents of one file into another.

//program to append the contents of one file into another #include<stdio.h> #include<conio.h>

21

Developed by Pawan Agarwal, Computer Engg. Dept., Hindustan College of Sc. & Tech.

void main() {

FILE *fs,*ft; char ch; char source[10], target[10]; clrscr(); printf("Enter the source filename"); scanf("%s",source); printf("Enter the Target filename"); scanf("%s",target); fs = fopen(source,"r"); if(fs==NULL) {

printf("Cannot open source e file ! Press key to exit."); getch(); exit();

} ft = fopen(target,"a"); if(ft==NULL) {

printf("Cannot copy file ! Press key to exit."); fclose(fs); getch(); exit();

} while(l) {

ch = getc(fs); if(ch==EOF) {

break; } else {

putc(ch,ft); } }

printf("File copied succesfully!"); fclose(fs); fclose(ft); getch();

33. Write a program to demonstrate the use of the structure entering the percentage, name and rollno of the students

//program to demonstrate the use of structure #include<stdio.h> #include<conio.h>

22

Developed by Pawan Agarwal, Computer Engg. Dept., Hindustan College of Sc. & Tech.

struct stud {

int rollno; char name [20]; int per;

}; typedef struct stud studl; void display(studl s2); void main() {

studl si; clrscr(); printf("Enter the rollno\n"); scanf("%d",&sl.rollno); printf("Enter the name\n"); scanf("%s",s 1 .name); printf("Enter the percentage"); scanf("%d",&sl.per); display (si); getch(); }

void display( studl s2) {

printf(" Roll no= %d and Name =%s and percentage=%d",s2.rollno,s2.name,s2.per); }

34. Write a program to demonstrate the dynamic memory allocation.

//program to demonstrate the dynamic memory allocation #include<stdio.h> #include<conio.h>

void main() {

int * a,n,b; clrscr(); printf("Enter the number of students\n"); scanf("%d",&n); a=(int *)malloc(sizeof(int)); for(b=l;b<=n;b++) {

printf("Enter marks of %d student",b); scanf("%d",a); a++;

} a-; for(b=l;b<=n;b++) {

printf("\n%d",*a); a-;

23

Developed by Pawan Agarwal, Computer Engg. Dept., Hindustan College of Sc. & Tech.

} a++; firee(a); getch(); }

35. Write a program to demonstrate the use of macros.

//program to display the use of preprocessor for macro and file inclusion #include<stdio.h> #include<conio.h> #define AREA(x) (3.14*x*x)

void main() {

int a; float b ; clrscr(); a=10; b=AREA(a); printf('Area of circle is %f',b ); getch();

}

//program to display the use of preprocessor for conditional compilation and function calling using pragma #include<stdio.h> #include<conio.h> #define PENTIUM void fun(); void funl(); #pragma startup fun #pragma exit funl

void main() {

#ifdef PENTIUM

printf("This code is for pentium machine written in main function\n");

#else

printf("This code is for APPLE Machine\n"); #endif

}

void fun() {

printf("Calling fun before main\n"); }

24

Developed by Pawan Agarwal, Computer Engg. Dept., Hindustan College of Sc. & Tech.

void fun 1() {

printf("Calling funl after main\n"); }

36. Write a program for the matrix addition

//program for matrix addition #include<stdio.h> #include<conio.h>

void main() {

inta[3][3],b[3][3],c[3][3],ij; clrscr();

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

for(]=0;j<3;j++) {

printf("Enter the Element position of [%d] [%d]",i,j); scanf("%d",&a[i][j]); } } for(i=0;i<3;i++) {

for(]=0;j<3;j++) {

printf("Enter the Element position of [%d] [%d]",i,j); scanf("%d",&b[i][j]); } } for(i=0;i<3;i++) {

for(]=0;j<3;j++) {

c[i]D]=a[i]D]+b[i]Dl; }

}

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

forG=0;j<3;j++) {

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

printf("\n"); }

} getch();

25

Developed by Pawan Agarwal, Computer Engg. Dept., Hindustan College of Sc. & Tech.

37. Write a program for matrix multiplication

//program for matrix multiplication #include<stdio.h> #include<conio.h>

void main() {

inta[3][3],b[3][3],c[3][3],i,j,k; clrscr(); for(i=0;i<3;i++) {

for(]=0;j<3;j++) {

printf("Enter the Element position of [%d] [%d]",i,j); scanf("%d",&a[i][j]); } }

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

forG=0;j<3;j++) {

printf("Enter the Element position of [%d] [%d]",i,j); scanf("%d",&b[i][j]);

c[i][j]=0; } }

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

forG=0;j<3;j++) {

for(k=0;k<3;k++) {

c[i]D]=c[i]D]+a[i][k]*b[k]D]; } } }

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

forG=0;j<3;j++) {

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

printf("\n");

}

getch();

26

Developed by Pawan Agarwal, Computer Engg. Dept., Hindustan College of Sc. & Tech.

38. Write a program to find the sum of the following series

1+2/2!+3/3!. ..... n/n!

//program to find the sum of series #include<stdio.h> #include<conio.h>

void main() {

int a,n; float i,b,c=0; clrscr(); printf("Enter the value of n\n\n"); scanf("%d",&n); for(a= 1 ;a<=n;a++) {

b=l; i=l; while(i<=a) {

b=b*i; i++;

} c=c+(a/b); }

printf("The sum is %f ',c); getch(); }

39. Write a program to find the transpose of given matrix

//program to transpose the matrix #include<stdio.h> #include<conio.h>

void main() {

inta[3][3],b[3][3],ij; clrscr();

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

forG=0;j<3;j++) {

printf("Enter the Element position of [%d] [%d]",i,j); scanf("%d",&a[i][j]); } }

27

Developed by Pawan Agarwal, Computer Engg. Dept., Hindustan College of Sc. & Tech.

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

for(]=0;j<3;j++) { b[i][j]=a[j][i]; }

}

printf("\n\n Transpose of \n\n");

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

for(]=0;j<3;j++) {

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

}

printf(" \nis this\n\n"); for(i=0;i<3;i++) {

for(]=0;j<3;j++) {

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

} printf("\n"); }

getch(); }

40 Write a program to pass the structure array into a function and display the result in the

called function

//program to demonstrate the passing of structure array to the user defined funtion #include<stdio.h> #include<conio.h>

struct stud {

int rollno; char name [20]; int per;

}; typedef struct stud studl; void display (studl s2[]);

28

Developed by Pawan Agarwal, Computer Engg. Dept., Hindustan College of Sc. & Tech.

void main() {

studl si[3]; int i; clrscr(); for(i=0;i<3;i++) {

printf("Enter the rollno\n"); scanf("%d",&sl[i].rollno); printf("Enter the name\n"); scanf("%s",s 1 [i] .name); printf("Enter the percentage"); scanf("%d",&sl[i].per);

} display (si); getch(); }

void display( studl s2[]) { int i; for(i=0;i<3;i++) {

printf("\n\nDetail of student no %d " ,i+l); printf("is Roll no= %d and Name =%s and

percentage=%d",s2[i].rollno,s2[i].name,s2[i].per); } }

41. write a program to find the percentage of the marks given by the user.

//program to find the percentage of marks given by the user #include<stdio.h> #include<conio.h>

void main() {

int marks; float percent; clrscr(); printf("Enter the marks\n"); scanf("%d",&marks); percent = (marks* 100)/500; printf("Percentage of student is %f',percent); getch(); }

29

Developed by Pawan Agarwal, Computer Engg. Dept., Hindustan College of Sc. & Tech.

42. write a program to convert the lowercase string into the upper case

//program to convert the string into upper case

#include<stdio.h>

#include<conio.h>

char * stringupper(char string[]);

void main()

{

char input[10];

char *a;

clrscr();

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

scanf("%s",input);

a=stringupper(input);

printf("%s",a);

getch(); }

char * stringupper(char string[])

{

int a=0;

while(string[a]!='\0')

{

if(string[a]>96)

{ string [a]=string [a] -32;

}

a++;

}

return string;

}

30

Developed by Pawan Agarwal, Computer Engg. Dept., Hindustan College of Sc. & Tech.

43. Write a program to find the factorial using recursion

//program to find factorial by recursion

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

int fact(int n)

{

int x,y;

if(n==0)

return 1;

x=n-l;

y=fact(x);

return n*y; }

void main()

{

intx;

clrscr();

printf("\nEnter the integer whose factorial value you want:");

scanf("%d",&x);

printf("\nThe facorial value is:-%d",fact(x));

getch();

31

Developed by Pawan Agarwal, Computer Engg. Dept., Hindustan College of Sc. & Tech.

32