0% found this document useful (0 votes)
30 views25 pages

Include

Copyright
© © All Rights Reserved
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
Download as odt, pdf, or txt
0% found this document useful (0 votes)
30 views25 pages

Include

Copyright
© © All Rights Reserved
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
Download as odt, pdf, or txt
Download as odt, pdf, or txt
You are on page 1/ 25

#include <stdio.

h>
int removeduplicate(int uni[])
{
int i,j,k,n=20;
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(uni[i]==uni[j])
{
for(k=j;k<n;k++)
{
uni[k]=uni[k+1];
}

k--;
n--;
}
}
}
}
int main ()
{
int a[10],b[10],k,i, j, n1,n2;

printf ("\nEnter the no of elements of an array a:");


scanf ("%d", &n1);

printf("\nEnter the no of elements of an array b:");


scanf("%d",&n2);

int uni[n1+n2];

printf ("\nEnter the elements of an array a:");

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

printf ("\nEnter the elements of an array b:");

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

//intersection

printf("\nThe intersection of the array is: ");

for(i=0;i<n1;i++)
{
for(j=0;j<n2;j++)
{
if(a[i]==b[j])

printf("%d ",a[i]);

}
}

//union

for(j=0;j<n1+n2;j++)
{
for(i=0;i<n1;i++)
{
uni[j]=a[i];
}
}

for(j=n1;j<n1+n2;j++)
{
for(i=0;i<n1;i++)
{
uni[j]=a[i];
}
}

printf("\nunion array is : ");

for(j=0;j<n1+n2;j++)
{
printf("%d",uni[j]);
}

removeduplicate(uni);

return 0;

********************************************************************************
Sort operation

for(i=0;i<9;i++)
{
for(j=i+1;j<10;j++)
{
if(A[i]<A[j])
{
temp=A[i];
A[i]=A[j];
A[j]=temp;
}
}

********************************************************************************

#include <stdio.h>//prime number


int main()
{
int n,m=0,count=0,i;

printf("\nEnter the number : ");


scanf("%d",&n);
m=n/2;
for(i=2;i<=m;i++)
{
if(n%i==0)
{
printf("\nIt's not a prime number");
count=1;
break;
}
}

if(count==0)
{
printf("\nIt's a prime number");
}
return 0;
}

*******************************************

#include <stdio.h> //square of the number

int Testdata(int a)
{
int s=a*a;
return s;
}

int main()
{
int a;
float sq;
printf("\nEnter the no to square: ");
scanf("%d",&a);

sq=Testdata(a);

printf("\nThe square of the number is : %.2f",sq);

return 0;
}

**************************************

#include <stdio.h> //function ex:1->sum of two numbers


int add(int a,int b);
int main()
{
int a,b,total;

printf("Enter two numbers: ");


scanf("\t%d \t%d",&a,&b);
total=add(a,b);
printf("\nThe sum of the numbers is : %d",total);
return 0;
}
int add(int a,int b)
{
int total=a+b;
return total;
}

**********************************

#include <stdio.h>// find the highest no of occurance character of a string


#include <string.h>
int main()
{
char ch[20],result;
int i,len;
int arr[250]={0};
int max=-1;

printf("Enter the string:");


scanf("%[^\n]s",ch);

len=strlen(ch);
printf("\nThe length of the string is : %d\n",len);
for(i=0;i<len-1;i++)
{
arr[ch[i]]++;
}
for(i=0;i<len;i++)
{
if(max<arr[ch[i]])
{
int temp=max;
max=arr[ch[i]];
arr[ch[i]]=temp;
result=ch[i];
}
}
i++;
printf("\nThe max occurences of the char is '%c' at %d times.",result,max);
return 0;
}

#include <stdio.h> //fibonacci series with recursion


void fibonacci(int n)
{
static int n1=0,n2=1,n3;

if (n>0)
{
n3=n1+n2;
n1=n2;
n2=n3;

printf("%d ",n3);

fibonacci(n-1);
}

int main()
{
int n;

printf("\nEnter the no of elements for fibonacci series: ");


scanf("%d",&n);

printf("\nThe fibonacci series is : ");

printf("%d %d ",0,1);
fibonacci(n-2);

return 0;
}

************************************

#include <stdio.h> //find the no of occurances of a character in string


#include <string.h>
int ptroccur(char ch[],char a);
int main()
{
char ch[12],a,count;

printf("\nEnter the string:");


gets(ch);

printf("\nEnter the char to find occurence: ");


a=getchar();

count = ptroccur(ch,a);

printf("\nthe no of occurances of the character is : %d",count);

return 0;
}

int ptroccur(char ch[],char a)


{
int i, count=0;

for(i=0;i<strlen(ch);i++)
{
if(ch[i]==a)
count++;
}
return count;
}

#include <stdio.h>//Reverse a string elements


#include <string.h>
int main()
{
char str[50], temp;
int len,i,j;
printf("\nEnter the string: ");
scanf("%[^\n]s",str);
len=strlen(str);
printf("\nThe length of the string is: %d",len);
printf("\nThe reverse of the string is:\n");
while(i<j)
{
i=0;
j=len;
temp=str[i];
str[i]=str[j];
str[j]=temp;
i++;
j--;
}
printf("%c ",str[i]);

return 0;
}

#include <stdio.h> //finding string length without using library function only characters
int StringLength(char str[]);
int main()
{
char str[50];
int len;
printf("\nEnter the string: ");
scanf("%[^\n]s",str);
len=StringLength(str);
printf("\nThe length of the string is: %d",len);
}
int StringLength(char str[])
{
int i=0,count=0;
while(str[i]!='\0')
{
if((str[i]>=65 && str[i]<=90) || (str[i]>=97 && str[i]<=122))
count++;
i++;
}
return count;
}

*****************************************

#include <stdio.h> //finding string length without using library function


int StringLength(char str[]);
int main()
{
char str[50];
int len;
printf("\nEnter the string: ");
scanf("%[^\n]s",str);
len=StringLength(str);
printf("\nThe length of the string is: %d",len);
}
int StringLength(char str[])
{
int i=0,count=0;
while(str[i]!='\0')
{
count++;
i++;
}
return count;
}

*************************************

#include <stdio.h> //sum of the diagonal of a matrix

int main()
{
int A[10][10],B[10][10],i,j,r,c,sum=0;
printf("\nEnter the no of rows:");
scanf("%d",&r);
printf("\nEnter the no of columns:");
scanf("%d",&c);

printf("\nEnter the elements of a matrix:\n");


for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d\t",&A[i][j]);
}
}

printf("\nThe entered matrix is:\n");


for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("%d\t",A[i][j]);
}
printf("\n");
}
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
if(i==j)
{
sum=sum+A[i][j];
}
}
}
printf("\nThe sum of the diagonal is : %d",sum);

return 0;
}

****************************************

#include <stdio.h> //transpose of a matrix

int main()
{
int A[10][10],B[10][10],i,j,r,c;
printf("\nEnter the no of rows:");
scanf("%d",&r);
printf("\nEnter the no of columns:");
scanf("%d",&c);

printf("\nEnter the elements of a matrix:\n");


for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d\t",&A[i][j]);
}
}

printf("\nThe entered matrix is:\n");


for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("%d\t",A[i][j]);
}
printf("\n");
}

for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
B[i][j]=A[j][i];
}
}

printf("\nThe transposed matrix is:\n");


for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("%d\t",B[i][j]);
}
printf("\n");
}

return 0;
}

********************************

#include <stdio.h>

int main()
{
int arr[10][10],r,c,i,j,sum;
printf("\nEnter the no of rows:");
scanf("%d",&r);
printf("\nEnter the no of colums:");
scanf("%d",&c);
printf("\nEnter the elements of an array:");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d\t",&arr[i][j]);
}
}
printf("\nthe matrix is:");
for(i=0;i<r;i++)
{
printf("\n");
for(j=0;j<c;j++)
{
printf("%d\t",arr[i][j]);
}
}

return 0;
}

***************************
#include <stdio.h> //rhombus from left end pattern
int main()
{
int r,c,i,j,k;

printf("\nenter the no of rows: ");


scanf("%d",&r);
printf("\nenter the no of columns: ");
scanf("%d",&c);

int arr[r][c];

printf("\nThe required matrix is:\n\n");

for(i=r-1;i>0;i--)
{
for(j=0;j<c-i;j++)
{
printf(" ");
}
for(k=0;k<=r;k++)
{
printf("1");
}

printf("\n");

return 0;
}

**********************************

#include <stdio.h> //box with x pattern


int main()
{
int r,c,i,j;

printf("\nenter the no of rows: ");


scanf("%d",&r);
printf("\nenter the no of columns: ");
scanf("%d",&c);

int arr[r][c];

printf("\nThe required matrix is:\n\n");


for(i=1;i<=r;i++)
{
for(j=1;j<=c;j++)
{
if(i==1 || j==1 || i==r || j==c || i==j || j==c-i+1)
{

printf("%d\t",1);

else
{
printf("0\t");
}

printf("\n");

return 0;
}

**************************************

#include <stdio.h> //full r&c with 1 pattern


int main()
{
int r,c,i,j;

printf("\nenter the no of rows: ");


scanf("%d",&r);
printf("\nenter the no of columns: ");
scanf("%d",&c);

int arr[r][c];

printf("\nThe required matrix is:\n");

for(i=1;i<=r;i++)
{
for(j=1;j<=c;j++)
{
if((i<=j) || (j<=c))
{

printf("%d\t",1);

}
else
{
printf("%d\t",0);

printf("\n");

return 0;
}

**********************************

#include <stdio.h> //x pattern


int main()
{
int r,c,i,j;

printf("\nenter the no of rows: ");


scanf("%d",&r);
printf("\nenter the no of columns: ");
scanf("%d",&c);

int arr[r][c];

printf("\nThe required matrix is:\n");

for(i=1;i<=r;i++)
{
for(j=1;j<=c;j++)
{
if((i==j) || (j==(c+1)-i))
{

printf("%d\t",1);
}
else
{
printf("%d\t",0);

printf("\n");

return 0;
}

#include <stdio.h> //vowels and consonents

int main()
{
char s[10];
int i=0,count=0,sum=0,a=0,b=0;

printf("\nEnter the string: ");

scanf("%s",s);

printf("\nthe entered string is:%s",s);

while(i<10)
{
if(s[i]=='a'||s[i]=='e'||s[i]=='i'||s[i]=='o'||s[i]=='u'||s[i]=='A'||s[i]=='E'||s[i]=='I'||s[i]=='O'||
s[i]=='U')
{
count ++;
}
if (s[i]>=48&&s[i]<=57)
{
sum++;
}
if(s[i]>=32&&s[i]<=77||s[i]>=58&&s[i]<=64||s[i]>=91&&s[i]<=96||s[i]>=123&&s[i]<=126)
{
a++;
}
else
{
b++;
}

i++;

}
printf("\nTotal no of vowels is :%d",count);
printf("\nTotal no of consonents is :%d",b);
printf("\nTotal no of digits is :%d",sum);
printf("\nTotal no of spl character is:%d",a);

return 0;
}

**********************************

#include <stdio.h> //removing dulicate no

int main()
{
int arr[10],n,i,j,a;
printf("\nenter the size of array:");
scanf("%d",&n);
printf("\nenter the elements of an array:");
for (i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}

for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(arr[i]==arr[j])
{
for(a=j;a<n-1;a++)
{
arr[a]=arr[a+1];
}

n--;
j--;
}

}
}
printf("\nAfter deletion of duplicate no from an array:");

for(i=0;i<n;i++)
{
printf("%d ",arr[i]);
}
return 0;
}

**************************************

#include <stdio.h> //task 1


int main()
{
int r,c,i,j;

printf("\nenter the no of rows: ");


scanf("%d",&r);
printf("\nenter the no of columns: ");
scanf("%d",&c);

int arr[r][c];

printf("\nThe required matrix is:\n");


for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
if(i%2==0)
{
printf("%d\t",1);

else
{
printf("%d\t",0);

}
}

printf("\n");
}
return 0;
}

**************************************

#include <stdio.h> //task 2


int main()
{
int r,c,i,j;

printf("\nenter the no of rows: ");


scanf("%d",&r);
printf("\nenter the no of columns: ");
scanf("%d",&c);

int arr[r][c];

printf("\nThe required matrix is:\n");

for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
if(i==0||i==r-1)
{

printf("%d\t",1);
}
else if (j==0||j==c-1 && i!=0)
{

printf("%d\t",1);
}
else
{
printf("%d\t",0);
}
}

printf("\n");
}

return 0;
}

****************************************

#include <stdio.h>
#include <string.h>
char sort(char arr[100],int len);
int main()
{

char arr[50],temp;
int i,j,len,count;

printf("\nEnter the string:");


scanf("%[^\n]",arr);

len=strlen(arr);

sort(arr,len);

printf("\nreordered string:");
for(i=0;i<len;i++)
{
printf("%c",arr[i]);
}

i=0;
while(i<len)

{
temp=arr[i];
count=1;

for(j=i+1;j<len;j++)
{
if(temp==arr[j])
{
count++;
}
}
printf("\n%c=%d",arr[i],count);
i=i+count;
}
return 0;
}

char sort(char arr[50],int len)


{
char temp;
int i,j;
for(i=0;i<len-1;i++)
{
for(j=i+1;j<len;j++)
{
if(arr[i]>arr[j])
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}

return arr;
}

*****************************************
int main()
{
int n1,n2,i,j,sum;

printf("enter the size of a1: ");


scanf("%d",&n1);
printf("enter the size of a2: ");
scanf("%d",&n2);

int a1[n1],a2[n2];

printf("enter the values of a1: ");

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

printf("enter the values of a2: ");

for(j=0;j<n2;j++)
{
scanf("%d",&a2[j]);
}

printf("sum of arrays:");

for(i=0,j=0;i<n1,j<n2;i++,j++)
{
sum=a1[i]+a2[j];
printf("\n%d",sum);
}

return 0;

*************************************

#include <stdlib.h> //calloc()


void main()
{
int n,*arr;
printf("\nEnter the no of array elements: ");
scanf("%d",&n);
arr=(int*)calloc(n,sizeof(int));
if(arr==NULL)
{
printf("No memory is available");
}
else
{
printf("\nThe elements are:");
for(int i=0;i<n;i++)
{
printf("%d ",*(arr+1));
}
}

*******************************************

int *getarray(int *a) // function to pointer


{
printf("\nEnter the array elements: ");
for (int i=0;i<5;i++)
{
scanf("%d",&a[i]);
}
return a;
}
int main()
{
int *n,a[5];
n=getarray(a);
printf("\nThe array elements are: ");
for(int i=0;i<5;i++)
{
printf("%d ",n[i]);
}
return 0;
}

***************************************

int *get_the_array(int *arr)


{
printf("\nEnter the array elements: ");
for(int i=0;i<5;i++)
{
scanf("%d",&arr[i]);
}
return arr;
}
int main()
{
int *n,arr[5];
n=get_the_array(arr);
printf("\nThe array elements are: ");
for(int i=0;i<5;i++)
{
printf("%d ",n[i]);
}
return 0;
}

*****************************

//returning array using malloc()


#include <stdio.h>
#include<malloc.h>
int *getarray()
{
int size;
printf("\nEnter the size of an array:");
scanf("%d",&size);
int *p=malloc(sizeof(size));
printf("\nEnter the elements of an array:");
for (int i=0;i<size;i++)
{
scanf("%d",&p[i]);
}
return p;
}

int main()
{
int *ptr;
ptr=getarray();
int length=sizeof(*ptr);
printf("\nThe elements you have entered are:");
for(int i=0;ptr[i]!='\0';i++)
{
printf("%d ",ptr[i]);
}
return 0;
}
//returning array using static variable
#include <stdio.h>
int *getarray()
{
static int arr[7];
printf("\nenter the elements of an array: ");
for(int i=0;i<7;i++)
{
scanf("%d",&arr[i]);
}
return arr;
}
int main()
{
int *ptr;
ptr=getarray();
printf("\nThe entered elements are:");
for(int i=0;i<7;i++)
{
printf("%d ",ptr[i]);
}
return 0;
}

****************************
#include <stdio.h>
#include <string.h>
char main()
{
char ch[20];
printf("\n Enter the string:");
scanf("%[^\n]s",ch);
printf("\nthe entered string is :%s",ch);
return 0;
}

#include <stdio.h> //vowels and consonents

int main()
{
char s[10];
int i=0,count=0,sum=0,a=0,b=0;

printf("\nEnter the string: ");

scanf("%s",s);

printf("\nthe entered string is:%s",s);

while(i<10)
{
if(s[i]=='a'||s[i]=='e'||s[i]=='i'||s[i]=='o'||s[i]=='u'||s[i]=='A'||s[i]=='E'||s[i]=='I'||s[i]=='O'||
s[i]=='U')
{
count ++;
}
if (s[i]>=48&&s[i]<=57)
{
sum++;
}
if(s[i]>=32&&s[i]<=77||s[i]>=58&&s[i]<=64||s[i]>=91&&s[i]<=96||s[i]>=123&&s[i]<=126)
{
a++;
}
else
{
b++;
}

i++;

}
printf("\nTotal no of vowels is:%d",count);
printf("\nTotal no of consonents is:%d",b);
printf("\nTotal no of digits is:%d",sum);
printf("\nTotal no of spl character is:%d",a);

return 0;
}

*************************************

#include <stdio.h> //fibonacci series without recursion


int main()
{
int n1=0,n2=1,n3,n;
printf("\nEnter the limit of fibonacci series:");
scanf("%d",&n);
printf("\nThe fibonacci series for the limit is:");
printf("%d %d ",n1,n2);
for (int i=1;i<n-1;i++)
{
n3=n1+n2;
n1=n2;
n2=n3;
printf("%d ",n3);
}
return 0;
}

You might also like