1. Print Hello world!
#include <stdio.h>
#include <conio.h>
void main() {
printf("Hello, World! \n");
getch();
}
Output:
Hello, World!
=====================================================================
2. Write a program that reads two nos. from key board and gives their addition,subtraction, multiplication, division and modulo.
#include<conio.h>
#include<stdio.h>
void main()
{
int a, b, add, sub, mul, div, mod;
printf("\n\t Enter Two Numbers:" );
scanf(“%d %d”, &a, &b);
printf("ARITHMETIC OPREATION\n" );
add=a+b;
sub=a-b;
mul=a*b;
div=a/b;
mod=a%b;
printf("\nAddition of two Number=%d",add);
printf("\nSubstraction of two Number=%d",sub);
printf("\nMultiplication of two Number=%d",mul);
printf("\nDivision of two Number=%d",div);
printf("\nModule of two Number=%d",mod);
}
Output:
Enter Two Numbers: 30 15
ARITHMETIC OPREATION
Addition of two Number= 45
Substraction of two Number= 15
Multiplication of two Number= 450
Division of two Number= 2
Module of two Number=0
=====================================================================
3. Write a program to convert days into months and days.
#include<stdio.h>
void main()
{
int d;
printf("Enter days\n");
scanf("%d",&d);
printf("%d years,%d months,%d days\n",d/365,(d%365)/30,(d%365)%30);
getch();
}
Output:
Enter Days: 365
1 years, 0 months, 0 days
Enter Days: 950
2 years, 7 months, 10 days
=====================================================================
4. Write a program to solve Quadratic Equation.
#include<stdio.h>
#include<math.h>
void main(){
float a,b,c;
float d,root1,root2;
printf("Enter a, b and c of quadratic equation: ");
scanf("%f%f%f",&a,&b,&c);
d = b * b - 4 * a * c;
if(d < 0){
printf("Roots are complex number.\n");
printf("Roots of quadratic equation are: ");
printf("%.3f%+.3fi",-b/(2*a),sqrt(-d)/(2*a));
printf(", %.3f%+.3fi",-b/(2*a),-sqrt(-d)/(2*a));
return 0;
}
else if(d==0){
printf("Both roots are equal.\n");
root1 = -b /(2* a);
printf("Root of quadratic equation is: %.3f ",root1);
return 0;
}
else{
printf("Roots are real numbers.\n");
root1 = ( -b + sqrt(d)) / (2* a);
root2 = ( -b - sqrt(d)) / (2* a);
printf("Roots of quadratic equation are: %.3f , %.3f",root1,root2);
}
getch();
}
Output:
Enter a, b and c of quadratic equation:
4 4 4 Roots are complex number.
Roots of quadratic equation are: -0.500+0.866i, -0.500-0.866i
Enter a, b and c of quadratic equation:
2 4 1
Roots are real number.
Roots of quadratic equation are: -0.293, -1.707
=====================================================================
5. Write a program to find area of cricle.
#include<stdio.h>
void main()
{
int d;
printf("Enter days\n");
scanf("%d",&d);
printf("%d years,%d months,%d days\n",d/365,(d%365)/30,(d%365)%30);
getch();
}
Output:
Radius: 2
Area of circle is: 12.56
=====================================================================
6. Write a program to check whether given number is even or odd numbers.
#include<stdio.h>
#include<conio.h>
void main()
{
int a;
clrscr();
printf("value of a:");
scanf("%d",&a);
if(a%2==0)
{
printf("%d is even",a);
}
else
{
printf("%d is odd",a);
}
getch();
}
Output:
Value of a: 5
5 is odd.
Value of a: 20
20 is even
=====================================================================
7. Write a program to read a marks of students from keyboards,where the students is pass or fail using if-else.
#include<stdio.h>
#include<conio.h>
void main()
{
int a;
clrscr();
printf("value of a:");
scanf("%d",&a);
if(a>40)
{
printf(“\n student is pass.”);
}
else
{
printf("\n student is fail.”);
}
getch();
}
Output:
Value of a: 50
Student is pass.
Value of a: 28
Student is fail
=====================================================================
8. Write a program to select & print the largest and smallest of the three nos. using Nested-If-Else statement.
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
printf("enter value of a:");
scanf("%d",&a);
printf("enter value of b:");
scanf("%d",&b);
printf("enter value of c:");
scanf("%d",&c);
if(a>b)
{
if(a>c)
{
printf("%d is max.",a);
}
else
{
printf("%d is max.",c);
}
}
else
{
if(b>c)
{
printf("%d is max.",b);
}
else
{
printf("%d is max.",c);
}
}
if(a<b)
{
if(a<c)
{
printf("\n%d is min.",a);
}
else
{
printf("\n%d is min.",c);
}
}
else
{
if(b<c)
{
printf("\n%d is min.",b);
}
else
{
printf("\n%d is min.",c);
}
}
getch();
}
Output:
enter value of a:44
enter value of b:0
enter value of c:-10
44 is max.
-10 is min.
enter value of a:4
enter value of b:4
enter value of c:-4
4 is max.
4 is min.
=====================================================================
9. Write a program to Add, subtract and multiply two nos. using switch statement.
# include <stdio.h>
#include<conio.h>
void main()
{
char op;
float num1,num2;
printf("Enter operator either + or - or * or divide : ");
scanf("%c",&op);
printf("Enter two operands: ");
scanf("%f%f",&num1,&num2);
switch(op)
{
case '+':
printf("%.1f + %.1f = %.1f",num1, num2, num1+num2);
break;
case '-':
printf("%.1f - %.1f = %.1f",num1, num2, num1-num2);
break;
case '*':
printf("%.1f * %.1f = %.1f",num1, num2, num1*num2);
break;
case '/':
printf("%.1f / %.1f = %.1f",num1, num2, num1/num2);
break;
default:
/* If operator is other than +, -, * or /, error message is shown */
printf("Error! operator is not correct");
break;
}
getch();
}
Output:
Enter operator either + or - or * or divide : +
Enter two operands: 3 5
3 + 5 = 8
Enter operator either + or - or * or divide : *
Enter two operands: 8 5
8 * 5 = 40
=====================================================================
10.Write a program in c which illustrate the use of increment and decrement operators.
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c,d,e;
clrscr();
printf("enter value:");
scanf("%d",&a);
e=a++;
printf("post increament:%d",e);
b=++a;
printf("\npre increament:%d",b);
c=a--;
printf("\npost decrement:%d",c);
d=--a;
printf("\npre decrement:%d",d);
getch();
}
Output:
enter value:5
post increament:5
pre increament:7
post decrement:7
pre decrement:5
enter value:-5
post increament:-5
pre increament:-3
post decrement:-3
pre decrement:-5
=====================================================================
11.Write a program to read marks from keyboard and your program should display equivalent grade according to following table.(Using ladder if else)
Marks Grade
100-80 Distinction
60-79 First class
35-59 Second class
0-34 Fail
#include<stdio.h>
#include<conio.h>
void main()
{
int e1,m1,m2,m3,m4,m5,total;
float avg;
char name;
clrscr();
printf("\n\t * * * * * Create Student Marksheet * * * * *");
printf("\n\t Enter Student Name::");
scanf("%c",&name);
printf("\n\t Enter Exam Seat No::");
scanf("%d",&e1);
printf("\n\t Enter Subject Mark1::");
scanf("%d",&m1);
printf("\n\t Enter Subject Mark2::");
scanf("%d",&m2);
printf("\n\t Enter Subject Mark3::");
scanf("%d",&m3);
printf("\n\t Enter Subject Mark4::");
scanf("%d",&m4);
printf("\n\t Enter Subject Mark5::");
scanf("%d",&m5);
total=m1+m2+m3+m4+m5;
avg=(total/5);
printf("\n\t Total Marks :: %d",total);
printf("\n\t Percentage of Marks :: %f",avg);
if(avg>=80 && avg<=100)
{
printf("\n\t Got Distinction.");
}
else if(avg>=60 && avg<79)
{
printf("\n\t Got First Class.");
}
else if(avg>=35 && avg<59)
{
printf("\n\t Got Second Class.");
}
else if(avg>=40 && avg<=59)
{
printf("\n\t Got Pass.");
}
else if(avg>=0 && avg<=39)
{
printf("\n\t Got Fail.");
}
else
{
printf("\n\t Invalid Input.");
}
getch();
}
Output:
* * * * * Create Student Marksheet * * * * *
Enter Student Name:: f
Enter Exam Seat No:: 1002
Enter Subject Mark1:: 66
Enter Subject Mark2:: 77
Enter Subject Mark3:: 88
Enter Subject Mark4:: 89
Enter Subject Mark5:: 92
Total Marks :: 412
Percentage of Marks :: 82.000000
Got Distinction.
=====================================================================
12.Write a C program to check whether given digit, lowercase and uppercase.
#include<stdio.h>
#include<conio.h>
void main()
{
char ch;
clrscr();
printf("Enter the character:");
scanf("%c",&ch);
if((ch>='0')&&(ch<='9'))
printf("The number is %c digit",ch);
else if((ch>='a')&&(ch<='z'))
printf("the character is %c lowercase",ch);
else if((ch>='A')&&(ch<='Z'))
printf("The character is %c uppercase",ch);
else
printf("The character is %c other type");
getch();
}
Output:
Enter the character:7
The number is 7 digit
Enter the character:R
The character is R uppercase
=====================================================================
13.Write a program to swap two numbers without using third variables.
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
clrscr();
printf("enter value of a:");
scanf("%d",&a);
printf("enter value of b:");
scanf("%d",&b);
a=a+b;
b=a-b;
a=b-a;
printf(“\n\t After swapping”);
printf("\nvalue of a:%d",a);
printf("\nvalue of b:%d",b);
getch();
}
Output:
Enter value of a: 10
Enter value of b: 20
After swapping
Value of a:20
Value of b:10
=====================================================================
14.Write a program to find sum of all integers greater than 100 & less than 200 and are divisible by 5.
#include<stdio.h>
#include<conio.h>
void main()
{
int i, sum=0;
clrscr();
printf("All nos. between 100 - 200 which is divisible by 5\n");
for(i=101;i<200;i++)
{
if(i%5==0)
{
printf("%5d",i);
sum+=i; }
}
printf("\n\t Sum is = %d",sum);
getch();
}
Output:
All nos. between 100 - 200 which is divisible by 5
105 110 115 120 125 130 135 140 145 150 155 160 165 170 175 180
185 190 195
Sum is = 2850
=====================================================================
(1) Write a program to print following patterns.
(a)
*
**
***
****
*****
#include <stdio.h>
#include<conio.h>
void main()
{
int row, col;
printf("Enter number of rows:\n");
scanf("%d", &n);
for (row = 1; row <= n ; row++ )
{
for(col = 1 ; col <= c ; col++ )
{
printf("*");
}
printf("\n");
}
getch();
}
Output:
Enter number of rows: 5
*
**
***
****
*****
=====================================================================
(b)
11111
2222
333
44
5
#include <stdio.h>
#include<conio.h>
void main()
{
int row, col;
for(row=1;row<=5;row++)
{
for(col=5;col>=i;col--)
{
printf("%d", row);
}
printf("\n");
}
getch();
}
Output:
11111
2222
333
44
5
=====================================================================
(c)
123456
23456
3456
456
56
6
#include<stdio.h>
#include<conio.h>
void main()
{
int row,col,n,sp;
clrscr();
printf("Enter n=");
scanf("%d",&n);
for(row=1;row<=n;row++)
{
for(sp=1;sp<row;sp++)
{
printf(" ");
}
for(col=row;col<=n;col++)
{
printf("%d",col);
}
printf("\n");
}
getch();
}
Output:
Enter n=6
123456
23456
3456
456
56
6
=====================================================================
(d)
AAAAA
BBBB
CCC
DD
E
#include <stdio.h>
#include <conio.h>
void main()
{
int row,col,n,sp;
char ch='A'-1;
clrscr();
printf("Enter n=");
scanf("%d",&n);
for(row=1;row<=n;row++)
{
for(col=n;col>=row;col--)
{
printf("%c",ch+row);
}
printf("\n");
}
getch();
}
Output:
Enter n=6
AAAAA
BBBB
CCC
DD
E
=====================================================================
(e)
10
1
1 0 1
0 1 0 1
1 0 1 0 1
#include<stdio.h>
#include<conio.h>
void main()
{
int row, col, k, n;
clrscr();
printf("Enter the number:");
scanf("%d", &n);
for(row=1;row<=n;row++)
{
if(row%2==0)
k=0;
else
k=1;
for(col=1; col<=row; col++)
{
printf("%d",k);
if(k==0)
k=1;
else
k=0;
}
printf("\n");
}
getch();
}
Output:
Enter the number: 5
10
1
1 0 1
0 1 0 1
1 0 1 0 1
=====================================================================
(f)
* * * * * * * * *
* * * * * * *
* * * * *
* * *
*
#include<stdio.h>
#include<conio.h>
void main()
{
int n,row,col,space;
clrscr();
printf("Enter number of rows:");
scanf("%d",&n);
for(row=n;row>=1;row--)
{
for(space=0;space<n-row;space++)
printf(" ");
for(col=row;col<=2*row-1;col++)
printf("* ");
for(col=0;col<row-1;col++)
printf("* ");
printf("\n");
}
getch();
}
Output:
Enter number of rows: 5
* * * * * * * * *
* * * * * * *
* * * * *
* * *
*
=====================================================================
(g)
1
123
12345
1234567
123456789
1234567
12345
123
1
#include<stdio.h>
#include<conio.h>
void main()
{
int row, col, sp;
clrscr();
for(row=1;row<=5;row++)
{
for(sp=row;sp<5;sp++)
{ printf(" "); }
for(col=1;col<(row*2);col++)
{ printf("%d", col);
}
printf("\n"); }
for(row=4;row>=1;row--)
{
for(sp=5;sp>row;sp--)
{ printf(" "); }
for(col=1;col<(row*2);col++)
{ printf("%d", col); }
printf("\n");
} getch();
}
Output:
1
123
12345
1234567
123456789
1234567
12345
123
1
=====================================================================
(h)
zyxwv
zyxw
zyx
zyz
#include <stdio.h>
#include <conio.h>
void main()
{
int row,col,n,sp;
clrscr();
for(row=n;row>=1;row--)
{
for(sp=1;sp<n-row+1;sp++)
{
printf(" ");
}
for(col=1;col<=row;col++)
{
printf("%c",123-col);
}
printf("\n");
}
getch();
}
Output:
zyxwv
zyxw
zyx
zy
z
=====================================================================
(2) Write a program to find sum of given series.
(a) sum= 1 + 3 + 5 + 7 +……….. + N.
#include<stdio.h>
#include<conio.h>
void main()
{
int n, i, sum=0;
clrscr();
printf("\n\t Enter any number:: ");
scanf("%d", &n);
for(i=1;i<=n; i=i+2)
{
printf(“%d +”,i);
sum= sum + i;
}
printf("\t = %d", sum);
getch();
}
Output:
Enter any number:: 9
The sum 1 to 9 is: 25
Enter any number:: 100
The sum 1 to 100 is: 2500
=====================================================================
(b) sum= 1+1/2+1/3+1/4+………+1/N.
#include<stdio.h>
#include<conio.h>
void main()
{
double n,sum=0,i;
clrscr();
printf("\n Please Give The Value of N: ");
scanf("%lf",&n);
for(i=1;i<=n;i++)
{
sum = sum + (1/i);
if(i==1)
printf("\n 1 +");
else if(i==n)
printf(" (1/%.lf) ",i);
else
printf(" (1/%.lf) + ",i);
}
printf("\t = %.2lf",sum);
getch();
}
Output:
Please Give The Value of N: 3
1 + (1/2) + (1/3) + = 1.83
Please Give The Value of N: 5
1 + (1/2) + (1/3) + (1/4) + (1/5) = 2.28
=====================================================================
(C) sum= 1+4+27+268+....+N.
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int n,i,sum=0,x;
clrscr();
printf("\n\t Enter Number::");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
X=pow(i,i);
printf("%d +",x);
sum=sum+x;
}
printf("\t=%d",sum);
getch();
}
Output:
Enter Number::3
1 + 4+ 27+ =32
Enter Number::5
1 +4 +27 +256 +3125 + =3413
=====================================================================
(d) sum=1! + 2! + 3! + 4! + 5! + .... +N!
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,sum=0,fact=1;
clrscr();
printf("\n\t Enter Number::");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
fact=fact*i;
printf("%d! +",i);
sum=sum+fact;
}
printf("\t=%d",sum);
getch();
}
Output:
Enter Number::3
1! +2! +3! + =9
Enter Number::5
1! +2! +3! +4! +5! + =153
=====================================================================
(e) sum= X + X^2 + X^3 +X^4 + .... + X^N
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int X,N,i,sum=0;
clrscr();
printf("\n\t Enter value of X:: ");
scanf("%d",&X);
printf("\n\t Enter Number::");
scanf("%d",&N);
for(i=1;i<=N;i++)
{
sum=sum+pow(X,i);
printf("\t");
printf("%d^%d +",X,i);
}
printf("\t=%d",sum);
getch();
}
Output:
Enter value of X:: 2
Enter Number::3
2^1 + 2^2 + 2^3 + = 14
Enter value of X:: 3
Enter Number::7
3^1 + 3^2 + 3^3 + 3^4 + 3^5 + 3^6 + 3^7 + =3279
=====================================================================
(f) sum= - 1 + 2 - 3 + 4 – 5 + …. + N
#include<stdio.h>
#include<conio.h>
void main()
{
int n,sum=0,i;
clrscr();
printf("\n Enter the Value of N:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
if(i%2==1)
{
printf("- %d ",i);
sum = sum-i;
}
else
{
printf("+ %d ",i);
sum=sum+i;
}
}
printf("\t = %d",sum);
getch();
}
Output:
Enter the Value of N:5
- 1 + 2 - 3 + 4 - 5 = -3
Enter the Value of N:10
- 1 + 2 - 3 + 4 - 5 + 6 - 7 + 8 - 9 + 10 = 5
=====================================================================
(3) Write a program for use of putchar( ) and getchar( ) function.
#include <stdio.h>
int main( )
{
int c;
printf( "Enter a value :");
c = getchar( );
printf( "\nYou entered: ");
putchar( c );
return 0;
}
Output:
Enter a value : 5
You entered: 5
Enter a value : rahul
You entered: r
Enter a value : 345
You entered: 3
=====================================================================
1. Write a program to print Fibonacci series. 0,1,1,2,3,5,……N
#include<stdio.h>
#include<conio.h>
void main()
{
int n1=0,n2=1,n,i,n3;
clrscr();
printf("enter n=");
scanf("%d",&n);
printf(" %d %d",n1,n2);
for (i=2;i<n;i++)
{
n3=n1+n2;
printf(" %d ",n3);
n1=n2;
n2=n3;
}
getch();
}
Output:
Enter n=5
0 1 1 2 3
Enter n=8
0 1 1 2 3 5 8 13
=====================================================================
2. Write a program to reverse the digit using while loop.
#include<stdio.h>
#include<conio.h>
void main()
{
int num,rev=0,r;
clrscr();
printf("\n\t Enter Number::");
scanf("%d",&num);
while (num>0)
{
r=num%10;
rev=(rev*10) +r;
num=num/10;
}
printf("\n\t Reverse Number=%d",rev);
getch();
}
Output:
Enter Number: 153
Reverse Number=351
Enter Number: 8697
Reverse Number=7968
=====================================================================
3. Write a program to check whether given number is Armstrong or not.
(eg. 153= (1)3 + (5)3 + (3)3=153. i.e sum of the cubes of its digits is equal to the number itself.)
#include <stdio.h>
#include <conio.h>
void main()
{
int n, n1, rem, num=0;
clrscr();
printf("\n\t Enter a Positive Number:: ");
scanf("%d", &n);
n1=n;
while(n1!=0)
{
rem=n1%10;
num=num+(rem*rem*rem);
n1=n1/10;
}
if(num==n)
{
printf("\n\t %d is an Armstrong Number.", n);
}
else
{
printf("\n\t %d is not an Armstrong Number.",n);
}
getch();
}
Output:
Enter a PositiveNumber: 371
371 is an Armstrong Number.
Enter a PositiveNumber: 151
151 is not an Armstrong Number.
=====================================================================
4. Write a program to Print the Number of Odd & Even Numbers in an Array.
#include <stdio.h>
#include <conio.h>
void main()
{
int a[50], i, num;
clrscr();
printf("\n\t Enter the size of an array:");
scanf("%d", &num);
printf("\n\t Enter the elements of the array: \n");
for (i =0; i<num; i++)
{
scanf("%d", &a[i]);
}
printf("\n\t Even numbers in the array are - ");
for (i = 0; i < num; i++)
{
if (a[i] % 2 == 0)
{
printf("%d \t", a[i]);
}
}
printf("\n Odd numbers in the array are -");
for (i = 0; i < num; i++)
{
if (a[i] % 2 != 0)
{
printf("%d \t", a[i]);
}
}
}
Output:
Enter the size of an array 6
Enter the elements of the array 12 19 45 69 98 23
Even numbers in the array are - 12 98
Odd numbers in the array are - 19 45 69 23
=====================================================================
5. Write a program to check whether given number is Palindrome or not.
#include<stdio.h>
#include<conio.h>
void main()
{
long int num,rev=0,r,temp;
clrscr();
printf("\n\t Enter Number ::");
scanf("%ld",&num);
temp=num;
while (num>0)
{
r=num%10;
rev=rev*10+r;
num=num/10;
}
if(rev==temp)
{
printf("\n\t No. is Palindrome");
}
else
{
printf("\n\t No. is not Palindrome");
}
getch();
}
Output:
Enter Number: 121
No. is Palindrome.
Enter Number: 549
No. is not Palindrome.
=====================================================================
6. Write a program to display all prime numbers between Two interval entered by user.
#include<stdio.h>
#include<conio.h>
void main()
{
int n1, n2,i, j, flag;
clrscr();
printf("\n\t Enter two numbers(intervals): ");
scanf("%d %d",&n1,&n2);
printf("\n\t Prime numbers between %d and %d are: ", n1, n2);
for(i=n1;i<=n2;++i)
{
flag=0;
for(j=2; j<i;++j)
{
if(i%j==0)
{
flag=1;
break;
}
}
if(flag==0)
printf("%d ",i);
}
getch();
}
Output:
Enter two numbers(intervals): 20 50
Prime numbers between 20 and 50 are: 23 29 31 37 41 43 47
Enter two numbers(intervals): 10 100
Prime numbers between 10 and 100 are: 11 13 17 19 23 29 31 37 41 43 47
53 59 61 67 71 73 79 83 89 97
=====================================================================
7. Write a c program to print Floyd’s triangle.
#include<stdio.h>
#include<conio.h>
void main()
{
int i, j, r, k=1;
clrscr();
printf("Enter the range: ");
scanf("%d",&r);
printf("FLOYD'S TRIANGLE\n\n");
for(i=1;i<=r;i++)
{
for(j=1;j<=i;j++,k++)
{
printf(" %d",k);
}
printf("\n");
}
getch();
}
Output:
Enter the range: 6
FLOYD'S TRIANGLE
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
16 17 18 19 20 21
=====================================================================
8. Write a c program to print Pascal triangle.
#include<stdio.h>
#include<conio.h>
void main()
{
int n, c=1,space,i,j;
clrscr();
printf("Enter number of rows: ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
for(space=1;space<=n-i; space++)
{
printf(" ");
}
for(j=0;j<=i;j++)
{
if (j==0||i==0)
{
c=1;
}
else
{
c=c*(i-j+1)/j;
}
printf("%4d",c);
}
printf("\n");
}
getch();
}
Output:
Enter number of rows: 5
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
=====================================================================
1. Write a program to add two matrixes.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[2][2], b[2][2], c[2][2],i,j;
clrscr();
printf("Enter the elements of 1st matrix::\n");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
scanf("%d”,&a[i][j]);
}
}
printf("Enter the elements of 2nd matrix::\n");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
scanf("%d",&b[i][j]);
}
}
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
c[i][j]=a[i][j]+b[i][j];
}
}
printf("\nSum of Matrix:");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
printf("%.d\t",c[i][j]);
}
printf(“\n”);
}
getch();
}
Output:
Enter the elements of 1st matrix::
2 3
4 5
Enter the elements of 2nd matrix::
2 3
4 5
Sum of Matrix:
4 6
8 10
Enter the elements of 1st matrix::
-2 -3
4 5
Enter the elements of 2nd matrix::
2 3
-4 -5
Sum of Matrix:
0 0
0 0
=====================================================================
2. Write a program to given no in ascending order.
#include <stdio.h>
#include<conio.h>
void main()
{
int i, j, temp, n, no[30];
clrscr();
printf("\n\t Enter the value of N::");
scanf("%d", &n);
printf("\n\t Enter the numbers:: \n");
for(i = 0; i< n; i++)
{
scanf("%d", &no[i]);
}
for(i =0; i<n; i++)
{
for(j = i+1; j< n; j++)
{
if (no[i] > no[j])
{
temp = no[i];
no[i] = no[j];
no[j] = temp;
}
}
}
printf("\n\t Ascending order list are given below:: \n");
for(i =0; i< n; i++)
{
printf("%d\n", no[i]);
}
getch();
}
Output:
Enter the value of N::6
Enter the numbers:
3
90
78
456
780
200
The numbers arranged in ascending order are given below:
3
78
90
200
456
780
Enter the value of N::5
Enter the numbers:
30
-7
0
56
-20
The numbers arranged in ascending order are given below:
-20
-7
0
30
56
=====================================================================
3. Write a program to read array of integers and print it in reverse order.
#include <stdio.h>
#include<conio.h>
void main()
{
int n, i, j, a[100], b[100];
clrscr();
printf("\n\t Enter the number of elements in array::\n");
scanf("%d", &n);
printf("\n\t Enter the array elements::\n");
for(i =0; i< n; i++)
{
scanf("%d", &a[i]);
}
for(i =n-1, j=0; i>=0;i--,j++)
{
b[j] = a[i];
}
printf("\n\t Reverse array is::\n");
for (i =0; i<n; i++)
{
printf("%d\n", b[i]);
}
getch();
}
Output:
Enter the number of elements in array: 5
Enter the array elements:
4
8
45
125
4789
Reverse array is:
4789
125
45
8
4
=====================================================================
4. Write a Program to Find the Largest Number in an Array.
#include <stdio.h>
#include <conio.h>
void main()
{
int a[50], n, i, max;
printf("\n Enter the size of the array: ");
scanf("%d", &n);
printf("\n Enter %d elements of the array: \n", n);
for (i = 0; i<n; i++)
{
scanf("%d", &a[i]);
}
max = a[0];
for (i=1; i<n; i++)
{
if (max< a[i])
max = a[i];
}
printf("\n Largest element present in the given array is : %d", max);
getch();
}
Output:
Enter the size of the array: 5
Enter 5 elements of the array:
12
56
100
78
34
Largest element present in the given array is: 100
=====================================================================
5. Write a program to find transpose of a Matrix.
#include <stdio.h>
#include <conio.h>
void main()
{
int a[10][10], trans[10][10], r, c, i, j;
clrscr();
printf("\n\t Enter rows and column of matrix: ");
scanf("%d %d", &r, &c);
printf("\n\t Enter elements of matrix:\n");
for(i=0; i<r; i++)
{
for(j=0; j<c; j++)
{
scanf("%d",&a[i][j]);
}
}
for(i=0; i<r; i++)
{
for(j=0; j<c; j++)
{
trans[j][i]=a[i][j];
}
}
printf("\nTranspose of Matrix:\n");
for(i=0; i<c; i++)
{
for(j=0; j<r; j++)
{
printf("%d ",trans[i][j]);
}
printf("\n");
}
getch();
}
Output:
Enter rows and column of matrix: 2 3
Enter elements of matrix:
1 2 3
4 5 6
Transpose of Matrix:
1 4
2 5
3 6
Enter rows and column of matrix: 3 3
Enter elements of matrix:
1 2 3
4 5 6
7 8 9
Transpose of Matrix:
1 4 7
2 5 8
3 6 9
=====================================================================
6. Write a Program to Search an element in Array.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[30], ser, num, i;
clrscr();
printf("\n Enter no of elements:");
scanf("%d", &num);
printf("\n Enter the values :");
for (i=0; i<num; i++)
{
scanf("%d", &a[i]);
}
printf("\n Enter the elements to be searched:");
scanf("%d", &ser);
i = 0;
while (i<num&&ele != a[i])
{
i++;
}
if (i<num)
{
printf("Number found at the location = %d", i + 1);
}
else
{
printf("Number not found");
}
getch();
}
Output:
Enter no of elements: 5
11 22 33 44 55
Enter the elements to be searched: 44
Number found at the location = 4
=====================================================================
7. Write a Program to insert an element in Array.
#include <stdio.h>
#include <conio.h>
void main()
{
int a[100], pos, c, n, value;
clrscr();
printf("\n\t Enter number of elements in array:");
scanf("%d", &n);
printf("\n\t Enter %d elements:\n", n);
for (c = 0; c < n; c++)
{
scanf("%d", &a[c]);
}
printf("\n\t Enter the location where you wish to insert an element:");
scanf("%d", &pos);
printf("\n\t Enter the value to insert:\n");
scanf("%d", &value);
for (c = n - 1; c >= position - 1; c--)
{
a[c+1] = array[c];
}
a[pos-1] = value;
printf("\n\t Resultant array is:\n");
for (c=0;c<= n;c++)
{
printf("%d\n", array[c]);
}
getch();
}
Output:
Enter number of elements in array: 5
Enter 5 elements:
29
489
55
301
8
Enter the location where you wish to insert an element: 4
Enter the value to insert: 10
Resultant array is:
29
489
55
10
301
8
=====================================================================
8. Write a Program to delete an element from specified location from array.
#include<stdio.h>
#include<conio.h>
void main()
{
int arr[30], num, i, loc;
clrscr();
printf("\n Enter no of elements :");
scanf("%d", &num);
printf("\n Enter %d elements :\n", num);
for (i = 0; i<num; i++)
{
scanf("%d", &arr[i]);
}
printf("\n Location of the element to be deleted :");
scanf("%d", &loc);
while (loc<num)
{
arr[loc - 1] = arr[loc];
loc++;
}
num--;
printf(“\n After deleting the array element:\n”);
for (i =0; i<num; i++)
{
printf("\n %d", arr[i]);
}
getch();
}
Output:
Enter no of elements: 5
Enter 5 elements:
12
20
0
8
55
Location of the element to be deleted: 3
After deleting the array element:
12
20
-8
55
=====================================================================
9. Write a Program to find multiplication of 2D array.
#include<stdio.h>
# include<conio.h> void main() {
int a[10][10], b[10][10], c[10][10], i, j, k, sum = 0;
clrscr(); printf("\n Enter First Matrix :\n");
for(i=0; i<3; i++)
{ for (j = 0; j < 3; j++) {
scanf("%d", &a[i][j]); }
} printf("\n Enter Second Matrix:\n"); for (i=0; i <3; i++) {
for (j = 0; j < 3; j++)
{ scanf("%d", &b[i][j]);
} }
printf("\n\t The First Matrix is: \n");
for (i = 0; i<3; i++)
{ for (j=0; j<3; j++) {
printf(" %d ", a[i][j]); }
printf("\n"); }
printf("\n\t The Second Matrix is : \n");
for (i=0; i<3; i++)
{ for (j=0; j<3; j++) {
printf(" %d ", b[i][j]); }
printf("\n"); }
for (i=0; i<=2; i++)
{ for (j=0; j<=2; j++) {
sum = 0; for (k=0; k<=2; k++) {
sum=sum + a[i][k] * b[k][j]; }
c[i][j]=sum; }
} printf("\n Multiplication of Two Matrices is: \n"); for (i=0; i<3; i++) {
for (j=0; j<3; j++)
{ printf(“%d ", c[i][j]);
} printf("\n");
} getch(); }
Output:
Enter First Matrix:
1 2 3
4 5 6
7 8 9
Enter Second Matrix:
1 2 3
4 5 6
7 8 9
The First Matrix is:
1 2 3
4 5 6
7 8 9
The Second Matrix is:
1 2 3
4 5 6
7 8 9
Multiplication of Two Matrices is:
30 36 42
66 81 96
102 126 150
=====================================================================
1. Write a program to count total words in text.
#include <stdio.h>
#include <conio.h>
#include <string.h>
void main()
{
char s[200];
int count = 0, i;
clrscr();
printf("\n\t Enter the string::");
scanf("%[^\n]s", s); /* used gets(s) for input string */
for (i =0;s[i]!= '\0';i++)
{
if (s[i] == ' ')
count++;
}
printf ("\n\t Number of words in given string is: %d\n", count +1);
getch();
}
Output:
Enter the string::LCS, COMPUTER
Number of words in given string is: 2
Enter the string::hi i am student. lcs,mogar anand
Number of words in given string is: 6
=====================================================================
2. Find length of string using strlen( ) function.
#include<stdio.h>
#include<conio.h>
void main()
{
char str[30];
int i,len;
clrscr();
printf("\n\t Enter String:");
gets(str);
len= strlen(str)
printf("\n\t Length of %s is %d",str,len);
getch();
}
Output:
Enter String:CSE Department
Length of CSE Department is 14
Enter String: LCS,COMPUTER
Length of LCS,COMPUTER is 13
=====================================================================
3. Write a program to copy one string to another string.
#include <stdio.h>
#include <conio.h>
#include <string.h>
void main()
{
char s1[100], s2[100], i;
clrscr();
printf("\n\t Enter String s1: ");
gets(s1);
for(i=0; s1[i]!='\0'; ++i)
{
s2[i]=s1[i];
}
s2[i]='\0';
printf("\n\t String s2 is: %s",s2);
getch();
}
Output:
Enter String s1: LCS
String s2 is: LCS
Enter String s1: CSE Department
String s2 is: CSE Department
Enter String s1: Electrical department
String s2 is: Electrical department
=====================================================================
4. Write a program to join two strings.
#include <stdio.h>
#include <string.h>
#include <conio.h>
void main()
{
char s1[100], s2[100], i, j;
printf("\n\t Enter first string: ");
gets(s1);
printf("\n\t Enter second string: ");
gets(s2);
for(i=0; s1[i]!='\0'; ++i); /* i contains length of string s1. */
for(j=0; s2[j]!='\0'; ++j, ++i)
{
s1[i]=s2[j];
}
s1[i]='\0';
printf("\n\t After concatenation: %s",s1);
getch();
}
Output:
Enter first string: bhupendra
Enter second string: LCS,COMPUTER
After concatenation: bhupendraLCS,COMPUTER
Enter first string: LCS
Enter second string: COMPUTER
After concatenation: LCSCOMPUTER
=====================================================================
5. Write a program convert character into Toggle character.
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char str[100];
int i;
clrscr();
printf("\n\t Enter a string: ");
gets(str);
for(i=0;str[i]!=NULL;i++)
{
if(str[i]>='A'&& str[i]<='Z')
str[i]+=32;
else if(str[i]>='a'&&str[i]<='z')
str[i]-=32;
}
printf("\n\t String in toggle case is: %s",str);
getch();
}
Output:
Enter a string: Rahul
String in toggle case is: rAHUL
Enter a string: raj PATEL
String in toggle case is: RAJ patel
=====================================================================
6. Find given string is palindrome or not using string library function.
#include <stdio.h>
#include <string.h>
void main()
{
char str[25], rev_str[25] = {'\0'};
int i, len = 0, flag = 0;
printf("\n\t Enter a string:");
gets(str);
for (i = 0; str[i] != '\0'; i++)
{
len++;
}
printf("The length of the string '%s' = %d\n", str, len);
for (i = len - 1; i >= 0 ; i--)
{
rev_str[len - i - 1] = str[i];
}
for (flag =1, i = 0; i<len ; i++)
{
if (rev_str[i] != str[i])
flag = 0;
}
if(flag == 1)
printf ("%s is a palindrome \n", str);
else
printf("%s is not a palindrome \n”, str);
getch();
}
Output:
Enter a string: mam
The length of the string 'mam' = 3
mam is a palindrome
Enter a string: LCS
The length of the string 'LCS' = 3
LCS is a not palindrome
=====================================================================
7. Write a Menu driven program for string function operation in C.
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int n,s;
char str1[20],str2[20];
clrscr();
do
{
printf("\n*********************************************************");
printf("\n\t S T R I N G M E N U" );
printf("\n*********************************************************");
printf("\n1)Length of the string \n2)concatenate the string \n3)reverse the string \n4)copy string \n5)compare string with case sensitive \n6)compare string without case sensitive \n7)string uppercase \n8)string lowercase \n9)exit");
printf("\n*********************************************************");
printf("\n\t Enter your choice:");
fflush(stdin);
scanf("%d",&n);
printf("\n ********************************************************");
switch(n)
{
case 1:
printf("\n\t Enter the First string:");
fflush(stdin);
gets(str1);
printf("\n\t string length is:%d",strlen(str1));
break;
case 2:
printf("\n\t Enter the First string:");
fflush(stdin);
gets(str1);
printf("\n\t Enter the second string:");
fflush(stdin);
gets(str2);
printf("\n\t The concatenated string is: %s",strcat(str1,str2));
break;
case 3:
printf("\n\t Enter the First string:");
fflush(stdin);
gets(str1);
printf("\n\t The reverse of the string is:%s",strrev(str1));
break;
case 4:
printf("\n\t Enter the First string:");
fflush(stdin);
gets(str1);
printf("\n\t Enter the second string:");
fflush(stdin);
gets(str2);
printf("\n\t The Copy string is:%s",strcpy(str1,str2));
break;
case 5:
printf("\n\t Enter the First string:");
fflush(stdin);
gets(str1);
printf("\n\t Enter the second string:");
fflush(stdin);
gets(str2);
s=strcmp(str1,str2);
if(s==0)
{
printf("\n\t The string is same.");
}
else
{
printf("\n\t The string is not same.");
}
break;
case 6:
printf("\n\t Enter the First string:");
fflush(stdin);
gets(str1);
printf("\n\t Enter the second string:");
fflush(stdin);
gets(str2);
s=strcmpi(str1,str2);
if(s==0)
{
printf("\n\t The string is same.");
}
else
{
printf("\n\t The string is not same.");
}
break;
case 7:
printf("\n\t Enter the First string:");
fflush(stdin);
gets(str1);
printf("\n\t The upper case string is:%s",strupr(str1));
break;
case 8:
printf("\n\t Enter the First string:");
fflush(stdin);
gets(str1);
printf("\n\t The Lower case string is:%s",strlwr(str1));
break;
case 9:
exit(0);
}
}
while(n!=0);
getch();
}
Output:
***************************************************************
S T R I N G M E N U
***************************************************************
1)Length of the string
2)concatenate the string
3)reverse the string
4)copy string
5)compare string with case sensitive
6)compare string without case sensitive
7)string uppercase
8)string lowercase
9)exit
***************************************************************
Enter your choice:1
***************************************************************
Enter the First string:bhupendra patel
string length is:11
***************************************************************
S T R I N G M E N U
***************************************************************
1)Length of the string
2)concatenate the string
3)reverse the string
4)copy string
5)compare string with case sensitive
6)compare string without case sensitive
7)string uppercase
8)string lowercase
9)exit
Enter your choice:2
***************************************************************
Enter the First string:bhupendra
Enter the second string:patel
The concatenated string is: bhupendrapatel
***************************************************************
S T R I N G M E N U
***************************************************************
1)Length of the string
2)concatenate the string
3)reverse the string
4)copy string
5)compare string with case sensitive
6)compare string without case sensitive
7)string uppercase
8)string lowercase
9)exit
***************************************************************
Enter your choice:3
***************************************************************
Enter the First string:bhupendra
The reverse of the string is:ardnepuhb
***************************************************************
S T R I N G M E N U
***************************************************************
1)Length of the string
2)concatenate the string
3)reverse the string
4)copy string
5)compare string with case sensitive
6)compare string without case sensitive
7)string uppercase
8)string lowercase
9)exit
***************************************************************
Enter your choice:4
***************************************************************
Enter the First string:bhupendra
Enter the second string:patel
The Copy string is:patel
***************************************************************
S T R I N G M E N U
***************************************************************
1)Length of the string
2)concatenate the string
3)reverse the string
4)copy string
5)compare string with case sensitive
6)compare string without case sensitive
7)string uppercase
8)string lowercase
9)exit
***************************************************************
Enter your choice:5
***************************************************************
Enter the First string:bhupendra patel
Enter the second string:BHUPENDRA PATEL
The string is not same.
***************************************************************
S T R I N G M E N U
***************************************************************
1)Length of the string
2)concatenate the string
3)reverse the string
4)copy string
5)compare string with case sensitive
6)compare string without case sensitive
7)string uppercase
8)string lowercase
9)exit
***************************************************************
Enter your choice:5
***************************************************************
Enter the First string:Bhupendra Patel
Enter the second string:Bhupendra Patel
The string is same.
***************************************************************
S T R I N G M E N U
***************************************************************
1)Length of the string
2)concatenate the string
3)reverse the string
4)copy string
5)compare string with case sensitive
6)compare string without case sensitive
7)string uppercase
8)string lowercase
9)exit
***************************************************************
Enter your choice:6
***************************************************************
Enter the First string:bhupendra patel
Enter the second string:Bhupendra Patel
The string is same.
***************************************************************
S T R I N G M E N U
***************************************************************
1)Length of the string
2)concatenate the string
3)reverse the string
4)copy string
5)compare string with case sensitive
6)compare string without case sensitive
7)string uppercase
8)string lowercase
9)exit
***************************************************************
Enter your choice:7
***************************************************************
Enter the First string:bhupendra Patel
The upper case string is:BHUPENDRA PATEL
***************************************************************
S T R I N G M E N U
***************************************************************
1)Length of the string
2)concatenate the string
3)reverse the string
4)copy string
5)compare string with case sensitive
6)compare string without case sensitive
7)string uppercase
8)string lowercase
9)exit
***************************************************************
Enter your choice:8
***************************************************************
Enter the First string:BHUPENDRA paTEL
The Lower case string is:bhupendra patel
***************************************************************
S T R I N G M E N U
***************************************************************
1)Length of the string
2)concatenate the string
3)reverse the string
4)copy string
5)compare string with case sensitive
6)compare string without case sensitive
7)string uppercase
8)string lowercase
9)exit
***************************************************************
Enter your choice:9
=====================================================================
1. Write a function program to add first N numbers.
#include <stdio.h>
#include <conio.h>
int add (int x)
{
int sum=0,i=1;
for(i=1;i<=x;i++)
{
sum=sum+i;
}
return sum;
}
void main ()
{
int no, sum=0;
clrscr();
printf("\n\t Please Enter the No:");
scanf("%d",&no);
sum=add(no);
printf("\n\t Sum up to given number is:%d",sum);
getch();
}
Output:
Please Enter the No:5
Sum up to given number is:15
Please Enter the No:10
Sum up to given number is:55
=====================================================================
2. Write a function find out maximum out of three numbers.
#include <stdio.h>
#include <conio.h>
int maximum(int x, int y, int z)
{
int max = x;
if (y > max)
{
max = y;
}
if (z > max)
{
max = z;
}
return max;
}
void main ()
{
int i, j, k, l;
clrscr();
i = 10;
j = 20;
k=30;
l = maximum(i, j,k);
printf("\n\t The value of k is=%d\n", l);
getch();
}
Output:
The value of k is=30
=====================================================================
3. Write a program to find factorial of a number using recursion.
#include<stdio.h> #include<conio.h>
int fact(int);
int main()
{
int num, f;
clrscr();
printf("\n Enter a number: ");
scanf("%d", &num);
f=fact(num);
printf("\n Factorial of %d is: %d", num, f);
return 0;
}
int fact(int n)
{
if(n==1)
return 1;
else
return(n*fact(n-1));
}
Output:
Enter a number:5
Factorial of 5 is=120
Enter a number: 10
Factorial of 5 is= 24320
=====================================================================
4. Write a program that used user defined function Swap ( ) and interchange the value of two variable.
#include <stdio.h>
#include <conio.h>
void swap(float *ptr1, float *ptr2);
void main()
{
float m, n;
clrscr();
printf("\n\t Enter the values of M and N: \n");
scanf("%f %f", &m, &n);
printf("\n\t Before Swapping: M = %.f N= %.f \n", m, n);
swap(&m, &n);
printf("\n\t After Swapping: M = %.f N= %.f \n", m, n);
}
void swap(float *ptr1, float *ptr2)
{
float temp;
temp = *ptr1;
*ptr1 = *ptr2;
*ptr2 = temp;
}
Output:
Enter the Value of M and N:
10 20
Before Swapping M=10 N=20
After Swapping M=20 N=10
=====================================================================
5. Write a function prime that return 1 if it‘s argument is prime and return 0 otherwise.
#include<stdio.h>
#include<conio.h>
int prime(int);
int main()
{
int n,p;
clrscr();
printf("\n\t Enter a number : ");
scanf("%d",&n);
p=prime(n);
if(p==1)
printf("\n\t %d is prime\n",n);
else
printf("\n\t%d is not prime\n",n);
getch();
return 0;
}
int prime(int n)
{
int i;
for(i=2;i<n;i++)
{
if(n%i==0)
return 0;
}
return 1;
}
Output:
Enter a number:3
3 is prime
Enter a number:33
33 is not prime
=====================================================================
1. Define a structure type, personal, that would contain person name, date of joining and salary. Using this structure, write a program to read this information for one person from the key board and print the same on the screen.
#include<stdio.h>
#include<conio.h>
#include<string.h>
struct person
{
int recno;
char name[20];
char doj[20];
int salary;
};
void main()
{
struct person p;
clrscr();
printf("\n\t Please Enter Name::");
gets(p.name);
printf("\n\t Please Enter Date of Joining::");
gets(p.doj);
printf("\n\t Please Enter Salary::");
scanf("%d",&p.salary);
printf("\n\t Persons Information IS");
printf("\n\t ***********************************************");
printf("\n\t Name:%s \t DOJ:%s \t salary:%d ",p.name, p.doj, p.salary);
getch();
}
Output:
Please Enter Name::Jasmin
Please Enter Date of Joining::1/12/2014
Please Enter Salary:20000
Personal Information
***************************************************
Name:Mayur
DOJ:01/02/2015
Salary: 25000
=====================================================================
2. Define a structure called cricket that will describe the following information:
a. Player name
b. Team name
c. Batting average
#include<stdio.h>
#include<conio.h>
#include<string.h>
struct cricket
{
char nm[20],team[20];
int avg;
};
#define total 2
int main()
{
struct cricket player[total],temp;
int i,j;
clrscr();
for(i=0;i<total;i++)
{
printf("\n\t For player: %d\n",i+1);
printf("\n\t Enter the name of player :");
fflush(stdin);
gets(player[i].nm);
printf("\n\t Enter the team : ");
fflush(stdin);
gets(player[i].team);
printf("\n\t Enter the batting average:");
fflush(stdin);
scanf("%d",&player[i].avg);
}
printf("\n\nTeam Name Average\n");
printf("\n");
for(i=0;i<total;i++)
{
printf("\t %s \t %s \t %d\n",player[i].team,player[i].nm,player[i].avg);
}
getch();
return 0;
}
Output:
Enter the name of Player:Virat
Enter Team:India
Enter Batting Average:40
Enter the name of Player:Rohit
Enter Team:India
Enter Batting Average:45
Team Name Average
India Virat 40
India Rohit 45
=====================================================================
3. Write a function to enter roll no, marks of the three subjects for 3 student and find total obtained by each student.
#include<stdio.h>
#include<conio.h>
void main()
{
struct student
{
int rollno;
char name[20];
int m1,m2,m3;
};
struct student s[20],t;
int i,j,n;
clrscr();
printf("\n Enter the Limit:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\n Enter the roll no:\n");
scanf("%d",&s[i].rollno);
printf("\n Enter the name: \n");
scanf("%s",s[i].name);
printf("\n Enter mark1=");
scanf("%d",&s[i].m1);
printf("\n Enter mark2=");
scanf("%d",&s[i].m2);
printf("\n Enter mark3=");
scanf("%d",&s[i].m3);
}
printf(“\n”);
for(i=0;i<n;i++)
{
printf("\n Rollno=%d",s[i].rollno);
printf("\n Name=%s",s[i].name);
printf("\n mark1=%d",s[i].m1);
printf("\n mark2=%d",s[i].m2);
printf("\n mark3=%d",s[i].m3);
}
}
Output:
Enter the Limit: 3
Enter the roll No:1
Enter the Name: Mayur
Enter mark 1:50
Enter mark 2:50
Enter mark 3:50
Enter the roll No:2
Enter the Name:Sandip
Enter mark 1:50
Enter mark 2:60
Enter mark 3:70
Enter the roll No:3
Enter the Name:Sanket
Enter mark 1:50
Enter mark 2:60
Enter mark 3:70
Roll no:1
Name:Mayur
mark 1:50
mark 2:50
mark 3:50
Roll no:2
Name:Sandip
mark 1:50
mark 2:60
mark 3:70
Roll no:3
Name:Sanket
mark 1:50
mark 2:60
mark 3:70
=====================================================================
1. Write a program using pointer to read an array of integer and print element in reverse order.
#include<stdio.h>
#include<conio.h>
void main()
{
int *ptr, i , n;
clrscr();
printf(“Enter the no of elements:”);
scanf(“%d”,&n);
for(i=0; i<n; i++)
{
printf(“Enter %d element :“,i+1);
scanf(“%d”,&ptr[i]);
}
printf(“Array in reverse order\n”);
for(i=n-1;i>=0; i--)
{
printf(“%d\n”,ptr[i]);
}
getch();
}
Output:
Enter the no of elements:5
Enter 1 element :11
Enter 2 element :22
Enter 3 element :-99
Enter 4 element :0
Enter 5 element :55
Array in reverse order
55
0
-99
22
11
=====================================================================
2. Write a program using pointer to compare two strings.
#include<conio.h>
#include<stdio.h>
int compare_string(char*, char*);
main()
{
char first[100], second[100], result;
clrscr();
printf("\n\t Enter first string::");
gets(first);
printf("\n\t Enter second string::");
gets(second);
result = compare_string(first, second);
if ( result == 0 )
printf("Both strings are same.\n");
else
printf("Both strings are not equal.\n");
return 0;
}
int compare_string(char *first, char *second)
{
while(*first==*second)
{
if ( *first == '\0' || *second == '\0' )
break;
first++;
second++;
}
if( *first == '\0' && *second == '\0' )
return 0;
else
return 1;
}
Output:
Enter first string:: shurabh
Enter second string:: shurabh
Both strings are same.
Enter first string:: Raj Patel
Enter second string::raj patel
Both strings are not equal.
=====================================================================
3. Write a program using pointer and function to determine the length of string.
#include<stdio.h>
#include<conio.h>
int string_len(char*);
void main()
{
char str[20];
int len;
clrscr();
printf("\n\t Enter any string:");
gets(str);
len=string_len(str);
printf("\n\t The length of the given string %s is :%d", str, len);
}
int string_len(char*p)
{
int count=0;
while(*p!='\0’)
{
count++;
p++;
}
return count;
}
Output:
Enter any string:harsh patel
The length of the given string harsh patel is :11
Enter any string:LCS, COMPUTER
The length of the given string LCS, COMPUTER is :13
=====================================================================
4. Write a program using pointer to copy one string to another string.
#include<stdio.h>
#include<conio.h>
void main()
{
char a[80],b[80],*pa,*pb;
clrscr();
printf("\n\t Given first string::");
scanf("%s",a);
pa=&a[0];
pb=&b[0];
while(*pa!='\0')
{
*pb=*pa;
pa++;
pb++;
}
*pb='\0';
printf("\n\t Copied string is::");
puts(b);
getch();
}
Output:
Given first string::mit shah
Copied string is::mit
Given first string::krunalgohel
Copied string is::krunalgohel
=====================================================================
5. Write a program using pointer to concate two strings.
#include<stdio.h>
#include<conio.h>
int main()
{
int i=0,j=0;
char *str1,*str2,*str3;
puts("\n\t Enter first string::");
gets(str1);
puts("\n\t Enter second string::");
gets(str2);
printf("\n\t Before concatenation the strings are::");
puts(str1);
puts(str2);
while(*str1)
{
str3[i++]=*str1++;
}
while(*str2)
{
str3[i++]=*str2++;
}
str3[i]='\0';
printf("\n\t After concatenation the strings are::");
puts(str3);
return 0;
}
Output:
Enter first string:: bhupendra
Enter second string::patel
Before concatenation the strings are::
bhupendra
patel
After concatenation the strings are::bhupendrapatel
=====================================================================
6. Write a program that uses a table of integers whose size will be specified interactively at runtime.
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
int *p, *table;
int size;
clrscr();
printf("\n What is the size of table?::");
scanf("%d",&size);
printf("\n");
if((table = (int*)malloc(size *sizeof(int))) == 0)
{
printf("No space available \n");
exit(1);
}
printf("\n Address of the first byte is:: %u\n", table);
/* Reading table values*/
printf("\n Input table values::\n");
for(p=table; p<table + size; p++)
{
scanf("%d", p);
}
for(p=table + size-1;p >= table; p--)
{
printf("%d is stored at address %u \n", *p, p);
}
getch();
}
Output:
What is the size of table?::5
Address of the first byte is:: 1974
Input table values::
11
3
0
-9
888
888 is stored at address 1982
-9 is stored at address 1980
0 is stored at address 1978
3 is stored at address 1976
11 is stored at address 1974
What is the size of table?::0
No space available
What is the size of table?::-3
No space available
=====================================================================
7. Write a program to store a character string in block of memory space created by malloc and then modify the same to store a large string.
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define NULL 0
void main()
{
char *buffer;
clrscr();
/* Allocating memory */
if((buffer = (char *)malloc(10)) == NULL)
{
printf("malloc failed.\n");
exit(1);
}
strcpy(buffer, " Rajasthan");
printf("\n Buffer contains: %s \n", buffer);
/* Realloction */
if((buffer = (char *)realloc(buffer, 15)) == NULL)
{
printf("Reallocation failed. \n");
exit(1);
}
printf("\nBuffer size modified. \n");
strcpy(buffer, " Maharashtra");
printf("\nBuffer now contains: %s \n",buffer);
/* Freeing memory */
free(buffer);
}
Output:
Buffer contains: Rajasthan
Buffer size modified.
Buffer now contains: Maharashtra
=====================================================================
1. A program to illustrate reading files contents.
#include<stdio.h>
#include<conio.h>
void main()
{
FILE *fptr;
char filename[15];
char ch;
clrscr();
printf("\n\t Enter the filename to be opened:");
scanf("%s", filename);
/* open the file for reading */
fptr = fopen(filename, "r");
if (fptr == NULL)
{
printf("Cannot open file \n");
exit(0);
}
ch = fgetc(fptr);
while (ch != EOF)
{
printf ("%c", ch);
ch = fgetc(fptr);
}
fclose(fptr);
getch();
}
Output:
Enter the filename to be opened: sample.txt
hi i am BHUPENDRA.
Enter the filename to be opened: sample1.txt
Cannot open file
=====================================================================
2. A program to illustrate the use of fgets( ).
#include <stdio.h>
#include <conio.h>
int main()
{
FILE *fp;
char str[60];
clrscr();
/* opening file for reading */
fp = fopen("sample.txt" , "r");
if(fp == NULL)
{
perror("Error opening file");
return(1);
}
if( fgets (str, 60, fp)!=NULL )
{
/* writing content to stdout */
puts(str);
}
fclose(fp);
return(0);
}
Output:
hi i am BHUPENDRA PATEL .
=====================================================================
3. A program to illustrate the use of fputc( ) and fputs( ).
/ * Program for fputs() */
#include<stdio.h>
#include<stdlib.h>
int main()
{
FILE *fptr;
char text[100];
clrscr();
printf("Enter a text:\n");
gets(text);
if((fptr = fopen("TEST.txt","w"))==NULL)
{
printf("Cannot open file\n");
exit(1);
}
fputs(text,fptr);
if(fclose(fptr))
printf("File close error\n");
getch();
return 0;
}
Output:
Enter a text:
LCS, COMPUTER
Enter a text:
LCS, CSE
=====================================================================
/* Program for fputc() */
#include <stdio.h>
#include<stdlib.h>
void main()
{
FILE *fptr;
char text[100];
int i=0;
clrscr();
printf("Enter a text:\n");
gets(text);
if((fptr = fopen("TEST.txt","w"))==NULL)
{
printf("Cannot open file\n");
exit(1);
}
while(text[i]!='\0')
fputc(text[i++],fptr);
if(fclose(fptr))
printf("File close error\n");
getch();
}
Output:
Enter a text:
LCS, CSE
Enter a text:
LCS, COMPUTER
=====================================================================
No comments:
Post a Comment