74
C Programming File Submitted by: MCA(I yr – 2009)

c Programming File

Embed Size (px)

Citation preview

Page 1: c Programming File

C Programming

File

Submitted by:MCA(I yr – 2009)

Page 2: c Programming File

Index

Program

.No.Program Date

Page No

T.Sign

1 To calculate the absolute value of any number 1

2Printing the numbers which are divisible by 7 and 100

2

3 Finding the largest number 3

4 Reverse of a number 4

5 Calculating the power of a number 5

6 Use of Bitwise Operators 6

7 Roots of Quadratic Equation 8

8Reading a character in Uppercase and check whether it is vowel or not.

9

9 Printing the Multiplication Tables 10

10Generating the Armstrong numbers between 2 – 1000

11

11 Fibonacci series using function 12

12 Factorial using recursion 13

13 Printing ASCII value and equivalent characters 14

14 Finding the binomial coefficient using functions 15

15Computing the average, maximum, minimum value

16

16 Pattern 1 2 3 4 5

2 3 4 5 63 4 5 6 7

17

Page 3: c Programming File

17 Calculating the GCD 18

18

Pattern ABCDCBA

ABC CBAAB BAA A

20

19 Prime number generation 21

20

Star Pattern *

********

22

21

Pattern 1

2 3 4 5 6 7 8 9 10

23

22 Binary Search 24

23To Calculate the sum of the seriesx-x3/3!+x5/5!-x7/7!...............

26

24 To compare two Square Matrices 28

25 To find the factors of a number 30

26 Calculating the LCM of two numbers 31

27To Divide the elements of the array into odd and even array

32

28To enter two variables and to calculate their reverse order using pointers

34

29To find the length of string including spaces and excluding spaces

36

30To print the sum of diagonals of a square matrix

38

31 Program to illustrate nested Structure 40

32To print different values being pointing to by

44

Page 4: c Programming File

an array of pointers

33Calculating the total number of digits and their sum in an alpha numeric string

46

34 Finding sum of numbers using functions 47

35 Passing of a Structure to a function 48

36Write menu driven program of file toADD, MODIFY, LIST, DELETE

49

37 Program of array of pointers to functions 51

38 Program of Command Line argument 52

Page 5: c Programming File

Program No – 1

Program code:

//To calculate the absolute value of any number#include<stdio.h>#include<conio.h>void main(){

int n,n1;clrscr();printf("\nEnter any number to get its absolute value: ");

scanf("%d",&n);if(n>=0){

n1=n;printf("The absolute value of |%d| is: %d",n,n1);

}else{

n1=-1*n;printf("The absolute value of |%d| is: %d",n,n1);

}getch();

}

Output

Enter any number to get its absolute value: -98The absolute value of |-98| is: 98

Page 6: c Programming File

Program No – 2

Program code:

//Printing the numbers which are divisible by 7 and 100#include<stdio.h>#include<conio.h>void main(){

int a[5],i;clrscr();printf("\nTo check whether the numbers are divisible by 7 and 100.");printf("\nEnter the numbers: ");for(i=0;i<5;i++)

scanf("%d",&a[i]);for(i=0;i<5;i++){

if((a[i]%7==0)&&(a[i]%100==0))printf("\n%d is divisible by 7 and 100.",a[i]);

elseprintf("\n%d is not divisible by 7 and 100.",a[i]);

}getch();

}

Output

To check whether the numbers are divisible by 7 and 100.Enter the numbers: 7005689014007

700 is divisible by 7 and 100.56 is not divisible by 7 and 100.890 is not divisible by 7 and 100.1400 is divisible by 7 and 100.7 is not divisible by 7 and 100.

Page 7: c Programming File

Program No – 3

Program code:

//Finding the largest number#include<stdio.h>#include<conio.h>void main(){

int a[5],max,i;clrscr();printf("Finding the largest number :-\n");printf("Enter the numbers :-\n");for(i=0;i<5;i++)

scanf("%d",&a[i]);max=a[0];for(i=1;i<5;i++){

if(max<a[i])max=a[i];

}printf("\nThe largest number is: %d.",max);getch();

}

Output

Finding the largest number :-Enter the numbers :- 56879756905The largest number is: 879

Page 8: c Programming File

Program No – 4

Program code:

//Reverse of a number#include<stdio.h>#include<stdlib.h>#include<conio.h>void main(){

int n,a,i;clrscr();printf("Enter the number to get the reverse: ");

scanf("%d", &n);//Checking the rangeif(n>32767){

printf("\nThe number u entered is out of range");exit(0);

}//printing the reverseprintf("\nThe reverse is: ");for(i=1;i<=5;i++){

a=n%10;n=n/10;if((a==0)&&(n==0))

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

}getch();

}

Output

Enter the number to get the reverse: 6570The reverse number is: 0756

Page 9: c Programming File

Program No – 5

Program code:

//Calculating the power of a number#include<stdio.h>#include<conio.h>int main(){ int n,p,i;

long int pow;clrscr();printf("\nEnter the number and its power: ");

scanf("%d%d",&n,&p);pow=1;//calculating the powerfor(i=1;i<=p;i++)

pow=(long int)pow*n; printf("\n%d to the power of %d is: %ld ",n,p,pow); getch(); return 0;}

Output

Enter the number and its power: 747 to the power of 4 is: 2401

Page 10: c Programming File

Program No – 6

Program code:

//Use of Bitwise Operators#include<stdio.h>#include<conio.h>void main(){int a,b,c,i;clrscr();;printf("\nSelect a bitwise operator to check \n");printf("\nTO CHECK \n& PRESS :1");printf("\n^ PRESS :2");printf("\n| PRESS :3");printf("\n~ PRESS :4");printf("\n<< PRESS :5");printf("\n>> PRESS :6");printf(“\nEnter your choice”);scanf("%d",&i);switch(i){case 1: printf("\nENTER a AND b\n");

scanf("\n%d\n%d",&a,&b); c=a&b; printf("\n%d",c); break;

case 2: printf("\nENTER a AND b\n"); scanf("\n%d\n%d",&a,&b); c=a^b; printf("\n%d",c); break;

case 3: printf("\nENTER a AND b\n"); scanf("\n%d%d",&a,&b); c=a|b; printf("\n%d",c); break;

case 4: printf("\nENTER a\n"); scanf("\n%d",&a); c=~a; printf("\n%d",c); break;

case 5: printf("\nENTER a AND b\n"); scanf("\n%d\n%d",&a,&b); c=a<<b; printf("\n%d",c); break;

case 6: printf("\nENTER a AND b\n"); scanf("\n%d\n%d",&a,&b); c=a>>b; printf("\n%d",c);

Page 11: c Programming File

break;default: printf("\nYOUR CHOICE IS NOT FROM MENU");

break;}getch();}

OutputSelect a bitwise operator to check

TO CHECK & PRESS :1 ^ PRESS :2 | PRESS :3 ~ PRESS :4<< PRESS :5 >> PRESS :6 Enter your choice: 2

ENTER a AND b 4 5

1

Page 12: c Programming File

Program No – 7

Program code:

//Roots of Quadratic Equation#include<stdio.h>#include<conio.h>#include<math.h>int main(){ float a,b,c,d,e,f;

printf("Enter the values of a,b,c: ");scanf("%f%f%f",&a,&b,&c);d=b*b-4*a*c;if(d>0){

e=(-b+sqrt(d))/(2*a);f=(-b-sqrt(d))/(2*a);printf("Root1 %f",e);printf("Root2 %f \n",f);printf("Roots are real and Unequal \n");

}elseif(d==0){

e=-b/(2*a);f=-b/(2*a);

printf("Root1 %f",e);printf("Root2 %f \n",f);printf("Roots are real and equal\n");

}elseprintf("Roots are complex & imaginary");getch();

return 0;}

Output

Enter the values of a,b,c:121Root1 -1.000000 Root2: -1.000000Roots are real and equal.

Page 13: c Programming File

Program No – 8

Program code:

//Reading a character in Uppercase and check whether it is vowel or not.#include<stdio.h>#include<conio.h>void main(){char ch;clrscr();do{ printf("\n"); ch=getche(); if(ch>=65&&ch<=90) { switch(ch) { case'A':printf("\nIt's a vowel");

break; case'E':printf("\nIt's a vowel");

break; case'I':printf("\nIt's a vowel");

break; case'O':printf("\nIt's a vowel");

break; case'U':printf("\nIt's a vowel");

break; default:printf("\nIt's not a vowel"); } } else printf("\nYOU HAVE ENTERED THE CHARACTER THAT IS NOT IN UPPER CASE"); }while(ch>=65&&ch<=90); getch(); }

Output

DIt's not a vowel A It's a vowel E It's a vowel G It's not a vowel Y It's not a vowel x YOU HAVE ENTERED THE CHARACTER THAT IS NOT IN UPPER CASE

Page 14: c Programming File

Program No – 9

Program code:

//Printing the Multiplication Tables#include<stdio.h>#include<conio.h>void main(){int i,j,k;clrscr();printf(“Tables up till 10 are:”);for(i=1;i<=10;i++) { for(j=1;j<=10;j++) { k=i*j; printf("%d",k); printf(" "); } printf("\n"); }getch();}

Output

Tables up till 10 are:

1 2 3 4 5 6 7 8 9 102 4 6 8 10 12 14 16 18 203 6 9 12 15 18 21 24 27 304 8 12 16 20 24 28 32 36 405 10 15 20 25 30 35 40 45 506 12 18 24 30 36 42 48 54 607 14 21 28 35 42 49 56 63 708 16 24 32 40 48 56 64 72 809 18 27 36 45 54 63 72 81 9010 20 30 40 50 60 70 80 90 100

Page 15: c Programming File

Program No – 10

Program code:

//Generating the Armstrong numbers between 2 – 1000#include<stdio.h>#include<conio.h>

void main(){int num,num1,num2,sum;clrscr();printf("Armstrong number is like:\nX^3+Y^3+Z^3=XYZ*\n");for(num=2;num<=1000;num++){num2=num;sum=0;while(num2!=0){num1=num2%10;sum=sum+num1*num1*num1;num2=num2/10;}if(sum==num)printf("\nArmstrong no=%d\n",num);}getch();}

OutputArmstrong number is like:

X^3+Y^3+Z^3=XYZ

Armstrong no=153

Armstrong no=370

Armstrong no=371

Armstrong no=407

Page 16: c Programming File

Program No – 11

Program code:

//Fibonacci seires using function#include<stdio.h>#include<conio.h>void fib(int n){

int x1 = 0, f;int x2 = 1;if(n >= 1){

printf("\nThe fibonacci series is: \n%d %d",x1,x2);for(int i=2;i<= n; i++){

f = x1+ x2;x1 = x2;x2 = f;printf(" %d",x2);

}}

}

void main(){

int r,n;clrscr();printf("\nEnter the limit of series: ");

scanf("%d",&n);fib(n);getch();

}

OutputEnter the limit of series: 5The fibonacci series is:0 1 1 2 3 5

Page 17: c Programming File

Program No – 12

Program code:

// Factorial using recurrion#include<stdio.h>#include<conio.h>long int fact(int x);void main(){

long int m;int n;clrscr();printf(\nEnter the number whose factorial is needed: );

scanf(“%d",&n);m=fact(n);printf(“\nThe factorial of given number is: %d”, m);getch();

}long int fact(int x){

Int I;long int f;f=1;for(i=1;i<=x;i++)

f=f*I;return (f);

}

Output

Enter the number whose Factorial is needed: 5The factorial of the given number is: 120

Page 18: c Programming File

Program No – 13

Program code:

//Printing ASCII value and equivalent characters#include<stdio.h>#include<conio.h>void main(){

int num;printf("Printing ASCII values Table...\n\n");num = 1;while(num<=255){

printf("\nValue:%d = ASCII Character:%c", num, num); /*This change has been made as per the comment. Thank you anonymous Blog

Viewer ... */num++;

}printf("\n\nEND\n");getch();

}

Output

Page 19: c Programming File

Program No – 14

Program code:

//Finding the binomial coefficient using functions#include<stdio.h>#include<conio.h>int fact(int);void main(){int n,r,x1,x2,x3,result;clrscr();printf("\n Enter the value of n=");scanf("%d",&n);printf("\nEnter the value of r=");scanf("%d",&r);if(n>0&&r==0)

result=1;else if (n>0&&r==1)

result=n;else if(n==r)

result=1;else if(n!=r){x1= fact(n);x2= fact(r);x3= fact(n-r);result=x1/(x2*x3);}printf("\nThe value of bionomial coeff.=%d",result);getch();}int fact(int temp){int no,fact=1; for(no=temp;no>=1;no--) fact=fact*no;return(fact);

}

Page 20: c Programming File

Output

Enter the value of n=7 Enter the value of r=3The value of bionomial coeff.=35

Page 21: c Programming File

Program No – 15

Program code:

//Computing the average, maximum, minimum value#include<stdio.h>#include<conio.h>void main(){int i,min=32767,max=0;float sum=0;int arr[10];clrscr();printf("\n ENTER TEN NOS\n");for(i=0;i<10;i++){scanf("%d",&arr[i]);if(arr[i]<min)min=arr[i];else if(arr[i]>max)max=arr[i];sum+=arr[i];}printf("\nMIN. VALUE = %d",min);printf("\nMAX. VALUE = %d",max);printf("\nAVG. VALUE = %f",sum/10);getch();}

Output

ENTER TEN NOS 1 2 3 4 5 6 7 8 9 12

MIN. VALUE = 1 MAX. VALUE = 12 AVG. VALUE = 5.700000

Page 22: c Programming File

Program No – 16

Program code:

/*Pattern1 2 3 4 52 3 4 5 63 4 5 6 7

*/#include<stdio.h>#include<conio.h>void main(){

int i,j,m;clrscr();for(i=1;i<=3;i++){

m=i;for(j=1;j<=5;j++){

printf("%d",m);m++;

}printf("\n");

}getch();

}

Page 23: c Programming File

Program No – 17

Program code:

//Calculating the GCD#include<stdio.h>#include<conio.h>void main(){

int a,b,r,max,min;clrscr();printf("\nFinding the GCD of the numbers.");printf("\nEnter the numbers: ");

scanf("%d%d",&a,&b);max=a;min=b;if(max<b){

max=b;min=a;

}

r=1;while(r!=0){

r=max%min;if(r==0){

printf("The GCD is: %d",min);break;

}else{

max=min;min=r;

}}getch();

}

Page 24: c Programming File

OutputFinding the GCD of two numbersEnter the numbers: 5612The GCD is 4

Page 25: c Programming File

Program No – 18

Program code:

/*Printing pattern of alphabetsABCDCBAABC CBAAB BAA A

*/#include<stdio.h>#include<conio.h>void main(){

int i,j,k,l,m; int a=65,d; clrscr(); for(i=4,m=0;i>=0;i--,m=m+2) {

a=65;for(j=1;j<=i;j++){

printf("%c",a);a++;

}for(l=1;l<=m;l++){

if(a==69)continue;printf(" ");

}for(k=i;k>0;k--){

a--;printf("%c",a);

}printf("\n");

} getch();

}

Page 26: c Programming File

Program No – 19

Program code:

//Prime number generation#include<stdio.h>#include<conio.h>void main(){int n,i,c,j;clrscr();printf("enter the no. upto which u want prime no.s\n\n");scanf("%d",&n);c=0;for(i=2;i<=n;i++){ c=0;

for(j=2;j<=i;j++)if(i%j==0)

c++;if(c==1)

printf("\n%d",i);}getch();}

Output

Enter the no. upto which u want prime nos: 23

2 3 5 7 11 13 17 19 23

Page 27: c Programming File

Program No – 20

Program code:

//Star Patern/* *

******** */

#include<stdio.h>#include<conio.h>void main(){

int i,j,k,m;clrscr();for(i=0,m=0;i<5;i=i+2,m++){

for(k=2;k>=m;k--)printf(" ");

for(j=0;j<=i;j++){ printf("*");}printf("\n");

}getch();

}

Page 28: c Programming File

Program No – 21

Program code:

/*pattern

1 2 3 4 5 6 7 8 9 10

*/#include<stdio.h>#include<conio.h>void main(){

int i,j,l,k=1;int space=20;clrscr();for(i=1;i<=4;i++){

printf("\n");for(l=1;l<=space;l++)printf(" ");

space--;for(j=1;j<=i;j++){

printf("%d ",k);k++;

}}getch();

}

Page 29: c Programming File

Program No – 22

Program code://Binary search#include<stdio.h>#include<conio.h>void main(){

int a[50],i,first,last,mid,temp,j,c,n;clrscr();printf("\nEnter the size of the list: ");

scanf("%d",&n);

printf("\nEnter the elements: ");for(i=0;i<n;i++)

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

//sorting the arrayfor(i=0;i<n;i++){

for(j=i+1;j<n;j++)if(a[i]>a[j]){

temp=a[i];a[i]=a[j];a[j]=temp;

}}//sorting is done//Now traversingprintf("\nThe sorted array is: \n");

for(i=0;i<n;i++)printf("%d ",a[i]);

printf("\nEnter the number that you want to search: ");scanf("%d",&temp);

//searching the elementfirst=0;last=n;c=0;while(c==0){

mid=(first+last)/2;if(a[mid]==temp)

Page 30: c Programming File

{printf("\nThe value %d is found at position %d.",temp,mid+1);c=1;

}elseif(temp<a[mid])

last=mid-1;else

first=mid+1;}if(c==0)

printf("\nThe number you entered is not found.");getch();

}

Output

Enter the size of the list: 10

Enter the elements: 5

9

7

56

23

45

12

89

78

47

The sorted array is:

5 7 9 12 23 45 47 56 78 89

Enter the number that you want to search: 23

The value 23 is found at position 5.

Page 31: c Programming File

Program No – 23

Program code://To calculate the sum of the series// x-x3/3!+x5/5!-x7/7!...............#include<stdio.h>#include<conio.h>void main(){

int x,n,i,j;float t,sum,fact,s;clrscr();printf("\nEnter the nth term and the value of x: ");

//scaning the nth term to calculate the sum of the termsscanf("%d%d",&n,&x);

//initializing sum to 0sum=0;

//calcualting the terms and then adding it to the sumfor(i=1;i<=n;i=i+2){

s=fact=1;//calcuting the power of x till ifor(j=1;j<=i;j++){

s*=x;fact*=j;

}t=s/fact;printf("t= %f\ts= %f\tfact= %f\n",t,s,fact);if(i%2==0)

sum=sum-t;else

sum=sum+t;}

//printing the sumprintf("\nThe sum of the series till the nth term is: %f",sum);getch();

}

Page 32: c Programming File

Output

Enter the nth term and the value of x: 7

5

t= 5.000000 s= 5.000000 fact= 1.000000

t= 20.833334 s= 125.000000 fact= 6.000000

t= 26.041666 s= 3125.000000 fact= 120.000000

t= 15.500992 s= 78125.000000 fact= 5040.000000

The sum of the series till the nth term is: 67.375992

Page 33: c Programming File

Program No – 24

Program code://To compare two square matrices#include<stdio.h>#include<conio.h>void main(){

int a[5][5],b[5][5],n,m,i,j,l=0,c;clrscr();printf("\nEnter the size of the square matrices: ");

//Scaning the size of the sqaure matricesscanf("%d%d",&m,&n);

printf("\nEnter the elements in the first matrix: ");//scaning the elements of the first matrixfor(i=0;i<m;i++)

for(j=0;j<n;j++)scanf("%d",&a[i][j]);

printf("\nEnter the elements in the second matrix: ");//Scaning the elements of the second matrixfor(i=0;i<m;i++)

for(j=0;j<n;j++)scanf("%d",&b[i][j])

c=m*n//Comapring the matricesfor(i=0;i<m;i++){

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

if(a[i][j]==b[i][j])l++;

}}if(l==c)

printf("\nThe matrices are equal.");else

printf("\nThe two matrices have different elements.");getch();

}

Page 34: c Programming File

Output

Enter the size of the square matrices: 33

Enter the elements in the first matrix: 123456789

Enter the elements in the second matrix: 123456789

The matrices are equal.

Page 35: c Programming File

Program No – 25

Program code://To find the factors of a number#include<stdio.h>#include<conio.h>void main(){

int n,i,d[50],j;clrscr();printf("\nEnter the number to find its factors: ");

scanf("%d",&n);j=1;d[0]=1;for(i=2;i<=n;i++){

if(n%i==0)d[j++]=i;

}

printf("\nThe factors of %d are: \n",n);for(i=0;i<j;i++)

printf("%d ",d[i]);getch();

}

Output

Enter the number to find its factors: 45

The factors of 45 are:1 3 5 9 15 45

Page 36: c Programming File

Program No – 26

Program code://CALCULATING THE LCM OF TWO NUMBERS#include<stdio.h>#include<conio.h>void main(){

int a,b,be,sm,d,r,LCM,GCD;clrscr();printf("\nEnter the two numbers: ");

scanf("%d%d",&a,&b);printf("\n");be=a;sm=b;if(sm>be){

sm=a;be=b;

}r=1;while(r!=0){

r=be%sm;if(r==0){

GCD=sm;break;

}else{

be=sm;sm=r;

}}LCM=(a*b)/GCD;printf("\nThe LCM of the numbers is: %d",LCM);getch();

}

Page 37: c Programming File

Output

Enter the two numbers: 123The LCM of the numbers is: 12

Page 38: c Programming File

Program No – 27

Program code://To Divide the elements of the array into odd and even array#include<stdio.h>#include<conio.h>void main(){

int a[50],b[50],c[50],i,n,j,k;clrscr();printf("\nEnter the size of the array: ");

//scaning the size of the arrayscanf("%d",&n);

printf("\nEnter the elements of the array: ");//scaning the elements of the array3for(i=0;i<n;i++)

scanf("%d",&a[i]);//checking the even and odd elements of tidee array and//storing them in different arrays

j=k=0; for(i=0;i<n;i++) {

if(a[i]%2==0){

b[j]=a[i];j++;

}else{

c[k]=a[i];k++;

} }

//traversing the arrays printf("\n The array of even numbers: \n"); for(i=0;i<j;i++)

printf("%d\t",b[i]); printf("\n The array of odd numbers: \n"); for(i=0;i<k;i++)

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

getch();}

Page 39: c Programming File

Output

Enter the size of the array: 15

Enter the elements of the array: 1

12

13

34

56

34

576

38

89

78

79

90

89

The array of even numbers:

12 34 56 34 576 38 78 90

The array of odd numbers:

1 13 89 79 89 23 45 89

Page 40: c Programming File

Program No – 28

Program code://To enter two variables and to calculate their reverse using pointers#include<stdio.h>#include<conio.h>#include<string.h>void main(){

char *a,*b,i,j,temp,l1,l2;clrscr();printf("\nEnter any two strings to get their reverse: ");

//scaning the two numbersgets(a);gets(b);

//reversing the stringsl1=strlen(a);l2=strlen(b);for(i=0,j=l1-1;i<l1/2;i++,j--){

temp=a[i];a[i]=a[j];a[j]=temp;

}for(i=0,j=l2-1;i<l2/2;i++,j--){

temp=b[i];b[i]=b[j];b[j]=temp;

}//traversing the reverse stringsprintf("\nThe reverse strings are: ");puts(a);printf("\n");puts(b);getch();

}

Page 41: c Programming File

Output Enter any two strings to get their reverse: therehelloThe reverse strings are: erehtolleh

Page 42: c Programming File

Program No – 29

Program code://To find the lenght of string including spaces and excluding spaces#include<stdio.h>#include<conio.h>void main(){

int i,l1,l2,c1,c2;char s[100];clrscr();printf("\nEnter a string to find its length: ");

gets(s);

//calculating the length of the string with spaces and wihtout spacesc1=c2=l1=l2=0;for(i=0;;i++){

if(s[i]=='\0')break;

else{

c1++;if(s[i]==' ')

c2++;}

}//finalizing the lengthl1=c1;l2=c1-c2;printf("\nThe length without spaces is: %d.\nThe length with spaces is: %d",l2,l1);getch();

}

Output

Enter a string to find its length: hi world its a great day today

The length without spaces is: 24.

The length with spaces is: 30

Page 43: c Programming File

Program No – 30

Program code://To print the sum of diagonals of matrix#include<stdio.h>#include<conio.h>void main(){

int a[5][5],i,j,sum1,sum2,n;clrscr();printf("\nEnter the size of square matrix: ");

//scaning the size of square matrixscanf("%d",&n);

//scaning the matrixprintf("\nEnter the elements of the matrix: ");for(i=0;i<n;i++)

for(j=0;j<n;j++)scanf("%d",&a[i][j]);

//initialinzing the sum(s)sum1=0;sum2=0;

//calculating the sum of diagonal elements of the square matrixfor(i=0;i<n;i++){

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

if(i==j)sum1+=a[i][i];

if(i+j==n-1)sum2+=a[i][j];

}}

printf("\nThe sum of diagonal elements are: \nRight diagonal is: %d\nLeft diagonal is: %d",sum2,sum1);

getch();}

Page 44: c Programming File

Output

Enter the size of square matrix: 3

Enter the elements of the matrix: 123456789

The sum of diagonal elements are:Right diagonal is: 15

Left diagonal is: 15

Page 45: c Programming File

Program No – 31

Program code://Program to illustrate nested Structure#include<stdio.h>#include<conio.h>typedef struct marks{

char sub_name[20];char sub_code[5];float m;

};typedef struct student{

char name[50];char add[70];int roll;int clas;marks mark[5];

};void main(){

student stud[25];int i,j,n;char nam[50],nam1[70];clrscr();printf("\nEnter the number of students: ");

scanf("%d",&n);//Taking the details of the student and its marksfor(i=0;i<n;i++){

printf("\nName of the student: ");scanf("%s",stud[i].name);

printf("\nAddress: ");scanf("%s",stud[i].add);

printf("\nRoll: ");scanf("%d",&stud[i].roll);

printf("\nClass: ");scanf("%d",&stud[i].clas);

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

printf("\nSubject name: ");scanf("%s",stud[i].mark[j].sub_name);

printf("\nSubject code: ");

Page 46: c Programming File

scanf("%s",stud[i].mark[j].sub_code);printf("\nMarks: ");

scanf("%f",&stud[i].mark[j].m);}

}

//Traversing the list nowclrscr();printf("\nThe list you entered is: \n");printf("\nName\taddress\t\tRoll\tClass\tSubject Name\t\tSubject Code\tMarks\t\n");for(i=0;i<n;i++){

printf("%s",stud[i].name);printf("\t");printf("%s",stud[i].add);printf("\t\t");printf("%d\t%d\t",stud[i].roll,stud[i].clas);for(j=0;j<3;j++){

printf("%s",stud[i].mark[j].sub_name);printf("\t\t");printf("%s",stud[i].mark[j].sub_code);printf("\t%f\n",stud[i].mark[j].m);

}}getch();

}

Page 47: c Programming File

Output

Enter the number of students: 3

Name of the student: NancyAddress: xyz, Delhi-94Roll: 09852Class: X

Subject Name: MathsSubject Code: 001Marks: 94

Subject Name: ITSubject Code: 002Marks: 91

Subject Name: CSubject Code: 021Marks: 90

Name of the student: SwatiAddress: xyz, Delhi-92Roll: 09878Class: X

Subject Name: MathsSubject Code: 001Marks: 93

Subject Name: ITSubject Code: 002Marks: 97

Subject Name: CSubject Code: 021Marks: 90

Name of the student: KirtiAddress: xtyz, Delhi-94Roll: 09342Class: X

Subject Name: MathsSubject Code: 001Marks: 92

Subject Name: IT

Page 48: c Programming File

Subject Code: 002Marks: 98

Subject Name: CSubject Code: 021Marks: 92

Name address Roll Class Subject Name Subject Code Marks

Nancy xyz, Delhi-94 09852 X Maths 001 94

IT 002 91

C 021 90

Swati xyz, Delhi-92 09878 X Maths 001 93

IT 002 97

C 021 90

Kiti xtyz, Delhi-94 09342 X Maths 001 92

IT 002 98

C 021 92

Page 49: c Programming File

Program No – 32

Program code://Calculating the total number of digits and their sum in an alpha numeric string#include<stdio.h>#include<conio.h>#include<string.h>void main(){

int i,l,sum,c,n[10],j;char a[10];clrscr();printf("Enter a string: ");

gets(a);l=strlen(a);c=sum=0;a[0]=48;for(i=1;i<10;i++)

n[i]=n[i-1]+1;for(i=0;i<l;i++){

for(j=0;j<10;j++){

if(a[i]==n[j]){

sum+=j;}

}if(((a[i]>=65)&&(a[i]<=90))||((a[i]>=97)&&(a[i]<=122)))

sum+=a[i];else

c++;}if(c==l)

printf("You entered all special characters.");else{ printf("The sum of characters you entered is: %d",sum);

if(c!=0)printf("You have entered few special characters");

}getch();

}

Page 50: c Programming File

Output

Enter a string: ABC1

The sum of characters you entered is: 199

Page 51: c Programming File

Program No – 33

Program code:

//TO PRINT DIFFERENT VALUES BEING POINTING TO BY AN ARRAY OF POINTERS #include<stdio.h> #include<conio.h> void main() { int i,*ptr[5]; int a=65,b=67,c=69,d=70,e=75; ptr[0]=&a; ptr[1]=&b; ptr[2]=&c; ptr[3]=&d; ptr[4]=&e; clrscr(); for(i=0;i<5;i++) printf("the pointer ptr[%d] points to:%d\n",i,*ptr[i]); printf("the base address of array ptr of pointers is :%u\n",ptr); for(i=1;i<5;i++) printf("The address stored in ptr[%d] is:%u\n",i,ptr[i]); getch(); }

Output

the pointer ptr[0] points to:65the pointer ptr[1] points to:67the pointer ptr[2] points to:69the pointer ptr[3] points to:70the pointer ptr[4] points to:75

the base address of array ptr of pointers is :65516

The address stored in ptr[1] is:65512The address stored in ptr[2] is:65510The address stored in ptr[3] is:65508The address stored in ptr[4] is:65506

Page 52: c Programming File

Program No – 34

Program code:

//Finding sum of numbers using functions#include<stdio.h>#include<conio.h>void main(){

int show(int,int); int a,b,c; clrscr(); printf("enter two no= ",a,b); scanf("%d%d",&a,&b); c=show(a,b);

printf("sum of two no=%d",c); getch();}show(int x,int y){ int s;

s=x+y; return(s);}

Output

enter two no= 5 2sum of two no=7

Page 53: c Programming File

Program No – 35

Program code:

//Passing of a Structure to a Function#include<stdio.h>#include<conio.h>struct book{char name[25];char auther[25];int callno;};void main(){struct book b1={"Let us c","YPK",101};clrscr();display (b1);getch();}display(struct book b){printf("\n%s%s%d",b.name,b.auther,b.callno);}

Output

Let us cYPK101

Page 54: c Programming File

Program No – 36

Program code://Write menu driven program of file //ADD, MODIFY, LIST, DELETE#include<stdio.h>#include<conio.h>void main(){

FILE *fp;fp=fopen("text.txt","A+");char *str, s;clrscr();if(fp==NULL)

printf("Unable to open file. ");else{

printf("File is success fully opened. ");printf("\nWhat do you want to do now? \nChoose your options: \n1. Write to file.\

n2.Read the file\n3.Edit file.\n4.Delete the material.");scanf("%c",s);

switch(ch);{

case 1:printf("What do you want to write to file: ");gets(str);

fputs(str,fp);break;

case 2:printf("The contents of the file are: ");fscanf(fp,"%s",str);

printf("String = %s",str);break;

case 3:printf("What do you want to edit in file: ");gets(str);

fputs(str,fp);break;

case 4:printf("\nYou want to erase the file......\n");remove(fp);printf("\nThe file is erased. ");break;

}}getch();

Page 55: c Programming File

}

Page 56: c Programming File

Program No – 38

Program code:

//Program for command line argument #include <stdio.h>main(int argc, char *argv[]){

int i;if( argc == 0 )

printf("Atleast one argument expected.\n");else

for(i = 0; i < argc; i++)printf("arg %d: %s\n", i, argv[i]);

return 0;}

Output

//entered //7 TestProgram Teach Yourself C++ In 21 Daysarg 0: TestProgram.exearg 1: Teacharg 2: Yourselfarg 3: C++ arg 4: Inarg 5: 21arg 6: Days