C Program
C Program
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();
avg=(a+b+c)/3;
getch();
}
OUTPUT
void main()
{
float celsius, fahrenheit;
clrscr();
getch();
}
OUTPUT
Fahrenheit = 107.6
a=3.14*r*r;
p=2*3.14*r;
getch();
}
OUTPUT
void main()
{
int x, y, temp;
clrscr();
temp = x;
x = y;
y = temp;
getch();
}
OUTPUT
void main()
{
int a, b;
clrscr();
a = a + b;
b = a - b;
a = a - b;
getch();
}
OUTPUT
c=(a>b)?a:b;
printf("largest is %d",c);
getch();
}
OUTPUT
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
void main()
{
int a, b, c;
else if (b > c)
printf("\n%d is greatest",b);
else
printf("\n%d is greatest",c);
return(0);
}
OUTPUT
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
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");
getch();
}
OUTPUT
Enter coefficients : 2 4 1
Roots are real numbers.
Root1= -0.293
Root2= -1.707
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
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
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
#include<stdio.h>
#include<conio.h>
void main()
{
int i,n;
clrscr();
for(i=1;i<=n;i++)
{
printf("%d\t",i);
}
getch();
}
OUTPUT
for(i=1;i<=n;i++)
{
sum=sum+i;
}
printf("Sum=%d",sum);
getch();
}
OUTPUT
11.FACTORIAL OF A NUMBER
#include <stdio.h>
#include<conio.h>
void main()
{
int i, n, fact = 1;
clrscr();
for(i=1;i<=n;i++)
{
fact=fact*i;
}
getch();
}
OUTPUT
Enter a number
5
Factorial of 5 = 120
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
getch();
}
OUTPUT
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("\nFibonacci Series:\n");
printf("%d", a);
printf("\t%d", b);
getch();
}
OUTPUT
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;
}
getch();
}
OUTPUT
17.REVERSE OF A NUMBER
#include<stdio.h>
#include<conio.h>
void main() {
int num, rem, rev = 0;
clrscr();
getch();
}
OUTPUT
18.PALINDROME OR NOT
#include<stdio.h>
#include<conio.h>
void main()
{
int i,n,temp,dig,rev=0;
clrscr();
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
19.ARMSTRONG OR NOT
#include<stdio.h>
#include<conio.h>
void main()
{
int i,n,temp,dig,sum=0;
clrscr();
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
20.EVALUATE SIN(X)
clrscr();
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
21.EVALUATE COS(X)
clrscr();
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
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();
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
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
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];
}
large=a[0];
for(i=1;i<n;i++)
{
if(a[i]>large)
{
large=a[i];
}
OUTPUT
LINEAR SEARCH
#include <stdio.h>
#include <conio.h>
void main()
{
int a[10];
int i, n, key, f=0;
clrscr();
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
#include <stdio.h>
#include <conio.h>
void main()
{
int a[10],l,high;
int i, n, key, f=0;
clrscr();
low=0;
high=n-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();
getch();
}
OUTPUT
Enter the number of elements :5
Enter the elements
3 67 12 31 23
Sorted array
3
12
23
31
67
#include <stdio.h>
#include<conio.h>
#include <string.h>
void main()
{
char str[20];
int i, word=0, chr=0;
clrscr();
for(i=0;str[i]!='\0';i++)
{
if (str[i] == ' ')
{
word++;
chr++;
}
else
{
chr++;
}
OUTPUT
count++;
i++;
}
getch();
}
OUTPUT
void main() {
char s[20][20], t[20];
int i, j,n;
clrscr();
getch();
}
OUTPUT
getch();
}
OUTPUT
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("\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
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
#include<stdio.h>
#include<conio.h>
int fact(int);
void main()
{
int factorial, num;
clrscr();
factorial = fact(num);
getch();;
}
int fact(int n)
{
if (n == 0)
{
return (1);
}
else
{
return (n * fact(n - 1));
}
}
OUTPUT
void main()
{
int a[10];
int i, sum = 0;
int *ptr;
ptr = a; /* a=&a[0] */
for (i = 0; i < 5; i++) {
sum = sum + *ptr;
ptr++;
}
getch();
}
OUTPUT
Enter 5 elements
7 3 10 4 1
The sum of array elements : 25
#include<stdio.h>
#include<conio.h>
void main() {
char str[20];
int length;
clrscr();
length = lstr(str);
printf("The length of %s is : %d", str, length);
getch();
}
#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();
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
fp = fopen(input.txt , w );
fclose(fp);
fp = fopen(input.txt , r);
fclose(fp);
getch();
}
fclose(fp);
fp = fopen(data.txt , r);
fo = fopen(odd.txt , w);
fe = fopen(even.txt, w);
fclose(fp);
fclose(fo);
fclose(fe);
fo = fopen(ODD , r);
fe = fopen(EVEN , r);
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");
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);
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();
}