Sie sind auf Seite 1von 69

INDEX

SR. PROGRAMS NO. 1. a) program to calculate sum=1+2+3+.........n b) program to calculate sum=(1*1)+(2*2)+(3*3)+.........(n*n) c) program to print the sum of series n+(n-2)+(n4)+......1 or 2 d) program to calculate sum=1*2*3*.........n e) program to print the sum of series 1-2+3-4+......n PAGE NO. 5-9

2.

Input any positive integer number N which should 10-11 be less than or equal to 1000. Print the Following: a)Prime number less than N. b) Fibonnaci series which should be less than or equal to N. Input any (1-999999999) digit positive integer number. a)Print the sum of the digits of the given number. b) Input any fractional number <=999999999. Print the sum of the digits of the given number Write a program which will print those 3 digit numbers where the sum of the cubes of the digits: 1*1*1+5*5*5+3*3*3-153 Write a program to display the Following pattern called Floyeds Triangle. Input two positive number<=32767 and print the H.C.F of two numbers. Allow user to print more numbers and then print print the H.C.F of all numbers Calculate exp(x) = 1+ x + x2 + x3 + x4 + ---------+xn -- -- ---1! 2! 3! 4! n! where x=.1, .2, .3, .4, .5, .6, .8, .9, .1.0 Calculate Tan(x) = Sin(x) / Cos(x) where x = 0 to 360 in step of 5. You have to convert x to radian. 12-14

3.

4.

15

5. 6.

16 17-18

7.

19-20

8.

21-22

Verify your result with the standard library function of Tan(x). 9. Calculate the value of PI. Where 23 PI = 3 x 2 (1 1/3*3 + 1/3*3*5 1/3*3*3*7 .) WAP that accepts an input integer n in the range 0- 24-25 9 inclusive and display the pyramid on the screen Program to find the sum, average, std dev of 20 values 26-27 Write a program to input 10 arbitrary numbers in one D array calculate frequency of each number and its frequency in a tabular form a) Write a program to print the scalar matrix 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 b) Write a program to print the scalar matrix 1 0 0 1 0 1 1 0 0 1 1 0 1 0 0 1 c) Write a program to print the pascal triangle Write a program to find the union of 2 arrays . Write a program to find the intersection of 2 sets Write a program to find out the symmetric difference of the two sets. Write a program to find the difference of 2 sets Write a program to calculate the sum, difference and product of two 3*3 matrices Write a program to convert any decimal number (19999999) to any system(2-16). to calculate the factorial & Binomial Coefficient A program which will arrange the positive and 28-29

10. 11. 12.

13.

30-32

14. 15. 16.

33-34 35 36-37

17. 18.

38-39 40-42

19.

43

20. 21.

44 45-46

22. 23.

negative numbers in a array in such a way that all positive should come first and then arrange all negative numbers will come without changing original sequence To sort the given data with menu driven program 47-50 a) Recursively add the series of the n natural number 51-54 b) Recursively multiply the series of the n natural number c) Recursively display the fibonnaci series d) d) Write a program to input distance 'n'(1-20). Print how many ways a robot can move the distance n. A program to Input any positive integer number (n<=3000). Convert the number into Roman numerals. A program for SEARCHING according to user's choice Program to print 3 digit numbers such that second number is 2 times as the first number and third number is 3 times as the first number. A program to input any positive integer number and convert it into words. Program for towers of Hanoi problem. To find the length of the string without using standard function a) Program to copy one string into another string without using standard function b) Program to concatenate 2 strings without using standard function 55

24.

25. 26.

56-59 60-61

27.

62-64

28. 29.

65 66

30.

67-68

// 1.(a) program to calculate sum=1+2+3+.........n


#include<stdio.h> #include<conio.h> void main() { int sum=0,n,i; clrscr(); printf("Enter the value of n between 1 and 20:"); scanf("%d",&n); if(n>=20||n<=0) printf("Invalid entry"); else { for(i=1;i<=n;i++) sum=sum+i; printf("\nSum is %d",sum); } getch(); }

OUTPUT:
Enter the value of n between 1 and 20:6 Sum is 21

// 1. (b) program to calculate sum=(1*1)+(2*2)+(3*3)+.........(n*n)


#include<stdio.h> #include<conio.h> void main() { int sum=0,n,i; clrscr(); printf("Enter the value of n between 1 and 20:"); scanf("%d",&n); if(n>=20||n<=0) printf("Invalid entry"); else { for(i=1;i<=n;i++) sum=sum+i*i; printf("\nSum is %d",sum); } getch(); }

OUTPUT:
Enter the value of n between 1 and 20:4 Sum is 30

// 1.(c) program to print the sum of series n+(n-2)+(n-4)+......1 or 2


#include<stdio.h> #include<conio.h> void main() { int n,sum=0,i; clrscr(); printf("Enter any number between 1 and 20:"); scanf("%d",&n); if(n>=20||n<=0) printf("Invalid entry"); if(n%2==0) { for(i=n;i>=2;i=i-2) { sum=sum+i; } } else { for(i=n;i>=1;i=i-2) { sum=sum+i; } } printf("\nSUM IS %d",sum); getch(); }

OUTPUT:
Enter any number between 1 and 20:7 SUM IS 16

// 1. (d) program to calculate sum=1*2*3*.........n


#include<stdio.h> #include<conio.h> void main() { long int product=1; int n,i; clrscr(); printf("Enter the value of n between 1 and 20:"); scanf("%d",&n); if(n>=20||n<=0) printf("Invalid entry"); else { for(i=1;i<=n;i++) product=product*i; printf("\nProduct is %ld",product); } getch(); }

OUTPUT:
Enter the value of n between 1 and 20:5 Product is 120

// 1. (e) program to print the sum of series 1-2+3-4+......n


#include<stdio.h> #include<conio.h> void main() { int n,sum=0,i; clrscr(); printf("Enter any number between 1 and 20:"); scanf("%d",&n); if(n>=20||n<=0) printf("Invalid entry"); for(i=1;i<=n;i++) { if(i%2!=0) sum=sum+i; else sum=sum-i; } printf("%d",sum); getch(); }

OUTPUT:
Enter any number between 1 and 20:6 -3

/* 2. A) Input any positive integer number N which should be less than or equal to 1000. Print the Following: a)Prime number less than N.*/
#include<stdio.h> #include<conio.h> void main() { int n,d,i,j; clrscr(); printf("enter any +ve number less than 1000:"); scanf("%d",&n); printf("And here are your prime numbers < %d \n",n); for(i=2;i<n;i++) { for(j=2;j<=i;j++) { if(i%j==0) break; } if(i==j) printf("%d\n",i); } getch(); }

OUTPUT:
enter any +ve number less than 1000:25 And here are your prime numbers < 25 2 3 5 7 11 13 17 19 23

/*2. b) Fibonnaci series which should be less than or equal to N. Where the first two number of the Fibonacci series are 1 and 1. The formula for generating Fibonacci series is: Fn=Fn-1+Fn-2 */
#include <stdio.h> #include <conio.h> int main() { int a=1,b=1; int c,i,N; clrscr(); printf("\n Enter a value for N: "); scanf("%d",&N); clrscr(); printf("\n Fibonnaci Series is....\n"); printf("%d %d",a,b); for(i=3;i<=N;i++) { c = a+b; a = b; b = c; printf(" %d",c); } getch(); return 0; }

OUTPUT:
Fibonnaci Series is.... 112358

10

/*3. (i) Input any (1-999999999) digit positive integer number. Print the sum of the digits of the given number.*/
#include <stdio.h> #include <conio.h> int main() { long int num; int sumDigits = 0; printf("\nEnter a number[b/w 1 to 9999999]: "); scanf("%ld",&num); clrscr(); printf("\nThe number entered was: %ld",num); //to find dthe sum of the digits while(num>0) { sumDigits += (num%10); num = num/10; } printf("\nThe sum of the digits of the number is: %d",sumDigits); getch(); return 0; }

OUTPUT:
The number entered was: 5555555 The sum of the digits of the number is: 35

11

/*3. (ii) Input any fractional number <=999999999. Print the sum of the digits of the given number.*/
#include<stdio.h> #include<conio.h> #include<string.h> int todig(char); void main() { int i,j,p=0,sum=0,a[100]; char ch='a'; a[0]=4; printf("Enter any real number:\n"); for(i=0;i<100;i++) { ch=getchar(); if(ch=='\n') break; a[i]=todig(ch); } for(p=0;p<i;p++) { sum=sum+a[p]; } printf("\nsum is %d\n",sum); getch(); } int todig(char chr) { int k=1; switch(chr) { case'.':k=0; break; case'0':k=0; break; case'1':k=1; break; case'2':k=2; break; case'3':k=3; break; case'4':k=4; break; case'5':k=5; break; case'6':k=6; break; case'7':k=7; 12

break; case'8':k=8; break; case'9':k=9; break; default:printf("typed wrong"); } return k; }

OUTPUT:
Enter any real number: 7886.97 sum is 45

13

/* 4. Write a program which will print those 3 digit numbers where the sum of the cubes of the digits: 1*1*1+5*5*5+3*3*3-153 .*/
#include <stdio.h> #include <conio.h> #include <math.h> int main() { int num,armSnum; clrscr(); printf("\nThe Armstrong Numbers b/w 100 to 999...\n"); for(num=100;num<=999;++num) { armSnum = pow((num%10),3)+pow(((num/10)%10),3)+pow((num/100),3); if((armSnum-num) == 0) printf("%d \n",armSnum); } getch(); return 0; }

OUTPUT:
The Armstrong Numbers b/w 100 to 999... 153 370 371 407

14

/*5. Write a program to display the Following pattern called Floyeds Triangle. 1 23 456 7 8 9 10 11 12 13 14 15 */
#include <stdio.h> #include <conio.h> #define SIZE 5 int main() { int a=1; int i,j; clrcsr(); for(i=0;i<SIZE;i++) { for(j=0;j<i+1;j++) { printf("%5d",a++); } printf("\n"); } getch(); return 0; }

OUTPUT:
1 2 4 7 11 3 5 6 8 9 10 12 13 14 15

15

/*6. Input two positive number<=32767 and print the H.C.F of two numbers. Allow user to print more numbers and then print print the H.C.F of all numbers */
#include <stdio.h> #include <conio.h> void main() { int ar[30],i,j,k,a,b,n; char ans='y'; int hcf(int , int); clrscr(); printf("\n if u want to print hcf of more than two numbers \t"); printf("\n Yes Or No\t"); ans=getche(); if(ans =='y'||ans=='Y') { printf("\n enter number of values for which you want to caluculate hcf \t"); scanf("%d",&n); for(i=0; i<n;i++) scanf ("%d", &ar[i]); k=hcf(ar[0],ar[1]); for(i=2; i<n; i++) k=hcf(ar[i],k); printf("\n hcf of %d numbers is\t %d",n,k); } else { printf("\n enter two numbers \t"); scanf("%d %d",&a,&b); k=hcf(a,b); printf("\n hcf of two numbers is\t %d",k); } getch(); } int hcf(int a, int b) { int r; r=a%b; while (r!=0) { a=b;b=r;r=a%b; } return b; }

16

OUTPUT:
if u want to print hcf of more than two numbers Yes Or No y enter number of values 6 enter the values 28 12 45 9 15 21 hcf of 6 numbers is 1

17

/*7. Calculate exp(x) = 1+ x + x2 + x3 + x4 + ---------+xn -- ----1! 2! 3! 4! n! where x=.1, .2, .3, .4, .5, .6, .8, .9, .1.0 */
#include <stdio.h> #include <conio.h> #include <math.h> #define ACCURACY 0.000001 double Exponent(double); int main() { double x; clrscr(); printf("Values for Exponential Function...\n\n"); printf("-------------------------"); printf("\n x\tExp(x)\tStandard\n"); printf("-------------------------\n"); for(x=0.1;x<=1.0;x=x+0.1) { printf("\n%1.1f%11.4f%10.4f\n",x,Exponent(x),exp(x)); } getch(); return 0; } double Exponent(double x) { double sum,term; int n=1; sum = term = 1.0; while(n<=100) { term = term*(x/n); sum += term; if(term < ACCURACY) n = 999; else n=n+1; } return sum; }

18

OUTPUT:
Values for Exponential Function... ------------------------x Exp(x) Standard ------------------------0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0 1.1052 1.2214 1.3499 1.4918 1.6487 1.8221 2.0138 2.2255 2.4596 2.7183 1.1052 1.2214 1.3499 1.4918 1.6487 1.8221 2.0138 2.2255 2.4596 2.7183

19

// 8. Calculate Tan(x) = Sin(x) / Cos(x) where x = 0 to 360 in step of 5. You have to convert x to radian. Verify your result with the standard library function of Tan(x).
#include <stdio.h> #include <conio.h> #include <math.h> double Sine(double); double Cosine(double); void main() { double tangent,x; int deg; clrscr(); printf("The values of the Tangent is: \n\n"); printf("----------------------------------\n"); printf("Angle\tCalculated\tStandard\n"); printf("----------------------------------\n"); for(deg=0;deg<=360;deg=deg+5) { x = (3.14*deg)/180.0; tangent = Sine(x)/Cosine(x); printf("\n%5d%13.4f%14.4f\n",deg,tangent,tan(x)); } getch(); } //calculating Sine Series: sin(x) = 1-x*x/2!+.... double Sine(double x) { double term,sum,fact,sign; int i,j; sum = x; sign = 1; for(i=1;i<=20;i++) { fact = 1; for(j=1;j<=(2*i+1);++j) fact = fact*j; sign*=-1; term = (sign*pow(x,(2*i+1)))/fact; sum+=term; } return sum; } //calculating Cosine Series: cos(x) = x-x*x*x/3!+.... 20

double Cosine(double x) { double term,sum,fact,sign; int i,j; sum = 1; sign = 1; for(i=1;i<=20;i++) { fact = 1; for(j=1;j<=2*i;++j) fact = fact*j; sign*=-1; term = (sign*pow(x,(2*i)))/fact; sum+=term; } return sum; }

OUTPUT:
The values of the Tangent is: ---------------------------------Angle Calculated Standard ---------------------------------0 5 10 15 20 25 30 35 40 0.0000 0.0874 0.1762 0.2678 0.3638 0.4660 0.5770 0.6997 0.8385 0.0000 0.0874 0.1762 0.2678 0.3638 0.4660 0.5770 0.6997 0.8385

-- More --

21

/* 9. Calculate the value of PI. Where PI = 3 x 2 (1 1/3*3 + 1/3*3*5 1/3*3*3*7 .) Verify your result with PI = 3.1415926535897932384 */
#include <stdio.h> #include <conio.h> #include <math.h> int main() { double PI,term,sign = 1; double sum = 1.0; int n; clrscr(); for(n=1;n<=30;++n) { sign *= -1; term = sign/(pow(3,n)*(2*n+1)); sum += term; } PI = 2*sqrt(3)*sum; printf("The value of PI: %5.19g",PI); getch(); return 0; }

OUTPUT:
The value of PI: 3.141592653589794

22

/*10. WAP that accepts an input integer n in the range 0-9 inclusive and display the pyramid on the screen*/
#include<stdio.h> #include<conio.h> void main() { int i,j,k,t,c,n; clrscr(); printf("enter the NUMBER\n"); scanf("%d",&n) ; c=n-1; for(i=n;i>=1;i--) { for(k=1;k<=c;k++) printf(" "); for(j=n;j>=i;j--) printf("%d",j); t=i; while(t!=n) { printf("%d",t+1); t++; } printf("\n"); c--; } //DOWNWARD PYRAMID c=1; //c=1 for(i=2;i<=n;i++) //i=2 instead of n { for(k=1;k<=c;k++) printf(" "); for(j=n;j>=i;j--) printf("%d",j); t=j+1; while(t!=n) { printf("%d",t+1); t++; } printf("\n"); c++; } getch(); }

23

OUTPUT:
enter the NUMBER 6 6 656 65456 6543456 654323456 65432123456 654323456 6543456 65456 656 6

24

// 11. Program to find the sum, average, std dev of 20 values


#include <stdio.h> #include <conio.h> #include <math.h> int main() { int arr[20]; double stdDev,avg,var=0.0; int sum = 0,max; int i; clrscr(); printf("Enter the 20 values one by one: \n"); //user inputs the values & system adds them in sum for(i=0;i<20;i++) { scanf("%d",&arr[i]); sum += arr[i]; } //calculates the average avg = sum/20; max = arr[0]; for(i=0;i<20;i++) { //calculates the maximum value in the list if(max<arr[i]) max = arr[i]; //calculates the variance var += pow((arr[i]-avg),2); } stdDev = sqrt(var/20); //Standard deviation clrscr(); printf("\nThe sum of the values entered is: %7d",sum); printf("\nThe Maximum term in the series: %7d",max); printf("\nThe Average of the values entered: %5.2f",avg); printf("\nThe Standard Deviation is: %14.3f",stdDev); getch(); return 0; }

25

OUTPUT:
Enter the 20 values one by one: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 16 15 17 18 19 20 The sum of the values entered is: 210 The Maximum term in the series: 20 The Average of the values entered: 10.5 The Standard Deviation is: 20.2

26

/* 12 .Write a program to input 10 arbitrary numbers in one D array calculate frequency of each number and its frequency in a tabular form */
#include<stdio.h> #include<conio.h> void main() { int ar[10],a,cnt,i,j,temp; a=1; cnt=1; clrscr(); printf("\n please enter values \t"); for(i=0; i<=10;i++) scanf("%d", &ar[i]); for(i=0;i<10;i++) { for(j=0;j<10;j++) { if(ar[j]>=ar[j+1]) { temp=ar[j]; ar[j]=ar[j+1]; ar[j+1]=temp; } } } printf("\n \t NUMBER \t Frequency"); for(i=0; i<10;i++) { cnt=1; for(j=i+1; j<10; j++) { if(ar[i]==ar[j]) { cnt++; i++; } } printf("\n\t %d \t \t %d", ar[i],cnt); } getch(); }

27

OUTPUT:
Please enter values 1325681536 NUMBER Frequency 1 2 3 2 2 1 5 2 6 2 8 1

28

// 13(i) Write a program to print the scalar matrix 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1


#include <stdio.h> #include <conio.h> #define N 4 int A(int,int); int main() { int i,j; clrscr(); for(i=0;i<N;i++) { for(j=0;j<N;j++) { printf("%4d",A(i,j)); } printf("\n"); } getch(); return 0; } int A(int p,int q) { if(p == q) return 1; else return 0; }

OUTPUT:
1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1

29

// 13(ii) Write a program to print the scalar matrix 2 0 0 1 0 1 1 0 0 1 1 0 1 0 0 1


#include <stdio.h> #include <conio.h> #define N 4 int A(int,int); int main() { int i,j; clrscr(); for(i=0;i<N;i++) { for(j=0;j<N;j++) { printf("%4d",A(i,j)); } printf("\n"); } getch(); return 0; } int A(int p,int q) { if((p == q) || (p+q) == N-1) return 1; else return 0; }

OUTPUT:
1 0 0 1 0 1 1 0 0 1 1 0 1 0 0 1

30

// 13(iii) Write a program to print the pascal triangle 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1


#include <stdio.h> #include <conio.h> #define N 5 int C(int,int); int main() { int i,j; clrscr(); for(i=0;i<5;i++) { for(j=0;j<i+1;j++) { printf("%3d",C(i,j)); } printf("\n"); } getch(); return 0; } int C(int p,int q) { if((p==q)||(q==0)) return 1; else return(C(p-1,q)+C(p-1,q-1)); }

OUTPUT:
1 1 1 1 1 1 2 1 3 3 1 4 6 4 1

31

// 14 Write a program to find the union of 2 arrays .


#include<stdio.h> #include<conio.h> void main() { int i,j,c[12],k,n=0,pos=0,pos1=0,temp; int a[6]={1,3,4,5,7,8}; int b[6]={1,3,4,5,10,11}; clrscr(); for(i=0;i<6;i++) { c[n++]=a[i]; } //pos=n; //printf("\n Pos= %d",pos); for(i=0;i<6;i++) { for(j=0;j<6;j++) { if(c[i]==b[j]) continue; else { c[n++]=b[j];i++; } }break; } printf("\nN=%d",n); // printf("\n Pos1= %d",pos1); for(i=0;i<n;i++) { for(j=0;j<n;j++) { if(c[j]>c[j+1]) { temp=c[j]; c[j]=c[j+1]; c[j+1]=temp; //n++; } } } for(i=0;i<n;i++) { if(c[i]==c[i+1]) continue; printf("\n%d",c[i]); } getch(); }

32

OUTPUT:
N=11 1 3 4 5 7 8 10 11

33

//15 Write a program to find the intersection of 2 sets


#include<stdio.h> #include<conio.h> # define N 5 void main() { int a[N],b[N],c[N],i,j=0,n=N,t,u[N+N]; clrscr(); printf("enter the numbers for set A\n"); for(i=0;i<N;i++) scanf("%d",&a[i]); printf("enter the numbers for set B\n"); for(i=0;i<N;i++) scanf("%d",&b[i]); for(i=0;i<N;i++) { for(t=0;t<N;t++) { if(b[t]==a[i]) { c[j]=a[i]; j++; break; } } } printf("\n the intersection of two sets is" ); for(i=0;i<j;i++) printf("\n %d\t",c[i]); printf("\n"); getch(); }

OUTPUT:
enter the numbers for set A 12345 enter the numbers for set B 34751 the intersection of two sets is 1 3 4 5

34

// 16 Write a program to find out the symmetric difference of the two sets.
#include<stdio.h> #include<conio.h> # define N 5 void main() { int a[N],b[N],c[N],i,j=0,n=0,t,u[N+N]; clrscr(); printf("enter the numbers for set A\n"); for(i=0;i<N;i++) scanf("%d",&a[i]); printf("enter the numbers for set B\n"); for(i=0;i<N;i++) scanf("%d",&b[i]); for(i=0;i<N;i++) { for(t=0;t<N;t++) { if(b[t]==a[i]) { c[j]=a[i]; j++; break; } } } for(i=0;i<N;i++) { for(t=0;t<j;t++) { if(c[t]==a[i]) goto a; else continue; } u[n]=a[i]; n=n+1; a: } for(i=0;i<N;i++) { for(t=0;t<j;t++) { if(c[t]==b[i]) goto b; else 35

continue; } u[n]=b[i]; n=n+1; b: } printf("AND HERE IS THE SYMMETRIC DIFFERENCE\n"); for(i=0;i<n;i++) printf("%d\t",u[i]); getch(); }

OUTPUT:
enter the numbers for set A 15327 enter the numbers for set B 46812 AND HERE IS THE SYMMETRIC DIFFERENCE 5 3 7 4 6 8

36

// 17 Write a program to find the difference of 2 sets


#include<stdio.h> #include<conio.h> # define N 5 void main() { int a[N],b[N],c[N],i,j=0,m=0,t; clrscr(); printf("enter the numbers for set A\n"); for(i=0;i<N;i++) scanf("%d",&a[i]); printf("enter the numbers for set B\n"); for(i=0;i<N;i++) scanf("%d",&b[i]); for(i=0;i<N;i++) { for(t=0;t<N;t++) { if(b[t]==a[i]) { c[j]=a[i]; j++; break; } } } // for(i=0;i<j;i++) // printf("\n %d",c[i]); printf("\n the difference is "); m=0; for(i=0;i<=j;i++) { for(t=m;t<N;t++) { if(c[i]!=a[t]) printf("%d\t",a[t]); else { m=t+1; goto b; }

37

OUTPUT:
enter the numbers for set A 1 2 5 8 10 enter the numbers for set B 3 10 7 2 4 the difference is 1 5 8

38

// 18 Write a program to calculate the sum, difference and product of two 3*3 matrices
#include <stdio.h> #include <conio.h> int main() { int a[3][3],b[3][3]; int sum[3][3],diff[3][3],mul[3][3]; int i,j,k; clrscr(); printf("\nEnter elements of matrix A: \n"); for(i=0;i<3;i++) for(j=0;j<3;j++) scanf("%d",&a[i][j]); printf("\nEnter elements of matrix B: \n"); for(i=0;i<3;i++) for(j=0;j<3;j++) scanf("%d",&b[i][j]); //calculate sum & diff for(i=0;i<3;i++) { for(j=0;j<3;j++) { sum[i][j] = a[i][j]+b[i][j]; diff[i][j] = a[i][j]-b[i][j]; mul[i][j] = 0; for(k=0;k<3;k++) { mul[i][j]+=(a[i][k]*b[k][j]); } } } //print matrix A printf("\nMatrix A: \n"); for(i=0;i<3;i++) { for(j=0;j<3;j++) { printf("%4d",a[i][j]); } printf("\n"); } //print matrix B printf("\nMatrix B: \n"); for(i=0;i<3;i++) { 39

for(j=0;j<3;j++) { printf("%4d",b[i][j]); } printf("\n"); } //print matrix sum printf("\nSum of Matrix A & B: \n"); for(i=0;i<3;i++) { for(j=0;j<3;j++) { printf("%4d",sum[i][j]); } printf("\n"); } //print matrix diff printf("\nDifference of Matrix A & B: \n"); for(i=0;i<3;i++) { for(j=0;j<3;j++) { printf("%4d",diff[i][j]); } printf("\n"); } //print product of matrix printf("\nProduct of Matrix A & B: \n"); for(i=0;i<3;i++) { for(j=0;j<3;j++) { printf("%4d",mul[i][j]); } printf("\n"); } getch(); return 0; }

OUTPUT:
Enter elements of matrix A: 124 461 170

40

Enter elements of matrix B: 268 217 540 Matrix A: 1 2 4 4 6 1 1 7 0 Matrix B: 2 6 8 2 1 7 5 4 0 Sum of Matrix A & B: 3 8 12 6 7 8 6 11 0 Difference of Matrix A & B: -1 -4 -4 2 5 -6 -4 3 0 Product of Matrix A & B: 26 24 22 25 34 74 16 13 57

41

// 19. Write a program to convert any decimal number (1-9999999) to any system(2-16).
#include <stdio.h> #include <conio.h> void Convert(long int,int); int main() { long int num; int base; printf("\nEnter a number: "); scanf("%d",&num); printf("\nEnter the base: "); scanf("%d",&base); printf("\n\nThe number entered is: %d",num); printf("\nIt's Equivalent in base %d is: ",base); Convert(num,base); getch(); return 0; } void Convert(long int n,int b) { if(n/b == 0) { if(n%b>9) printf("%c",n%b-10+65); else printf("%d",n%b); } else { Convert(n/b,b); if(n%b>9) printf("%c",n%b-10+65); else printf("%d",n%b); } }

OUTPUT:
Enter a number: 200 Enter the base: 16 The number entered is: 200 the Equivalent number is: C8

42

// 20. to calculate the factorial & Binomial Coefficient


#include <stdio.h> #include <conio.h> int fact(int n) { int prod=1,i; for(i=1;i<=n;i++) prod*=i; return (prod); } void main() { int n,r,coff; int fact(int); clrscr(); printf("\n enter values for n and r to calculate binomial cofficient \t"); scanf("%d %d",&n,&r); coff=fact(n)/(fact(r)*fact(n-r)); printf("\n Binomial coefficient is \t%d",coff); getch(); }

OUTPUT:
enter values for n and r to calculate binomial cofficient Binomial coeffcient is 10 5 2

43

/* 21. A program which will arrange the positive and negative numbers in a array in such a way that all positive should come first and then arrange all negative numbers will come without changing original sequence. */
#include<stdio.h> #include<conio.h> void main() { int ar[10],ar2[10],i,j,k,pos; printf("\n Enter values both positive and negative \t"); clrscr(); for(i=0;i<10;i++) { scanf("%d",&ar[i]); } printf("\n The values are\t"); for(i=0;i<10;i++) { printf("\n %d",ar[i]); } //printf("\n Sorted array is"); k=0; for(i=0;i<10;i++) { if(ar[i]>0) { ar2[k]=ar[i]; k++; } } pos=k; for(i=0;i<10;i++) { if(ar[i]<0) { ar2[pos]=ar[i]; pos++; } } //printf("\n %d K is ",k); //pos=k; printf("\n The values are\t"); for(i=0;i<10;i++) { printf("\n %d",ar2[i]); } getch(); }

44

OUTPUT:
Enter values in both positive and negtive numbers -1 2 -2 3 -3 4 -4 5 -5 The values are 1 -1 2 -2 3 -3 4 -4 5 -5 The values are 1 2 3 4 5 -1 -2 -3 -4 -5 1

45

//22. To sort the given data with menu driven program


#include <stdio.h> #include <conio.h> #define SIZE 10 void Input(int [],int); void Bubble_Sort(int [],int); void Selection_Sort(int [],int); void Insertion_Sort(int [],int); void Merge_Sort(int[],int [],int,int); void Display(int [],int); int main() { int response; int arr[SIZE]; clrscr(); do { clrscr(); printf("Enter [0] to Exit\n"); printf("Enter [1] to Input Array Elements.\n"); printf("Enter [2] to Bubble Sort.\n"); printf("Enter [3] to Slection Sort.\n"); printf("Enter [4] to Insertion Sort.\n"); printf("Enter [5] to Merge Sort.\n"); printf("Enter [6] to Display the Sorted Array.\n"); printf("\n\nEnter the choice: "); scanf("%d",&response); switch(response) { case 0: exit(1); case 1: Input(arr,SIZE); break; case 2: Bubble_Sort(arr,SIZE); break; case 3: Selection_Sort(arr,SIZE); break; case 4: Insertion_Sort(arr,SIZE); break; case 5:

46

//Merge_Sort(arr,SIZE); break; case 6: Display(arr,SIZE); break; } }while((response >0) && (response <=6)); getch(); return 0; } void Input(int a[],int n) { int i; printf("\nEnter the elements.\n"); for(i=0;i<n;i++) scanf("%d",&a[i]); } void Display(int a[],int n) { int i; printf("\nThe Array is....\n"); for(i=0;i<n;i++) printf("%d ",a[i]); getch(); } //function to perform bubble sort void Bubble_Sort(int arr[],int N) { int temp,i,j; for(i=0;i<N;i++) { for(j=0;j<(N-1)-i;j++) { if(arr[j]>arr[j+1]) { temp = arr[j]; arr[j] = arr[j+1]; arr[j+1] = temp; } } } } //function to perform selection sort void Selection_Sort(int arr[],int N) {

47

int min,pos,tmp; int i,j; for(i=0;i<N;++i) { min = arr[i]; for(j=i+1;j<N;++j) { if(arr[j]<min) { min = arr[j]; pos = j; } tmp = arr[i]; arr[i] = arr[pos]; arr[pos] = tmp; } } } //function to perform Insertion Sort void Insertion_Sort(int arr[],int N) { int tmp,j,i; for(i=1;i<N;++i) { tmp = arr[i]; j=i-1; while(tmp<=arr[j]) { arr[j+1] = arr[j]; j--; } arr[j+1] = tmp; } } //function to perform Merge Sort /*void Merge_Sort(int[],int [],int,int) { }*/

Enter [1] to Bubble Sort. Enter [2] to Insertion Sort. Enter [3] to Selection Sort. Enter [4] to Merge Sort. Enter [5] to Exit

48

OUTPUT:
Enter the choice: 1 Enter the elements. 1 9 2 8 3 8 4 7 5 6 The Array is.... 1 2 3 4 5 6 7 8 8 9

49

//23 (a) Recursively add the series of the n natural number


#include <stdio.h> #include <conio.h> int seriesSum(int); int main() { int N,sum; printf("Enter a value for N (b/w 1 to 200): "); scanf("%d",&N); sum = seriesSum(N); printf("\nSum of series, 1+2+3...+N, : %d",sum); getch(); return 0; } int seriesSum(int n) { if(n==0) return 0; else if(n == 1) return 1; else return(n+seriesSum(n-1)); }

OUTPUT:
Enter a value for N (b/w 1 to 200): 5 Sum of series, 1+2+3...+N, : 1

50

//23 (b) Recursively multiply the series of the n natural number


#include <stdio.h> #include <conio.h> int seriesProduct(int); int main() { int N; long int product; printf("Enter a value for N (b/w 1 to 20): "); scanf("%d",&N); product = seriesProduct(N); printf("\nProduct of series, 1*2*3...*N : %d",product); getch(); return 0; } int seriesProduct(int n) { if(n == 0) return 0; else if(n == 1) return 1; else return(n*seriesProduct(n-1)); }

OUTPUT:
Enter a value for N (b/w 1 to 20): 6 Product of series, 1*2*3...*N : 720

51

//23 (c) Recursively display the fibonnaci series


#include <stdio.h> #include <conio.h> int Fibonacci(int); int main() { int N,i; printf("Enter a value for N (b/w 1 to 20): "); scanf("%d",&N); printf("\nFibonacci Series: \n"); for(i=1;i<=N;i++) printf("%d ",Fibonacci(i)); getch(); return 0; } int Fibonacci(int n) { if(n<3) return 1; else return(Fibonacci(n-1)+Fibonacci(n-2)); }

OUTPUT:
Enter a value for N (b/w 1 to 20): 6 Fibonacci Series: 1 1 2 3 5 8

52

// 23 d) Write a program to input distance 'n'(1-20). Print how many ways a robot can move the distance n' provided the robot can move 1 meter or 2 meters in one step. Extend the idea if the robot can move 1 meter or 2 meter or 3 meter in one step.
#include<stdio.h> #include<conio.h> long int Permutation(int,int); int main() { int step,n; long int noOfways; clrscr(); printf("Enter the distance that the robot has to cover (b/w 1 to 20): "); scanf("%d",&n); while((n<1)||(n>20)) { printf("\nInvalid distance entered"); printf("\nEnter the valid distance again (b/w 1 to 20): "); scanf("%d",&n); } step = 2; noOfways=Permutation(n,step); printf("\nThe robot can cover %d m in steps of 1m or 2m in %ld ways",n,noOfways); step = 3; noOfways=Permutation(n,step); printf("\nThe robot can cover %d m in steps of 1m or 2m or 3m in %ld ways",n,noOfways); getch(); } long int Permutation(int n,int r) { if(r==1) return n; else return(n*Permutation(n-1,r-1)); }

OUTPUT:
Enter the distance that the robot has to cover (b/w 1 to 20): 5 The robot can cover 5 m in steps of 1m or 2m in 20 ways The robot can cover 5 m in steps of 1m or 2m or 3m in 60 ways

53

// 24 . A program to Input any positive integer number (n<=3000). Convert the number into Roman numerals.*/
#include <stdio.h> #include <conio.h> int main() { int n[7] = {1000,500,100,50,10,5,1}; char roman[7] = {'M','D','C','L','X','V','I'}; int i,j,k,num; clrscr(); printf("Enter a number(<=3000): "); scanf("%d",&num); printf("\nThe Roman Equivalent: "); for(i=0;i<7;++i) { j = num/n[i]; for(k=1;k<=j;k++) printf("%c",roman[i]); num = num%n[i]; } getch(); return 0; }

OUTPUT:
Enter a number(<=3000): 1234 The Roman Equivalent: MCCXXXIIII

54

// 25. A program for SEARCHING according to user's choice.


#include<stdio.h> #include<conio.h> # include<process.h> # define SIZE 30 void sort(int [],int); void Linear_serch(int [],int); void Binary_search(int [],int); //void Display(int [],int); void main() { int choice; int ar[SIZE]; clrscr(); do { printf("\n Press 0 for Exit \t"); printf("\n press 1 for LINEAR SEARCH\t"); printf("\n Press 2 for BINARY SEARCH \t"); scanf ("%d",&choice); switch(choice) { case 0: exit(1); break; case 1: Linear_serch(ar,SIZE); break; case 2: sort(ar,SIZE); Binary_search(ar,SIZE); break; } }while(choice>0 && choice<3); getch(); } void Linear_serch(int a[],int n) { int flag=0,i,num,temp; printf("\nEnter the elements.\n"); for(i=0;i<n;i++) scanf("%d",&a[i]); printf("\n Enter number for search\t"); scanf("%d",&temp); for(i=0;i<n;i++) {

55

if(a[i]==temp) { flag=1; break; } else flag=0; } if(flag==1) printf(" THE ELEMENT IS FOUND AT POSITION :"); printf("%d",(i+1)); if(flag==0) printf(" THE ELEMENT IS NOT FOUND"); } void Binary_search(int ar[],int n) { int beg,end,mid,item,loc=-1; printf("\n Enter number for search\t"); scanf("%d",&item); beg=0; end=n-1; while(beg<=end) { mid=(beg+end)/2; if(ar[mid]==item) { loc=mid; printf("\n Number is in sorted array at %d loc",loc+1); break; } else if(ar[mid]>item) end=mid-1; else beg=mid+1; } if (loc==-1) printf("\n Number not found"); } //void Input(int a[],int n) void sort(int a[],int n) { int i,j,tmp; printf("\nEnter the elements.\n"); for(i=0;i<n;i++) scanf("%d",&a[i]); // int i,j,tmp; //printf("\nThe Sorted Array is....\n");

56

for(i=0;i<n;i++) { for(j=0;j<i;j++) { if (a[j]>a[j+1]) { tmp=a[j]; a[j]=a[j+1]; a[j+1]=tmp; } } } // printf("%d ",a[i]); } OUTPUT: Press 0 for Exit press 1 for LINEAR SEARCH Press 2 for BINARY SEARCH Enter the elements. 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 1 2 3 3 4 4 4 4 4

57

4 4 4 4 Enter number for search 2 THE ELEMENT IS FOUND AT POSITION : 2 Press 0 for Exit press 1 for LINEAR SEARCH Press 2 for BINARY SEARCH

58

//26. Program to print 3 digit numbers such that second number is 2 times as the first number and third number is 3 times as the first number. Note the all 3 numbers should contain 1-9 digits and there should not be repetition of the digit.
#include<stdio.h> #include<conio.h> void main() { signed int i=0,x,y,z,A=0,B=0,C=0,C1; clrscr(); for(x=1;x<=6;x++) { for(y=1;y<=6;y++) { for(z=1;z<=6;z++) { if(x!=y && x!=z && y!=z) { A=100*x+10*y+z; B=2*A; C=3*A; C1=C; while(C>0) { C=C/10; i++; } if(i>3) goto b; else { printf("%d\t",A); printf("%d\t",B); printf("%d\n",C1); } } b: i=0; } } } getch(); }

59

OUTPUT:
123 124 125 126 132 134 135 136 142 143 145 146 152 153 154 156 162 163 164 165 213 214 215 216 231 246 248 250 252 264 268 270 272 284 286 290 292 304 306 308 312 324 326 328 330 426 428 430 432 462 369 372 375 378 396 402 405 408 426 429 435 438 456 459 462 468 486 489 492 495 639 642 645 648 693

60

// 27. A program to input any positive integer number and convert it into words.
#include<stdio.h> #include<conio.h> void main() { long int num; int a[10],i,j,c,b; clrscr(); printf("Enter any number(<999999999) : "); scanf("%ld",&num); a[0]=num%100; //56 2 num=num/100; a[1]=num%10; num=num/10; i=2; while(num!=0) { a[i]=num%100; num=num/100; i=i+1; } //printf("%d\n",i); for(j=i-1;j>=0;j--) //ist digit <-> last digit { // printf(" %d",j); flag2 : if(a[j]==0) goto flag1; if(a[j]<=20 && a[j]>0) { if(a[j]==1) printf("one "); if(a[j]==2) printf("two "); if(a[j]==3) printf("three "); if(a[j]==4) printf("four "); if(a[j]==5) printf("five "); if(a[j]==6) printf("six "); if(a[j]==7) printf("seven "); if(a[j]==8) printf("eight "); if(a[j]==9) 61

printf("nine "); if(a[j]==10) printf("ten "); if(a[j]==11) printf("eleven "); if(a[j]==12) printf("twelve "); if(a[j]==13) printf("thirteen "); if(a[j]==14) printf("forteen "); if(a[j]==15) printf("fifteen "); if(a[j]==16) printf("sixteen "); if(a[j]==17) printf("seventeen "); if(a[j]==18) printf("eighteen "); if(a[j]==19) printf("ninteen "); if(a[j]==20) printf("twenty "); } else { c=a[j]/10; //bcoz in previous eq. there is no j=0 a[j]=a[j]%10; if(c==2) printf("twenty "); if(c==3) printf("thirty "); if(c==4) printf("forty "); if(c==5) printf("fifty "); if(c==6) printf("sixty "); if(c==7) printf("seventy "); if(c==8) printf("eighty "); if(c==9) printf("ninty "); goto flag2; } if(j==4) printf("crore "); if(j==3) printf("lakh ");

62

if(j==2) printf("thousand "); if(j==1) printf("hundred "); flag1: } getch(); }

OUTPUT:
Enter any number(<999999999) : 1234 one thousand two hundred thirty four

63

//28. Program for towers of Hanoi problem.


#include<stdio.h> #include<conio.h> void move(int,char,char,char); int count=0; void main() { int n; clrscr(); printf("Enter the number of disks:"); scanf("%d",&n); move(n,'L','R','C'); printf("\nNumber of operations performed:%d",count); getch(); } void move(int n,char from,char to,char temp) { if(n>0) { count++; move(n-1,from,temp,to); printf("\nMove disk %d from %c to %c",n,from,to); move(n-1,temp,to,from); } }

OUTPUT:
Enter the number of disks:3 Move disk 1 from L to R Move disk 2 from L to C Move disk 1 from R to C Move disk 3 from L to R Move disk 1 from C to L Move disk 2 from C to R Move disk 1 from L to R Number of operations performed:7

64

//29. To find the length of the string without using standard function
#include <stdio.h> #include <conio.h> #define SIZE 80 int StringLenght(char *s); int main() { char str[SIZE]; int len; printf("\nPlease Enter the String: "); gets(str); len = StringLenght(str); printf("\nThe entered string is: \n"); puts(str); printf("\nThe length of the string: %d", len); getch(); return 0; } //to find the length of the string int StringLenght(char *s) { char *p = s; while(*p!='\0') p++; return p-s; }

OUTPUT:
Please Enter the String: pranti The entered string is: pranti The length of the string:

65

/ /30 (i). Program to copy one string into another string without using standard function
#include <stdio.h> #include <conio.h> #define SIZE 80 void StringCopy(char *,char *); int main() { char str1[SIZE] = "C is a Structured Programming Language."; char str2[SIZE]; printf("Copying string1 into string2...\n"); StringCopy(str1,str2); printf("\nString1: %s",str1); printf("\n\nString2: %s",str2); getch(); return 0; } //copy strings to each other void StringCopy(char *s,char *t) { while((*t = *s) != '\0') { t++; s++; } }

OUTPUT:
Copying string1 into string2... String1: C is a Structured Programming Language. String2: C is a Structured Programming Language.

66

// 30(ii). Program to concatenate 2 strings without using standard function


#include <stdio.h> #include <conio.h> #define SIZE 80 void Concatenate(char *,char *); int main() { char str1[SIZE],str2[SIZE]; printf(Enter 1st string: ); gets(str1); printf(Enter 2nd string: ); gets(str2); printf("Concatenating string2 into string1...\n"); Concatenate(str1,str2); printf("\nString : %s",str1); getch(); return 0; } // concatenate the string void Concatenate(char *s, char *t) { while(*s != '\0') // find the end of the string1 s++; while((*s = *t)!='\0') //concatenate at the end of 1st string { s++; t++; } }

OUTPUT:
Enter 1st string: Hi Enter 2nd string: How are you Concatenating string2 into string1... String : HiHow are you

67

68

69

Das könnte Ihnen auch gefallen