0% found this document useful (0 votes)
1 views35 pages

PROGRAMS

The document contains various C programming examples covering a range of topics including loops, functions, recursion, and array manipulations. Key examples include displaying numbers, computing factorials, generating Fibonacci series, and performing matrix operations such as addition and multiplication. It also includes sorting algorithms like bubble sort and selection sort, as well as searching algorithms like linear and binary search.

Uploaded by

gourituppad2005
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)
1 views35 pages

PROGRAMS

The document contains various C programming examples covering a range of topics including loops, functions, recursion, and array manipulations. Key examples include displaying numbers, computing factorials, generating Fibonacci series, and performing matrix operations such as addition and multiplication. It also includes sorting algorithms like bubble sort and selection sort, as well as searching algorithms like linear and binary search.

Uploaded by

gourituppad2005
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/ 35

* C prog to display numbers from 1 to n * /

#include

Void main()

int n,i ;

printf(“\n enter the value of n :”);

scanf(“%d”, &n);

printf(“\n The numbers displayed are : “);

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

Printf(“%d/n”,i);

* C prog to display numbers from n down to 1 * /

* C prog to Compute factorial of a number *

#include
Void main()

int n,fact,i ;

printf(“\n enter the value of n :”);

scanf(“%d”, &n);

fact = 1 ;

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

fact* = i;

Printf(“%d ! = %d \n ”, n , fact );

*C program to compute sum of series 1+2+3+……..+n */

• Write a C program to check the given number is positive or negative using ternary
operator

• #include<stdio.h>

• int main( )

• {

• int N;

• clrscr( );

• printf(“Enter a Integer\n”);

• scanf(“%d”,&N);

• (N>0)? Printf(“Number is +ve”): printf(“Numner is –Ve);

• }
Write a program to print sum of numbers using functions

• #include<stdio.h>

• int add (int a,int b); Function declaration /Function prototype

• int main()

• {

• int a,b,sum,res;

• printf(“Enter the value of a & b”);

• scanf(“%d%d”,&a,&b);

• res=add(a,b); Function call

• printf(“Sum=%d”,sum);

• }

• int add(int a,int b) Function Definition

• {

• int sum;

• sum=a+b;

• return sum;

PASS BY VALUE

• #include<stdio.h>

• void exchange (int m, int n)

• {

• int temp;

• temp=m;

• m=n;
• n=temp;

• }

• void main()

• {

• int a, b;

• a=10, b=20;

• exchange (a , b);

• printf(“a= %d and b= %d\n”, a, b);

• }

PASS BY ADDRESS

• include<stdio.h>

• void exchange (int *m, int *n);

• void exchange (int *m, int *n)

• {

• int temp;

• temp=*m;

• *m=*n;

• *n=temp;

• }

• void main()

• {

• int a, b;

• a=10, b=20;

• exchange (&a , &b);

• printf(“a= %d and b= %d\n”, a, b);

• }
FACTORIAL OF NUMBER USING RECURSION

include<stdio.h>

int fact(int n)

if(n==1) return 1;

return n*fact(n-1);

int main()

int n, factorial;

printf("Enter a number\n");

scanf("%d", &n);

factorial=fact(n);

printf("factorial of num=%d“, factorial);

BINOMIAL COEFFICIENT USING RECURSION

#include<stdio.h>

int main()

int n, r, ncr;

printf("Enter n and r\n");

scanf("%d%d", &n, &r);

ncr=fact(n)/(fact(n-r)* fact(r));

printf("NCR =%d", ncr);

int fact(int n)

if(n==1) return 1;
return n*fact(n-1);

FIBONACCI SERIES OF NUMBER USING RECURSION

#include<stdio.h>

int Fib(int);

int main()

int n, i = 0, c;

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

scanf("%d", &n);

printf("Fibonacci series\n");

for (c = 1 ;c <= n ; c++)

printf("%d\n", Fib(i));

i++;

int Fib(int n)

if (n == 0)

return 0;

else if (n == 1)

return 1;

else

return (Fib(n-1) + Fib(n-2));


}

Program to read n items from keyboard and display n elements on the monitor

int main()

int i, a[100],n;

printf(“Enter the size of an array “);

scanf(“%d”, &n); // read array size

printf(“Enter %d elements\n”, n);

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

scanf(“%d”, &a[i] ); // to read array elements

printf(“Array elements are \n”);

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

printf(“%d”, a[i]); // to print array elements

Write a c-program to find sum of N-array elements

include<stdio.h>

main()

int a[100],i,n,sum=0;

printf("Enter array size\n");

scanf("%d",&n);

printf("Enter %d elements of array\n",n);

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

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

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

sum=sum+a[i];

}
printf("sum of %d array elements is=%d",n,sum);

Write a C program to generate and print first – N Fibonacci series using array

include<stdio.h>

main()

int n, i, fib[100];

printf("\n enter n value\n");

scanf("%d", &n);

fib[0]=0;

fib[1]=1;

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

fib[i]=fib[i-1]+fib[i-2];

printf("First %d Fibnonacci numbers are\n",n);

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

printf("%d\n",fib[i]);

LINEAR SEARCH

include<stdio.h>

int main()

int a[100],n, i, key, found=0;

printf("Enter the number of elements\n");

scanf("%d",&n);

printf("Enter the %d elements of array\n", n);


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

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

printf("Enter the key to search\n");

scanf("%d", &key

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

if(key==a[i])

found=1;

break;

if(found==1)

printf("Item found in position %d\n",i+1);

else

printf("Item not found\n");

BINARY SEARCH

#include<stdio.h>

int main()

int i, n, a[10],mid, low, high, key, found=0;

printf("\n Enter the number of elements:\n");


scanf("%d", &n);

printf("Enter the array element in the ascending order\n");

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

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

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

scanf("%d", &key);

low=0;

high=n-1;

while(low<=high)

mid=(low +high )/2;

if(key==a[mid])

found=1;

break;

else if(key>a[mid])

low=mid+1;

else

high=mid-1;

if(found ==1)

printf(“Item found in position : %d”,mid+1);

else

printf("\n Item not found\n");

}
BUBBLESORT

include<stdio.h>

int main()

int a[100],n, i, j, temp;

printf("Enter the number of elements\n");

scanf("%d",&n);

printf("Enter the %d elements of array\n", n);

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

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

printf("The Input array is\n");

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

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

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

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

if(a[j]>a[j+1])

temp=a[j];

a[j]=a[j+1];

a[j+1]=temp;
}

printf("\nThe sorted array is\n");

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

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

SELECTION SORT

#inclde<stdio.h>

#include<math.h>

int main()

int i,n,j,temp,a[100],pos;

printf("Enter the number of elements \n");

scanf("%d",&n);

printf("Enter the elements of array \n");

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

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

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

pos=i;

for(j=i+1;j<n;j++)

if(a[j]<a[pos])

pos=j;
}

temp=a[pos];

a[pos]=a[i];

a[i]=temp;

printf("\n\nThe sorted array is \n");

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

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

C program to read and write 2-d array

include<stdio.h>

int main()

int a[10][10],m,n,i,j;

printf("Enter the size of matrix:\n");

scanf("%d%d",&m,&n);

printf("Enter the elements to matrix A\n");

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

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

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

include<stdio.h>

int main()

int a[10][10],m,n,i,j;
printf("Enter the size of matrix:\n");

scanf("%d%d",&m,&n);

printf("Enter the elements to matrix A\n");

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

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

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

2D ARRAY

Write a c-program to find sum of elements of a given matrix

#include<stdio.h>

main()

int a[10][10],i,j,m,n,sum=0;

printf("Enter The row & column size\n");

scanf("%d%d",&m,&n);

printf("Enter the Elements to matrix\n",);

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

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

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

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

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

sum=sum+a[i][j];

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

Write a c-program to find trace of a given square matrix.


We know that the trace of a matrix is defined as the sum of the leading diagonal elements.

Note : Trace is possible only for a square matrix.

include<stdio.h>

int main()

int a[10][10],i,j,m,n,trace=0;

printf("Enter the size for square matrix\n");

scanf("%d%d",&m,&n);

printf("Enter the Elements to matrix \n");

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

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

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

printf("Matrix\n");

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

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

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

printf("\n");

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

trace=trace+a[i][i];

printf("trace=%d",trace);

Write a c-program to find transpose of a given matrix

#include<stdio.h>
int main()

int a[10][10],t[10][10],i,j,m,n;

printf("Enter the size for square matrix\n");

scanf("%d%d",&m,&n);

printf("Enter the Elements to matrix \n");

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

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

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

printf("Matrix\n");

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

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

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

printf("\n");

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

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

t[j][i]=a[i][j];

printf("\nTranspose matrix is \n");

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

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

printf("%d\t",t[i][j]);

printf("\n");
}

Write c –program to read elements of matrix of size m×n and to compute

1. Sum of the elements of the row

2. Sum of the elements of the column

#include<stdio.h>

int main()

int a[10][10],i,j,m,n,rsum[10],csum[10],sum;

printf("Enter The row & column size\n");

scanf("%d%d",&m,&n);

printf("Enter the Elements\n");

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

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

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

// calculate row sum

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

sum=0;

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

sum=sum+a[i][j];

rsum[i]=sum;

// calculate column sum

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

{
sum=0;

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

sum=sum+a[i][j];

csum[j]=sum;

. Write a C program to read two matrices a (m x n) and b (p x q) and compute the product
of a and b after checking compatibility for multiplication. Output the input matrices and
the resultant matrix with suitable headings and format.

include<stdio.h>

int main()

int a[10][10],b[10][10],c[10][10],i,j,k,m,n,p,q;

printf("enter the size of matrixA");

scanf("%d%d",&m,&n);

printf("enter the size of matrixB");

scanf("%d%d",&p,&q);

// checking compatibility for multiplication

if(n!=p)

printf("matrix multiplication is not possible");

else

// to read elements of -a matrix

printf("enter elements of matrix A\n“);

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

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

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

// to read elelments of b matrix


printf("enter elements of matrix B\n“);

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

for(j=0;j<q;j++)

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

// to compute multiplication of a & b

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

for(j=0;j<q;j++).

c[ i ][ j ]=0;

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

c[ i][ j ]=c[ i ][ j ]+a[ i ][k]*b[k][ j ];

printf(‘A-matrix is\n");

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

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

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

printf("\n");

printf("B- matrix is \n");

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

for(j=0;j<q;j++)
{

printf("%d\t",b[i][j]);

printf("\n");

printf("The resultant matrix C is \n");

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

for(j=0;j<q;j++)

printf("%d\t",c[i][j]);

printf("\n");

strlen (str): string length

Example:
#include <stdio.h>
#include <string.h>
void main ( )
{
char s[ ] = “LAKSHMANA”;
printf (“length = %d”, strlen (s));
}
Output:
length = 9.

include <stdio.h>
int my_strlen (char s[ ])
{
int i = 0;
while (s[i]! = ‘\0’)
i++;
return i;
}
User defined function
int main()
{
char s[25]; int i;
printf (“Enter the string \n”);
gets(s);
i = my_strlen (s);
printf (“length = %d”, i);
}

strcpy (dest,src) - String Copy


strcpy (char dest[ ], char src[ ]);
#include <stdio.h>
#include <string.h>
int main ( )
{
char src[ ] = “DSATM”;
char dest[10];
strcpy (dest, src);
printf (“Destination = %s”, dest);
}

User defined function


#include <stdio.h>
void my_strcpy (char dest[ ], char src[ ])
{
int i = 0;
while (src [i] != ‘\0’)
{
dest[i] = src[i];
i++;
}
dest[i] = ‘\0’;
}
void main ( )
{
char src[25], dest[25];
printf (“Enter string \n”);
scanf (“%s”, src); OR gets (src);
my_strcpy (dest,src);
printf (“Dest=%s”, dest);
}

strcat (str1, str2) - append string str2 to str1.


include<stdio.h>
#include<string.h>
int main()
{
char s1[30],s2[15];
printf(“\n Enter first string:”);
gets(s1);
printf(“\n Enter second string:”);
gets(s2);
if(sizeof(s1)>strlen(s1)+srtlen(s2))
{
strcat(string1,string2);
printf(“\n Concatenated string=%s”,string1);
}
else
printf(“Concatenation not possible\n)
}

User defined function


void concatenate(char s1[], char s2[])
{
int i, j;
i = 0;
while (s1[i] != '\0')
{
i++;
}
j = 0;
while (s2[j] != '\0')
{
s1[i] = s2[j];
j++;
i++;
}
s1[i] = '\0';
}
int main()
{
char s1[30],s2[15];
printf(“\n Enter first string:”);
gets(s1);
printf(“\n Enter second string:”);
gets(s2);
if(sizeof(s1)>strlen(s1)+srtlen(s2))
{
concatenate(string1,string2);
printf(“\n Concatenated string=%s”,string1);
}
else
printf(“Concatenation not possible\n)
}

strcmp() compare two strings str1 & str2.


#include <stdio.h>
#include <string.h>
int main()
{ char str1[] = "abcd", str2[] = "abCd", str3[] = "abcd";
int result1,result2;
// comparing strings str1 and str2
result1 = strcmp(str1, str2);
printf("strcmp(str1, str2) = %d\n", result1);

// comparing strings str1 and str3


result2 = strcmp(str1, str3);
printf("strcmp(str1, str3) = %d\n", result2);
}
• Output
strcmp(str1, str2) = 32
strcmp(str1, str3) = 0
Write a C program to print array of strings:
include <stdio.h>
int main()
{

int i;
char city[5][10] = { "DELHI",
"CHENNAI",
"BANGALORE",
"HYDERABAD",
"MUMBAI" };

printf("strings are:\n");
for(i=0;i<=5;i++)
{
printf("%s\n", city[i]);
}
}

Write a C program that reads a sentence and prints the frequency of each of
the vowels and total count of consonants
#include <stdio.h>
void main()
{
char str[80];
int i, vowels = 0, consonants = 0;
printf("Enter a str \n");
gets(str);
for (i = 0; str[i] != '\0'; i++)
{
if(isalpha(str[i]))
{
ch=tolower(str[i])
if (ch == 'a' || ch== 'e' || ch== 'i' || ch== 'o' || ch== 'u')
{
vowels = vowels + 1;
}
else
{
consonants = consonants + 1;
}
}

printf("No. of vowels = %d\n", vowels);


printf("No. of consonants= %d\n", consonants);
}

To print frequency of each vowel and total count of consonants in a sentence


#include<stdio.h>
void main()
{
char s[100];
int j, ac=0, ec=0, ic=0, oc=0, uc=0, cc=0;
printf("Enter a sentence\n");
gets(s);
for(j=0;s[j]!='\0';j++)
{
if(isalpha(s[j]))
{
if(s[j]=='A'||s[j]=='a')
ac++;
else if(s[j]=='E'||s[j]=='e’)
ec++;
else if(s[j]=='I'||s[j]=='i’)
ic++;
else if(s[j]=='O'||s[j]=='o’)
oc++;
else if(s[j]=='U'||s[j]=='u’)
uc++;
else if(s[j]!=' ‘)
cc++;
}
}
printf("\nIn the sentence \"%s\" :",s);
printf("\nThe frequency vowel of A/a is %d",ac);
printf("\nThe frequency vowel of E/e is %d",ec);
printf("\nThe frequency vowel of I/i is %d",ic);
printf("\nThe frequency vowel of O/o is %d",oc);
printf("\nThe frequency vowel of U/u is %d",uc);
printf("\nThe total count of all consonents is %d",cc);

Example for array within structure


struct Student
{
int Roll;
char Name[25];
int Marks[3]; //Statement 1 : array of marks
int Total;
float Avg;
};

void main()
{
int i;
struct Student S;

printf("\n\nEnter Student Roll : ");


scanf("%d",&S.Roll);
printf("\n\nEnter Student Name : ");
scanf("%s",&S.Name);

S.Total = 0;

for(i=0;i<3;i++)
{
printf("\n\nEnter Marks %d : ",i+1);
scanf("%d",&S.Marks[i]);

S.Total = S.Total + S.Marks[i];


}

S.Avg = S.Total / 3;

printf("\nRoll : %d",S.Roll);
printf("\nName : %s",S.Name);
printf("\nTotal : %d",S.Total);
printf("\nAverage : %f",S.Avg);
}
Write a C program to add two numbers using pointers
include<stdio.h>
int main()
{
int a, b, sum;
int *p,*q;
printf(“Enter the value of a & b\n”);
scanf(“%d%d”,&a,&b);
p=&a;
q=&b;
sum=*p+*q;
printf(“Sum=%d\n”, sum);
}

ARRAY & POINTERS


#include<stdio.h>
Int main()
{
int []={5,10,15,20}; array
int *p,*q; 5 10 15 20
p=a[0];
q=a[3];
while(p<=q)
{
printf(“%d\t”,p);
p++;
}
}
Output:
5 10 15 20

Develop a program using pointers to compute the sum, mean and standard
deviation of all elements stored in an array of n real numbers.

Mean= Sum of all elements / no of elements


standard deviation

int main()
{
float a[10],*ptr, mean, std, sum=0,sumstd=0;
int n,i;
printf("Enter the number of elements\n");
scanf("%d",&n);
printf("Enter array elements\n");
for(i=0;i<n;i++)
{
scanf("%f",&a[i]);
}
ptr=a;
for(i=0;i<n;i++)
{
sum=sum+*ptr;
ptr++;
}
mean=sum/n;
ptr=a;
for(i=0;i<n;i++)
{
sumstd = sumstd + pow((*ptr-mean),2);
ptr++;
}
std=sqrt(sumstd/n);
printf("Sum=%.3f\t",sum);
printf("Mean=%.3f\t",mean);
printf("Standard Deviation =%.3f\t",std)
}
OUTPUT:
.....................................................................
Enter the number of elements
5
Enter array elements
1
2
3
4
5
Sum=15.000
Mean=3.000
Standard Deviation =1.414

UNION
include <stdio.h>
#include <string.h>
union Example
{
int i;
float f;
char str;
};
int main( )
{
union Example e1;
e1.i = 10;
printf( "e1.i : %d\n", e1.i);
e1.f = 220.5;
printf( "e1.f : %f\n", e1.f);
e1.str='a';
printf( "e1.str : %c\n", e1.str);
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy