1. Program to get input of two numbers and performing arithmetic operations
//getting the input of two values
//and performing aritmatic operation
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,s;
clrscr();
printf("\nEnter First value : ");
scanf("%d",&a);
printf("\nEnter Second value : ");
scanf("%d",&b);
s=a+b;
printf("\nSum : %d",s);
s=a-b;
printf("\nSubtraction : %d",s);
s=a*b;
printf("\nMultiplicattio : %d",s);
s=a/b;
printf("\nDivision : %d",s);
s=a%b;
printf("\nRemainder : %d",s);
getch();
}
2. Program to Calculate the Area of Circle
//claculate the area of circle
#include<stdio.h>
#include<conio.h>
void main()
{
float rad,area;
clrscr();
printf("Enter the Radius : ");
scanf("%f",&rad);
area=3.14*rad*rad;
printf("\nArea of Circle is %f",area);
getch();
}
3. Program to Swap the values of variables using three variables
//swapping the values of variables using three variables
#include<stdio.h>
#include<conio.h>
void main()
{
int x,y,s;
clrscr();
printf("Enter two numbers : ");
scanf("%d%d",&x,&y);
printf("\nValues of variables before Swapping");
printf("\nX = %d Y = %d",x,y);
s=x;
x=y;
y=s;
printf("\nValues of variables after Swapping");
printf("\nX = %d Y = %d",x,y);
getch();
}
4. Program to Swap the values of variables using two variables
//swapping the values of variables using three variables
#include<stdio.h>
#include<conio.h>
void main()
{
int x,y,s;
clrscr();
printf("Enter two numbers : ");
scanf("%d%d",&x,&y);
printf("\nValues of variables before Swapping");
printf("\nX = %d Y = %d",x,y);
x=x+y;
y=x-y;
x=x-y;
printf("\nValues of variables after Swapping");
printf("\nX = %d Y = %d",x,y);
getch();
}
5. Program to print the ASCII value of given Character
#include<stdio.h>
#include<conio.h>
void main()
{
char ch;
clrscr();
printf("Enter a Character : ");
ch=getchar();
printf("\nASCII value %d",ch);
getch();
}
6. Program to find number is Even or Odd
//Program to find number is Even or Odd
#include<stdio.h>
#include<conio.h>
void main()
{
int x;
clrscr();
printf("Enter a numbers : ");
scanf("%d",&x);
if(x%2==0)
{
printf("\nNumber is even - %d",x);
}
else
{
printf("\nNumber is odd - %d",x);
}
getch();
}
7. Program to find Number is Negative or Positive
//Program to find number is Negative or Positive
#include<stdio.h>
#include<conio.h>
void main()
{
int x;
clrscr();
printf("Enter a numbers : ");
scanf("%d",&x);
if(x>0)
{
printf("\nNumber is Positive - %d",x);
}
else
{
printf("\nNumber is Negative - %d",x);
}
getch();
}
8. Program to find greater between two numbers
//Program to find number is greater than other
#include<stdio.h>
#include<conio.h>
void main()
{
int x,y;
clrscr();
printf("Enter two numbers : ");
scanf("%d%d",&x,&y);
if(x>y)
{
printf("\n%d is greater than %d",x,y);
}
else
{
printf("\n%d is greater than %d",y,x);
}
getch();
}
9. Program to find number is Even, Odd or Zero using if-else-if Statement
//Program to find number is zero, even or odd
#include<stdio.h>
#include<conio.h>
void main()
{
int x;
clrscr();
printf("Enter a numbers : ");
scanf("%d",&x);
if(x==0)
{
printf("\nNumber is Zero - %d",x);
}
else if(x%2==0)
{
printf("\nNumber is Even - %d",x);
}
else
{
printf("\nNumber is Odd - %d",x);
}
getch();
}
10. Program to find Number is Zero, Negative or Positive using if-else-if statement
//Program to find number is zero, Negative or Positive
#include<stdio.h>
#include<conio.h>
void main()
{
int x;
clrscr();
printf("Enter a numbers : ");
scanf("%d",&x);
if(x==0)
{
printf("\nNumber is Zero - %d",x);
}
else if(x<0)
{
printf("\nNumber is negative - %d",x);
}
else
{
printf("\nNumber is positive - %d",x);
}
getch();
}
11. Program to find Greater among three number using nested if-else
//Program to find greater among three numbers
#include<stdio.h>
#include<conio.h>
void main()
{
int x,y,z;
clrscr();
printf("Enter three numbers : ");
scanf("%d%d%d",&x,&y,&z);
if(x>y)
{
if(x>z)
{
printf("\nX is Greater");
}
else
{
printf("\nZ is Greater");
}
}
else
{
if(y>z)
{
printf("\nY is Greater");
}
else
{
printf("\nZ is Greater");
}
}
getch();
}
12. Program to find greater among three numbers using Logical && operator
//Program to find greater among three numbers (&& operator)
#include<stdio.h>
#include<conio.h>
void main()
{
int x,y,z;
clrscr();
printf("Enter three numbers : ");
scanf("%d%d%d",&x,&y,&z);
if(x>y && x>z)
{
printf("\nX is Greater");
}
else if(y>x && y>z)
{
printf("\nY is Greater");
}
else
{
printf("\nZ is Greater");
}
getch();
}
13. Example of Bit-Wise Operators
//Example to show the use of Bit-Wise Operators
#include<stdio.h>
#include<conio.h>
void main()
{
int x,y,z;
clrscr();
x=5;
y=9;
z=6;
printf("\nLoginal AND operation : %d",x&y);
printf("\nLogicall OR operation : %d",x|y);
printf("\nLogical Ex-OR Operation : %d",x^y);
printf("\nShifting values right : %d",z>>2);
printf("\nShifting values left : %d",z<<3);
printf("\nComplement Operation : %d",~z);
getch();
}
14. Example to show the use of conditional Operator
#include<conio.h>
#include<stdio.h>
void main()
{
int x,y,r;
clrscr();
printf("Enter two numbers : ");
scanf("%d%d",&x,&y);
r=(x>y)?x:y;
printf("\n%d is greater",r);
getch();
}
15. Program to find greater among three numbers using conditional operator
//Greater among three numbers using conditional operator
#include<conio.h>
#include<stdio.h>
void main()
{
int x,y,z,r;
clrscr();
printf("Enter two numbers : ");
scanf("%d%d%d",&x,&y,&z);
r=(x>y)?(x>z)?x:z:(y>z)?y:z;
printf("\n%d is greater");
getch();
}
16. Program to print the name of the day using switch statement
#include<stdio.h>
#include<conio.h>
void main()
{
int num;
clrcsr();
printf("Enter the number of the day : ");
scanf("%d",&num);
switch(num)
{
case 1:
{
printf("\nFirst day is Monday");
break;
}
case 2:
{
printf("\nSecond day is Tuesday");
break;
}
case 3:
{
printf("\nThird day is Wednesday");
break;
}
case 4:
{
printf("\nForth day is Thursday");
break;
}
case 5:
{
printf("\nFifth day is Friday");
break;
}
case 6:
{
printf("\nSixth day is Saturday");
break;
}
case 7:
{
printf("\nSeventh day is Sunday");
break;
}
default:
{
printf("\nPlz.. Enter the correct Number ");
}
}
getch();
}
17. Finding the gender using Switch Statement
//Greater among three numbers using conditional operator
#include<conio.h>
#include<stdio.h>
void main()
{
char s;
clrscr();
printf("Enter M or m for Male and F or f for Female : ");
scanf("%c",&s);
switch(s)
{
case 'M':
case 'm':
{
printf("\nYou are a Male");
break;
}
case 'F':
case 'f':
{
printf("\nYoy are a Female");
break;
}
default:
{
printf("\Wrong Detail");
}
}
getch();
}
18. Program to find given number is vowel or not using switch statement
//Printing character is vowel or not using Switch statement
#include<stdio.h>
#include<conio.h>
void main()
{
char ch;
printf("Enter a character : ");
ch=getchar();
switch(ch)
{
case 'a':
case 'A':
case 'e':
case 'E':
case 'i':
case 'I':
case 'o':
case 'O':
case 'u':
case 'U':
{
printf("\n%c is a vowel",ch);
break;
}
default:
{
printf("%c is not a vowel");
}
}
getch();
}
19. Example for simple calculator with the help of switch statement
//Simple calculator using switch statement
#include<conio.h>
#include<stdio.h>
void main()
{
float a,b,s;
char ch;
printf("Enter the first Number : ");
scanf("%f",&a);
// fflush(stdin); // use only if required
printf("Enter the Symbol for Operation (+,-,/,*) : ");
ch=getchar();
printf("Enter the second Number : ");
scanf("%f",&b);
switch(ch)
{
case '+':
{
s=a+b;
break;
}
case '-':
{
s=a-b;
break;
}
case '/':
{
s=a/b;
break;
}
case '*':
{
s=a*b;
break;
}
}
printf("\nResult of %f %c %f is %f",a,ch,b,s);
getch();
}
20. Character input/output functions-getchar() and putchar()
//character input/output using getchar() and putchar()
//getchar() requires Enter key and echos the input character
#include<conio.h>
#include<stdio.h>
void main()
{
char ch;
clrscr();
printf("Enter a Character : ");
ch=getchar();
printf("Character entered by You : ");
putchar(ch);
getch();
}
21. Character input/output functions-getch() and putchar()
//character input/output using getch() and putch()
//getch() does not require Enter key and does not echo the input character
#include<conio.h>
#include<stdio.h>
void main()
{
char ch;
clrscr();
printf("Enter a Character : ");
ch=getch();
printf("\nCharacter entered by You : ");
putch(ch);
getch();
}
21. Character input/output functions-getche() and putchar()
//character input/output using getche() and putchar()
//getche does not requires Enter key and echos the input character ('e' for echo)
#include<conio.h>
#include<stdio.h>
void main()
{
char ch;
clrscr();
printf("Enter a Character : ");
ch=getche();
printf("\nCharacter entered by You : ");
putchar(ch);
getch();
}
22. Example of While Loop-Printing the number in ascending and descending order
#include<stdio.h>
#include<conio.h>
void main()
{
int i=1; //initialization ofloop-starting of loop
clrscr();
while(i<=10)
{
printf("%d\t",i);
i++; //increment the counter variable
}
//Printing the numbers in reverse order
i=10; //initialization ofloop-starting of loop
while(i>=1) //condition specification-end of the loop
{
printf("%d\t",i);
i--; //decrement the counter variable
}
getch();
}
23. Example of Do-While Loop-Printing the number in ascending and descending order
#include<stdio.h>
#include<conio.h>
void main()
{
int i=1; //initialization ofloop-starting of loop
clrscr();
do
{
printf("%d\t",i);
i++; //incrementing the counter variable
}
while(i<=10); //condition specification-end of the loop
//Printing the numbers in reverse order
i=10; //initialization ofloop-starting of loop
do
{
printf("%d\t",i);
i--; //decrementing the counter variable
}
while(i>=1); //condition specification-end of the loop
getch();
}
23. Example of For Loop-Printing the number in ascending and descending order
#include<stdio.h>
#include<conio.h>
void main()
{
int i;
clrscr();
for(i=1;i<=10;i++)
{
printf("%d\t",i);
}
//Printing the numbers in reverse order
for(i=10;i>=1;i--)
{
printf("%d\t",i);
}
getch();
}
24. Printing the Even and Odd numbers upto 10-without if statement
#include<stdio.h>
#include<conio.h>
void main()
{
int i=2;
clrscr();
printf("\nEven : ");
while(i<=10)
{
printf(" %d ",i);
i=i+2;
}
printf("\nOdd : ");
i=1;
while(i<=10)
{
printf(" %d ",i);
i=i+2;
}
getch();
}
24. Printing the Even and Odd numbers upto 10-without if statement
#include<stdio.h>
#include<conio.h>
void main()
{
int i=2;
clrscr();
printf("\nEven : ");
for(i=1;i<=10;i++)
{
if(i%2==0) //True when remainder is equal to zero
{
printf(" %d ",i);
}
}
printf("\nOdd : ");
for(i=1;i<=10;i++)
{
if(i%2!=0) //True when remainder is not equal to zero. also try if(i%2==1)
{
printf(" %d ",i);
}
}
getch();
}
24. Program to find the factorial of given number using For Loop
#include<stdio.h>
#include<conio.h>
void main()
{
int i,num,fact=1;
clrscr();
printf("\nEnter a number to find the factorial : ");
scanf("%d",&num);
for(i=num;i>=1;i--)
{
fact=fact*i;
}
printf("\nFactorial of %d id %d",num,fact);
getch();
}
25. Program to Find the factorial using While Loop
#include<stdio.h>
#include<conio.h>
void main()
{
int num,fact=1;
clrscr();
printf("\nEnter a number to find the factorial : ");
scanf("%d",&num);
while(num>=1)
{
fact=fact*num;
num--;
}
printf("\nFactorial is %d",fact);
getch();
}
26. Program to Reverse the Given Number
#include<conio.h>
#include<stdio.h>
void main()
{
int num,r,rev=0;
clrscr();
printf("Enter a Number : ");
scanf("%d",&num);
while(num>0)
{
r=num%10;
rev=rev*10+r;
num=num/10;
}
printf("\nReverse of Number %d",rev);
getch();
}
27. Program to Print the series of alphabets from A to Z
#include<conio.h>
#include<stdio.h>
void main()
{
char i;
for(i='A';i<='Z';i++)
{
printf(" %c ",i);
}
getch();
}
28. Program to print the sum of digits of number
#include<conio.h>
#include<stdio.h>
void main()
{
int num,r,s=0;
clrscr();
printf("Enter a number : ");
scanf("%d",&num);
while(num>0)
{
r=num%10;
s=s+r;
num=num/10;
}
printf("Sum of digits :: %d",s);
getch();
}
29. Program to find Number is Palindrome or Not.
#include<conio.h>
#include<stdio.h>
void main()
{
int num,cp,r,arm;
printf("Enter a number : ");
scanf("%d",&num);
cp=num;
while(num>0)
{
r=num%10;
arm=r*r*r;
num=num/10;
}
if(cp==rev)
{
printf("Number is Palindrome");
}
else
{
printf("Number is not Palindrome");
}
getch();
}
30. Program to find number is Armstrong or not.
#include<conio.h>
#include<stdio.h>
void main()
{
int num,cp,r,arm=0;
printf("Enter a number : ");
scanf("%d",&num);
cp=num;
while(num>0)
{
r=num%10;
arm=arm+r*r*r;
num=num/10;
}
if(cp==arm)
{
printf("Number is Palindrom");
}
else
{
printf("Number is not Palindrom");
}
getch();
}
31. Program to print the sum of even numbers up to 10
#include<conio.h>
#include<stdio.h>
void main()
{
int i,sum=0;
for(i=2;i<=10;i++)
{
if(i%2==0)
{
sum=sum+i;
}
}
printf("Sum of even numbers :: %d",sum);
getch();
}
32. Program to print the Pyramid of '*'
#include<conio.h>
#include<stdio.h>
void main()
{
int i,j;
clrscr();
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
printf("* ");
}
printf("\n");
}
getch();
}
33. Program to print the Pyramid of Numbers
34. Program to print Pyramid of Alphabets
#include<conio.h>
#include<stdio.h>
void main()
{
char i,j;
for(i='A';i<='G';i++)
{
for(j='A';j<=i;j++)
{
printf(" %c ",j);
}
printf("\n");
}
getch();
}
35. Program to print the Table of given
#include<stdio.h>
#include<conio.h>
void main()
{
int i,num,m;
clrscr();
printf("Enter a number to print table : ");
scanf("%d",&num);
for(i=1;i<=10;i++)
{
m=num*i;
printf("\n%d x %d = %d",num,i,m);
}
getch();
}
36. Program to convert from Binary to Decimal
#include<conio.h>
#include<stdio.h>
#include<math.h>
void main()
{
int b,d=0,r,i=0;
printf("Enter a Binary Number : ");
scanf("%d",&b);
while(b>0)
{
r=b%10;
d=d+r*pow(2,i);
b=b/10;
i++;
}
printf("Decimal Equvilant is : %d",d);
getch();
}
37. Program to convert from Decimal to Binary
#include<conio.h>
#include<stdio.h>
void main()
{
int d,b=0,f=0,r,n=0,rev=0,i;
clrscr();
printf("Enter a Decimal Number : ");
scanf("%d",&d);
while(d>0)
{
r=d%2;
if(r==0 && f==0)
{
n++;
}
else
{
f=1;
b=b*10+r;
}
d=d/2;
}
while(b>0)
{
r=b%10;
rev=rev*10+r;
b=b/10;
}
for(i=1;i<=n;i++)
{
rev=rev*10;
}
printf("\n Binary Equivalent : %d",rev);
getch();
}
38. Program to Print the Fibonacci Series
//Fibonacci
#include<stdio.h>
#include<conio.h>
void main()
{
int n,n1,n2,s;
clrscr();
printf("Enter the final term of the series : ");
scanf("%d",&n);
n1=0;
n2=1;
printf("%d\n%d",n1,n2);
s=n1+n2;
while(s<=n)
{
printf("\n%d",s);
n1=n2;
n2=s;
s=n1+n2;
}
getch();
}
39. Program to store the elements of Array, print the elements.
#include<stdio.h>
#include<conio.h>
void main()
{
int arr[10],i;
clrscr();
for(i=0;i<10;i++)
{
printf("Enter the value for arr[%d] : ",i);
scanf("%d",&arr[i]);
}
for(i=0;i<10;i++)
{
printf("\narr[%d] = %d",i,arr[i]);
}
getch();
}
40. Program to print the elements of array in reverse order.
#include<stdio.h>
#include<conio.h>
void main()
{
int n,arr[20],i;
clrscr();
printf("Enter the number of elements less than 10 : ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the value for arr[%d] : ",i);
scanf("%d",&arr[i]);
}
for(i=n-1;i>=0;i--)
{
printf("\n%d",arr[i]);
}
getch();
}
41. Program to search an element in array.
#include<stdio.h>
#include<conio.h>
void main()
{
int i,n,arr[10]={12,32,43,38,25,65,51,55,10,29};
clrscr();
printf("Enter a number to find in array : ");
scanf("%d",&n);
for(i=0;i<10;i++)
{
if(arr[i]==n)
{
printf("\nThe element %d is found at location %d",n,i+1);
getch();
exit(0);
}
}
printf("\nElement not found");
getch();
}
42. Program to find the sum and mean of array elements.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[50],n,i,sum=0,m;
clrscr();
printf("Enter the number of elemnts : ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the element %d : ",i+1);
scanf("%d",&a[i]);
}
for(i=0;i<n;i++)
{
sum=sum+a[i]; //or sum+=a[i]
}
m=sum/n;
printf("\nThe Sum of array elements is %d",sum);
printf("\nThe mean of array elements is %d",m);
getch();
}
43. Program to find the largest element in the array.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[50],n,i,max;
clrscr();
printf("Enter the number of elemnts : ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the element %d : ",i+1);
scanf("%d",&a[i]);
}
max=a[0];
for(i=0;i<n;i++)
{
if(a[i]>max)
{
max=a[i];
}
}
printf("The maximum value is %d",max);
getch();
}
44. Program to sort the array using bubble sort.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[50],n,i,j,s;
printf("Enter the number of elemnts : ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the element %d : ",i+1);
scanf("%d",&a[i]);
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(a[j]>a[j+1])
{
s=a[j];
a[j]=a[j+1];
a[j+1]=s;
}
}
}
for(i=0;i<n;i++)
{
printf("\n%d",a[i]);
}
getch();
}
45. Program to sort array elements using selection sort.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[50],n,i,j,s;
printf("Enter the number of elemnts : ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the element %d : ",i+1);
scanf("%d",&a[i]);
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(a[i]<a[j])
{
s=a[i];
a[i]=a[j];
a[j]=s;
}
}
}
for(i=0;i<n;i++)
{
printf("\n%d",a[i]);
}
getch();
}
46. Program to get insert values into two dimensional Array and display them.
#include<conio.h>
#include<stdio.h>
void main()
{
int i,j,a[3][3];
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("Enter the value a[%d][%d] : ",i,j);
scanf("%d",&a[i][j]);
}
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d ",a[i][j]);
}
printf("\n");
}
getch();
}
47. Program to multiply two matrix.
#include<conio.h>
#include<stdio.h>
void main()
{
int m1[3][3],m2[3][3],m[3][3],i,j,k;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("Enter the values of first Matrix ");
scanf("%d",&m1[i][j]);
}
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("Enter the values of second Matrix ");
scanf("%d",&m1[i][j]);
}
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
m[i][j]=0;
for(k=0;k<3;k++)
{
m[i][j]=m[i][j]+m1[i][k]*m2[k][j];
}
}
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d",m[i][j]);
}
printf("\n");
}
getch();
}
48. Program to Transpose the Matrix
#include<conio.h>
#include<stdio.h>
void main()
{
int m1[10][10],m2[10][10],i,j,r,c;
printf("Enter the number of rows and columns : ");
scanf("%d%d",&r,&c);
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("Enter the values of Matrix [%d][%d]",i+1,j+1);
scanf("%d",&m1[i][j]);
}
}
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
m2[j][i]=m1[i][j];
}
}
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("%d ",m1[i][j]);
}
printf("\n");
}
printf("\nMatrix After Tranpose \n");
for(i=0;i<c;i++)
{
for(j=0;j<r;j++)
{
printf("%d ",m2[i][j]);
}
printf("\n");
}
getch();
}
49. Program to calculate the Trace of matrix
#include<stdio.h>
#include<conio.h>
void main()
{
int n,s=0,i,j,m[10][10];
printf("Enter ths size of matrix (n) : ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("Enter the value for [%d][%d] element : ",i,j);
scanf("%d",&m[i][j]);
}
}
printf("\nThe Matrix is :\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("%d ",m[i][j]);
}
printf("\n");
}
for(i=0;i<n;i++)
{
s=s+m[i][i];
}
printf("\nThe Trace of Matrix : %d",s);
getch();
}
50. Example for Character Function - getchar() and putchar()
#include<conio.h>
#include<stdio.h>
void main()
{
char ch;
clrscr();
printf("Enter a Character : ");
ch=getchar();
printf("The Character entered : ");
putchar(ch);
getch();
}
51. Example for Character Function - getch() and putch()
#include<conio.h>
#include<stdio.h>
void main()
{
char ch;
clrscr();
printf("Enter a Character : ");
ch=getch();
printf("The Character entered : ");
putch(ch);
getch();
}
50. Example for Character Function - getche() and putch()
#include<conio.h>
#include<stdio.h>
void main()
{
char ch;
clrscr();
printf("Enter a Character : ");
ch=getche();
printf("The Character entered : ");
putch(ch);
getch();
}
51. Character input and output using -scanf() and printf()
#include<conio.h>
#include<stdio.h>
void main()
{
char ch;
clrscr();
printf("Enter a Character : ");
scanf("%c",&ch);
printf("\nThe Character entered by you is : %c",ch);
getch();
}
52. String Input and Output using - gets() and puts()
#include<conio.h>
#include<stdio.h>
void main()
{
char st[15];
clrscr();
puts("Enter Your Name : ");
gets(st);
puts("Your Name is : ");
puts(st);
getch();
}
53. String input and output using - scanf() and printf()
#include<conio.h>
#include<stdio.h>
void main()
{
char st[15];
clrscr();
printf("Enter Your Name : ");
scanf("%s",st);
printf("Your Name is : %s",st);
getch();
}
54. Calculating the length of string using strlen() function.
#include<conio.h>
#include<stdio.h>
#include<string.h>
void main()
{
char st[15];
clrscr();
printf("Enter Your Name : ");
gets(st);
printf("Number os characters is your name (length) : %d",strlen(st));
getch();
}
55. Copying the string using strcpy() function
#include<conio.h>
#include<stdio.h>
#include<string.h>
void main()
{
char st[15],cp[15];
clrscr();
strcpy(st,"Programming");
printf("\nString copied to st variable : %s",st);
strcpy(cp,st);
printf("\nString Copied to cp variable : %s",cp);
getch();
}
56. Concatenating the string using strcat() function.
#include<conio.h>
#include<stdio.h>
#include<string.h>
void main()
{
char st1[15],st2[15];
clrscr();
strcpy(st1,"Programming");
strcpy(st2,"-Language");
strcat(st1,st2);
printf("\nStrings After concatenation : %s",st1);
getch();
}
57. Comparing string using strcmp() function.
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char st1[15]="CLanguage";
char st2[15]="CPPLanguage";
char st3[15]="CLanguage";
int r;
clrscr();
r=strcmp(st1,st3); //Returns zero when strings are equal
printf("\nResult : %d",r);
r=strcmp(st1,st2); //Returns a -ve number if 1st string is smaller
printf("\nResult : %d",r);
r=strcmp(st2,st1); //Returns a +ve number if 1st string is bigger
printf("\nResult : %d",r);
getch();
}
58. Checking strings are equal, greater or smaller.
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char st1[15],st2[15];
clrscr();
printf("Enter First String : ");
gets(st1);
printf("Enter Second String : ");
gets(st2);
if(strcmp(st1,st2)==0)
{
printf("Both strings are Equal");
}
else if(strcmp(st1,st2)>0)
{
printf("First String is Greater");
}
else
{
printf("Second string is Greater");
}
getch();
}
59. Calculating the number (length) of characters in a string without strlen().
#include<stdio.h>
#include<conio.h>
void main()
{
char st[15];
int i=0;
printf("Enter a String : ");
gets(st);
while(st[i]!='\0')
{
i++;
}
printf("Length of String is %d",i);
getch();
}
60. Calculating the number of vowels in a string.
#include<stdio.h>
#include<conio.h>
void main()
{
char st[15],ch;
int i,c=0;
printf("Enter a String : ");
gets(st);
for(i=0;i<strlen(st);i++)
{
switch(st[i])
{
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
case 'a':
case 'e':
case 'o':
case 'i':
case 'u':
c++;
}
}
printf("Number of Vowel's in String is %d",c);
getch();
}
61. Calculating the number of occurrence of specific character in a string.
#include<stdio.h>
#include<conio.h>
void main()
{
char st[15],ch;
int i=0,c=0;
printf("Enter a String : ");
gets(st);
printf("Enter a charcater you want to count : ");
ch=getchar();
while(st[i]!='\0')
{
if(st[i]==ch)
{
c++;
}
i++;
}
printf("The character %c occurs %d Times ",ch,c);
getch();
}
62. Concatenating two strings without using built in function strcat().
#include<stdio.h>
#include<conio.h>
void main()
{
char st1[15],st2[15],st[30];
int i=0,j=0;
printf("Enter first String : ");
gets(st1);
printf("Enter second String : ");
gets(st2);
while(i<strlen(st1))
{
st[j]=st1[i];
i++;
j++;
}
i=0;
while(i<strlen(st2))
{
st[j]=st2[i];
i++;
j++;
}
printf("The character %s ",st);
getch();
}
63. Copying string without using built in function (strcpy).
#include<conio.h>
#include<stdio.h>
void main()
{
char str1[15],str2[15];
int i=0;
printf("Enter a string : ");
gets(str1);
while(str1[i]!='\0')
{
str2[i]=str1[i];
i++;
}
printf("The copied String %s",str2);
getch();
}
64. Function without arguments and without return type.
#include<stdio.h>
#include<conio.h>
//An example of function without return type and without arguments
void show();// Function Declaration or Prototype
void main()
{
show(); //first call to function
show(); // second call to function.
getch();
}
void show() //function definition
{
printf("\nSimpe Function Example");
}
//getting the input of two values
//and performing aritmatic operation
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,s;
clrscr();
printf("\nEnter First value : ");
scanf("%d",&a);
printf("\nEnter Second value : ");
scanf("%d",&b);
s=a+b;
printf("\nSum : %d",s);
s=a-b;
printf("\nSubtraction : %d",s);
s=a*b;
printf("\nMultiplicattio : %d",s);
s=a/b;
printf("\nDivision : %d",s);
s=a%b;
printf("\nRemainder : %d",s);
getch();
}
2. Program to Calculate the Area of Circle
//claculate the area of circle
#include<stdio.h>
#include<conio.h>
void main()
{
float rad,area;
clrscr();
printf("Enter the Radius : ");
scanf("%f",&rad);
area=3.14*rad*rad;
printf("\nArea of Circle is %f",area);
getch();
}
3. Program to Swap the values of variables using three variables
//swapping the values of variables using three variables
#include<stdio.h>
#include<conio.h>
void main()
{
int x,y,s;
clrscr();
printf("Enter two numbers : ");
scanf("%d%d",&x,&y);
printf("\nValues of variables before Swapping");
printf("\nX = %d Y = %d",x,y);
s=x;
x=y;
y=s;
printf("\nValues of variables after Swapping");
printf("\nX = %d Y = %d",x,y);
getch();
}
4. Program to Swap the values of variables using two variables
//swapping the values of variables using three variables
#include<stdio.h>
#include<conio.h>
void main()
{
int x,y,s;
clrscr();
printf("Enter two numbers : ");
scanf("%d%d",&x,&y);
printf("\nValues of variables before Swapping");
printf("\nX = %d Y = %d",x,y);
x=x+y;
y=x-y;
x=x-y;
printf("\nValues of variables after Swapping");
printf("\nX = %d Y = %d",x,y);
getch();
}
5. Program to print the ASCII value of given Character
#include<stdio.h>
#include<conio.h>
void main()
{
char ch;
clrscr();
printf("Enter a Character : ");
ch=getchar();
printf("\nASCII value %d",ch);
getch();
}
6. Program to find number is Even or Odd
//Program to find number is Even or Odd
#include<stdio.h>
#include<conio.h>
void main()
{
int x;
clrscr();
printf("Enter a numbers : ");
scanf("%d",&x);
if(x%2==0)
{
printf("\nNumber is even - %d",x);
}
else
{
printf("\nNumber is odd - %d",x);
}
getch();
}
7. Program to find Number is Negative or Positive
//Program to find number is Negative or Positive
#include<stdio.h>
#include<conio.h>
void main()
{
int x;
clrscr();
printf("Enter a numbers : ");
scanf("%d",&x);
if(x>0)
{
printf("\nNumber is Positive - %d",x);
}
else
{
printf("\nNumber is Negative - %d",x);
}
getch();
}
//Program to find number is greater than other
#include<stdio.h>
#include<conio.h>
void main()
{
int x,y;
clrscr();
printf("Enter two numbers : ");
scanf("%d%d",&x,&y);
if(x>y)
{
printf("\n%d is greater than %d",x,y);
}
else
{
printf("\n%d is greater than %d",y,x);
}
getch();
}
9. Program to find number is Even, Odd or Zero using if-else-if Statement
//Program to find number is zero, even or odd
#include<stdio.h>
#include<conio.h>
void main()
{
int x;
clrscr();
printf("Enter a numbers : ");
scanf("%d",&x);
if(x==0)
{
printf("\nNumber is Zero - %d",x);
}
else if(x%2==0)
{
printf("\nNumber is Even - %d",x);
}
else
{
printf("\nNumber is Odd - %d",x);
}
getch();
}
10. Program to find Number is Zero, Negative or Positive using if-else-if statement
//Program to find number is zero, Negative or Positive
#include<stdio.h>
#include<conio.h>
void main()
{
int x;
clrscr();
printf("Enter a numbers : ");
scanf("%d",&x);
if(x==0)
{
printf("\nNumber is Zero - %d",x);
}
else if(x<0)
{
printf("\nNumber is negative - %d",x);
}
else
{
printf("\nNumber is positive - %d",x);
}
getch();
}
11. Program to find Greater among three number using nested if-else
//Program to find greater among three numbers
#include<stdio.h>
#include<conio.h>
void main()
{
int x,y,z;
clrscr();
printf("Enter three numbers : ");
scanf("%d%d%d",&x,&y,&z);
if(x>y)
{
if(x>z)
{
printf("\nX is Greater");
}
else
{
printf("\nZ is Greater");
}
}
else
{
if(y>z)
{
printf("\nY is Greater");
}
else
{
printf("\nZ is Greater");
}
}
getch();
}
12. Program to find greater among three numbers using Logical && operator
//Program to find greater among three numbers (&& operator)
#include<stdio.h>
#include<conio.h>
void main()
{
int x,y,z;
clrscr();
printf("Enter three numbers : ");
scanf("%d%d%d",&x,&y,&z);
if(x>y && x>z)
{
printf("\nX is Greater");
}
else if(y>x && y>z)
{
printf("\nY is Greater");
}
else
{
printf("\nZ is Greater");
}
getch();
}
13. Example of Bit-Wise Operators
//Example to show the use of Bit-Wise Operators
#include<stdio.h>
#include<conio.h>
void main()
{
int x,y,z;
clrscr();
x=5;
y=9;
z=6;
printf("\nLoginal AND operation : %d",x&y);
printf("\nLogicall OR operation : %d",x|y);
printf("\nLogical Ex-OR Operation : %d",x^y);
printf("\nShifting values right : %d",z>>2);
printf("\nShifting values left : %d",z<<3);
printf("\nComplement Operation : %d",~z);
getch();
}
14. Example to show the use of conditional Operator
#include<conio.h>
#include<stdio.h>
void main()
{
int x,y,r;
clrscr();
printf("Enter two numbers : ");
scanf("%d%d",&x,&y);
r=(x>y)?x:y;
printf("\n%d is greater",r);
getch();
}
15. Program to find greater among three numbers using conditional operator
//Greater among three numbers using conditional operator
#include<conio.h>
#include<stdio.h>
void main()
{
int x,y,z,r;
clrscr();
printf("Enter two numbers : ");
scanf("%d%d%d",&x,&y,&z);
r=(x>y)?(x>z)?x:z:(y>z)?y:z;
printf("\n%d is greater");
getch();
}
16. Program to print the name of the day using switch statement
#include<stdio.h>
#include<conio.h>
void main()
{
int num;
clrcsr();
printf("Enter the number of the day : ");
scanf("%d",&num);
switch(num)
{
case 1:
{
printf("\nFirst day is Monday");
break;
}
case 2:
{
printf("\nSecond day is Tuesday");
break;
}
case 3:
{
printf("\nThird day is Wednesday");
break;
}
case 4:
{
printf("\nForth day is Thursday");
break;
}
case 5:
{
printf("\nFifth day is Friday");
break;
}
case 6:
{
printf("\nSixth day is Saturday");
break;
}
case 7:
{
printf("\nSeventh day is Sunday");
break;
}
default:
{
printf("\nPlz.. Enter the correct Number ");
}
}
getch();
}
//Greater among three numbers using conditional operator
#include<conio.h>
#include<stdio.h>
void main()
{
char s;
clrscr();
printf("Enter M or m for Male and F or f for Female : ");
scanf("%c",&s);
switch(s)
{
case 'M':
case 'm':
{
printf("\nYou are a Male");
break;
}
case 'F':
case 'f':
{
printf("\nYoy are a Female");
break;
}
default:
{
printf("\Wrong Detail");
}
}
getch();
}
18. Program to find given number is vowel or not using switch statement
//Printing character is vowel or not using Switch statement
#include<stdio.h>
#include<conio.h>
void main()
{
char ch;
printf("Enter a character : ");
ch=getchar();
switch(ch)
{
case 'a':
case 'A':
case 'e':
case 'E':
case 'i':
case 'I':
case 'o':
case 'O':
case 'u':
case 'U':
{
printf("\n%c is a vowel",ch);
break;
}
default:
{
printf("%c is not a vowel");
}
}
getch();
}
19. Example for simple calculator with the help of switch statement
//Simple calculator using switch statement
#include<conio.h>
#include<stdio.h>
void main()
{
float a,b,s;
char ch;
printf("Enter the first Number : ");
scanf("%f",&a);
// fflush(stdin); // use only if required
printf("Enter the Symbol for Operation (+,-,/,*) : ");
ch=getchar();
printf("Enter the second Number : ");
scanf("%f",&b);
switch(ch)
{
case '+':
{
s=a+b;
break;
}
case '-':
{
s=a-b;
break;
}
case '/':
{
s=a/b;
break;
}
case '*':
{
s=a*b;
break;
}
}
printf("\nResult of %f %c %f is %f",a,ch,b,s);
getch();
}
20. Character input/output functions-getchar() and putchar()
//character input/output using getchar() and putchar()
//getchar() requires Enter key and echos the input character
#include<conio.h>
#include<stdio.h>
void main()
{
char ch;
clrscr();
printf("Enter a Character : ");
ch=getchar();
printf("Character entered by You : ");
putchar(ch);
getch();
}
21. Character input/output functions-getch() and putchar()
//character input/output using getch() and putch()
//getch() does not require Enter key and does not echo the input character
#include<conio.h>
#include<stdio.h>
void main()
{
char ch;
clrscr();
printf("Enter a Character : ");
ch=getch();
printf("\nCharacter entered by You : ");
putch(ch);
getch();
}
21. Character input/output functions-getche() and putchar()
//character input/output using getche() and putchar()
//getche does not requires Enter key and echos the input character ('e' for echo)
#include<conio.h>
#include<stdio.h>
void main()
{
char ch;
clrscr();
printf("Enter a Character : ");
ch=getche();
printf("\nCharacter entered by You : ");
putchar(ch);
getch();
}
22. Example of While Loop-Printing the number in ascending and descending order
#include<stdio.h>
#include<conio.h>
void main()
{
int i=1; //initialization ofloop-starting of loop
clrscr();
while(i<=10)
{
printf("%d\t",i);
i++; //increment the counter variable
}
//Printing the numbers in reverse order
i=10; //initialization ofloop-starting of loop
while(i>=1) //condition specification-end of the loop
{
printf("%d\t",i);
i--; //decrement the counter variable
}
getch();
}
23. Example of Do-While Loop-Printing the number in ascending and descending order
#include<stdio.h>
#include<conio.h>
void main()
{
int i=1; //initialization ofloop-starting of loop
clrscr();
do
{
printf("%d\t",i);
i++; //incrementing the counter variable
}
while(i<=10); //condition specification-end of the loop
//Printing the numbers in reverse order
i=10; //initialization ofloop-starting of loop
do
{
printf("%d\t",i);
i--; //decrementing the counter variable
}
while(i>=1); //condition specification-end of the loop
getch();
}
23. Example of For Loop-Printing the number in ascending and descending order
#include<stdio.h>
#include<conio.h>
void main()
{
int i;
clrscr();
for(i=1;i<=10;i++)
{
printf("%d\t",i);
}
//Printing the numbers in reverse order
for(i=10;i>=1;i--)
{
printf("%d\t",i);
}
getch();
}
24. Printing the Even and Odd numbers upto 10-without if statement
#include<stdio.h>
#include<conio.h>
void main()
{
int i=2;
clrscr();
printf("\nEven : ");
while(i<=10)
{
printf(" %d ",i);
i=i+2;
}
printf("\nOdd : ");
i=1;
while(i<=10)
{
printf(" %d ",i);
i=i+2;
}
getch();
}
24. Printing the Even and Odd numbers upto 10-without if statement
#include<stdio.h>
#include<conio.h>
void main()
{
int i=2;
clrscr();
printf("\nEven : ");
for(i=1;i<=10;i++)
{
if(i%2==0) //True when remainder is equal to zero
{
printf(" %d ",i);
}
}
printf("\nOdd : ");
for(i=1;i<=10;i++)
{
if(i%2!=0) //True when remainder is not equal to zero. also try if(i%2==1)
{
printf(" %d ",i);
}
}
getch();
}
24. Program to find the factorial of given number using For Loop
#include<stdio.h>
#include<conio.h>
void main()
{
int i,num,fact=1;
clrscr();
printf("\nEnter a number to find the factorial : ");
scanf("%d",&num);
for(i=num;i>=1;i--)
{
fact=fact*i;
}
printf("\nFactorial of %d id %d",num,fact);
getch();
}
25. Program to Find the factorial using While Loop
#include<stdio.h>
#include<conio.h>
void main()
{
int num,fact=1;
clrscr();
printf("\nEnter a number to find the factorial : ");
scanf("%d",&num);
while(num>=1)
{
fact=fact*num;
num--;
}
printf("\nFactorial is %d",fact);
getch();
}
26. Program to Reverse the Given Number
#include<conio.h>
#include<stdio.h>
void main()
{
int num,r,rev=0;
clrscr();
printf("Enter a Number : ");
scanf("%d",&num);
while(num>0)
{
r=num%10;
rev=rev*10+r;
num=num/10;
}
printf("\nReverse of Number %d",rev);
getch();
}
27. Program to Print the series of alphabets from A to Z
#include<conio.h>
#include<stdio.h>
void main()
{
char i;
for(i='A';i<='Z';i++)
{
printf(" %c ",i);
}
getch();
}
28. Program to print the sum of digits of number
#include<conio.h>
#include<stdio.h>
void main()
{
int num,r,s=0;
clrscr();
printf("Enter a number : ");
scanf("%d",&num);
while(num>0)
{
r=num%10;
s=s+r;
num=num/10;
}
printf("Sum of digits :: %d",s);
getch();
}
#include<conio.h>
#include<stdio.h>
void main()
{
int num,cp,r,arm;
printf("Enter a number : ");
scanf("%d",&num);
cp=num;
while(num>0)
{
r=num%10;
arm=r*r*r;
num=num/10;
}
if(cp==rev)
{
printf("Number is Palindrome");
}
else
{
printf("Number is not Palindrome");
}
getch();
}
30. Program to find number is Armstrong or not.
#include<conio.h>
#include<stdio.h>
void main()
{
int num,cp,r,arm=0;
printf("Enter a number : ");
scanf("%d",&num);
cp=num;
while(num>0)
{
r=num%10;
arm=arm+r*r*r;
num=num/10;
}
if(cp==arm)
{
printf("Number is Palindrom");
}
else
{
printf("Number is not Palindrom");
}
getch();
}
31. Program to print the sum of even numbers up to 10
#include<conio.h>
#include<stdio.h>
void main()
{
int i,sum=0;
for(i=2;i<=10;i++)
{
if(i%2==0)
{
sum=sum+i;
}
}
printf("Sum of even numbers :: %d",sum);
getch();
}
32. Program to print the Pyramid of '*'
#include<stdio.h>
void main()
{
int i,j;
clrscr();
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
printf("* ");
}
printf("\n");
}
getch();
}
33. Program to print the Pyramid of Numbers
#include<conio.h>
#include<stdio.h>
void main()
{
int i,j;
clrscr();
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
printf("%d ",i);
}
printf("\n");
}
getch();
}
#include<conio.h>
#include<stdio.h>
void main()
{
char i,j;
for(i='A';i<='G';i++)
{
for(j='A';j<=i;j++)
{
printf(" %c ",j);
}
printf("\n");
}
getch();
}
35. Program to print the Table of given
#include<stdio.h>
#include<conio.h>
void main()
{
int i,num,m;
clrscr();
printf("Enter a number to print table : ");
scanf("%d",&num);
for(i=1;i<=10;i++)
{
m=num*i;
printf("\n%d x %d = %d",num,i,m);
}
getch();
}
36. Program to convert from Binary to Decimal
#include<conio.h>
#include<stdio.h>
#include<math.h>
void main()
{
int b,d=0,r,i=0;
printf("Enter a Binary Number : ");
scanf("%d",&b);
while(b>0)
{
r=b%10;
d=d+r*pow(2,i);
b=b/10;
i++;
}
printf("Decimal Equvilant is : %d",d);
getch();
}
37. Program to convert from Decimal to Binary
#include<conio.h>
#include<stdio.h>
void main()
{
int d,b=0,f=0,r,n=0,rev=0,i;
clrscr();
printf("Enter a Decimal Number : ");
scanf("%d",&d);
while(d>0)
{
r=d%2;
if(r==0 && f==0)
{
n++;
}
else
{
f=1;
b=b*10+r;
}
d=d/2;
}
while(b>0)
{
r=b%10;
rev=rev*10+r;
b=b/10;
}
for(i=1;i<=n;i++)
{
rev=rev*10;
}
printf("\n Binary Equivalent : %d",rev);
getch();
}
38. Program to Print the Fibonacci Series
//Fibonacci
#include<stdio.h>
#include<conio.h>
void main()
{
int n,n1,n2,s;
clrscr();
printf("Enter the final term of the series : ");
scanf("%d",&n);
n1=0;
n2=1;
printf("%d\n%d",n1,n2);
s=n1+n2;
while(s<=n)
{
printf("\n%d",s);
n1=n2;
n2=s;
s=n1+n2;
}
getch();
}
39. Program to store the elements of Array, print the elements.
#include<stdio.h>
#include<conio.h>
void main()
{
int arr[10],i;
clrscr();
for(i=0;i<10;i++)
{
printf("Enter the value for arr[%d] : ",i);
scanf("%d",&arr[i]);
}
for(i=0;i<10;i++)
{
printf("\narr[%d] = %d",i,arr[i]);
}
getch();
}
40. Program to print the elements of array in reverse order.
#include<stdio.h>
#include<conio.h>
void main()
{
int n,arr[20],i;
clrscr();
printf("Enter the number of elements less than 10 : ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the value for arr[%d] : ",i);
scanf("%d",&arr[i]);
}
for(i=n-1;i>=0;i--)
{
printf("\n%d",arr[i]);
}
getch();
}
41. Program to search an element in array.
#include<stdio.h>
#include<conio.h>
void main()
{
int i,n,arr[10]={12,32,43,38,25,65,51,55,10,29};
clrscr();
printf("Enter a number to find in array : ");
scanf("%d",&n);
for(i=0;i<10;i++)
{
if(arr[i]==n)
{
printf("\nThe element %d is found at location %d",n,i+1);
getch();
exit(0);
}
}
printf("\nElement not found");
getch();
}
42. Program to find the sum and mean of array elements.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[50],n,i,sum=0,m;
clrscr();
printf("Enter the number of elemnts : ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the element %d : ",i+1);
scanf("%d",&a[i]);
}
for(i=0;i<n;i++)
{
sum=sum+a[i]; //or sum+=a[i]
}
m=sum/n;
printf("\nThe Sum of array elements is %d",sum);
printf("\nThe mean of array elements is %d",m);
getch();
}
43. Program to find the largest element in the array.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[50],n,i,max;
clrscr();
printf("Enter the number of elemnts : ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the element %d : ",i+1);
scanf("%d",&a[i]);
}
max=a[0];
for(i=0;i<n;i++)
{
if(a[i]>max)
{
max=a[i];
}
}
printf("The maximum value is %d",max);
getch();
}
44. Program to sort the array using bubble sort.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[50],n,i,j,s;
printf("Enter the number of elemnts : ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the element %d : ",i+1);
scanf("%d",&a[i]);
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(a[j]>a[j+1])
{
s=a[j];
a[j]=a[j+1];
a[j+1]=s;
}
}
}
for(i=0;i<n;i++)
{
printf("\n%d",a[i]);
}
getch();
}
45. Program to sort array elements using selection sort.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[50],n,i,j,s;
printf("Enter the number of elemnts : ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the element %d : ",i+1);
scanf("%d",&a[i]);
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(a[i]<a[j])
{
s=a[i];
a[i]=a[j];
a[j]=s;
}
}
}
for(i=0;i<n;i++)
{
printf("\n%d",a[i]);
}
getch();
}
46. Program to get insert values into two dimensional Array and display them.
#include<conio.h>
#include<stdio.h>
void main()
{
int i,j,a[3][3];
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("Enter the value a[%d][%d] : ",i,j);
scanf("%d",&a[i][j]);
}
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d ",a[i][j]);
}
printf("\n");
}
getch();
}
47. Program to multiply two matrix.
#include<conio.h>
#include<stdio.h>
void main()
{
int m1[3][3],m2[3][3],m[3][3],i,j,k;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("Enter the values of first Matrix ");
scanf("%d",&m1[i][j]);
}
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("Enter the values of second Matrix ");
scanf("%d",&m1[i][j]);
}
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
m[i][j]=0;
for(k=0;k<3;k++)
{
m[i][j]=m[i][j]+m1[i][k]*m2[k][j];
}
}
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d",m[i][j]);
}
printf("\n");
}
getch();
}
48. Program to Transpose the Matrix
#include<conio.h>
#include<stdio.h>
void main()
{
int m1[10][10],m2[10][10],i,j,r,c;
printf("Enter the number of rows and columns : ");
scanf("%d%d",&r,&c);
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("Enter the values of Matrix [%d][%d]",i+1,j+1);
scanf("%d",&m1[i][j]);
}
}
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
m2[j][i]=m1[i][j];
}
}
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("%d ",m1[i][j]);
}
printf("\n");
}
printf("\nMatrix After Tranpose \n");
for(i=0;i<c;i++)
{
for(j=0;j<r;j++)
{
printf("%d ",m2[i][j]);
}
printf("\n");
}
getch();
}
49. Program to calculate the Trace of matrix
#include<stdio.h>
#include<conio.h>
void main()
{
int n,s=0,i,j,m[10][10];
printf("Enter ths size of matrix (n) : ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("Enter the value for [%d][%d] element : ",i,j);
scanf("%d",&m[i][j]);
}
}
printf("\nThe Matrix is :\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("%d ",m[i][j]);
}
printf("\n");
}
for(i=0;i<n;i++)
{
s=s+m[i][i];
}
printf("\nThe Trace of Matrix : %d",s);
getch();
}
50. Example for Character Function - getchar() and putchar()
#include<conio.h>
#include<stdio.h>
void main()
{
char ch;
clrscr();
printf("Enter a Character : ");
ch=getchar();
printf("The Character entered : ");
putchar(ch);
getch();
}
51. Example for Character Function - getch() and putch()
#include<conio.h>
#include<stdio.h>
void main()
{
char ch;
clrscr();
printf("Enter a Character : ");
ch=getch();
printf("The Character entered : ");
putch(ch);
getch();
}
50. Example for Character Function - getche() and putch()
#include<conio.h>
#include<stdio.h>
void main()
{
char ch;
clrscr();
printf("Enter a Character : ");
ch=getche();
printf("The Character entered : ");
putch(ch);
getch();
}
51. Character input and output using -scanf() and printf()
#include<conio.h>
#include<stdio.h>
void main()
{
char ch;
clrscr();
printf("Enter a Character : ");
scanf("%c",&ch);
printf("\nThe Character entered by you is : %c",ch);
getch();
}
52. String Input and Output using - gets() and puts()
#include<conio.h>
#include<stdio.h>
void main()
{
char st[15];
clrscr();
puts("Enter Your Name : ");
gets(st);
puts("Your Name is : ");
puts(st);
getch();
}
53. String input and output using - scanf() and printf()
#include<conio.h>
#include<stdio.h>
void main()
{
char st[15];
clrscr();
printf("Enter Your Name : ");
scanf("%s",st);
printf("Your Name is : %s",st);
getch();
}
54. Calculating the length of string using strlen() function.
#include<conio.h>
#include<stdio.h>
#include<string.h>
void main()
{
char st[15];
clrscr();
printf("Enter Your Name : ");
gets(st);
printf("Number os characters is your name (length) : %d",strlen(st));
getch();
}
55. Copying the string using strcpy() function
#include<conio.h>
#include<stdio.h>
#include<string.h>
void main()
{
char st[15],cp[15];
clrscr();
strcpy(st,"Programming");
printf("\nString copied to st variable : %s",st);
strcpy(cp,st);
printf("\nString Copied to cp variable : %s",cp);
getch();
}
56. Concatenating the string using strcat() function.
#include<conio.h>
#include<stdio.h>
#include<string.h>
void main()
{
char st1[15],st2[15];
clrscr();
strcpy(st1,"Programming");
strcpy(st2,"-Language");
strcat(st1,st2);
printf("\nStrings After concatenation : %s",st1);
getch();
}
57. Comparing string using strcmp() function.
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char st1[15]="CLanguage";
char st2[15]="CPPLanguage";
char st3[15]="CLanguage";
int r;
clrscr();
r=strcmp(st1,st3); //Returns zero when strings are equal
printf("\nResult : %d",r);
r=strcmp(st1,st2); //Returns a -ve number if 1st string is smaller
printf("\nResult : %d",r);
r=strcmp(st2,st1); //Returns a +ve number if 1st string is bigger
printf("\nResult : %d",r);
getch();
}
58. Checking strings are equal, greater or smaller.
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char st1[15],st2[15];
clrscr();
printf("Enter First String : ");
gets(st1);
printf("Enter Second String : ");
gets(st2);
if(strcmp(st1,st2)==0)
{
printf("Both strings are Equal");
}
else if(strcmp(st1,st2)>0)
{
printf("First String is Greater");
}
else
{
printf("Second string is Greater");
}
getch();
}
59. Calculating the number (length) of characters in a string without strlen().
#include<stdio.h>
#include<conio.h>
void main()
{
char st[15];
int i=0;
printf("Enter a String : ");
gets(st);
while(st[i]!='\0')
{
i++;
}
printf("Length of String is %d",i);
getch();
}
60. Calculating the number of vowels in a string.
#include<stdio.h>
#include<conio.h>
void main()
{
char st[15],ch;
int i,c=0;
printf("Enter a String : ");
gets(st);
for(i=0;i<strlen(st);i++)
{
switch(st[i])
{
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
case 'a':
case 'e':
case 'o':
case 'i':
case 'u':
c++;
}
}
printf("Number of Vowel's in String is %d",c);
getch();
}
61. Calculating the number of occurrence of specific character in a string.
#include<stdio.h>
#include<conio.h>
void main()
{
char st[15],ch;
int i=0,c=0;
printf("Enter a String : ");
gets(st);
printf("Enter a charcater you want to count : ");
ch=getchar();
while(st[i]!='\0')
{
if(st[i]==ch)
{
c++;
}
i++;
}
printf("The character %c occurs %d Times ",ch,c);
getch();
}
62. Concatenating two strings without using built in function strcat().
#include<stdio.h>
#include<conio.h>
void main()
{
char st1[15],st2[15],st[30];
int i=0,j=0;
printf("Enter first String : ");
gets(st1);
printf("Enter second String : ");
gets(st2);
while(i<strlen(st1))
{
st[j]=st1[i];
i++;
j++;
}
i=0;
while(i<strlen(st2))
{
st[j]=st2[i];
i++;
j++;
}
printf("The character %s ",st);
getch();
}
63. Copying string without using built in function (strcpy).
#include<conio.h>
#include<stdio.h>
void main()
{
char str1[15],str2[15];
int i=0;
printf("Enter a string : ");
gets(str1);
while(str1[i]!='\0')
{
str2[i]=str1[i];
i++;
}
printf("The copied String %s",str2);
getch();
}
64. Function without arguments and without return type.
#include<stdio.h>
#include<conio.h>
//An example of function without return type and without arguments
void show();// Function Declaration or Prototype
void main()
{
show(); //first call to function
show(); // second call to function.
getch();
}
void show() //function definition
{
printf("\nSimpe Function Example");
}
64. Function with arguments and without return type.
#include<stdio.h>
#include<conio.h>
//An example of function without return type and with two arguments(x,y)
void sum(int x, int y);// Function Declaration or Prototype
void main()
{
int a,b;
printf("Enter two Numbers : ");
scanf("%d %d",&a,&b);
sum(a,b); //first call- two actual arguments a and b
sum(4,8); //second call- two actual arguments 4 and 8
getch();
}
void sum(int x,int y) //function definition - formal arguments x and y
{
printf("\nSum %d",x+y);
}
64. Function with arguments and with return type.
#include<stdio.h>
#include<conio.h>
//An example of function with return type and with two arguments(x,y)
int sum(int x, int y);// Function Declaration or Prototype
void main()
{
int a,b,s;
printf("Enter two Numbers : ");
scanf("%d %d",&a,&b);
s=sum(a,b); //first call- two actual arguments a and b
printf("\nSum is %d",s);
printf("\nSum is %d",sum(4,8)); //second call- two actual arguments 4 and 8
getch();
}
void sum(int x,int y) //function definition - formal arguments x and y
{
return (x+y); //returning the output
}
65. Calculate cube using function with no arguments and no return type.
#include<stdio.h>
#include<conio.h>
void cube();// Function Declaration or Prototype
void main()
{
clrscr();
cube(); //function call
getch();
}
void cube() //function definition
{
int n;
printf("Enter a Number : ");
scanf("%d",&n);
printf("Cube is %d",n*n*n);
}
66. Calculate cube using function with arguments and no return type.
#include<stdio.h>
#include<conio.h>
void cube(int n);// Function Declaration or Prototype
void main()
{
int a,s;
printf("Enter two Numbers : ");
scanf("%d",&a);
cube(a); //function call
getch();
}
void cube(int n) //function definition
{
printf("Cube is %d",n*n*n);
}
#include<stdio.h>
#include<conio.h>
int cube(int n);// Function Declaration or Prototype
void main()
{
int a,s;
printf("Enter two Numbers : ");
scanf("%d",&a);
s=cube(a); //function call
printf("\nSum is %d",s);
printf("\nSum is %d",cube(4)); //function call
getch();
}
int cube(int n) //function definition
{
return (n*n*n); //returning the output
}
68. Functions with return Type
#include<stdio.h>
#include<conio.h>
int mul(int x,int y);// Function Declaration or Prototype
int square(int n);
void main()
{
int a,b,r;
clrscr();
printf("Enter the value for A and B : ");
scanf("%d%d",&a,&b);
r=square(a)+square(b)+mul(2,mul(a,b));
printf("\nResult is : %d",r); //function call
getch();
}
int mul(int x,int y) //function definition
{
return (x*y);
}
int square(int n)
{
return n*n;
}
69. Recursion - Calculating Factorial using Recursive Function
#include<stdio.h>
#include<conio.h>
int fact(int n);
void main()
{
int num;
printf("Enter a number : ");
scanf("%d",&num);
printf("Factorial : %d",fact(num);
getch();
}
int fact(int n)
{
if(n==1)
{
return 1;
}
else
{
return n*fact(n-1);
}
}
#include<stdio.h>
#include<conio.h>
void cube();// Function Declaration or Prototype
void main()
{
clrscr();
cube(); //function call
getch();
}
void cube() //function definition
{
int n;
printf("Enter a Number : ");
scanf("%d",&n);
printf("Cube is %d",n*n*n);
}
66. Calculate cube using function with arguments and no return type.
#include<stdio.h>
#include<conio.h>
void cube(int n);// Function Declaration or Prototype
void main()
{
int a,s;
printf("Enter two Numbers : ");
scanf("%d",&a);
cube(a); //function call
getch();
}
void cube(int n) //function definition
{
printf("Cube is %d",n*n*n);
}
67. Calculate cube using function with arguments and return type.
#include<stdio.h>
#include<conio.h>
int cube(int n);// Function Declaration or Prototype
void main()
{
int a,s;
printf("Enter two Numbers : ");
scanf("%d",&a);
s=cube(a); //function call
printf("\nSum is %d",s);
printf("\nSum is %d",cube(4)); //function call
getch();
}
int cube(int n) //function definition
{
return (n*n*n); //returning the output
}
#include<stdio.h>
#include<conio.h>
int mul(int x,int y);// Function Declaration or Prototype
int square(int n);
void main()
{
int a,b,r;
clrscr();
printf("Enter the value for A and B : ");
scanf("%d%d",&a,&b);
r=square(a)+square(b)+mul(2,mul(a,b));
printf("\nResult is : %d",r); //function call
getch();
}
int mul(int x,int y) //function definition
{
return (x*y);
}
int square(int n)
{
return n*n;
}
#include<stdio.h>
#include<conio.h>
int fact(int n);
void main()
{
int num;
printf("Enter a number : ");
scanf("%d",&num);
printf("Factorial : %d",fact(num);
getch();
}
int fact(int n)
{
if(n==1)
{
return 1;
}
else
{
return n*fact(n-1);
}
}
70. Pointers and Addresses
#include<conio.h>
#include<stdio.h>
void main()
{
int n=10,*p;
p=&n;
printf("\nAddress of Varible n : %u",&n);
printf("\nAddress of Variable n using pointer : %u",p);
printf("\nValue of variable n using pointer : %d",*p);
printf("\nAddress of pointer p : %u",&p);
getch();
}
70. Arithmetic operation using Pointers
#include<conio.h>
#include<stdio.h>
void main()
{
int *a,*b,*c;
*a=10;
*b=15;
*c=*a+*b;
printf("Sum is %d",c);
getch();
}
70. Arrays implementation using pointer
#include<conio.h>
#include<stdio.h>
void main()
{
int *p,i;
clrscr();
for(i=0;i<10;i++)
{
printf("Enter a number : ");
scanf("%d",p+i);
}
for(i=0;i<10;i++)
{
printf(" %d",*(p+i));
}
getch();
}