You are on page 1of 56

C

Programming
File
Submitted by:
MCA(I yr 2009)

Index
Progra
m
.No.

Program

To calculate the absolute value of any number

Printing the numbers which are divisible by 7


and 100

Finding the largest number

Reverse of a number

Calculating the power of a number

Use of Bitwise Operators

Roots of Quadratic Equation

Reading a character in Uppercase and check


whether it is vowel or not.

Printing the Multiplication Tables

10

10

Generating 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

15

Computing the average, maximum, minimum


value

16

16

Pattern
12345
23456

Date

Page No

17

T.Sign

34567

17

Calculating the GCD

18

Pattern
ABCDCBA
ABC CBA
AB
BA
A
A

18

20

19

Prime number generation

21

20

Star Pattern
*
***
*****

22

Pattern
21

1
2 3
4 5 6
7 8 9 10

23

22

Binary Search

24

23

To Calculate the sum of the series


x-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

27

To Divide the elements of the array into odd


and even array

32

28

To enter two variables and to calculate their


reverse order using pointers

34

29

To find the length of string including spaces


and excluding spaces

36

30

To print the sum of diagonals of a square


matrix

38

31

Program to illustrate nested Structure

40

32

To print different values being pointing to by


an array of pointers

44

33

Calculating 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

36

Write menu driven program of file to


ADD, MODIFY, LIST, DELETE

49

37

Program of array of pointers to functions

51

38

Program of Command Line argument

52

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: -98
The absolute value of |-98| is: 98

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]);
else
printf("\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: 700
56
890
1400
7
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.

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 :- 56
879
756
90
5
The largest number is: 879

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 range
if(n>32767)
{
printf("\nThe number u entered is out of range");
exit(0);
}
//printing the reverse
printf("\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: 6570
The reverse number is: 0756

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 power
for(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: 7
4
7 to the power of 4 is: 2401

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);

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

Output
Select 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

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");
}
else
if(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");
}
else
printf("Roots are complex & imaginary");
getch();
return 0;
}

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

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
D
It'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

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
10

2
4
6
8
10
12
14
16
18
20

3 4
6 8
9 12
12 16
15 20
18 24
21 28
24 32
27 36
30 40

5
10
15
20
25
30
35
40
45
50

6
12
18
24
30
36
42
48
54
60

7
14
21
28
35
42
49
56
63
70

8
16
24
32
40
48
56
64
72
80

9
18
27
36
45
54
63
72
81
90

10
20
30
40
50
60
70
80
90
100

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();
}

Output
Armstrong number is like:
X^3+Y^3+Z^3=XYZ
Armstrong no=153
Armstrong no=370
Armstrong no=371
Armstrong no=407

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();
}

Output
Enter the limit of series: 5
The fibonacci series is:
011235

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: 5
The factorial of the given number is: 120

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

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);

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

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

Program No 16
Program code:
/*Pattern
12345
23456
34567
*/
#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();
}

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();
}

Output
Finding the GCD of two numbers
Enter the numbers: 56
12
The GCD is 4

Program No 18
Program code:
/*Printing pattern of alphabets
ABCDCBA
ABC CBA
AB
BA
A
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();
}

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

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();
}

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();
}

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 array
for(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 traversing
printf("\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 element
first=0;
last=n;
c=0;
while(c==0)
{
mid=(first+last)/2;
if(a[mid]==temp)

{
printf("\nThe value %d is found at position %d.",temp,mid+1);
c=1;
}
else
if(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.

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 terms
scanf("%d%d",&n,&x);
//initializing sum to 0
sum=0;
//calcualting the terms and then adding it to the sum
for(i=1;i<=n;i=i+2)
{
s=fact=1;
//calcuting the power of x till i
for(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 sum
printf("\nThe sum of the series till the nth term is: %f",sum);
getch();
}

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

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 matrices
scanf("%d%d",&m,&n);
printf("\nEnter the elements in the first matrix: ");
//scaning the elements of the first matrix
for(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 matrix
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&b[i][j])
c=m*n
//Comapring the matrices
for(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();
}

Output
Enter the size of the square matrices:
3
3
Enter the elements in the first matrix:
1
2
3
4
5
6
7
8
9
Enter the elements in the second matrix:
1
2
3
4
5
6
7
8
9
The matrices are equal.

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

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();
}

Output
Enter the two numbers: 12
3
The LCM of the numbers is: 12

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 array
scanf("%d",&n);
printf("\nEnter the elements of the array: ");
//scaning the elements of the array3
for(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();
}

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

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 numbers
gets(a);
gets(b);
//reversing the strings
l1=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 strings
printf("\nThe reverse strings are: ");
puts(a);
printf("\n");
puts(b);
getch();
}

Output
Enter any two strings to get their reverse:
there
hello
The reverse strings are:
ereht
olleh

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 spaces
c1=c2=l1=l2=0;
for(i=0;;i++)
{
if(s[i]=='\0')
break;
else
{
c1++;
if(s[i]==' ')
c2++;
}
}
//finalizing the length
l1=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

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 matrix
scanf("%d",&n);
//scaning the matrix
printf("\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 matrix
for(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();
}

Output
Enter the size of square matrix: 3
Enter the elements of the matrix:
1
2
3
4
5
6
7
8
9
The sum of diagonal elements are:
Right diagonal is: 15
Left diagonal is: 15

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 marks
for(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: ");

scanf("%s",stud[i].mark[j].sub_code);
printf("\nMarks: ");
scanf("%f",&stud[i].mark[j].m);
}
}
//Traversing the list now
clrscr();
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();
}

Output
Enter the number of students: 3
Name of the student: Nancy
Address: xyz, Delhi-94
Roll: 09852
Class: X
Subject Name: Maths
Subject Code: 001
Marks: 94
Subject Name: IT
Subject Code: 002
Marks: 91
Subject Name: C
Subject Code: 021
Marks: 90

Name of the student: Swati


Address: xyz, Delhi-92
Roll: 09878
Class: X
Subject Name: Maths
Subject Code: 001
Marks: 93
Subject Name: IT
Subject Code: 002
Marks: 97
Subject Name: C
Subject Code: 021
Marks: 90

Name of the student: Kirti


Address: xtyz, Delhi-94
Roll: 09342
Class: X
Subject Name: Maths
Subject Code: 001
Marks: 92
Subject Name: IT

Subject Code: 002


Marks: 98
Subject Name: C
Subject Code: 021
Marks: 92

Name

address

Roll

Nancy

xyz, Delhi-94

09852

Swati

Kiti

xyz, Delhi-92

xtyz, Delhi-94

09878

09342

Class
X

Subject Name

Subject Code

Marks

Maths

001

94

IT

002

91

021

90

Maths

001

93

IT

002

97

021

90

Maths

001

92

IT

002

98

021

92

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();
}

Output

Enter a string: ABC1


The sum of characters you entered is: 199

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:65
the pointer ptr[1] points to:67
the pointer ptr[2] points to:69
the pointer ptr[3] points to:70
the pointer ptr[4] points to:75
the base address of array ptr of pointers is :65516
The address stored in ptr[1] is:65512
The address stored in ptr[2] is:65510
The address stored in ptr[3] is:65508
The address stored in ptr[4] is:65506

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 2
sum of two no=7

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

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();

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 Days
arg 0: TestProgram.exe
arg 1: Teach
arg 2: Yourself
arg 3: C++
arg 4: In
arg 5: 21
arg 6: Days

You might also like