0% found this document useful (0 votes)
6 views

c programs document

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

c programs document

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

BASIC PROGRAMS

1. Write a C program to print a statement

#include<stdio.h>

void main()

printf("INDIA");

getch();

clrscr();

Output:

2. Write a C program to perform Arithmetic Operations

#include<stdio.h>

#include<conio.h>

void main()

int x,y;

printf("enter the values of x and y");

scanf("%d%d",&x,&y);

printf("\nthe addition value of x and y is %d\n",x+y);

printf("the sutraction value of x and y is %d\n",x-y);

printf("the multiplication value of x and y is %d\n",x*y);

printf("the modulus value of x and y is %d\n",x%y);

printf("the division value of x and y is %d",x/y);

getch();

clrscr();

Output:
3. Write a C program to perform Relational Operations

#include<stdio.h>

#include<conio.h>

void main()

int x,y;

printf("enter the values of x and y");

scanf("%d%d",&x,&y);

printf("\n x < y %d\n",x<y);

printf("\n x <= y %d\n",x<=y);

printf("\n x > y %d\n",x>y);

printf("\n x >= y %d\n",x>=y);

printf("\n x == y %d\n",x==y);

printf("\n x != y %d\n",x!=y);

getch();

clrscr();

Output:

4. Write a C program to perform logical Operations

#include<stdio.h>

#include<conio.h>

void main()

int x,y;

printf("enter the values of x and y");

scanf("%d%d",&x,&y);

printf("\n x && y %d\n",x&&y);

printf("\n x || y %d\n",x||y);
printf("\n !x %d\n",!x);

getch();

clrscr();

Output:

5. Write a C program to perform Unary Operations

#include<stdio.h>

#include<conio.h>

void main()

int x=1,y=2,p=3,q=4;

x++;

printf("\n post increment x++ %d\n",x);

y--;

printf("\n post decrement y-- %d\n",y);

++p;

printf("\n pre increment ++p %d\n",p);

--q;

printf("\n pre decrement ++q %d\n",q);

getch();

clrscr();

Output:

6. Write a C program to perform bitwise Operations

#include<stdio.h>

#include<conio.h>
void main()

int x,y;

printf("enter the values of x and y");

scanf("%d%d",&x,&y);

printf("\n x & y %d\n",x&y);

printf("\n x | y %d\n",x|y);

printf("\n x ^ y %d\n",x^y);

printf("\n ~x %d\n",~x);

getch();

clrscr();

Output:

7. Write a C program to perform special Operations

#include<stdio.h>

#include<conio.h>

void main()

int x,y,p,q;

x=sizeof(int);

y=sizeof(char);

p=sizeof(float);

q=sizeof(double);

printf("the size of int data type %d\n",x);

printf("the size of char data type %d\n",y);

printf("the size of float data type %d\n",p);

printf("the size of double data type %d\n",q);

getch();

}
Output:

8. Write a C program to perform conditional Operations

#include<stdio.h>

void main()

int x,y;

printf("enter a and y values");

scanf("%d %d",&x,&y);

x<y?printf("x is less than y"):printf("x is greater than y");

getch();

clrscr();

Output:

9. Write a C program to perform assignment Operations

#include<stdio.h>

void main()

int x,y,a,b,z=2;

x=9,b=10;

y=a=x;

z+=1;

printf("the value of x %d\n",x);

printf("the value of y %d\n",y);

printf("the value of a %d\n",a);

printf("the value of b %d\n",b);

printf("the value of z %d\n",z);

getch();

clrscr();
}

Output:

10. Write a C program to calculate Area of circle

#include<stdio.h>

int main()

float radius, area;

printf("\nEnter the radius of Circle : ");

scanf("%d", &radius);

area = 3.14 * radius * radius;

printf("\nArea of Circle : %f", area);

return (0);

Output:

Enter the radius of Circle : 2.0

Area of Circle : 6.14

11. Write a C program to calculate Area of Rectangle

#include<stdio.h>

#include<conio.h>

int main()

int length, breadth, area;

printf("\nEnter the Length of Rectangle : ");

scanf("%d", &length);

printf("\nEnter the Breadth of Rectangle : ");

scanf("%d", &breadth);

area = length * breadth;

printf("\nArea of Rectangle : %d", area);

return (0);

Output:

Enter the Length of Rectangle : 5


Enter the Breadth of Rectangle : 4

Area of Rectangle : 20

12. Write a C program to calculate Area of Square

#include<stdio.h>

int main()

int side, area;

printf("\nEnter the Length of Side : ");

scanf("%d", &side);

area = side * side;

printf("\nArea of Square : %d", area);

return (0);

Output:

Enter the Length of Side : 5

Area of Square : 25

13. Write a C program to calculate Area of Triangle

#include<stdio.h>

#include<conio.h>

#include<math.h>

void main()

int a, b, c, s, Area;

clrscr();
printf(" Enter the values of a,b and c\n ");

scanf(" %d %d %d " , &a, &b, &c);

S=(a+b+c)/2;

Area = sqrt( s (s-a) (s-b) (s-c) ); // sqrt used for Square Root

printf(" Area of Triangle = %d\n " , Area);

Output:
14. Write a C program using simple if

#include <stdio.h>

int main()

int x = 20;

int y = 22;

if (x<y)

printf("Variable x is less than y");

return 0;

Output:

Variable x is less than y

15. Write a C program to find biggest among 3 given numbers using nested if

#include<stdio.h>

void main()

int a,b,c;

printf("enter 3 values");

scanf("%d %d %d",&a,&b,&c);

if(a>b)

if(a>c)

printf("%d is big",a);

if(b>a)

if(b>c)

printf("%d is big",b);

}
}

if(c>a)

if(c>b)

printf("%d is big",c);

getch();

Output:

16. Write a C program to check whether the given number is even or odd using if else

#include<stdio.h>

void main()

int n;

printf("enter a number");

scanf("%d",&n);

if(n%2==0)

printf("you have entered even number");

else

printf("you have entered odd number");

getch();

Output:
17. Write a C program to find biggest among 3 given numbers using nested if else

#include<stdio.h>

void main()

int a,b,c;

printf("enter 3 values");

scanf("%d %d %d",&a,&b,&c);

if(a>b)

if(a>c)

printf("%d is big",a);

else

printf("%d is big",c);

else

if(c>b)

printf("%d is big",c);

else

printf("%d is big",b);

getch();

Output:
18. Write a C program to print week days using else if ladder (if else if)

#include<stdio.h>

void main()

int n;

printf("enter a number between 1 to 7:");

scanf("%d",&n);

if(n==1)

printf("monday");

else if(n==2)

printf("tuesday");

else if(n==3)

printf("wednsday");

else if(n==4)

printf("thursday");

else if(n==5)

printf("friday");

else if(n==6)

printf("saturday");

else if(n==7)

printf("sunday");
}

else

printf("you entered wrong input");

getch();

clrscr();

Output:

19. Write a C program to check whether the given year is leap year or not using else if ladder (if else if)

#include<stdio.h>

void main()

int y;

printf("enter year");

scanf("%d",&y);

if(y%100==0 && y%400==0)

printf("%d is leap year",y);

else if(y%100!=0 && y%4==0)

printf("%d is leap year",y);

else

printf("%d is not leap year",y);

getch();

clrscr();

Output:
20. Write a C program to check whether the student passed in first class or not using else if ladder (if else if)

#include<stdio.h>

void main()

int m;

printf("enter marks");

scanf("%d",&m);

if(m>=360)

printf("student passed in first class");

else if(m>=350 && m<=359)

printf("student passed in second class");

else

printf("student passed in third clas");

getch();

Output:

21. Write a C program to print 1 to n numbers (ascending order) using while

#include<stdio.h>

void main()

{
int i=1,n;

printf("enter n value");

scanf("%d",&n);

while(i<=n)

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

i++;

getch();

clrscr();

Output:

22. Write a C program to print 1 to n numbers in descending order using while

#include<stdio.h>

void main()

int i=1,n;

printf("enter n value");

scanf("%d",&n);

while(n>=i)

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

n--;

getch();

clrscr();

Output:
23. Write a C program to print even numbers between 1 to n using while

#include<stdio.h>

void main()

int i=2,n;

printf("enter limits");

scanf("%d",&n);

while(i<=n)

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

i=i+2;

getch();

clrscr();

Output:

24. Write a C program to print odd numbers between 1 to n using while

#include<stdio.h>

void main()

int i=1,n;

printf("enter limits");

scanf("%d",&n);

while(i<=n)

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

i=i+2;

getch();

clrscr();

}
Output:

25. Write a C program to print factorial of a given number using while

#include<stdio.h>

void main()

int n,f=1;

printf("enter a number");

scanf("%d",&n);

while(n>0)

f=f*n;

n--;

printf("the factorial of n is %d",f);

getch();

clrscr();

Output:

26. Write a C program to print factorial of a given number using for

#include<stdio.h>

void main()

int n,i,f=1;

printf("enter a number");

scanf("%d",&n);

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

f=f*i;

printf("factorial of given number is %d",f);


getch();

clrscr();

Output:

27. Write a C program to check whether the given number is prime number or not using while

#include<stdio.h>

void main()

int n,i=1,c=0;

printf("enter a number:");

scanf("%d",&n);

while(i<=n)

if(n%i==0)

c++;

i++;

if(c==2)

printf("given number is prime");

else

printf("given number is not prime");

getch();

clrscr();

Output:
28. Write a C program to check whether the given number is prime number or not using for

#include<stdio.h>

void main()

int n,i,c=0;

printf("enter a number:");

scanf("%d",&n);

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

if(n%i==0)

c++;

if(c==2)

printf("given number is prime");

else

printf("given number is not prime");

getch();

clrscr();

Output:

29. Write a C program to check whether the given number is perfect number or not using while

#include<stdio.h>

void main()

{
int n,i=1,sum=0;

printf("enter a number:");

scanf("%d",&n);

while(i<n)

if(n%i==0)

sum=sum+i;

i++;

if(sum==n)

printf("given number is perfect");

else

printf("given number is not perfect");

getch();

clrscr();

30. Write a C program to check whether the given number is perfect number or not using for

#include<stdio.h>

void main()

int n,i,sum=0;

printf("enter a number:");

scanf("%d",&n);

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

if(n%i==0)
{

sum=sum+i;

if(sum==n)

printf("given number is perfect");

else

printf("given number is not perfect");

getch();

clrscr();

Output:

31. Write a C program to check whether the given number is palindrome or not using while

#include<stdio.h>

void main()

int n,r,sum=0,temp;

printf("enter a number");

scanf("%d",&n);

temp=n;

while(n>0)

r=n%10;

sum=sum*10+r;

n=n/10;

if(sum==temp)

{
printf("palindrome");

else

printf("not a palindrome");

getch();

clrscr();

Output:

32. Write a C program to check whether the given number is palindrome or not using for

#include<stdio.h>

void main()

int n,t,i;

printf("enter a number");

scanf("%d",&n);

t=n;

for(i=0;n>0;n=n/10)

i=i*10;

i=i+(n%10);

if(t==i)

printf("palindrome");

else

printf("not a palindrome");

}
getch();

clrscr();

Output:

33. Write a c program to print fibnocci series using for loop

#include<stdio.h>

#include<conio.h>

void main()

int l,i,f1=1,f2=0,f3=0;

clrscr();

printf("Enter the limit\n");

scanf("%d",&l);

for (i=1;i<=l;i++)

printf("%d,",f3);

f3=f1+f2;

f1=f2;

f2=f3;

getch();

Output

34. Write a c program to print fibnocci series using do while loop

#include<stdio.h>

void main()

int f1,f2,f3,l,i;

clrscr();

f1=0; f2=1; i=3;


printf("Enter the limit:");

scanf("%d",&l);

printf("%d %d",f1,f2);

do

f3=f1+f2;

printf(" %d",f3);

f1=f2;

f2=f3;

i=i+1;

while (i<=l);

getch();

Output:

35. Write a c program to print fibnocci series using while loop

#include<stdio.h>

int main()

int f1,f2,f3,l,i;

printf("Entet the limit");

scanf("%d",&l);

i=1; f1=0; f2=1;

while(i<=l)

printf("%d ",f1);

f3=f1+f2;

f1=f2;

f2=f3;

i++;

getch();
clrscr();

return 0;

Output:

36. Write a c program to print squares of 1 to n natural numbers

#include <stdio.h>

int main()

int n, square,i = 1;

printf("Enter the value for n:");

scanf("%d", &n);

printf("squares of 1 to %d natural numbers\n",n);

while (i<=n)

square = i*i;

printf("%d ",square);

i++;

getch();

clrscr();

return 0;

Output:

37. Write a c program to print cubes of 1 to n natural numbers

#include<stdio.h>

int main()

int n,cube,i = 1;

printf("Enter the value for n:");


scanf("%d", &n);

printf("cubes of 1 to %d natural numbers\n",n);

while (i<=n)

cube=i*i*i;

printf("%d ",cube);

i++;

getch();

clrscr();

return 0;

Output:

38. Write a c program to print sum squares of 1 to n n natural numbers

#include <stdio.h>

int main()

int n, square,sum = 0,i = 1;

printf("Enter the value for n:");

scanf("%d", &n);

while (i<=n)

square = i * i;

sum = sum + square;

i++;

printf("Sum of squares of first %d natural numbers is %d\n",n,sum);

getch();

return 0;

Output:
39. Write a c program to print sum cubes of 1 to n n natural numbers

#include <stdio.h>

int main()

int n, square,sum = 0,i = 1;

printf("Enter the value for n:");

scanf("%d", &n);

while (i<=n)

square = i*i*i;

sum = sum + square;

i++;

printf("Sum of cubes of first %d natural numbers is %d\n",n,sum);

getch();

clrscr();

return 0;

Output:

40. Write a c program to perform swapping.

#include <stdio.h>

int main()

int a,b,t;

printf("Enter values for a&b");

scanf("%d%d", &a,&b);

printf("values of and b before swap a=%d,b=%d\n",a,b);

t=a;

a=b;

b=t;
printf("values of and b after swap a=%d,b=%d",a,b);

getch();

clrscr();

return 0;

Output:

41. Write a c program to check wether the given number is amstrong or not

#include<stdio.h>

int main()

int n,r,t,sum=0;

printf("Enter a three digit integer: ");

scanf("%d", &n);

t=n;

while(n!=0)

r=n%10;

sum=sum+(r*r*r);

n=n/10;

if(t==sum)

printf("%d is an Armstrong number.",sum);

else

printf("%d is not an Armstrong number.",sum);

getch();

clrscr();

return 0;

Output:
42. Write a c program to print sum of individual digits in a given number.

#include <stdio.h>

int main()

int num,r,sum=0;

printf("Enter any number ");

scanf("%d",&num);

while(num!=0)

r=num % 10;

sum=sum+r;

num = num / 10;

printf("\nSum of digits = %d", sum);

getch();

clrscr();

return 0;

Output:

43. Write a c program to print reverse of given number.

#include <stdio.h>

int main()

int n,r,rev=0;

printf("Enter an integer: ");

scanf("%d", &n);

while(n!=0)

r=n%10;

rev=rev*10+r;

n/=10;
}

printf("Reversed Number = %d", rev);

getch();

return 0;

Output:

ARRAYS
1DIMENSIONAL ARRAY’S
1. Write a C program for accessing and printing elements into and from an array
#include<stdio.h>
void main()
{
int a[10],n,i;
printf("enter araay size\n");
scanf("%d",&n);
printf("enter array elements\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("array elements are\n");
for(i=0;i<n;i++)
{
printf("%d\t",a[i]);
}
getch();
clrscr();
}

Output:

2. Write a C program to perform sum and average of elements of an array


#include<stdio.h>
#include<stdio.h>
void main()
{
int a[10],n,i,avg,sum=0;
printf("enter araay size\n");
scanf("%d",&n);
printf("enter array elements\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<n;i++)
{
sum=sum+a[i];
}
printf("sum of array element %d",sum);
avg=sum/n;
printf("\nthe average of array elements %d",avg);
getch();
clrscr();
}}

Output:

3. Write a C program to print minimum and maximum elements of an array.


#include<stdio.h>
void main()
{
int a[10],n,i,s;
printf("enter araay size\n");
scanf("%d",&n);
printf("enter array elements\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("array elements are\n");
for(i=0;i<n;i++)
{
printf("%d ",a[i]);
}
s=a[0];
for(i=0;i<n;i++)
{
if(a[i]>s)
{
s=a[i];
}
}
printf("\nthe biggest element in an array is %d",s);
for(i=0;i<n;i++)
{
if(a[i]<s)
{
s=a[i];
}
}
printf("\nthe smallest element in an array is %d",s);
getch();
clrscr();
}

Output:
4. Write a C program to print reverse order of array elements.
#include<stdio.h>
void main()
{
int a[10],n,i,t;
printf("enter araay size\n");
scanf("%d",&n);
printf("enter array elements\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("array elements are\n");
for(i=0;i<n;i++)
{
printf("%d ",a[i]);
}
//to reverse array elements
for(i=0;i<n/2;i++)
{
t=a[i];
a[i]=a[n-1-i];
a[n-1-i]=t;
}
printf("\nreverse order of array elements\n");
for(i=0;i<n;i++)
{
printf("%d ",a[i]);
}
getch();
clrscr();
}

Output:

5. Write a C program to sort the array elements.


#include<stdio.h>
void main()
{
int a[10],n,i,j,t;
printf("enter araay size\n");
scanf("%d",&n);
printf("enter array elements\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("array elements are\n");
for(i=0;i<n;i++)
{
printf("%d ",a[i]);
}
//for ascending order of sorting
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
}
printf("\narray elements after sorting(ascending order)\n");
for(i=0;i<n;i++)
{
printf("%d ",a[i]);
}
//for descending order of sorting
for(i=0;i<n;i++)
{

for(j=i+1;j<n;j++)
{
if(a[i]<a[j])
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
}
printf("\narray elements after sorting(descending order)\n");
for(i=0;i<n;i++)
{
printf("%d ",a[i]);
}
getch();
clrscr();
}
Output:

6. Write a C program to pass array elements as arguments to user defined function.


#include<stdio.h>
//user defined function to print array elements
void printarray(int a)
{
printf("%d ",a);
}
void main()
{
int a[10],n,i;
printf("enter araay size\n");
scanf("%d",&n);
printf("enter array elements\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("array elements are\n");
for(i=0;i<n;i++)
{
printarray(a[i]); //passing array element
}
getch();
clrscr();
}
Output:

7. Write a C program to pass array as arguments to user defined function.


#include<stdio.h>
//user defined function to print array elements
void printarray(int a[])
{
int i,n;
printf("enter no of elements that u want to print from array\n");
scanf("%d",&n);
printf("array elements are\n");
for(i=0;i<n;i++)
{
printf("%d ",a[i]);
}
}

void main()
{
int a[10],n,i;
printf("enter araay size\n");
scanf("%d",&n);
printf("enter array elements\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printarray(a);//passing array
getch();
clrscr();
}
Output:
2DIMENSIONAL ARRAY’S
8. Write a C program to perform addition of two matrices.
#include<stdio.h>
void main()
{
int a[20][20],b[20][20],res[20][20],r,c,i,j;
printf("enter row size\n");
scanf("%d",&r);
printf("enter column size\n");
scanf("%d",&c);
printf("enter array elements for 1st matrix\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("enter array elements for 2nd matrix\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&b[i][j]);
}
}
printf("addition of two matrices \nthe result is\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
res[i][j]=0;
res[i][j]=a[i][j]+b[i][j];
printf("%d ",res[i][j]);
}
printf("\n");
}
getch();
clrscr();
}
Output:

9. Write a C program to perform multiplication of two matrices.


#include<stdio.h>
void main()
{
int a[20][20],b[20][20],res[20][20];
int n,i,j,k;
printf("enter size\n");
scanf("%d",&n);
printf("enter array elements for 1st matrix\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("enter array elements for 2nd matrix\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&b[i][j]);
}
}
printf("product of two matrices\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
res[i][j]=0;
for(k=0;k<n;k++)
{
res[i][j]=res[i][j]+a[i][k]*b[k][j];
}
}
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("%d ",res[i][j]);
}
}
getch();
clrscr();
}
Output:

10. Write a C program to perform transpose of a matrix.


#include<stdio.h>
void main()
{
int a[20][20],n,i,j;
printf("enter the size of matrix\n");
scanf("%d",&n);
printf("enter elements of matrix\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("actual matrix\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("%d ",a[i][j]);
}
printf("\n");
}
printf("transpose matrix\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("%d ",a[j][i]);
}
printf("\n");
}
getch();
clrscr();
}
Output:

STRINGS
1. Write a c program to that illustrates String manipulation function or string library
functions

#include<stdio.h>

#include<string.h>

void main()

char str1[]="ece";

char str2[]="2a section";

char str3[]="ABC";

char str4[]="ABC";

int n;

printf("concatination of string1, string2\n");

strcat(str1,str2);

puts(str1);

printf("copying of string2 into string1\n");

strcpy(str1,str2);

puts(str1);
printf("comparing string1 and string2 with case sensitiveness\n");

n=strcmp(str1,str2);

printf("%d",n);

printf("\nlength of string2\n");

n=strlen(str2);

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

printf("converting uppercase string into lowercase string\n");

strlwr(str3);

puts(str3);

printf("converting lowercase string into uppercase string\n");

strupr(str3);

puts(str3);

printf("copying first 2 characters of string2 into string1\n");

strncpy(str1,str2,2);

puts(str1);

printf("concatinating first 2 characters of string2 into string1\n");

strncat(str1,str2,2);

puts(str1);

printf("comparing string3 and string4 without case sensitiveness\n");

n=stricmp(str3,str4);

printf("%d",n);

getch();

clrscr();

}Output
2. Write c program to check whether the given string is palindrome or not

#include<stdio.h>

void main()

char str1[10],str2[10];

printf("enter a string: ");

gets(str1);

strcpy(str2,str1);

strrev(str2);

if(strcmp(str1,str2)==0)

printf("given string is palindrome");

else

printf("given string is not a palindrome");

getch();

clrscr();

Output:

3. Write a C Program to reverse the given string without using string manipulation functions
#include<stdio.h>
#include<string.h>
void main()
{
char str[100],temp;
int i,j=0;
printf("enter the string:");
gets(str);
i=0;
j=strlen(str)-1;
while(i<j)
{
temp=str[i];
str[i]=str[j];
str[j]=temp;
i++;
j--;
}
printf("Reversed string is:\n %s",str);
getch();
}
Output:

4.Write a C program to search for the specific character in the given string without using string manipulation
functions
#include<stdio.h>
void main()
{
char str[50],ch;
int i;
clrscr();
printf("enter string:");
gets(str);
printf("enter the character to be found:\n");
scanf("%c",&ch);
for(i=0;str[i]!='\0';i++)
{
if(str[i]==ch)
break;
}
printf("character is found at %d",i);

getch();
}

5. Write a C program to copy one string to another without using string manipulation functions.
#include<stdio.h>
void main()
{
char str1[100],str2[100];
int i;
clrscr();
printf("enter the content to string1:");
gets(str1);
i=0;
while(str1[i]!='\0')
{
str2[i]=str1[i];
i++;
}
str2[i]='\0';
printf("the content in sting2 is:");
puts(str2);
getch();
}
Output:
6. Write a C program to replace the specific character in a string:
#include<stdio.h>
void main()
{
char str1[10],ch1,ch2;
int i=0;
clrscr();
printf("enter the string:\n");
gets(str1);
printf("original string is: %s\n",str1);
printf("what character do u want to replace?\n");
ch1=getchar();
fflush(stdin);
printf("enter the character with which to be replaced?\n");
ch2=getchar();
while(str1[i]!='\0')
{
if(str1[i]==ch1)
{
str1[i]=ch2;
}
i++;
}
printf("string after replacement is: %s",str1);
getch();
}
Output:

7. Write a C program to replace a character of string either from beginning or ending or at a specified
location:
#include<stdio.h>
#include<string.h>
main()
{
char s[50];
char *p,c,ch;
clrscr();
printf("enter string:");
gets(s);
printf("enter characetr to be replaced:");
scanf("%c",&ch);
printf("enter the character with which to be replaced?");
fflush(stdin);
c=getchar();
while((p=strchr(s,ch))!=NULL)
{
*p=c;
strcpy(p+1,p+1);
}
printf("string after replacement:");
puts(s);
getch();
}
Output:

FUNCTIONS
1. Write a C program to generate Fibonacci sequence using functions
#include<stdio.h>
void fibonacci(int);
void main()
{
int n;
clrscr();
printf("enter n value:");
scanf("%d",&n);
fibonacci(n);
getch();
}
void fibonacci(int a)
{
int first=0,second=1,next,i;
printf("%d\t%d\t",first,second);
for(i=3;i<=a;i++)
{
next=first+second;
printf("%d\t",next);
first=second;
second=next;
}
}
Output:

2. Write a C program to calculate the power of a given number using functions


#include<stdio.h>
float power (float,int);
void main()
{
int b;
float a,p;
clrscr();
printf("Enter two numbers");
scanf("%f%d",&a,&b);
p=power(a,b);
printf("\n %f to the power of %d =%f",a,b,p);
getch();
}
float power(float x,int y)
{
if(y==0)
return(1);
else
return(x*power (x,y-1));
}
Output:
3. Write a C program to find the sum and product of digits in a given number using functions
#include <stdio.h>
/*Function for sum of digits*/
int sumDigits(int num)
{
int sum=0,rem;
while(num > 0)
{
rem=num%10;
sum+=rem;
num=num/10;
}

return sum;
}
/*Function for product of digits*/
int proDigits(int num)
{
int pro=1,rem;
while(num > 0)
{
rem=num%10;
pro*=rem;
num=num/10;
}
return pro;
}
void main()
{
int n;
printf("\nEnter an integer number :");
scanf("%d",&n);
printf("\nSUM of all Digits is : %d",sumDigits(n));
printf("\nPRODUCT of all digits: %d",proDigits(n));
getch();
}
Output:

4. Write a C program to perform all arithmetic operations using functions


#include<stdio.h>
int add(int,int);
float div(int,int);
int mul(int,int);
int mod(int,int);
int sub(int,int);
void main()
{
int a,b;
clrscr();
printf("enter a,b values:");
scanf("%d %d",&a,&b);
printf("Addition result=%d\n",add(a,b));
printf("Subtraction result=%d\n",sub(a,b));
printf("Division result=%f\n",div(a,b));
printf("multiplication result=%d\n",mul(a,b));
printf("modulous division result=%d\n",mod(a,b));
getch();
}
int add(int a,int b)
{
return(a+b);
}
int mul(int a,int b)
{
return(a*b);
}
int mod(int a,int b)
{
return(a%b);
}
int sub(int a,int b)
{
return(a-b);
}
float div(int a,int b)
{
return(a/b);
}
Output:

5. Write a C program to print multiplication table using functions.


#include<stdio.h>
void multiplication_table(int num,int limit)
{
int i;
for(i=1;i<=limit;i++)
{
printf("%d X %d=%d\n",num,i,(num*i));
}
}
void main()
{
int num,limit;
clrscr();
printf("\n enter a number:\n");
scanf("%d",&num);
printf("enter the limit:\n");
scanf("%d",&limit);
multiplication_table(num,limit);
getch();
}
Output:
6. Write a C program to calculate sum and average of array elements using functions
#include<stdio.h>
void sum_average(int a[]);
int n;
void main()
{
int a[20],i;
clrscr();
printf("enter no.of elemnets:");
scanf("%d",&n);
printf("enter elements to an array:\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
sum_average(a);
getch();
}
void sum_average(int a[])
{
int s=0,i;
float avg;
for(i=0;i<n;i++)
{
s=s+a[i];
}
printf("array elemnets sum is %d\n",s);
avg=(float)(s/n);
printf("array elemnets average is %f\n",avg);
}
Output:
STRUCTURES
1) Write c program to create structure and access the members of structure

#include<stdio.h>

struct student

char name[10];

int rollno;

}s;

void main()

printf("enter the name and rollno\n");

scanf("%s %d",s.name,&s.rollno);

printf("name: %s\nrollno: %d",s.name,s.rollno);

getch();

clrscr();

Output:

2) Write c program to illustrate assigning structure variables

#include<stdio.h>

struct student

char name[10];

int rno;

float per;

char grade;

}s1={"devi",95,85.5,'a'},s2;

void main()

int n;
s2=s1; // assigning structure variables

printf("name: %s\nrno: %d\nper: %f\ngrade: %c",s1.name,s1.rno,s1.per,s1.grade);

printf("name: %s\nrno: %d\nper: %f\ngrade: %c",s2.name,s2.rno,s2.per,s2.grade);

n=strcmp(s1.name,s2.name);

if(n==0 && (s1.rno==s2.rno) && (s1.per==s2.per) && (s1.grade==s2.grade))

printf("s1 and s2 are equal");

else

printf("s1 and s2 are equal");

getch();

clrscr();

Output:

3) Write a c program to print size of structure

#include<stdio.h>

struct student

char name[10];

int rno;

float per;

char grade;

}s;

void main()

//size of structure using sizeof operator


printf("the size os student structure is: %d\n",sizeof(s));

printf("the size os student structure is: %d",sizeof(struct student));

getch();

clrscr();

Output:

4) Write a c program to illustrate nested structures

#include<stdio.h>

struct address

char dno[30];

char street[30];

char city[30];

char mandal[30];

char district[30];

long int PIN;

};

struct student

char name[10];

int rno;

float per;

char grade;

struct address a;

}s={"devi",261,85,'a'};

void main()

printf("enter dno\n");

scanf("%s",&s.a.dno);

printf("enter street\n");
scanf("%s",&s.a.street);

printf("enter city\n");

scanf("%s",&s.a.city);

printf("enter mandal\n");

scanf("%s",&s.a.mandal);

printf("enter distict\n");

scanf("%s",&s.a.district);

printf("enter PIN\n");

scanf("%ld",&s.a.PIN);

printf("student details are\n");

printf("_______ _______ ___\n");

printf("name: %s\nrollno: %d\npercentage: %f\ngrade: %c\n",s.name,s.rno,s.per,s.grade);

printf("doorno: %s\nstreet: %s\ncity: %s\nmandal: %s\ndistrict: %s\nPIN:


%ld",s.a.dno,s.a.street,s.a.city,s.a.mandal,s.a.district,s.a.PIN);//accessing nested structure members

getch();

clrscr();

Output:

5) Write c program to pass structure member to user defined function

#include<stdio.h>

struct student

char name[10];
int rno;

float per;

char grade;

}s={"devi",95,85.5,'a'};

void print(float x)

printf("%f",x);

void main()

print(s.per); //passing structure member

getch();

clrscr();

Output:

6) Write c program to pass entire structure to user defined function

#include<stdio.h>

struct student

char name[10];

int rno;

float per;

char grade;

}s={"devi",95,85.5,'a'};

void print(struct student s)

printf("name: %s\n",s.name);

printf("rno: %d\n",s.rno);

printf("per: %f\n",s.per);

printf("grade: %c",s.grade);

}
void main()

print(s); //passing entire structure

getch();

clrscr();

Output:

7) Write a c program to illustrate structure containing Pointer

#include<stdio.h>

struct student

char *name;

int *rollno;

}s;

void main()

char sname[10]="devi";

int rno=261;

s.name=sname;

s.rollno=&rno;

printf("student name: %s\nstudent rollno: %d\n",s.name,*s.rollno);

getch();

clrscr();

Output:

8) Write a c program to illustrate Pointer to structure

#include<stdio.h>

struct student
{

char name[10];

int rno;

float per;

char grade;

}s={"devi",95,85.5,'a'};

void print(struct student *s1)

printf("name: %s\n",s1->name);

printf("rno: %d\n",s1->rno);

printf("per: %f\n",s1->per);

printf("grade: %c",s1->grade);

void main()

print(&s);

getch();

clrscr();

Output:

9) Write a c program to illustrate array of structures

#include<stdio.h>

struct student

char name[10];

int rollno;

}s[2];

void main()

{
int i;

printf("enter the name and rollno\n");

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

scanf("%s %d",s[i].name,&s[i].rollno);

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

printf("student name: %s\nstudent rollno: %d\n",s[i].name,s[i].rollno);

getch();

clrscr();

Output:

POINTERS
1. Write a C program on pointer declaration and initialization.

#include<stdio.h>

main()

int n=10;

int *p;

clrscr();

p=&n;

printf("%u\n",p);

printf("%d\n",*p);

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

printf("%u\n",&n);

*p=*p+10;
printf("%d\n",*p);

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

getch();

Output:

Write a c program to perform pointer arithmetic

#include<stdio.h>
#include<conio.h>
void main()
{
inta,b,x,y,z,*p,*q;
a=12;b=4;
p=&a;q=&b;
x=*p + *q +10;
y= *p * *q +4 -2;
printf("the value of expression is:%d\n",x);
printf("the value of expression is:%d",y);
getch();
clrscr();
}

Output:

Write c program to illustrate call by value

#include<stdio.h>
#include<conio.h>
void swap(intx,int y)
{
int t;
t=x;
x=y;
y=t;
printf("after swap\na=%d\nb=%d",x,y);
}
void main()
{
int a=10,b=20;
printf("before swap\na=%d\nb=%d\n",a,b);
swap(a,b);
getch();
clrscr();
}
Output:

Write c program to illustrate call by reference

#include<stdio.h>
#include<conio.h>
void swap(int *x,int *y)
{
int t;
t=*x;
*x=*y;
*y=t;
printf("after swap\na=%d\nb=%d",*x,*y);
}
void main()
{
int a=10,b=20;
printf("before swap\na=%d\nb=%d\n",a,b);
swap(&a,&b);
getch();
clrscr();
}
Output:

Write a c program to illustrate relationship between pointers and arrays

//accessing and printing array elements


#include<stdio.h>
void main()
{
int a[10],*p,i;
printf("enter elements\n");
p=a;
for(i=0;i<10;i++)
{
scanf("%d",p);
p++;
}
printf("array elements are\n");
p=a;
for(i=0;i<10;i++)
{
printf("%d ", *p);
p++;
}
getch();
clrscr();
}

Output:

Write a c program to illustrate pointers arrays

//accessing and printing array elements


//pointer arrays
#include<stdio.h>
void main()
{
int a[10]={1,2,3,4,5,6,7,8,9,10};
int *p[10],i;
for(i=0;i<10;i++)
{
p[i]=&a[i];
}
printf("array elements are\n");
for(i=0;i<10;i++)
{
printf("%d ",*p[i]);
}
getch();
clrscr();
}
Output:

Write a c program to illustrate function pointers

#include<stdio.h>
int add(intx,int y)
{
returnx+y;
}
void main()
{
int c;
int (*fp)(int,int);//function pointer
fp=add;//assigning function to function pointer
c=(*fp)(20,30);
printf("the addition value is: %d",c);
getch();
clrscr();
}
Output:

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