Documente Academic
Documente Profesional
Documente Cultură
#include<stdio.h>
void main()
{
printf("hello World\n");
}
Output:
hello World
#include<stdio.h>
void main()
{
int num;
printf("Enter a number");
scanf("%d",&num);
printf("Number is %d",num);
}
Output:
Enter a number4
Number is 4
Program-3: Write a program that will obtain the length and width of a rectangle from user and calculate
its area, perimeter and diagonal.
#include<stdio.h>
#include<math.h>
void main()
{
float l,b,a,p,d;
printf("Enter l & b:");
scanf("%f%f",&l,&b);
a=l*b;
p=2*(l+b);
d=sqrt((l*l)+(b*b));
printf("Area is %f\n",a);
printf("perimeter is %f\n",p);
printf("diagonal is %f\n",d);
}
Output:
Program4: The distance between two cities in KM is input through keyboard. Write a program to convert and print
this distance in meters, feet, inches, centimeters.
#include<stdio.h>
void main()
{
float km,m,cm,ft,inch;
printf("Enter km:");
scanf("%f",&km);
m=1000*km;
cm=100000*km;
ft=3281*km;
inch=39370*km;
printf("M is %f\n",m);
printf("CM is %f\n",cm);
printf("FT is %f\n",ft);
printf("INCH is %f\n",inch);
}
Output:
Enter km:5
M is 5000.000000
CM is 500000.000000
FT is 16405.000000
INCH is 196850.000000
Program5: Write a program to determine the salvage value of an item when the purchase price, years of service and the
annual depreciation are given.
#include<stdio.h>
void main()
{
float d,pp,sv;
int y;
printf("Enter the values of Purchase price,years of service and annual depreciation:\n");
scanf("%f%d%f",&pp,&y,&d);
sv=pp-d*y;
printf("Salvage value=%f\n",sv);
}
Output:
Program6: Write a program to compute the area of the triangle given the values of a,b,c.
#include<stdio.h>
#include<math.h>
void main()
{
int a,b,c,s;
float area;
printf("Enter the values of a,b & c:\n");
scanf("%d%d%d",&a,&b,&c);
s=(a+b+c)/2;
area=sqrt(s*(s-a)*(s-b)*(s-c));
printf("Area of triangle is %f\n",area);
}
Output:
#include<stdio.h>
void main()
{
float c,f;
printf("Enter celsius:");
scanf("%f",&c);
f=1.8*c+32;
printf("the value in fahrenheit is %f\n",f);
}
Output:
Enter celsius:40
the value in fahrenheit is 104.000000
Program8: Write a program to find the sum of the digits of a 3-digit integer constant.
#include<stdio.h>
void main()
{
int a,b;
printf("Please enter the three digit number");
scanf("%d",&a);
b=(a/100)+(a%10)+((a/10)%10);
printf("the sum of digits of the number is %d",b);
}
Output:
#include<stdio.h>
void main()
{
int a,b;
printf("Please enter the three digit number");
scanf("%d",&a);
b=(a/100)+((a%10)*100)+((a/10)%10)*10;
printf("reverse of the number is %d",b);
}
Output:
#include<stdio.h>
void main()
{
int x,y;
printf("Enter the value of x:");
scanf("%d",&x);
printf("Enter the value of y:");
scanf("%d",&y);
x=x+y;
y=x-y;
x=x-y;
printf("x is %d\n",x);
printf("y is %d\n",y);
}
Output:
Program11: Write a program to assign value of one variable to another using post and pre increment
operator and print the results.
#include<stdio.h>
void main()
{
int a,b,c=4;
a=++c;
b=a++;
c=++b;
printf("a=%d b=%d c=%d",a,b,c);
}
Output:
#include<stdio.h>
void main()
{
float r;
int r1,p1;
printf("Enter the amount:");
scanf("%f",&r);
r1=r;
p1=(r-r1)*100;
printf("rupees are %d & paise is are %d\n",r1,p1);
}
Output:
#include<stdio.h>
void main()
{
int m,d;
printf("Enter the no of days:");
scanf("%d",&d);
m=d/30;
d=d%30;
printf("No of months is %d months and %d days\n",m,d);
}
Output:
Program14: Write a program that reads nos. from keyboard and gives addition, subtraction, multiplication, division and
modulo.
#include<stdio.h>
void main()
{
int a,b;
printf("Enter the value of a & b:");
scanf("%d%d",&a,&b);
printf("Addition is %d+%d=%d\n",a,b,a+b);
printf("Subtraction is %d-%d=%d\n",a,b,a-b);
printf("Multiplication is %d*%d=%d\n",a,b,a*b);
printf("division is %d/%d=%d\n",a,b,a/b);
printf("Modulo is %d%%%d=%d\n",a,b,a%b);
}
Output:
#include<stdio.h>
void main()
{
int m;
printf("Enter marks:");
scanf("%d",&m);
if(m>=80&&m<100)
printf("Distinction\n");
else if(m<80&&m>=60)
printf("first class\n");
else if(m<60&&m>=35)
printf("Second class\n");
else if(m<35&&m>=0)
printf("Fail\n");
else
printf("Invalid Input\n");
}
Output:
Enter marks:75
first class
#include<stdio.h>
#include<math.h>
void main()
{
int a,b,c,D;
float x1,x2;
printf("Enter a b c:");
scanf("%d%d%d",&a,&b,&c);
D=(b*b)-(4*a*c);
if(D>0)
{
x1=(-b+sqrt(D))/(2*a);
x2=(-b-sqrt(D))/(2*a);
printf("x1=%f\tx2=%f",x1,x2);
}
else if(D==0)
{x1=x2=-b/(2*a);
printf("x1=x2=%f",x1);
}
else
{printf("Roots are imaginary");
}
}
Output:
Enter a b c:3
4
5
Roots are imaginary
#include<stdio.h>
void main()
{
int n1,n2,ch,ans;
printf("Enter the value of n1 and n2\n and enter the choice");
scanf("%d%d%d",&n1,&n2,&ch);
switch(ch)
{
case 1:ans=n1+n2;
break;
case 2:ans=n1-n2;
break;
case 3:ans=n1*n2;
break;
case 4:ans=n1/n2;
break;
case 5:ans=n1%n2;
break;
default: printf("wrong input");
}
printf("Answer is %d",ans);
}
Output:
Set-2
Program4: Find maximum and minimum of three numbers using ternary operator.
#include<stdio.h>
void main()
{
int a,b,c,max,min;
printf("Enter three numbers");
scanf("%d%d%d",&a,&b,&c);
if(a>b)
{
if(a>c)
printf("max=%d",a);
else
printf("max=%d",c);
}
else
{
if(b>c)
printf("Max=%d",b);
else
printf("Max=%d",c);
}
if(a<b)
{
if(a<c)
printf("min=%d",a);
else
printf("min=%d",c);
}
else
{
if(b<c)
printf("min=%d",b);
else
printf("min=%d",c);
}
}
Output:
#include<stdio.h>
void main()
{
int year;
printf("Enter year:\n");
scanf("%d",&year);
if(year%4==0)
printf("%d is a leap year\n",year);
else
printf("%d is not a leap year\n",year);
}
Output:
Enter year:
2018
2018 is not a leap year
Set-2
#include<stdio.h>
#include<ctype.h>
void main()
{
char b;
printf("Enter an alphabet");
putchar('\n');
b=getchar();
if(islower(b))
putchar(toupper(b));
else
putchar(tolower(b));
}
Output:
Enter an alphabet
y
Y
Set-2
Program7: Write a program to find out Net salary, HRA, DA, PI of employee according to Basic salary. Do using if and
also using switch statements.
Basic salary HRA DA
>=10000 20% 15%
>=5000 and <10000 15% 10%
<5000 10% 5%
Net salary=Basic + HRA + DA
#include<stdio.h>
void main()
{
float bs,hra,da,net;
printf("Enter the basic salary of employee");
scanf("%f",&bs);
if (bs>=10000)
{
hra=0.2*bs;
da=0.15*bs;
net=bs+hra+da;
printf("hra=%f\tda=%f\tnet=%f\t",hra,da,net);
}
else if(bs>=5000&&bs<10000)
{
hra=0.15*bs;
da=0.1*bs;
net=hra+da+bs;
printf("hra=%f\tda=%f\tnet=%f\t",hra,da,net);
}
else if(bs<5000)
{
hra=0.1*bs;
da=0.05*bs;
net=hra+da+bs;
printf("hra=%f\tda=%f\tnet=%f\t",hra,da,net);
}
else
{
printf("Invalid Input");
}}
Output:
Program8: The cost of one type of mobile service is Rs. 250 plus Rs. 1.25 for each call made over above 100 calls. Write a
program to read costumer codes and calls made and print the bill for each customer.
#include<stdio.h>
void main()
{
int n,cc;
float bill;
printf("Enter customer code and calls:");
scanf("%d%d",&cc,&n);
if(n>100)
bill=250+1.25*(n-100);
else
bill=250;
printf("Total Bill=%f",bill);
}
Output:
#include<stdio.h>
void main()
{
int n,i=1;
float sum=0.00,avg;
printf("Enter n:\n");
scanf("%d",&n);
while(i<=n)
{
printf("%d\n",i);
sum=sum+i;
i++;
}
avg=sum/n;
printf("Sum is %f",sum);
printf("Avg is %f",avg);
}
Output:
Enter n:
5
1
2
3
4
5
Sum is 15.000000Avg is 3.000000
Set-3
Program2: Write a program to print squares & cubes of 1st N natural numbers & calculate their sum & avg.
#include<stdio.h>
void main()
{
int i=1,n,s,c;
float sums=0,avgs=0,sumc=0,avgc=0;
printf("enter a number till which you want to print:");
scanf("%d",&n);
printf("number\t \t square\t \t cube \n");
while(i<=n)
{
s=i*i;
c=i*i*i;
printf("%d \t \t %d \t \t %d \n",i,s,c);
sums=sums+s;
sumc=sumc+c;
i=i+1;
}
avgs=sums/n;
avgc=sumc/n;
printf("sum \t %f \t %f \n",sums,sumc);
printf("avg \t %f \t %f \n",avgs,avgc);
}
Output:
enter a number till which you want to print:5
number square cube
1 1 1
2 4 8
3 9 27
4 16 64
5 25 125
sum 55.000000 225.000000
avg 11.000000 45.000000
Set-3
#include<stdio.h>
void main()
{
int n,i;
printf("Enter n:\n");
scanf("%d",&n);
i=-n;
while(i>=-n && i<=0)
{
printf("%d\n",i);
i++;
}
i=1;
while(i<=n)
{
printf("%d\n",i);
i++;
}
}
Output:
Enter n:
5
-5
-4
-3
-2
-1
0
1
2
3
4
5
Set-3
Program4: Write a program to print 1st N odd & even numbers & calculate their sum & avg.
#include<stdio.h>
void main()
{
int n,i,b,sum;
float avg;
printf("enter the value n:");
scanf("%d",&n);
b=0;
sum=0;
avg=1;
printf("your odd numbers are\n");
for(i=1;i<=n;i++)
{
if(i%2!=0)
{
printf("%d\n",i);
sum=sum+i;
b=b+1;
}
}
avg=sum/b;
printf("sum=%d,Avg=%f\n",sum,avg);
b=0;
sum=0;
avg=1;
printf("your even numbers are\n");
for(i=1;i<=n;i++)
{
if(i%2==0)
{
printf("%d\n",i);
sum=sum+i;
b=b+1;
}
}
avg=sum/b;
printf("sum=%d,Avg=%f\n",sum,avg);
}
Output:
enter the value n:10
your odd numbers are
1
3
5
7
9
sum=25,Avg=5.000000
your even numbers are
2
4
6
8
10
sum=30,Avg=6.000000
Set-3
Program5: Write a program to print all numbers between given two numbers x & y including x & y,& calculate their sum
& avg.
#include<stdio.h>
void main()
{
int x,y,n=0;
float avg,sum=0;
printf("enter first no.:\n");
scanf("%d",&x);
printf("enter second no.:\n");
scanf("%d",&y);
while(x<=y)
{
printf("%d\n",x);
sum=sum+x;
x++;
n++;
}
printf("sum is %f\n",sum);
avg=sum/n;
printf("avg is %f\n",avg);
}
Output:
Program6: Write a program to print all odd & even numbers between given two numbers x & y including x & y,& their
sum & avg.
#include<stdio.h>
void main()
{
int x,y,i,b,sum;
float avg;
printf("enter x:");
scanf("%d",&x);
printf("enter y:");
scanf("%d",&y);
b=0;
sum=0;
avg=1;
printf("your odd numbers are\n");
for(i=x;i<=y;i++)
{
if(i%2!=0)
{
printf("%d\n",i);
sum=sum+i;
b=b+1;
}
}
avg=sum/b;
printf("sum=%d , Avg=%f\n",sum,avg);
b=0;
sum=0;
avg=1;
printf("your even numbers are\n");
for(i=x;i<=y;i++)
{
if(i%2==0)
{
printf("%d\n",i);
sum=sum+i;
b=b+1;
}
}
avg=sum/b;
printf("sum=%d , Avg=%f\n",sum,avg);
}
Output:
enter x:10
enter y:20
your odd numbers are
11
13
15
17
19
sum=75 , Avg=15.000000
your even numbers are
10
12
14
16
18
20
sum=90 , Avg=15.000000
Set-3
Program7: Write a program to print every third number beginning from 2 untill number<100, & calculate their sum &
avg.
#include<stdio.h>
void main()
{
int i,sum=0,c=0;
float avg;
for(i=2;i<100;i=i+3)
{
printf("\nthe numbers are : %d",i);
sum+=i;
c++;
}
printf("\nthe sum is %d",sum);
avg=sum/c;
printf("\navg is %f",avg);
}
Output:
Program8: Write a program to print all numbers exactly divisible by 5 until number < 100,& calculate their sum & avg.
Use modulus operator to check divisibility.
#include<stdio.h>
void main()
{
int i=0,n=0;
float avg,sum=0;
while(i<100)
{
if(i%5==0)
{
printf("%d\t",i);
sum=sum+i;
n++;
}
i++;
}
avg=sum/n;
printf("sum is %f and avg is %f\n",sum,avg);
}
Output:
0 5 10 15 20 25 30 35 40 45 50 55 60 65 70
75 80 85 90 95 sum is 950.000000 and avg is 47.500000
#include<stdio.h>
void main()
{
int a;
a=0;
for(a=-15;a<=15;a=a+5)
{
printf("%d\t",a);
}
}
Output:
-15 -10 -5 0 5 10 15
Set-3
Program10: Write a program to print the value of the following series: -1,x,-x^2,x^3,-x^4…etc.
#include<stdio.h>
#include<math.h>
void main()
{
int x,i,n,ans;
printf("\n enter x & n\n");
scanf("%d %d",&x,&n);
for(i=0;i<=n;i++)
{
ans=pow(x,i) *pow(-1,i+1);
printf("%d\n",ans);
}
}
Output:
enter x & n
2
5
-1
2
-4
8
-16
32
#include<stdio.h>
void main()
{
int i,j;
for(i=1;i<=10;++i)
{
for(j=1;j<=10;++j)
{
printf("%d*%d=%d\t",j,i,i*j);
}
printf("\n");
}
}
Output:
1*1=1 2*1=2 3*1=3 4*1=4 5*1=5 6*1=6 7*1=7 8*1=8 9*1=9 10*1=10
1*2=2 2*2=4 3*2=6 4*2=8 5*2=10 6*2=12 7*2=14 8*2=16 9*2=18 10*2=20
1*3=3 2*3=6 3*3=9 4*3=12 5*3=15 6*3=18 7*3=21 8*3=24 9*3=27 10*3=30
1*4=4 2*4=8 3*4=12 4*4=16 5*4=20 6*4=24 7*4=28 8*4=32 9*4=36 10*4=40
1*5=5 2*5=10 3*5=15 4*5=20 5*5=25 6*5=30 7*5=35 8*5=40 9*5=45 10*5=50
1*6=6 2*6=12 3*6=18 4*6=24 5*6=30 6*6=36 7*6=42 8*6=48 9*6=54 10*6=60
1*7=7 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=49 8*7=56 9*7=63 10*7=70
1*8=8 2*8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*8=56 8*8=64 9*8=72 10*8=80
1*9=9 2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81 10*9=90
1*10=10 2*10=20 3*10=30 4*10=40 5*10=50 6*10=60 7*10=70 8*10=80 9*10=90 10*10=100
Set-3
Program12: Write a program to calculate the power of a number without using pow() function.
#include<stdio.h>
void main()
{
int base,exp;
int result=1;
printf("Enter a base number:");
scanf("%d",&base);
printf("Enter an exponent");
scanf("%d",&exp);
while(exp!=0)
{
result*=base;
--exp;
}
printf("Answer =%d",result);
}
Output:
#include<stdio.h>
void main()
{
float x,f=1;
printf("enter no.:\n");
scanf("%f",&x);
while(x>0)
{
f=f*x;
x--;
}
printf("factorial is %f\n",f);
}
Output:
enter no.:
5
factorial is 120.000000
Set-3
Program14: Write a program to print all letters of the alphabet in upper & lower case.
#include<stdio.h>
void main()
{
int i;
for(i=65;i<91;i++)
{
printf("%c",i);
}
printf("\n");
for(i=97;i<123;i++)
{
printf("%c",i);
}
}
Output:
ABCDEFGHIJKLMNOPQRSTUVWXYZ
abcdefghijklmnopqrstuvwxyz
Program15: Write a program to print all characters between given 2 numbers x & y.
#include<stdio.h>
void main()
{
int i,x,y;
printf("Enter x and y:");
scanf("%d %d",&x,&y);
for(i=x;i<=y;i++)
{
printf("%d",i);
printf("\t");
}
}
Output:
Set-3
#include<stdio.h>
void main()
{
int n,count=3,c,a=0,b=1;
printf("enter n:\n");
scanf("%d",&n);
printf("%d\n",a);
printf("%d\n",b);
while(count<=n)
{
c=a+b;
printf("%d\n",c);
a=b;
b=c;
count++;
}
}
Output:
enter n:
4
0
1
1
2
#include<stdio.h>
void main()
{
int n,i,c=0;
printf("Enter any number\n");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
if(n%i==0)
{
c++;
}
}
if(c==2)
{
printf("n is a Prime number");
}
else
{
printf("n is not a Prime number");
}
}
Output:
Set-3
#include<stdio.h>
#include<math.h>
void main()
{
float x,sum,term;
int i,n;
printf("Enter the value of x and (n) Number of term to be sum \t:");
scanf("%f%d",&x,&n);
sum=1; term=1;
for(i=1;i<n;i++)
{
term=pow(-1,i)*term*x/(float)i;
sum=sum+term;
}
printf("\n the sum=%f \n Number of terms=%d\n value of x=%f\n",sum,n,x);
}
Output:
the sum=-1.000000
Number of terms=5
value of x=2.000000
Set-4: C Programs for Pattern Generation
1
12
123
1234
#include<stdio.h>
void main()
{
int i,j,n;
printf("enter the number of rows in triangle");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
{
printf("%d ",j);
}
printf("\n");
}
}
1
22
333
4444
#include<stdio.h>
void main()
{
int i,j,n;
printf("enter the number of rows in triangle");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
{
printf("%d ",i);
}
printf("\n");
}
}
Set-4
1
121
12321
1234321
#include<stdio.h>
void main()
{
int i,j,k,n;
printf("enter the no. of rows");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(k=1;k<=n-i;k++)
{
printf(" ");
}
for(j=1;j<=i;j++)
{
printf("%d ",j);
}
for(j=i-1;j!=0;j--)
{
printf("%d ",j);
}
printf("\n");
}
}
GHIJ
DEF
BC
A
#include<stdio.h>
void main()
{
int i,j,n,b=1;
char a=65;
printf("enter the no of rows");
scanf("%d",&n);
for(i=n;i>=0;i--)
{
for(j=1;j<=i;j++)
{
printf("%c ",a+2*(n-1));
a++;
}
a=a-(i+n)+b;
b++;
printf("\n");
}
}
Set-4
1
01
101
0101
#include<stdio.h>
void main()
{
int i=1,n,j;
printf("enter a number");
scanf("%d",&n);
while(i<=n)
{j=1;
if (i%2!=0)
{
while(j<=i)
{
if (j%2!=0)
{
printf("%d ",1);
}
else
{
printf("%d ",0);
}
j++;
}
i++;
printf("\n");
}
else
{
while(j<=i)
{
if (j%2!=0)
{
printf("%d ",0);
}
else
{
printf("%d ",1);
}
j++;
}
i++;
printf("\n");
}
}
}
Set-4
*
**
***
****
#include<stdio.h>
void main()
{
int n,i,j;
printf("enter the number of rows");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
{printf("* ");
}
printf("\n");
}
}
AAAAA
BBBB
CCC
DD
E
#include<stdio.h>
void main()
{
int n,i,j;
char a=65;
printf("enter the number of rows\n");
scanf("%d",&n);
for(i=n;i!=0;i--)
{
for(j=i;j!=0;j--)
{
printf("%c ",a);
}
a++;
printf("\n");
}
}
Set-5: C Programs using an Array
Program1: Write a program to read N integers and print N integers using an array.
#include <stdio.h>
void main()
{
int arr[10];
int i;
printf("Input 10 elements in the array :\n");
for(i=0; i<10; i++)
{
printf("element - %d : ",i);
scanf("%d", &arr[i]);
}
Output:
Program2: Write a program to find the smallest and largest number in an array of N integers.
#include<stdio.h>
int main()
{
int a[50],i,n,large,small;
printf("How many elements:");
scanf("%d",&n);
printf("Enter the Array:");
for(i=0;i<n;++i)
scanf("%d",&a[i]);
large=small=a[0];
for(i=1;i<n;++i)
{
if(a[i]>large)
large=a[i];
if(a[i]<small)
small=a[i];
}
return 0;
}
Output:
#include <stdio.h>
void main()
{
int i, j, a, n, number[30];
printf("Enter the value of N \n");
scanf("%d", &n);
a = number[i];
number[i] = number[j];
number[j] = a;
Output:
#include <stdio.h>
void main ()
{
int number[30];
int i, j, a, n;
printf("Enter the value of N\n");
scanf("%d", &n);
Output:
Program6: Write a program to insert value at ith location or value entered by user using one dimensional
array.
#include<stdio.h>
void main()
scanf("%d", &num);
scanf("%d", &arr[i]);
scanf("%d", &element);
scanf("%d", &location);
num++;
arr[location - 1] = element;
return (0);
OUTPUT:
Enter no of element:2
Enter no to be inserted:6
Enter the location:2
n 4n 6n 5n
Set-5
Program7: Write a program to delete value at ith location or value entered by user using one dimensional
array.
include<stdio.h>
int main() {
scanf("%d", &num);
scanf("%d", &arr[i]);
scanf("%d", &loc);
arr[loc - 1] = arr[loc];
loc++;
num--;
return (0);}
OUTPUT:
Enter element:4
4
Set 5
Program 8:
#include <stdio.h>
int main()
scanf("%d", &first[c][d]);
scanf("%d", &second[c][d]);
printf("%d\t", sum[c][d]);
printf("\n");
} return 0;
Output:
3
4
Sum of matrices:
2 4 6
8 10 12
14 16 18
Set-5
Program9: Write a program to check whether given 3*3 matrices is magic square or not.
#include<stdio.h>
int main() {
int matrix[3][3];
int flag = 0;
scanf("%d", &matrix[row][column]);
printf("\n");
printf("\t%d", matrix[row][column]);
sum = 0;
if (row == column)
sum1 = 0;
if (sum == sum1)
flag = 1;
else {
flag = 0;
break;
sum2 = 0;
if (sum == sum2)
flag = 1;
else {
flag = 0;
break;
if (flag == 1)
printf("\nMagic square");
else
return 0;
OUTPUT:
Enter matix:2
6
9
276
951
438
It is magic matrix
#include <stdio.h>
#include <string.h>
voidmain()
{
charstring[30];
int i, n;
printf("Enter number of strings to input\n");
scanf("%d",&n);
Output:
#include <stdio.h>
#include <string.h>
void main()
{
char s[200];
int count =0, i;
Set-6
Program3: Write a program to Reverse a String
#include <stdio.h>
#include <string.h>
int main()
{
charStr[100], RevStr[100];
int i, j, len;
j = 0;
len = strlen(Str);
return 0;
}
Output:
Set-6
#include <stdio.h>
#include <string.h>
int main()
{
charstr[100];
int i, len, flag;
flag = 0;
len = strlen(str);
return 0;
}
Out Put:
#include <stdio.h>
void main()
{
charch;
Output:
Set-6
#include <stdio.h>
#define MAX_SIZE 100
int main()
{
charstr[MAX_SIZE];
chartoSearch;
int i, count;
count = 0;
i=0;
while(str[i] != '\0')
{
/*
* If character is found in string then
* increment count variable
*/
if(str[i] == toSearch)
{
count++;
}
i++;
}
return 0;
}
Output:
Set-6
Program7 : Write a program to count total number of words in a string
#include <stdio.h>
int main()
{
charstr[MAX_SIZE];
charprevChar;
int i, words;
i = 0;
words = 0;
prevChar = '\0';
while(1)
{
if(str[i]==' ' || str[i]=='\n' || str[i]=='\t' || str[i]=='\0')
{
if(prevChar != ' ' &&prevChar != '\n' &&prevChar != '\t' &&prevChar != '\0')
{
words++;
}
}
if(str[i] == '\0')
break;
else
i++;
}
return 0;
}
Output:
#include <stdio.h>
int main()
{
char str1[50], str2[50], i, j;
printf("\nEnter first string: ");
scanf("%s",str1);
printf("\nEnter second string: ");
scanf("%s",str2);
for(i=0; str1[i]!='\0'; ++i);
return 0;
}
Output:
#include <stdio.h>
int main()
{
char s1[100], s2[100], i;
s2[i] = '\0';
printf("String s2: %s", s2);
return 0;
}
Output:
#include<stdio.h>
Void odd();
Void main()
int a;
Scanf (“%d”&a);
Odd();
Void odd()
{int a;
If(a%2==0)
Else
#include<stdio.h>
long factorial(int);
int main()
int n;
long f;
scanf("%d", &n);
if (n < 0)
else
f = factorial(n);
return 0;
long factorial(int n)
if (n == 0)
return 1;
else
return(n * factorial(n-1));}
#include<conio.h>
#include<math.h>
Int series(int n);
void main()
{
inti,n;
float x,sum=0;
printf (“enter the value of x”);
scanf (“%f”,&x);
series(n);
printf(“1+x+x^2+……+x^n”);
for(i=1;i<=n;++i)
sum+=pow(x,i);
sum++;
printf(“nSum=%f”,sum);
int series(int n)
{
int
Printf (“nnEnter the value of x and n:”);
scanf (“%d”,&n);
return(n);
}
#include<stdio.h>
{int l=0;
If(a>b&&a>c)
{l=a;}
Elseif(b>c&&b>a)
{l=b;}
Else
{l=c;}
return l;
Int main()
inta,b,c;
return 0;
}
#include<stdio.h>
#include<math.h>
Void main()
{intx,y,z;
Z=power(x,y);
int pow(intx,int y)
{int p;
P=pow(x,y);
Return p;
6. //WAP that used user defined function swap() and interchange the value of 2 variables
#include <stdio.h>
int main()
{ int x, y
scanf("%d%d",&x,&y);
swap(&x, &y);
return 0;
}
{ int t;
t = *b;
*b = *a;
*a = t;
/**
* Function declarations for calculator
*/
floatadd(float num1,float num2);
floatsub(float num1,float num2);
floatmult(float num1,float num2);
floatdiv(float num1,float num2);
intmain()
{
char op;
float num1, num2, result=0.0f;
switch(op)
{
case'+':
result =add(num1, num2);
break;
case'-':
result =sub(num1, num2);
break;
case'*':
result =mult(num1, num2);
break;
case'/':
result =div(num1, num2);
break;
default:
printf("Invalid operator");
}
/**
* Function to add two numbers
*/
floatadd(float num1,float num2)
{
return num1 + num2;
}
/**
* Function to subtract two numbers
*/
floatsub(float num1,float num2)
{
return num1 - num2;
}
/**
* Function to multiply two numbers
*/
floatmult(float num1,float num2)
{
return num1 * num2;
}
/**
* Function to divide two numbers
*/
floatdiv(float num1,float num2)
{
return num1 / num2;
}
Set -9 : C Programs using Pointers
Program1: Write a program to print the address of different data type variables along with its value.
#include<stdio.h>
void main()
{
int a;
float b;
char c;
int *ptr_a=&a;
float *ptr_b=&b;
char *ptr_c=&c;
printf(“%d is stored at address %u.\n”,a,&a);
printf(“%f is stored at address %u.\n”,b,&b);
printf(“%c is stored at address %u.\n”,c,&c);
}
Output:
a is stored in 1024
b is stored in 1025
C is stored in 1026
Set-9
Program2: Write a program using a pointer to read in a array of integers and print its element in reverse order.
#include<stdio.h>
void main()
{
int size, i, arr[20];
int *ptr;
clrscr();
ptr = &arr[0];
printf("\nEnter the size of array : ");
scanf("%d", &size);
Program3: Write a program that compares two integer arrays to see whether they are
identical.
#include<studio.h>
Void main()
{
int a[15],b[15],k=0;i;
printf(“enter the integer array 1:”);
for(i=0;i<15;i++)
{
scanf(“%d”,a[i]);
}
printf(“Enter the integer array 2:”);
for(i=0;i<15;i++)
{
scanf(“%d”,b[i]);
}
for(i=0;i<=15;i++)
{
if(a[I]==b[I])
k++;
}
if(k==5)
Printf(“\n identical”);
else
printf(“\n not identical”);
}
Output:
Enter the integer array 1:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Enter the integer array 2:
16 17 18 19 20 21 21 22 23 24 25 26 27 28 29 30
NOT IDENTICAL
Set-9
Program4: Write a program using pointer to sort a array in ascending or descending order.
#include <stdio.h>
void main()
{
int *a,i,j,temp,n[20];
Program 5: Write a program using pointer to copy one string to another string.
#include<stdio.h>
voidcopystr(char*,char*);
void main()
{
char*str1="CPU is easy";
char str2[30];
clrscr();
copystr(str2,str1);
printf("\n %s",str2);
getch();
}
voidcopystr(char *dest, char *src)
{
while(*src!='\0')
*dest++=*src++;
*dest='\0';
return;
}
Output:
Cpu is easy
Cpu is easy