0% found this document useful (0 votes)
41 views

C Program

Program in C

Uploaded by

Joona John
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views

C Program

Program in C

Uploaded by

Joona John
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 67

SIMPLE C PROGRAMS

SINO PROGRAM
1 PRINT HELLO
2 PRINT ADDRESS
3 AVERAGE OF THREE NUMBERS
4 TEMPERATURE CONVERSION
F=(9/5)*c + 32
c=(5/9)*(f-32)
5 AREA AND PERIMETER OF CIRCLE
6 SWAP WITH TEMPORARY VARIABLE
7 SWAP WITHOUT TEMPORARY VARIABLE
8 LARGEST AMONG TWO NUMBERS WITH CONDITIONAL
OPERATOR
1.PRINT HELLO
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();

printf("hello");

getch();
}

OUTPUT

hello
2.PRINT ADDRESS

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

clrscr();

printf("Arun\nIcet\nMulavoor\n686605") ;

getch();
}

OUTPUT

Arun
Icet
Mulavoor
686605
3.AVERAGE OF THREE NUMBERS
#include<stdio.h>
#include<conio.h>
void main()
{
float a,b,c,avg;
clrscr();

printf("enter any 3 numbers");


scanf("%f%f%f",&a,&b,&c);

avg=(a+b+c)/3;

printf("the average is %f",avg);

getch();
}

OUTPUT

enter any 3 numbers


4.2 3.4 5.2
the average is 4.266666666666666
4.CONVERSION OF CELSIUS TO FARENHEIT
#include<stdio.h>
#include<conio.h>

void main()
{
float celsius, fahrenheit;
clrscr();

printf("\nEnter temp in Celsius : ");


scanf("%f", &celsius);

fahrenheit = (1.8 * celsius) + 32;

printf("\nFahrenheit = %f ", fahrenheit);

getch();
}

OUTPUT

Enter temp in Celsius


42

Fahrenheit = 107.6

5.AREA AND PERIMETER OF CIRCLE


#include<stdio.h>
#include<conio.h>
void main()
{
float r,a,p;
clrscr();

printf("enter the radius");


scanf("%f",&r);

a=3.14*r*r;
p=2*3.14*r;

printf("the area and perimeter is %f and %f",a,p);

getch();
}

OUTPUT

enter the radius


4
the area and perimeter is 50.24 and 25.12

6.SWAPPING 2 NUMBERS WITH TEMPORARY VARIABLE


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

void main()
{
int x, y, temp;
clrscr();

printf("Enter the value of x and y\n");


scanf("%d%d", &x, &y);

printf("Before Swapping\nx = %d\ny = %d\n",x,y);

temp = x;
x = y;
y = temp;

printf("After Swapping\nx = %d\ny = %d\n",x,y);

getch();
}

OUTPUT

Enter the value of x and y


10 20
Before Swapping
x = 10
y = 20
After Swapping
x = 20
y = 10

7.SWAPPING 2 NUMBERS WITHOUT TEMPORARY VARIABLE


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

void main()
{
int a, b;
clrscr();

printf("Enter two integers to swap\n");


scanf("%d%d", &a, &b);

a = a + b;
b = a - b;
a = a - b;

printf("a = %d\nb = %d\n",a,b);

getch();
}

OUTPUT

Enter two integers to swap


10 20
a = 20
b = 10

8.LARGEST OF 2 NUMBERS USING CONDITIONAL OPERATOR


#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();

printf("enter any 2 numbers");


scanf("%d%d",&a,&b);

c=(a>b)?a:b;

printf("largest is %d",c);

getch();
}

OUTPUT

enter any 2 numbers


4 5
largest is 5

DECISION MAKING AND LOOPS


SINO PROGRAM
1 ODD OR EVEN
2 LARGEST AMONG THREE NUMBERS
3 GRADE OF STUDENT
4 LEAP YEAR OR NOT
5 ROOTS OF QUADRATIC EQUATION
6 DIGITS IN WORDS
7 ARITHMETIC OPERATIONS
8 VOWEL OR NOT
9 PRINT UPTO N NATURAL NUMBERS
10 SUM OF N NATURAL NUMBERS
11 FACTORIAL OF A NUMBER
12 PRIME OR NOT
13 PRIME NUMBERS UPTO A RANGE
14 PERFECT OR NOT
15 FIBONACCI SERIES
16 SUM OF DIGITS
17 REVERSE OF A NUMBER
18 PALINDROME OR NOT
19 ARMSTRONG OR NOT
20 SIN(X)
21 COS(X)
22 e^x

1.ODD OR EVEN
#include<stdio.h>
#include<conio.h>
void main()
{
int n;
clrscr();

printf("Enter a number\n");
scanf("%d",&n);

if(n%2 == 0)
printf("Even\n");
else
printf("Odd\n");

getch();

OUTPUT

Enter a number
10
Even

Enter a number
5
Odd

2.LARGEST AMONG THREE NUMBERS


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

void main()
{
int a, b, c;

printf("\nEnter value of a, b & c : ");


scanf("%d %d %d", &a, &b, &c);

if ((a > b) && (a > c))


printf("\n%d is greatest",a);

else if (b > c)
printf("\n%d is greatest",b);

else
printf("\n%d is greatest",c);

return(0);
}

OUTPUT

Enter value of a, b & c : 10 13 5


13 is greatest

4.LEAP YEAR
#include<stdio.h>
#include<conio.h>

void main()
{
int year;
clrscr();

printf("Enter a year\n");
scanf("%d", &year);

if ( year%400 == 0)
printf("%d is a leap year.\n", year);

else if ( year%100 == 0)
printf("%d is not a leap year.\n", year);

else if ( year%4 == 0 )
printf("%d is a leap year.\n", year);

else
printf("%d is not a leap year.\n", year);

getch();
}

OUTPUT

Enter a year
2012 is a leap year

5.ROOTS OF A QUADRATIC EQUATION


#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int a,b,c;
float d,r1,r2;

printf("Enter coefficients:");
scanf("%d%d%d",&a,&b,&c);

d = b * b - 4 * a * c;

if(d < 0)
{
printf("Roots are complex number.\n");
}

else if(d==0)
{
printf("Both roots are equal.\n");

r1 = -b /(2* a);
printf("Root = %f ",r1);

else
{
printf("Roots are real numbers.\n");

r1 = ( -b + sqrt(d)) / (2* a);


r2 = ( -b - sqrt(d)) / (2* a);
printf("Root1=%f\n Root2=%f",r1,r2);
}

getch();
}
OUTPUT
Enter coefficients : 2 4 1
Roots are real numbers.
Root1= -0.293
Root2= -1.707

6.PRINT DIGITS(15) IN WORDS


#include<stdio.h>
#include<conio.h>
void main()
{
int a;
clrscr();
printf("enter any number between 1-5");
scanf("%d",&a);

if(a==1)
{
printf("ONE");
}
else if(a==2)
{
printf("TWO");
}
else if(a==3)
{
printf("THREE");
}
else if(a==4)
{
printf("FOUR");
}
else if(a==5)
{
printf("FIVE");
}
else if(a==6)
{
printf("SIX");
}
else
{
printf("invalid entry");
}
getch();
}
OUTPUT
enter any number between 1-5
2
TWO

7.ARITHMETIC OPERATION USING SWITCH


#include<stdio.h>
#include<conio.h>
void main()
{
char opr;
int a,b,c;
clrscr();

printf("enter the operator(+,-,*,/)");


scanf("%c",&opr);

printf("\nenter any 2 numbers");


scanf("%d%d",&a,&b);

switch(opr)
{
case '+':c= a+b;
printf("\n%d+%d=%d",a,b,c);
break;
case '-':c= a-b;
printf("\n%d-%d=%d",a,b,c);
break;
case '*':c= a*b;
printf("\n%d*%d=%d",a,b,c);
break;
case '/':c= a/b;
printf("\n%d/%d=%d",a,b,c);
break;
default: printf(\nInvalid operator);
break;
}

getch();
}

OUTPUT
enter the operator(+,-,*,/)
*
enter any 2 numbers
5 4
5*4=20

8.VOWEL OR NOT USING SWITCH


#include<stdio.h>
#include<conio.h>
void main()
{
char ch;
float a,b,c;
clrscr();

printf("enter the character");


scanf("%c",&ch);

switch(ch)
{
case 'a': printf("%c is vowel,ch);
break;
case 'e': printf("%c is vowel,ch);
break;
case 'i': printf("%c is vowel,ch);
break;
case 'o': printf("%c is vowel,ch);
break;
case 'u': printf("%c is vowel,ch);
break;
default: printf(%c is not a vowel,ch);
break;

getch();
}

OUTPUT
enter the character
a
a is vowel

enter the character


b
b is not a vowel
9.PRINT N NATURAL NUMBERS

#include<stdio.h>
#include<conio.h>
void main()
{
int i,n;
clrscr();

printf("enter the limit");


scanf("%d",&n);

for(i=1;i<=n;i++)
{
printf("%d\t",i);
}

getch();
}

OUTPUT

enter the limit 5


1 2 3 4 5

10.SUM OF N NATURAL NUMBERS


#include<stdio.h>
#include<conio.h>
void main()
{
int i,n,sum=0;
clrscr();

printf("Enter the value of N");


scanf("%d",&n);

for(i=1;i<=n;i++)
{
sum=sum+i;
}

printf("Sum=%d",sum);

getch();
}

OUTPUT

Enter the value of N 5


Sum=15

11.FACTORIAL OF A NUMBER
#include <stdio.h>
#include<conio.h>

void main()
{
int i, n, fact = 1;
clrscr();

printf("Enter a number \n");


scanf("%d", &n);

for(i=1;i<=n;i++)
{
fact=fact*i;
}

printf("Factorial of %d = %d\n", n, fact);

getch();
}

OUTPUT

Enter a number
5
Factorial of 5 = 120

12.PRIME NUMBERS OR NOT


#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,j,f=0;
clrscr();

printf(enter the number);


scanf(%d,&n);

for(i=2;i<n;i++)
{
if(n%i==0)
{
f=1;
break;
}
}

if(f==0)
{
printf("prime");
}
else
{
printf("not prime");
}

getch();
}

OUTPUT

enter a number 7
prime

13.FIRST N PRIME NUMBERS


#include <stdio.h>
#include<conio.h>
void main()
{
int i, j, n;
clrscr();

printf("\nEnter the range : ");


scanf("%d", &n);

printf(Prime numbers are\n);

for (i=2; i<=n; i++)


{
for (j=2; j<=i; j++)
{
if(i%j == 0)
break;
else
{
printf("\n%d", i);
break;
}
}
}

getch();
}

OUTPUT

Enter the range 10


Prime numbers are
2
3
5
7

14.PERFECT NUMBER OR NOT


#include<stdio.h>
#include<conio.h>
void main()
{
int num,i=1,sum=0;
clrscr();

printf("Enter a number: ");


scanf("%d",&num);

while(i<num)
{
if(num%i==0)
{
sum=sum+i;
}
i++;
}

if(sum==num)
{
printf("%d is a perfect number",num);
}
else
{
printf("%d is not a perfect number",num);
}

getch()
}

OUTPUT

Enter a number 6
6 is a perfect number

15.FIBONACCI SERIES
#include <stdio.h>
#include<conio.h>
void main()
{
int a, b, c, n, i;
clrscr();

a=0;
b=1;

printf("\nEnter value of n: ");


scanf("%d", &n);

printf("\nFibonacci Series:\n");
printf("%d", a);
printf("\t%d", b);

for( i=0; i<n-2; i++)


{
c = a+b;
printf("\t%d", c);
a = b;
b = c;
}

getch();
}

OUTPUT

Enter value of n:7


Fibonacci Series:
0 1 1 2 3 5 8

16.SUM OF DIGITS
#include <stdio.h>
#include<conio.h>
void main()
{
int n, sum = 0, rem;

printf("Enter a number\n");
scanf("%d",&n);

while(n > 0)
{
rem = n % 10;
sum = sum + remainder;
n = n / 10;
}

printf("Sum of digits = %d\n",sum);

getch();
}

OUTPUT

Enter a number 121


Sum of digits = 4

17.REVERSE OF A NUMBER
#include<stdio.h>
#include<conio.h>
void main() {
int num, rem, rev = 0;
clrscr();

printf("\nEnter the number : ");


scanf("%d", &num);

while (num >= 1)


{
rem = num % 10;
rev = rev * 10 + rem;
num = num / 10;
}

printf("\nReversed Number : %d", rev);

getch();
}

OUTPUT

Enter the number : 123


Reversed Number : 321

18.PALINDROME OR NOT
#include<stdio.h>
#include<conio.h>
void main()
{
int i,n,temp,dig,rev=0;
clrscr();

printf("enter any number");


scanf("%d",&n);

temp=n;

while(n>0)
{
dig=n%10;
rev=rev*10+dig;
n=n/10;
}

if(rev==temp)
{
printf("the number is palindrome");
}
else
{
printf("the number is not palindrome");
}

getch();
}

OUTPUT

enter any number 121


the number is palindrome

19.ARMSTRONG OR NOT

#include<stdio.h>
#include<conio.h>
void main()
{
int i,n,temp,dig,sum=0;
clrscr();

printf("enter any number");


scanf("%d",&n);

temp=n;

while(n>0)
{
dig=n%10;
sum=sum+dig*dig*dig;
n=n/10;
}

if(sum==temp)
{
printf("the number is armstrong");
}
else
{
printf("the number is not armstrong");
}

getch();
}

OUTPUT

enter any number 121


the number is not armstrong

20.EVALUATE SIN(X)

x x3/3! + x5/5! x7/7! + ..............


#include <stdio.h>
#include <conio.h>
#include <math.h>
void main()
{
int i,angle;
float sum=0,term, x;

clrscr();

printf("Enter the value of x (in degrees):");


scanf("%d",&angle);

x = angle*(3.14/180);

term = x;

for(i=1;i<fabs(0.0001);i++)
{
sum= sum + term;
term = term * -(x * x) / (2*i)*(2*i+1);
}

printf("\nsin(%d)= %f",angle,sum);

getch();

OUTPUT

Enter the value of x (in degrees):90


Sin(90)= 1.00000

21.EVALUATE COS(X)

cosx= 1 x2/2! + x4/4! x6/6! + ..


#include <stdio.h>
#include <conio.h>
#include <math.h>
void main()
{
int i,angle;
float sum=0,term, x;

clrscr();

printf("Enter the value of x (in degrees):");


scanf("%d",&angle);

x = angle*(3.14/180);

term = 1;

for(i=1;i<fabs(0.0001);i++)
{
sum = sum + term;
term = term * -(x * x) / (2*i-1)*(2*i);
}

printf("\ncos(%d)= %f",angle,sum);

getch();

OUTPUT

Enter the value of x (in degrees):0


cos(0)= 1.00000
22.EVALUATE EXPONETIAL SERIES

e(x) = 1+x+x2/2!+x3/3!+..

#include <stdio.h>
#include <conio.h>
#include <math.h>
void main()
{
int i,x;
float sum=0,term;

clrscr();

printf("Enter the value of x \n");


scanf("%d",&x);

term = 1;

for(i=1;i<fabs(0.0001);i++)
{
sum = sum + term;
term = term * x / i;
}

printf("\ne(%d)= %f",x,sum);

getch();

OUTPUT

Enter the value of x (in degrees)


0
e(0)= 1.00000

ARRAYS

SINO PROGRAM
1 LARGEST AND SMALLEST ELEMENT
2 SELECTION SORT
3 LINEAR SEARCH
4 BINARY SEARCH
5 COUNT WORDS AND CHARACTERS
6 COUNT VOWELS
7 SORT NAMES
8 MATRIX ADDITION
9 MATRIX MULTIPLICATION
10 MATRIX TRANSPOSE
11 PRINCIPLE DIAGONAL SUM
12

1.SMALLEST AND LARGEST ELEMENT IN AN ARRAY


#include<stdio.h>
#include<conio.h>
void main()
{
int a[50],i,n,small,large;
clrscr();

printf("enter the limit");


scanf("%d",&n);

printf("enter the %d numbers into array",n);

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

small=a[0];

for(i=1;i<n;i++)
{
if(a[i]<small)
{
small=a[i];
}

printf("The smallest is %d ",small);

large=a[0];

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

printf("The largset is %d ",large);


getch();
}

OUTPUT

enter the limit


5
enter the 5 numbers into array
1 2 3 4 5
The smallest is 1
The largest is 5

LINEAR SEARCH
#include <stdio.h>
#include <conio.h>
void main()
{
int a[10];
int i, n, key, f=0;

clrscr();

printf("Enter the value of N\n");


scanf("%d",&n);

printf("Enter the elements one by one\n");


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

printf("Enter the element to be searched\n");


scanf("%d",&key);

for ( i=0; i < n ; i++)


{
if( keynum == a[i] )
{
f = 1;
break;
}
}

if ( f == 1)
printf("SUCCESSFUL SEARCH\n");
else
printf("Search is FAILED\n");

getch();
}
BINARY SEARCH
[low, high] denotes the range in which element has to be
present.
Initially low = 0, high = number_of_elements which
covers entire range.
In every step we reduce the range by doing the
following

(i) If middle element is the key, then key present in


array
ii) If the middle element (mid) is less than key then key
has to present in range [mid+1 , high]
(iii) If the middle element is greater than key then
key has to be present in the range [low,mid-1]

#include <stdio.h>
#include <conio.h>
void main()
{
int a[10],l,high;
int i, n, key, f=0;

clrscr();

printf("Enter the value of N\n");


scanf("%d",&n);

printf("Enter the elements in ascending order: ");


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

printf("Enter the element to be searched\n");


scanf("%d",&key);

low=0;
high=n-1;

while(low <= high)


{
mid = (low + high)/2;
if(a[mid] == key)
{
f=1;
break;
}
else if(a[mid] <= key)
{
low=mid+1;
}
else
{
high = mid-1;
}

}
if ( f == 1)
printf("The number is found.\n");
else
printf("The number not found.");

getch();

OUTPUT
Enter the size of an array: 5
Enter the elements in ascending order: 4 7 8 11 21
Enter the number to be search: 11
The number is found.

SELECTION SORT
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main() {
char a[20],temp;
int i, j,n;
clrscr();

printf("\nEnter the number of elements : ");


scanf(%d,&n);

printf("\nEnter the elements : ");


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

for (i = 0; i < n-1; i++)


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

printf("\nSorted array : ");


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

getch();
}

OUTPUT
Enter the number of elements :5
Enter the elements
3 67 12 31 23
Sorted array
3
12
23
31
67

COUNT WORDS AND CHARACTERS

#include <stdio.h>
#include<conio.h>
#include <string.h>
void main()
{
char str[20];
int i, word=0, chr=0;
clrscr();

printf("\nEnter any string: ");


gets(str);

for(i=0;str[i]!='\0';i++)
{
if (str[i] == ' ')
{
word++;
chr++;
}
else
{
chr++;
}

printf("\nNumber of characters: %d", chr);


printf("\nNumber of words: %d", word+1);
getch();
}

OUTPUT

Enter any string:how are you


Number of characters:11
Number of words:3

COUNT VOWELS IN A STRING


#include <stdio.h>
#include<conio.h>
#include <string.h>
void main()
{
char str[20];
int count=0, i=0;
clrscr();

printf("\nEnter any string: ");


gets(str);

while (str[i] != '\0')


{
if (str[i]=='a' || str[i]=='e' || str[i]=='i' ||
str[i]=='o' || str[i]=='u')

count++;

i++;
}

printf("\nNo. of vowels: %d", count);

getch();
}
OUTPUT

Enter any string:how are you


No. of vowels:5

SORT SET OF STRINGS


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

void main() {
char s[20][20], t[20];
int i, j,n;
clrscr();

printf("\nEnter the number of strings : ");


scanf(%d,&n);

printf("\nEnter the strings : ");


for (i = 0; i < n; i++)
scanf("%s", s[i]);

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


for (j = i+1; j < n; j++) {
if (strcmp(s[i], s[j]) > 0)
{
strcpy(t, s[i]);
strcpy(s[i], s[j]);
strcpy(s[j], t);
}
}
}
printf("\nStrings in order are : ");
for (i = 0; i < n; i++)
printf("\n%s", s[i]);

getch();
}

OUTPUT

Enter the number of strings 5

Enter the strings


Neethu
Arjun
Bittu
Merin
Jerry

Strings in order are


Arjun
Bittu
Jerry
Merin
Neethu
ADD TWO MATRIX
#include <stdio.h>
#include<conio.h>
main()
{
int a[10][10],b[10][10],c[10][10],i,j,row,col;
clrscr();

printf("\nEnter number of rows and columns: ");


scanf("%d %d", &row, &col);

printf("\nEnter elements of Matrix A:\n");


for (i=0; i<row; i++)
{
for (j=0; j<col; j++)
{
scanf("%d", &a[i][j]);
}
}

printf("\nEnter elements of Matrix B:\n");


for (i=0; i<row; i++)
{
for (j=0; j<col; j++)
{
scanf("%d", &b[i][j]);
}
}

printf("\nMatrix Addition is:\n");


for (i=0; i<row; i++)
{
for (j=0; j<col; j++)
{
c[i][j] = a[i][j] + b[i][j];
printf("\t%d", c[i][j]);
}
printf("\n");

getch();
}

OUTPUT

Enter number of rows and columns: 2 3


Enter elements of Matrix A:
1 2 3
4 5 6
Enter elements of Matrix B:
2 5 3
2 3 1
Matrix Addition is:
3 7 6
6 8 7
MATRIX MULTIPLICATION
#include <stdio.h>
#include<conio.h>
void main()
{
int a[10][10],b[10][10],c[10][10],i,j,k,r1,r2,c1,c2;
clrscr();

printf("\nEnter the rows and columns of Matrix A: ");


scanf("%d %d", &r1, &c1);

printf("\nEnter the rows and columns of Matrix B: ");


scanf("%d %d", &r2, &c2);

if (c1 != r2)
{
printf("\n\nMultiplication is not possible\n");
}
else
{
printf("\n\nEnter elements of Matrix A:\n");
for (i=0; i<r1; i++)
{
for (j=0; j<c1; j++)
{
scanf("%d", &a[i][j]);
}
}

printf("\nEnter elements of Matrix B:\n");

for (i=0; i<r2; i++)


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

for (i=0; i<r1; i++)


{
for (j=0; j<c2; j++)
{
c[i][j] = 0;
for (k=0; k<c1; k++)
{
c[i][j]= c[i][j] + a[i][k] * b[k][j];
}
}
}

printf("\n\nMultiplication of Matrices:\n\n");
for (i=0; i<r1; i++)
{
for (j=0; j<c2; j++)
{
printf("\t%d", c[i][j]);
}
printf("\n");

getch();
}
OUTPUT

Enter the rows and columns of Matrix A:3 3


Enter the rows and columns of Matrix B:3 3
Enter elements of Matrix A:
5 6 9
8 5 3
2 9 1
Enter elements of Matrix A:
5 6 9
8 5 3
2 9 1
Multiplication of Matrices:
72 103 57
27 43 18
87 125 56
TRANNSPOSE TWO MATRIX
#include <stdio.h>
#include<conio.h>
main()
{
int a[10][10],i,j,row,col;
clrscr();

printf("\nEnter number of rows and columns: ");


scanf("%d %d", &row, &col);

printf("\nEnter elements\n");
for (i=0; i<row; i++)
{
for (j=0; j<col; j++)
{
scanf("%d", &a[i][j]);
}
}

printf("\nTranspose is \n");
for (i=0; i<col; i++)
{
for (j=0; j<row; j++)
{
printf("%d\t", a[j][i]);
}
printf(\n);
}
getch();
}
OUTPUT
Enter number of rows and columns:2 3
Enter elements
1 2 3
4 5 6
Transpose is
1 4
2 5
3 6

FACTORIAL OF A NUMBER USING RECURSION

#include<stdio.h>
#include<conio.h>
int fact(int);

void main()
{
int factorial, num;
clrscr();

printf("Enter the value of num :");


scanf("%d", &num);

factorial = fact(num);

printf("Factorial is %d", factorial);

getch();;
}

int fact(int n)
{
if (n == 0)
{
return (1);
}
else
{
return (n * fact(n - 1));
}
}

OUTPUT

Enter the value of num 5


Factorial is 120
FIBONACCI USING RECURSION

SUM OF ELEMENTS IN AN ARRAY USING POINTER


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

void main()
{
int a[10];
int i, sum = 0;
int *ptr;

printf("\nEnter 5 elements : ");

for (i = 0; i < 5; i++)


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

ptr = a; /* a=&a[0] */
for (i = 0; i < 5; i++) {
sum = sum + *ptr;
ptr++;
}

printf("The sum of array elements : %d", sum);

getch();
}
OUTPUT

Enter 5 elements
7 3 10 4 1
The sum of array elements : 25

LENGTH OF A STRING USING POINTER

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

int lstr(char *);

void main() {
char str[20];
int length;
clrscr();

printf("\nEnter any string : ");


gets(str);

length = lstr(str);
printf("The length of %s is : %d", str, length);
getch();
}

int lstr(char *p) /* p=&str[0] */


{
int l= 0;
while (*p != '\0')
{
l++;
p++;
}
return l;
}
OUTPUT

Enter any string :


Pritesh
The length of %s is :7
SORTING STRUCTURE ELEMENTS

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

struct student
{
char name[20];
int rollno;
int age;
} s[10], temp;

void main()
{
int i, j, n=3;
clrscr();

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


{
printf("Enter Name:%s,&s[i].name);
printf(\nEnter Rollno:%d\n,&s[i].rollno);
printf(\nEnter Agege:%d,&s[i].age);
}
for (i =1; i < n-1; i++)
{
for (j =i+1; j < n ; j++)
{
if (strcmp(s[i].name,s[j].name)> 0)
{
temp = s[i];
s[i] = s[j];
s[j] = temp;
}
}
}

printf(\nAfter sorting\n);
for (i = 0; i < n; i++)
{
printf("%s\t%d\t%d",s[i].name,s[i].rollno,s[i].age);
}
getch();
}

OUTPUT
Enter Name:Jinu
Enter Rollno :7
Enter Age : 20

Enter Name:Anu
Enter Rollno :4
Enter Age : 21

Enter Name:Basil
Enter Rollno :6
Enter Age : 20

After sorting

Anu 4 21
Basil 6 20
Jinu 7 20

WRITING TO AND READING FROM A FILE


#include<stdio.h>
#include<conio.h>
void main ( )
{
FILE *fp;
char c;
clrscr();

fp = fopen(input.txt , w );

while((c = getchar( ))!=EOF)


{
putc(c,fp);
}

fclose(fp);

fp = fopen(input.txt , r);

while ((c =getc( fp))!=EOF)


{
printf(%c, c);
}

fclose(fp);
getch();
}

HANDLING OF INTEGER DATA FILES


#include<stdio.h>
#include<conio.h>
void main ( )
{
FILE *fp, *fo, *fe;
int a, i;
clrscr();

printf(Contents of DATA file\n\n);


fp = fopen(data.txt, w);

for(i =1; i< =30; i++)


{
scanf(%d, &a);
putw(a,fp);
}

fclose(fp);

fp = fopen(data.txt , r);
fo = fopen(odd.txt , w);
fe = fopen(even.txt, w);

while((a = getw(fp))!= EOF)


{
if(a %2 = = 0)
{
putw(a,fe);
}
else
{
putw(a,fo);
}
}

fclose(fp);
fclose(fo);
fclose(fe);

fo = fopen(ODD , r);
fe = fopen(EVEN , r);

printf(\n\nContents of ODD file \n\n);


while((a = getw(fo)) != EOF)
{
printf(%d,a);
}

printf(\n\n Contents of EVEN file\n\n);


while((a = getw(fe)) ! = EOF)
{
printf(%d ,a);
}
fclose(fe);

getch();

}
Countcharacters,linesandspacesinafile
#include<stdio.h>
#include<conio.h>

voidmain()
{
FILE*fp;
intc,nc,ns,f=0;
clrscr();

ns=0;
nc=0;
nl=0;

fp=fopen(data.txt,r);

if(fp==NULL)
{
printf(Cannotopen);
exit(1);
}


while(c=getc(fp)=EOF)
{
nc++;
if(c==\n)
nl++;

elseif(c==)
ns++;

else
f=1;
}

fclose(fp);

if(nc!=0)
{
printf(characters=%d\n,nc);

printf(lines=%d\n,nl);
printf(Spaces=%d\n,ns);

}
else
printf(File:%sisempty\n,filename);

getch();
}
WRITE AND READ BLOCK OF DATA USING FILE

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

struct Student
{
int roll;
char name[12];
int age;
}s1,s2;

void main()
{
FILE *fp;
int n,i;
clrscr();

fp = fopen("input.txt", "w");

printf(Enter the number of students\);


scanf(%d,&n);

for(i=0;i<n;i++)
{
printf(Enter the details of student %d,i+1);
scanf(%d%s%d,&s1[i].roll,&s[i].name,&s[i].age);

fwrite(&s1, sizeof(struct student, 1, fp);


}
fclose(fp);

fp = fopen("ip.txt", "r");

printf(student details\n);
printf("\nRoll :\tName : \tAge:\n);

for(i=0;i<n;i++)
{
fread(&s2, sizeof(struct student), 1, fp);
printf("%d\t%s\t
%d\n",s2.roll,s2.name,s2.age);
}

getch();
}

You might also like