The document contains 50 C programming programs with their source code and output. The programs cover basic concepts like data types, operators, input/output, conditional statements, loops, functions, arrays, strings, pointers, structures etc. Some example programs include printing data types in different bases, calculating simple interest, converting temperature scales, finding the greatest among three numbers, calculating factorials and sums using loops, multiplication tables, string operations and more.
The document contains 50 C programming programs with their source code and output. The programs cover basic concepts like data types, operators, input/output, conditional statements, loops, functions, arrays, strings, pointers, structures etc. Some example programs include printing data types in different bases, calculating simple interest, converting temperature scales, finding the greatest among three numbers, calculating factorials and sums using loops, multiplication tables, string operations and more.
The document contains 50 C programming programs with their source code and output. The programs cover basic concepts like data types, operators, input/output, conditional statements, loops, functions, arrays, strings, pointers, structures etc. Some example programs include printing data types in different bases, calculating simple interest, converting temperature scales, finding the greatest among three numbers, calculating factorials and sums using loops, multiplication tables, string operations and more.
S.NO PROGRAMS 1 Program to print octal integer decimal integer & hexadecimal integer 2 Program to print character & its ascii value 3 Program to calculate simple interest 4 Program to convert temperature in centigrade into Fahrenheit 5 Program to read length & breadth of rectangle & calculate its area 6 Program to find compound interest 7 Program to print date of birth 8 Program to read 2 integer from keyboard & check whether 1 st integer is exactly divisible by 2 nd or not.
9 Program to find greatest number among 3 different numbers 10 Program to read the marks of student in various subjects of B.Sc.CSIT 1 st sem then calculate the percent obtained by them & output the division. The condition to be used are:
11 .Write a program to read an integer number n from keyboard & Display the message get well soon n times 12 Program to calculate the factorial of n number using for loop 13 Program to calculate sum of n natural numbers 14 Multiplication table for 1 to 10 15 Program to compute the sum of square of n number 16 Program to check prime number 17 Program to add 2 numbers & display their sum. The program must ask next 2 numbers & add till user wants.
18 Program to ask two number & display message either number is negative, if one of number is negative , otherwise display message both number are positive.
19 Program to take a float value as an input & display the rightmost digit of the integral part of it.
3
20 Program to add subtract multiply & divide 2 complex number using switch statement.
21 Program for reversing a number 22 Program to check Armstrong 23 Program to check palindrome number 24 Program to access array elements 25 Program to show memory address of array element 26 Program to sort array elements 27 Program to read a matrix of size m*n & display matrix on a screen 28 Program to find transpose of matrix 29 Program to find the sum of square of diagonal of square matrix 30 Program to compute the product of 2 matrixes if possible.
31 Program to initializing string 32 Program to check palindrome 33 Program to reverse a string 34 Program to count length of the string 35 Program to copy string 36 Program to combining 2 strings 37 Write a program to count no. of occurrences of 2 vowels in succession in a line of text.
38 Program to count the no. of space & words in a string. 39 Program to multiply an integer to a float number using function. The return type must be float.
40 Write a program using function to find the area of 2 different circle which accepts radius of float value & return the area of circle .use symbolic constant to define PI.
41 Program using function to find greatest number among 3 different number
4
42 Program to swap the values of 2 variables 43 Factorial of a number using recursion 44 Program to find a b using recursion 45 A program to display memory location reserved by a variable 46 Program to calculate average marks of 10 student in a subject using pointer.
47 Write a function that takes 1-D array of n numbers & sort the elements in ascending order. Use dynamic memory allocation 48 Program to copy an array of double using pointer 49 Create a structure named student that has name, roll, marks, and remarks as members. Assume appropriate types and size of member. Write a program using structure to read and display the data entered by the user.
50 Given a text file, create another text file deleting the following words three, bad, and time
5
1.Program to print octal integer decimal integer & hexadecimal integer. #include <stdio.h> #include <conio.h> void main() { inta,b,c; clrscr(); a=100; b=101; c=0x9F; printf("Decimal integer is %d",a); printf("\nOctal integer is %d",b); printf("\nHexadecimal integer is %d",c); getch(); } OUTPUT:- Decimal integer is 100 Octal integer is 101 Hexa decimal integer is 159
2.Program to print character & its ascii value #include <stdio.h> #include <conio.h> void main() { char x=a; clrscr(); printf(the character is %c,x); printf("\nASCII value of %c is character %d",x,x); getch(); } OUTPUT:- The character is a. ASCII value of character is 97.
int P,T,R,S; clrscr(); P=1000; T=2; R=5; S=(P*T*R)/100; printf(simple intrest is %d,S); getch(); } OUTPUT:- Simple intrest is 10
4.Program to convert temperature in centigrade into Fahrenheit #include<stdio.h> #include<conio.h> void main() { floatf,c; clrscr(); printf(enter the temperature in centigrade); scanf(%f,&c); f=1.8*c+32; printf(temperature in Fahrenheit is %f,f); getch(); } OUTPUT:- Enter the temperature in centigrade 25 Temperature in fahrenheit is 77
5.Program to read length & breadth of rectangle & calculate its area #include<stdio.h> #include<conio.h> void main() { float l,b,a; clrscr(); printf(enter length & breadth of rectangle); scanf(%f%f,&l,&b); a=l*b; printf(area is %f,a); getch();
7
} OUTPUT:- Enter length and breadth of rectangle 7 5 Area is 35.
6.Program to find compound interest #include<stdio.h> #include<conio.h> #include<math.h> void main() { floatp,t,r,CI; clrscr(); printf(enter principal); scanf(%f,&p); printf(enter time); scanf(%f,&t); printf(enter rate); scanf(%f,&r); CI=p*(pow(1+r/100,t)-1); printf(the compound interest is %f,CI); getch(); } OUTPUT:- Enter principal 10000 Enter time 2 Enter rate 5 The compound interest is 1025.
7.Program to print date of birth #include<stdio.h> #include<conio.h> void main() { intday,month,year; clrscr(); printf(enter day month year in dd-mm-yyyy format); scanf(%d-%d-%d,&day,&month,&year); printf(\n day:%d\tmonth:%d\tyear:%d,day,month,year); getch(); }
8
OUTPUT:- Enter day month and year in dd-mm-yyyy format 26 03 2051 Day:26 month:03 year:2051
8.Program to read 2 integer from keyboard & check whether 1 st integer is exactly divisible by 2 nd or not. #include<stdio.h> #include<conio.h> void main() { int num1,num2,rem; clrscr(); printf(enter 1 st number); scanf(%d,&num1); printf(enter 2 nd numder); scanf(%d,&num2); rem=num1%num2; if(rem==0) printf(1 st integer is exactly divisible by 2 nd ); else printf(1 st integer is not exactly divisible by 2 nd ); getch(); } OUTPUT:- Enter 1 st number 4 Enter 2 nd number 2 1 st number is exactly divisible by 2 nd number.
9.Program to find greatest number among 3 different numbers. #include<stdio.h> #include<conio.h> void main() { int n1,n2,n3; clrscr(); printf(enter 3 numbers); scanf(%d%d%d,&n1,&n2,&n3); if(n1>n2 && n1>n3) printf(%d is greatest,n1); else if(n2>n1 &&n2>n3) printf(%d is greatest,n2);
9
else printf(%d is greatest ,n3); getch(); } OUTPUT:- Enter 3 numbers 2 5 3 5 is greatest number
10.Program to read the marks of student in various subjects of B.Sc.CSIT 1 st sem then calculate the percent obtained by them & output the division. The condition to be used are: %greater than or equal to 80->distinction %between 70 &79 ->1 st division %between 60 & 69->2 nd division %between 50 & 59->3 rd division %less than 50->fail. #include<stdio.h> #include<conio.h> void main() { floata,b,c,d,e,percent; clrscr(); printf("enter marks of stat:"); scanf("%f",&a); printf("enter marks of stat &prob:"); scanf("%f",&b); printf("enter marks of calculus:"); scanf("%f",&c); printf("enter marks of FIT:"); scanf("%f",&d); printf("enter marks of C.programming:"); scanf("%f",&e); percent=((a+b+c+d+e)/5)*100; if (percent>=80) printf("distintion"); else if (70<=percent<=79) printf("first division"); else if (60<=percent<=69) printf("second division"); else if (50<=percent<=59)
10
printf("third division"); else printf("fail"); getch(); } OUTPUT:- Enter marks of stat 69 Enter mark of stat &prob 71 Enter marks of caculus 51 Enter marks of FIT 66 Enter mark of c.progaramming 56 Second division
11.Write a program to read an integer number n from keyboard & Display the message get well soon n times. #include<stdio.h> #include<conio.h> void main() { intc,n; printf(enter n); scanf(%d,&n); for(c=0;c<n;c++) printf(\n get well soon); getch(); } OUTPUT:- Enter n: 2 Get well soon Get well soon
12.Program to calculate the factorial of n number using for loop. #include<stdio.h> #include<conio.h> void main() { intn,c,fact=1; printf(enter number); scanf(%d,n);
11
for(c=0;c<n;c++) fact=fact*c; printf(the factorial is %d,fact); getch(); } OUTPUT:- Enter number 4 The factorial is 24
13.Program to calculate sum of n natural numbers. #include<stdio.h> #include<conio.h> void main() { intn,c,sum=0; printf(enter number); scanf(%d,n); for(c=1;c<=n;c++); sum+=c; printf(sum is %d,sum); getch(); } OUTPUT:- Enter number 4 Sum is 10
Getch(); } OUTPUT:- Its output will be the multiplication table of 1 to 10.
15.Program to compute the sum of square of n number. #include<stdio.h> #include<conio.h> void main() { intn,c,sum=0; clrscr(); printf(enter number); scanf(%d,n); while(c<=n) { sum+=c*c; c++; } printf(sum of square of n number is %d,sum); getch(); } OUTPUT:- Enter number 3 Sum of square of n number is 14
16.Program to check prime number. #include<stdio.h> #include<conio.h> void main() { intn,c; printf(enter number); scanf(%d,n); for(c=0;c<n;c++) { if(n%c==0) { printf(\n not prime); break;
13
} } if(c==n) printf(\n prime number); getch(); } OUTPUT:- Enter number 5 Prime number
17.Program to add 2 numbers & display their sum. The program must ask next 2 numbers & add till user wants. #include<stdio.h> #include<conio.h> void main() { inta,b,sum;char choice=y; while(choice==y) { printf(enter 2 number to be added:\t); scanf(%d%d,&a,&b); sum=a+b; printf(the sum is %d,sum); printf(\n do you want to add another two numder?); printf(\npress y for yes & other characters for exit); scanf( %c,choice); } getch(); } OUTPUT:- Enter two number to be added 2 3 The sum is 5.Press y for yes & other character for exit
18.Program to ask two number & display message either number is negative, if one of number is negative , otherwise display message both number are positive. #include<stdio.h> #include<conio.h> void main()
14
{ int num1,num5; clrscr(); printf(enter 1 st number); scanf(%d,&num1); if(num1<0) goto negative; printf(enter 2 nd number); scanf(%d,&num2); if(num2<0) goto negative; printf(both number are positive); getch(); return; negative:printf(\neither number is positive); getch(); } OUTPUT:- Enter 1 st number 3 Enter 2 nd number 2 Both numbers are positive
19.Program to take a float value as an input & display the rightmost digit of the integral part of it. #include<stdio.h> #include<conio.h> void main() { float f; int i; clrscr(); printf(enter float value); scanf(%f,&f); i=f; printf(the integral part is %d,i); i=i%10; printf(the rightmost digit is %d,i); getch(); } OUTPUT:- Enter float value 12.6
15
The integral part is 12 The rightmost digit is 0.6
20.Program to add subtract multiply & divide 2 complex number using switch statement. #include <stdio.h> #include <conio.h> void main() { inta,b,x,y,real,img;charopr; clrscr(); printf("\nEnter first complex number of the form (a+ib):"); scanf("%d+i%d", &a, &b); printf("\nEnter second complex number of the form (x+iy):"); scanf("%d+i%d", &x, &y); printf("\nEnter one of the operators among +, -, *, /:\t"); scanf(" %c",&opr);
switch(opr) { case '+': real=ax; imp=by; printf("\the addition is:%d+i%d.",real,img); break; case '-': real=a-x; imp=b-y; printf("\the subtraction is:%d+i%d.",real,img); break; case '*': real=a*x-b*y; imp=a*y+b*x; printf("\the multiplication is:%d+i%d",real,img); break; case '/': real=(a*x+b*y)/(x*x+y*y); imp=(b*x-a*y)/(x*x+y*y); printf("\the division is:%d+i%d",real,img); break; default: printf("\nInvalid Operator."); }
16
getch(); } OUTPUT:- Enter first complex number of the form (a+ib): 2+3i Enter second complex number in the form(x+iy): 1+2i Enter one of the operator among +,-,*,/ + The addition is 3+5i
21.Program for reversing a number. #include<stdio.h> #include<conio.h> void main() { intn,digit,rev=0; printf(enter number you want to reverse); scanf(%d,&n); while(n!=0) { digit=n%10; rev=rev*10+digit; n=n/10 } printf(\n the reverse of the entered number is %d,rev); getch(); } OUTPUT:- Enter number you want to reverse 123 The reverse of entered number is 321
22.Program to check Armstrong #include <stdio.h> #include<conio.h> void main() { intnum,digit,sum=0,temp; printf(enter number to be checked); scanf(%d,&num);
17
temp=num; while(num!=0) { digit=num%10; sum+=digit*digit*digit; num/=10; } if(temp==sum) printf(\n Armstrong number); else printf(\n not Armstrong); getch(); } OUTPUT:- Enter number to be checked 153.Armstrong number
23.Program to check palindrome number. #include<stdio.h> #include<conio.h> void main() { intnum,digit,rev=0,temp; clrscr(); printf(enter number to be checked); scanf(%d,&num); temp=num; while(num!=0) { digit=n%10; rev=rev*10+digit; n=n/10 } if(temp==rev) printf(palindrome); else printf(not palindrom); getch(); } OUTPUT:- Enter the number is to be checked 121
18
Palindrome
24.Program to access array elements. #include<stdio.h> #include<conio.h> void main() { int a[5]; printf("\n enter 5 number:\t"); for(i=0;i<5;i++) scanf("%d",&a[i]); printf("\nwe have entered these 5 numbers:\t"); for(i=0;i>=5;i--) Printf(\t a[%d]=%d,i,a[i]); getch(); } OUTPUT:- Enter 5 number 5 2 3 6 4 We have enterd these 5 numbers A[0]=5 a[1]=2 a[2]=3 a[3]=6 a[4]=4
25.Program to show memory address of array element. #include<stdio.h> #include<conio.h> void main() { int a[5]; printf("\n enter 5 number:\t"); for(i=0;i<5;i++) scanf("%d",&a[i]); printf("\nwe have entered these 5 numbers:\t"); for(i=0;i>=5;i--) printf(\t a[%d]=%d,i,a[i]); printf(\nthe continuous memory locations are:\t); for(i=0;i<5;i++) printf(\n[%d]=%d is located at \t %u,i,a[i],&a[i]); getch(); }
19
OUTPUT:- Enter 5 number 5 2 3 6 4 We have enterd these 5 numbers A[0]=5 a[1]=2 a[2]=3 a[3]=6 a[4]=4 The continuous memory location are A[0]=5 is located ar . A[1]=2 is located at
26.Program to sort array elements. #include<srdio.h> #include<conio.h> void main() { inti,j,temp,num[n],n; clrscr(); printf(how many number you want to sort:); scanf(%d,&n); for(i=0;i<n;i++) scanf(%d,&num[i]); for(i=0;i<n-1;i++) { for(j=i+1;j<n;j++) { if(nums[i]>nums[j]) { temp=nums[i]; nums[i]=nums[j]; nums[j]=temp; } }
} printf("\the numbers in ascending order are:\n"); for(i=0;i<n;i++) printf("\t%d",nums[i]); } getch(); }
OUTPUT:-
20
How many number u want to sort 5 5 3 9 4 7 The numbers in ascending order are 3 4 5 7 9
27.Program to read a matrix of size m*n & display matrix on a screen. #include<stdio.h> #include<conio.h> #define M 2 #define N 3 void main() { int A[M][N],i,j; clrscr(); printf("\nEnter elements of matrix A of order %d*%d:",M,N); for(i=0;i<M;i++) { for(j=0;j<N;j++) { scanf("%d",&A[i][j]); } } printf(\n the entered matrix is:\n); for(i=0;i<M;i++) { for(j=0;j<N;j++) { printf(%d\t,A[i][j]); } printf(\n); getch(); } OUTPUT:- Enter the elements of matrix a of order 2*3 4 5 9 8 2 1 The entered matrix is
29.Program to read two matrix of order m*n & display their sum. #include<stdio.h> #include<conio.h> #define M 3
21
#define N 3 void main() { int A[M][N],B[M][N],sum[M][N],i,j; clrscr(); printf("\nEnter elements of matrix A of order %d*%d:",M,N); for(i=0;i<M;i++) { for(j=0;j<N;j++) scanf("%d",&A[i][j]); } printf(\n the entered matrix is:\n); for(i=0;i<M;i++) { for(j=0;j<N;j++) printf(%d\t,A[i][j]); printf(\n); printf("\nEnter elements of matrix B of order %d*%d:",M,N); for(i=0;i<M;i++) { for(j=0;j<N;j++) scanf("%d",&B[i][j]); } printf(\n the entered matrix is:\n); for(i=0;i<M;i++) { for(j=0;j<N;j++) printf(%d\t,B[i][j]); printf(\n); for(i=0;i<M;i++) { for(j=0;j<N;j++) sum[i][j]=A[i][j]+B[i][j]; } Printf(the sum of the matrix is \n); for(i=0;i<M;i++) { for(j=0;j<N;j++) printf(\t%d\t,sum[i][j]); } getch(); } OUTPUT:- Enter the element of matrix a of oder 3*3 1 2 3 6 9 5 4 8 7 The entered matrix is
Enter the element of second matrix of oder 3*3 5 4 7 8 6 3 3 2 1
22
The entered matrix is
The sum of the matrix is
28.Program to find transpose of matrix. #include<stdio.h> #include<conio.h> #define M 3 #define N 3 void main() { int A[M][N],transpose[M][N],i,j; clrscr(); printf("\nEnter elements of matrix A of order %d*%d:",M,N); for(i=0;i<M;i++) { for(j=0;j<N;j++) { scanf("%d",&A[i][j]); } } printf(\n the matrix to be transposed is:\n); for(i=0;i<M;i++) { for(j=0;j<N;j++) printf(%d\t,A[i][j]); printf(\n); } for(i=0;i<M;i++) { for(j=0;j<N;j++) transpose[j][i]=A[i][j]; } printf(\n the transposed matrix is :\n); for(i=0;i<M;i++)
23
{ for(j=0;j<N;j++) printf(%d\t,transpose[i][j]); printf(\n); } getch(); } OUTPUT:- Enter elements of matrix of order 3*3: 4 5 6 9 8 7 1 2 3 The matrix is to be transpose is
The transpose of matrix is
29.Program to find the sum of square of diagonal of square matrix. #include<stdio.h> #include<conio.h> #define M 3 #define N 3 void main() { int A[M][N],sum[M][N],i,j; clrscr(); printf("\nEnter elements of matrix A of order %d*%d:",M,N); for(i=0;i<M;i++) { for(j=0;j<N;j++) { scanf("%d",&A[i][j]); } } printf(\n the given matrix is:\n); for(i=0;i<M;i++) { for(j=0;j<N;j++) printf(%d\t,A[i][j]); printf(\n);
24
} for(i=0;i<M;i++) { for(j=0;j<N;j++) { if(i==j) sum+=a[i][j]*a[i][j]; } } printf(the sum of square of diagonal is %d,sum); getch(); }
OUTPUT:- Enter the element of matrix a of order 3*3; 1 2 3 4 5 6 7 8 9 The given matrix is
The sum of square of diagonals are; 107
30.Program to compute the product of 2 matrixes if possible. #include<stdio.h> #injclude<conio.h> void main() { int matrix1[10][10],matrix2[10][10],product[10][10]; intm,n,p,q,i,j,row_mul_col=0; clrscr(); printf("\nEnter order of first matrix (less than 10*10):\t"); scanf("%d %d",&m,&n); printf("\nEnter order of second matrix (less than 10*10):\t"); scanf("%d %d",&p,&q); if (n!=p) { printf("\the matrices are unsuitable for multiplication.\n"); getch(); exit(); } printf("\nEnter the elements of first matrix:\t"); for(i=0;i<m;i++)
25
{ for(j=0;j<n;j++) { scanf("%d",&matrix1[i][j]); } } printf("\nEnter the elements of second matrix:\t"); for(i=0;i<p;i++) { for(j=0;j<q;j++) { scanf("%d",&matrix2[i][j]); } } for(i=0;i<m;i++) { for(j=0;j<q;j++) { for(k=0;k<n;k++) { row_mul_col = row_mul_col + matrix1[i][k]*matrix2[k][j]; } product[i][j] = row_mul_col; row_mul_col=0; } }
32.Program to check palindrome. #include <stdio.h> #include <conio.h> void main() { char string[50],rev[50]; int diff=0; clrscr(); printf("Enter string:\t"); gets(string);
27
strcpy(rev,string); strrev(string); diff=strcmp(string,rev); if(diff==0) printf("\the input string is palindrome.");
else printf("\the input string is not palindrome."); } getch(); } OUTPUT:- Liril The input string is palindrome.
33.Program to reverse a string. #include <stdio.h> #include <conio.h> void main() { char string[50]; clrscr(); printf("Enter string:\t"); gets(string); strrev(string); printf(the reversed string is %s,string); getch(); } OUTPUT:- Enter string:asana The reversed string is anasa
34.Program to count length of the string. #include <stdio.h> #include <conio.h> void main() { char string[50];int l;
28
clrscr(); printf("Enter string:\t"); gets(string); l=strlen(string); printf(\n the length is %d,l); getch(); } OUTPUT:- Enter string Manisha The length is 8
35.Program to copy string. #include <stdio.h> #include <conio.h> void main() { char copy[50],paste[50]; clrscr(); printf("\nEnter your name (to copy):\t"); gets(copy); strcpy(paste,copy); printf("\n the name is pasted as :\t); puts(paste); getch(); } OUTPUT:- Enter your name to copy Manisha The name is pasted as kabita
37.Write a program to count no. of occurrences of 2 vowels in succession in a line of text. #include<stdio.h> #include<conio.h> void main() { char c[100]; int i=0,j,count=0; printf("enter a string\t"); gets(c); for(i=0;c[i]!='\0';i++) { if(c[i]=='a'||c[i]=='e'||c[i]=='i'||c[i]=='0'||c[i]=='u') { j=i+1; if(c[j]=='a'||c[j]=='e'||c[j]=='i'||c[j]=='o'||c[j]=='u') { count++; i=j; } } } printf("%d",count); getch(); } OUTPUT:- Enter a string Manisha 4
38.Program to count the no. of space & words in a string. #include<stdio.h> #include<conio.h> void main() { char c[100]; intI,l=0,w=0; printf("enter a string\t"); gets(c); for(i=0;c[i]!=\0;i++) {
30
if(s[i]== ) l=l+1; else w=w+1; } printf(no. of words in a string is %d,w); printf(\n no, of spaces in a string is %d,l); getch(); } OUTPUT:- Enter a string Manisha No. of words in a string 7 No of space in a string 0
N NE NEP NEPA NEPAL #include<stdio.h> #include<conio.h> void main() { char c[6]=NEPAL; inti,j; for(i=0;i<6;i++) { j=i+1; printf(%-5.*s\n,j,c); } getch(); }
39.Program to multiply an integer to a float number using function. The return type must be float. #include<stdio.h> #include<conio.h> floatmul(float c,int d) {
40.Write a program using function to find the area of 2 different circle which accepts radius of float value & return the area of circle .use symbolic constant to define PI. #include<stdio.h> #include<conio.h> #define PI 3.14 float area(float r) { float a; a=PI*r*r; return a; } void main() { float r1,r2,a1,a2; printf(enter radius of 1 st & 2 nd circle ); scanf(%f%f,&r1,&r2); a1=area(r1); a2=area(r2); printf(area of 1 st circle is %f,a1); printf(area of 2 nd circle is %f,a2); getch(); } OUTPUT:- Enter radii of 1 st and 2 nd circle 4.5 6.6 Area of 1 st circle is 63.585 Area of 2 nd circle is 136.7784
32
41.Program using function to find greatest number among 3 different number. #include<stdio.h> #include<conio.h> int greater(int,int); void main() { inta,b,c,d,e; printf(enter 3 numbers); scanf(%d%d%d,&a,&b,&c); d=greater(a,b); e=greater(b,c); printf(\n the greatest number is %d,e); getch(); } int greater(intx,int y) { if(x>y) return x; else return y; } OUTPUT:- Enter 3 numbers 5 7 2 The greatest number is 7.
42.Program to swap the values of 2 variables . #include <stdio.h> #include <conio.h> void swap(int ,int); void main() { int a=50,b=100; printf("\nBefore swap function call: a=%d and b=%d",a,b); swap(a,b); printf("\nAfter swap function call: a=%d and b=%d",a,b); getch(); } void swap(int x, int y) { int temp; temp=x; x=y; y=temp; printf("\nValues within swap: x=%d and y=%d",x,y);
33
} 43.Factorial of a number using recursion. #include<stdio.h> #include<conio.h> longint fact(int n) { if(n==1) return 1; else return(n*fact(n-1)); } void main() { intnum; long int x; clrscr(); printf(enter a number whose factorial is needed); scanf(%d,num); x=fact(num); printf(\n the factorial is %ld,x); getch(); }
44.Program to find a b using recursion. #include<stdio.h> #include<conio.h> int f(intx,int y) { if(y==1) return x; else return(x*f(x,y-1)); } void main(); { intnum; long inta,b,c; printf(enter 2 number); scanf(%d%d,&a,&b); c=f(a,b); printf(the value of a^b is %d,c); getch();
34
}
45.A program to display memory location reserved by a variable. #include<stdio.h> #include<conio.h> void main() { int a; printf(enter the value of a :); scanf(%d,&a); printf(the address of a is %u,&a); printf(the value of a is %d,a); getch(); }
46.Program to calculate average marks of 10 student in a subject using pointer. #include<stdio.h> #include<conio.h> void main() { int marks[10],I,sum; floatavg; printf(enter marks of each student:); for(i=0;i<10;i++) { scanf(%d,marks+i); sum+=*(marks+i); } avg=sum/10; printf(\n the average is %f,avg); getch(); }
47.Write a function that takes 1-D array of n numbers & sort the elements in ascending order. Use dynamic memory allocation. #include<stdio.h> #include<conio.h>
35
void sort(float *,int ); void main(); { float *f; intn,i; printf(\n enter the number of element in your array); scanf(%d,&n); f=(float*)malloc(n*sizeof(float)); printf(enter the array elements \n); for(i=0;i<n;i++) scanf(%f,f+i); printf(\n the unsorted list of array elements & their address are:\n); for(i=0;i<n;i++) printf(\n\t%f\t\t%u,*(f+i),f+i); sort(f,n); printf(\n the sorted list & their address are :\n); for(i=0;i<n;i++) printf(\n\t%f\t\t%u,*(f+i),f+i); getch(); } void sort(float *g,int n) { inti,j; float temp; for(i=0;i<n;i++) { for(j=i+1;j<n;j++) { if(*(g+i)>*(g+j)) { temp=*(g+i); *(g+i)=*(g+j); *(g+j)=temp; } } } }
48.Program to copy an array of double using pointer. #include<stdio.h> #include<conio.h> #define M 5
49.Create a structure named student that has name, roll, marks, and remarks as members. Assume appropriate types and size of member. Write a program using structure to read and display the data entered by the user. #include<stdio.h> #include<conio.h> void main() { struct student { char name[20]; int roll; float marks; char remarks; }; struct student s; clrscr(); printf("Enter name:\t"); gets(s.name); printf("\n Enter roll:\t"); scanf("%d", &s.roll); printf("\n Enter marks:\t"); scanf("%f", &s.marks); printf("\n Enter remarks(P for pass or F for fail):\t"); scanf(" %c", &s.remarks); printf("\n Name \t Roll \t Marks \t Remarks\n"); printf("\n...................................\n");