0% found this document useful (0 votes)
80 views30 pages

Print Hello World #Include #Include Void Main (Printf (" Hello World") Getch )

The document contains C program code snippets for various algorithms and data structures concepts like printing hello world, addition of two numbers, checking even-odd number, switch case for arithmetic operations, checking vowels, checking leap year, addition of digits, factorial, addition of n numbers, swapping two numbers, reversing a number, checking palindrome number, pattern printing, diamond pattern printing, checking prime number, checking Armstrong number, generating Armstrong numbers, Fibonacci series, Floyd's triangle, Pascal's triangle, finding maximum and minimum element in an array, linear search, binary search, reversing an array, inserting and deleting elements in an array, merging two arrays, sorting arrays using bubble sort, selection sort, and addition of matrices.

Uploaded by

AstitvaTyagi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
80 views30 pages

Print Hello World #Include #Include Void Main (Printf (" Hello World") Getch )

The document contains C program code snippets for various algorithms and data structures concepts like printing hello world, addition of two numbers, checking even-odd number, switch case for arithmetic operations, checking vowels, checking leap year, addition of digits, factorial, addition of n numbers, swapping two numbers, reversing a number, checking palindrome number, pattern printing, diamond pattern printing, checking prime number, checking Armstrong number, generating Armstrong numbers, Fibonacci series, Floyd's triangle, Pascal's triangle, finding maximum and minimum element in an array, linear search, binary search, reversing an array, inserting and deleting elements in an array, merging two arrays, sorting arrays using bubble sort, selection sort, and addition of matrices.

Uploaded by

AstitvaTyagi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 30

// print hello world

#include<stdio.h>
#include<conio.h>
void main()
{
printf(" hello world");
getch();
}

// addition

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
printf("enter the no.");
scanf("%d%d",&a,&b);
c=a+b;
printf(" %d+%d=%d",a,b,c);
getch();
}

// even odd
#include<stdio.h>
#include<conio.h>
void main()
{
int a;
clrscr();
printf("enter the no.");
scanf("%d",&a);
if (a%2==0)
{
printf(" even ");
}
else
{
printf(" odd ") ;
}
getch();
}

// add, sub, mul,div

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
printf(\n 1-add \n 2-sub \n 3-mul \n 4-div);
printf(enter the two no.);
scanf(%d%d,&a,&b);
printf(enter the your choice.);
scanf(%d,&c);
switch(c)
{
case 1:
printf(\n add=%d,a+b);
break;
case 2:
printf(\n sub =%d,a-b);
break;
case 3:
printf(\n mul=%d,a*b);
break;
case 4:
printf(\ndiv=%d,a/b);
break;
default :
printf(\n wrong choice );
}
getch();
}

// check vowel
#include<stdio.h>
#include<conio.h>
void main()
{
char n;
clrscr();
printf("enter the any character.");
scanf("%c",&n);

if (n=='a'||n=='e'||n=='i'||n=='o'||n=='u'||n==A||n==E||n==I||n==O||
n==U)
{
printf("%c is a vowel",n);
}
else
{
printf(" consonent ");
}
getch();
}

//LEAP YEAR
#include<stdio.h>
#include<conio.h>
void main(){
int year;
printf("Enter a year: ");
scanf("%d",&year);
if(year%4 == 0)
{
if( year%100 == 0)
{
if ( year%400 == 0)
printf("%d is a leap year.", year);
else
printf("%d is not a leap year.", year);
}
else
printf("%d is a leap year.", year );
}
else
printf("%d is not a leap year.", year);
getch();
}

// addition of digit
#include<stdio.h>
#include<conio.h>
void main()
{
int i,n,c,sum=0;
clrscr();
printf(enter any no. of digit );
scanf(%d,&n);
for(i=1;n>0;i++)
{
c=n%10;
sum=sum + c;
n=n/10;
}
printf( \n sum of digit=%d,sum);
getch();
}

// factorial
#include<stdio.h>
#include<conio.h>
void main()
{
int i,n,fact=1;
clrscr();
printf("enter any no. ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{

fact=fact*i;
}
printf(" \n factorial of no =%d",fact);
getch();
}

// addition of n number
#include<stdio.h>
#include<conio.h>
void main()
{
int i,n,sum=0;
clrscr();
printf(enter any no. );
scanf(%d,&n);
for(i=1;i<=n;i++)
{
sum=sum +i;
}
printf( \n sum of no. =%d,sum);
getch();
}

//Swapping of two numbers


#include<stdio.h>
#include<conio.h>
void main()
{
int x, y, temp;
printf("Enter the value of x and y: ");
scanf("%d %d", &x, &y);
printf("Before swapping x=%d, y=%d ", x, y);
temp = x;
x = y;
y = temp;
printf("After swapping x=%d, b=%d", x, y);
getch();
}

// reverse of a number
#include<stdio.h>
#include<conio.h>
void main()
{
int n,d;
clrscr();
printf("enter the no");
scanf("%d",&n);
while(n>0)
{
d=n%10;
printf("%d",d);
n=n/10;
}
getch();
}

//palindrome number
#include<stdio.h>
#include<conio.h>
void main()
{
int i,x=0,n,m=0;
clrscr();
printf("enter the no");
scanf("%d",&n);
x=n;
while(n>0)

{
x=n%10;
n=n/10;
m=(m*10)+x;
}
if(x==m)
{
printf("it is a not palindrome number");
}
else
{
printf("it is a palindrome number");
}
printf("/n thank you ");
getch();
}

// pattern printing
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j;
clrscr();
printf("pattern \n");
for(i=0;i<=4;i++)
{
for(j=0;j<=i;j++)
{
printf( *);
}
printf(\n);
}
printf("\n thank you");
getch();
}

// diamond
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,a=1;
clrscr();
printf("\n diamond\n");
for(i=0;i<=5;i++)
{
for(j=5;j>i;j--)
{
printf( );
}
for(j=0;j<=i;j++)
{
printf(" *);
}
for(j=i;j>0;j--)
{
printf(" *");
}
printf(\n);
}
getch();
}

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

printf("enter the no");


scanf("%d",&n);
for(i=2;i<=n-1;i++)
{
if(n%i==0)
{
c++;
break;
}
}
if(c==0&&n!=1)
{
printf("prime number");
}
else
{
printf("not a prime number");
}
getch();
}

//Armstrong number
#include<stdio.h>
#include<conio.h>
void main()
{
int n, n1, rem, num=0;
printf("Enter a positive integer: ");
scanf("%d", &n);
n1=n;
while(n1!=0)
{
rem=n1%10;
num+=rem*rem*rem;
n1/=10;
}
if(num==n)
printf("%d is an Armstrong number.",n);
else
printf("%d is not an Armstrong number.",n);
getch();
}

//Generate Armstrong Number


# include <stdio.h>
# include <conio.h>
void main()
{
int i, a, r, s, n ;
clrscr() ;
printf("Enter the limit : ") ;
scanf("%d", &n) ;
printf("\nThe armstrong numbers are :\n\n") ;
for(i = 0 ; i <= n ; i++)
{
a=i;
s=0;
while(a > 0)
{
r = a % 10 ;
s = s + (r * r * r) ;
a = a / 10 ;
}
if(i == s)
printf("%d\t", i) ;
}
getch() ;
}

// fibonacci series
#include<stdio.h>
#include<conio.h>

void main()
{
int i,a,b,c,n;
clrscr();
a=0;
b=1;
printf("enter the term ");
scanf("%d",&n);
printf("the series as follows");
printf( %d %d,a,b);
for(i=2;i<n-2;i++)
{
c=a+b;
a=b;
b=c;
printf(" %d",c);
}
getch();
}

//floyd triangle
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,a=1;
clrscr();
printf("\nfloyd triangle\n");
for(i=0;i<=5;i++)
{
for(j=5;j>i;j--)
{
printf( );
}
for(j=0;j<=i;j++)
{
printf(" %d",j+1);
}
for(j=i;j>0;j--)
{
printf(" %d",j);
}

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

//pascal triangle
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,a=1;
clrscr();
printf("pascal triangle\n");
for(i=0;i<=3;i++)
{
for(j=0;j<=i;j++)
{
printf(" %d",a);
a++;
}
printf(\n);
}
getch();
}

// maximum no. in array


#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,a[5],t;

clrscr();
printf("enter the nos. in array");
for(i=0;i<5;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<5;i++)
{
for(j=i+1;j<5;j++)
{
if(a[i]>a[j])
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
}
printf("\n maximum no. in array ");
printf(" %d",a[4]);
getch();
}

// minimum no. in array


#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,a[5],t;
clrscr();
printf("enter the nos. in array");
for(i=0;i<5;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<5;i++)
{
for(j=i+1;j<5;j++)
{
if(a[i]>a[j])
{
t=a[i];
a[i]=a[j];

a[j]=t;
}
}
}
printf("\n minimum no. in array ");
printf(" %d",a[0]);
getch();
}

// linear search
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,a[10];
clrscr();
printf("enter the nos. in array");
for(i=0;i<10;i++)
{
scanf("%d,&a[i]);
}
printf(\n enter the no. to be searched );
scanf(%d,&j);
// searching
for(i=0;i<10;i++)
{
if(a[i]==j)
{
printf("\n no. is found at %d position,i+1);
break;
}
}
getch();
}

// binary search
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,a[10],end,beg,mid,k;
clrscr();
printf("enter the nos. in array in sorted order");
for(i=0;i<10;i++)
{
scanf("%d",&a[i]);
}
printf("\n enter the no. to be searched ");
scanf("%d",&k);
// searching
end=9;
beg=0;
while(beg<=end)
{
mid=(beg+end)/2;
if(a[mid]==k)
{
printf("\n found at position =%d",mid+1);
break;
}
else if(a[mid]>k)
{
end=mid-1;
}
else
{
beg=mid+1;
}
}

if(beg>end)
{
}
getch();

// reverse array
#include<stdio.h>
#include<conio.h>
void main()
{
int i,a[10];
clrscr();
printf("enter the nos. in array");
for(i=0;i<10;i++)
{
scanf("%d",&a[i]);
}
printf("\n reverse order");
for(i=9;i>=0;i--)
{
printf("\n%d",a[i]);
}
getch();
}

// insert in array
#include<stdio.h>
#include<conio.h>
void main()
{
int array[100],position,i,n,value;
clrscr();
printf("Enter number of elements in array\n");
scanf("%d", &n);
printf("Enter %d elements\n", n);
for (i= 0;i<n;i++)
scanf("%d",&array[i]);
printf("Enter the location where you wish to insert an element\n");
scanf("%d",&position);
printf("Enter the value to insert\n");
scanf("%d",&value);
for (i=n-1;i>=position-1;i--)
array[i+1] = array[i];
array[position-1] = value;
printf("Resultant array is\n");
for (i=0;i<=n;i++)
printf("%d\n",array[i]);
getch();
}

// delete in array
#include <stdio.h>
#include<conio.h>
void main()
{
int array[100], position, c, n;
clrscr();
printf("Enter number of elements in array\n");
scanf("%d", &n);
printf("Enter %d elements\n", n);
for ( c = 0 ; c < n ; c++ )
scanf("%d", &array[c]);
printf("Enter the location where you wish to delete element\n");
scanf("%d", &position);
if ( position >= n+1 )
printf("Deletion not possible.\n");
else
{
for ( c = position - 1 ; c < n - 1 ; c++ )
array[c] = array[c+1];
printf("Resultant array is\n");
for( c = 0 ; c < n - 1 ; c++ )
printf("%d\n", array[c]);
}
getch();
}

//Merging of two arrays


#include<stdio.h>
#include<conio.h>
void main()
{
int arr1[30], arr2[30], res[60];
int i, j, k, n1, n2;
clrscr();
printf("\nEnter no of elements in 1st array :");
scanf("%d", &n1);
for (i = 0; i < n1; i++) {
scanf("%d", &arr1[i]);
}
printf("\nEnter no of elements in 2nd array :");
scanf("%d", &n2);
for (i = 0; i < n2; i++) {
scanf("%d", &arr2[i]);
}
i = 0;
j = 0;
k = 0;
// Merging starts
while (i < n1 && j < n2) {
if (arr1[i] <= arr2[j]) {
res[k] = arr1[i];
i++;
k++;
} else {
res[k] = arr2[j];
k++;
j++;
}
}

/* Some elements in array 'arr1' are still remaining


where as the array 'arr2' is exhausted */
while (i < n1) {
res[k] = arr1[i];
i++;
k++;
}
/* Some elements in array 'arr2' are still remaining
where as the array 'arr1' is exhausted */
while (j < n2) {
res[k] = arr2[j];
k++;
j++;
}
//Displaying elements of array 'res'
printf("\nMerged array is :");
for (i = 0; i < n1 + n2; i++)
printf("%d ", res[i]);
getch();
}

// bubble sorting
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,a[10],t;
clrscr();

printf("enter the nos. in array");


for(i=0;i<10;i++)
{
scanf("%d,&a[i]);
}
for(i=0;i<10;i++)
{
for(j=0;j<10-i;j++)
{
if(a[j]>a[j+1])
{
t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
}
}
printf(\n sorted list );
for(i=0;i<10;i++)
{
printf( %d,a[i]);
}
getch();
}

// selection sorting
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,a[10],t;
clrscr();
printf("enter the nos. in array");
for(i=0;i<10;i++)
{
scanf("%d,&a[i]);
}

// sorting
for(i=0;i<10;i++)
{
for(j=i+1;j<10;j++)
{
if(a[i]>a[j])
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
}
printf(\n sorted list );
for(i=0;i<10;i++)
{
printf( %d,a[i]);
}
getch();
}

// addition of matrices
#include<stdio.h>
#include<conio.h>
void main()
{
int Matrix1 [2][2] , Matrix2 [2][2] , Matrixsum[2][2],i, j;
clrscr();
printf(" Enter the elements of Matrix1\n");
for(i=0 ; i<2 ; i++)
{
for(j=0 ; j<2 ; j++)
{
scanf("%d" , &Matrix1[i][j] );
}
}
printf(" Enter the elements of Matrix2\n");

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


{
for(j=0 ; j<2 ; j++)
{
scanf("%d" , &Matrix2[i][j] );
}
}
for(i=0 ; i<2 ; i++)
{
for(j=0 ; j<2 ; j++)
{
Matrixsum[i][j] = Matrix1[i][j] + Matrix2[i][j] ;
}
}
printf(" Sum Matrix \n");
for(i=0 ; i<2 ; i++)
{
for(j=0 ; j<2 ; j++)
{
printf(" %d" , Matrixsum[i][j] );
}
printf("\n");
}
getch();
}

// transpose of matrices
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,a[2][2],b[2][2];
printf("enter the nos.of matrix a");
{
for(i=0;i<=1;i++)
{
for(j=0;j<=1;j++)

{
scanf("%d",&a[i][j]);
}
}
printf("\n transpose ");
for(i=0;i<=1;i++)
{
for(j=0;j<=1;j++)
{
b[j][i]=a[i][j];
}
printf("\n");
}
}
for(i=0;i<=1;i++)
{
for(j=0;j<=1;j++)
{
printf(" %d",b[i][j]);
}
printf("\n");
}
getch();
}

// multiply of two matrices


#include<conio.h>
#include<stdio.h>
void main()
{
int i,j,k,a[2][2],b[2][2],c[2][2];
clrscr();
printf("enter the first matrix");
for(i=0;i<=1;i++)
{
for(j=0;j<=1;j++)

{
scanf("%d",&a[i][j]);
}
}
printf("enter the second matrix");
for(i=0;i<=1;i++)
{
for(j=0;j<=1;j++)
{
scanf("%d",&b[i][j]);
}
}
for(i=0;i<=1;i++)
{
for(j=0;j<=1;j++)
{
c[i][j]=0;
for(k=0;k<=1;k++)
{
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
}
}
printf("the product of two matrices");
for(i=0;i<=1;i++)
{
for(j=0;j<=1;j++)
{
printf(" %d",c[i][j]);
}
printf("\n");
}
getch();
}

// lenth of string

#include<stdio.h>
#include<conio.h>
void main()
{
int i,count=0;
char a[100];
clrscr();
printf("\nenter any string\n");
gets(a);
for(i=0;a[i]!=\0;i++)
{
count++;
}
printf(" \n lenth of string =%d",count);
printf("\n Thank you");
getch();
}

//String compare
#include <stdio.h>
#include <string.h>
#include<conio.h>
void main()
{
char a[100], b[100];
printf("Enter the first string\n");
gets(a);
printf("Enter the second string\n");
gets(b);
if (strcmp(a,b) == 0)
printf("Entered strings are equal.\n");
else
printf("Entered strings are not equal.\n");
getch();
}

//Copy string
#include <stdio.h>
#include <string.h>
#include<conio.h>
void main()
{
char source[1000], destination[1000];
printf("Input a string\n");
gets(source);
strcpy(destination, source);
printf("Source string:
\"%s\"\n", source);
printf("Destination string: \"%s\"\n", destination);
}

getch();

//String concatenation
#include <stdio.h>
#include <string.h>
#include<conio.h>
void main()
{
char a[1000], b[1000];
printf("Enter the first string\n");
gets(a);
printf("Enter the second string\n");
gets(b);
strcat(a,b);
printf("String obtained on concatenation is %s\n",a);
getch();
}

//string reverse
#include<stdio.h>
#include<string.h>
#include<conio.h>
void main(){
char str[50];
char *rev;
printf("Enter any string : ");
scanf("%s",str);
rev = strrev(str);
printf("Reverse string is : %s",rev);
getch();
}

//String sorting
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char string[128], temp;
int n, i, j;
clrscr();
printf("\nEnter string: ");
gets(string);
n = strlen(string);
for (i=0; i<=n-1; i++)
{
for (j=i+1; j<n; j++)
{
if (string[i] > string[j])
{
temp = string[i];
string[i] = string[j];
string[j] = temp;
}
}

}
printf("\n%s", string);
printf("\n");
getch();
}

You might also like