Вы находитесь на странице: 1из 35

1-WAP to print your name.

\* Print your name*\


#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
printf(“My name is Pankaj”);
getch();
}

/* Output :

My name is Pankaj
*/
2-WAP to add two numbers .

\* Addition of two numbers *\


#include<stdio.h>
#include<conio.h>
void main()
{
int a = 5 , b = 10 , c;
clrscr();
c = a+b;
printf(“Sum of two numbers is %d”,c);
getch();
}

/*Output:

Sum of two numbers is 15.


*/
3- WAP to swap two numbers using third variable.

\*Swapping of two numbers using third variable.*\


#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int a,b,c;
printf(“Enter the values of a and b”);
scanf(“%d%d”,&a,&b);
c=a;
a=b;
b=c;
printf(“Value after swapping a=%d b=%d”,a,b);
getch();
}

/*Output:

Enter the values of a and b


5,10
Value after swapping a=10 b=5
*/
4- Wap to swap two numbers without using third variable

\* Swapping of two numbers without using third variable*\


#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
clrscr();
printf(“Enter the values of a and b”);
scanf(“%d%d”,&a,&b);
a=a+b;
b=a-b;
a=a-b;
printf(“Value after swapping a=%d b=%d”,a,b);
getch();
}

/* Output:

Enter the values of a and b


5,10
Value after swapping a=10 b=5
*/
5-wap to calculate temperature in fahrenheit to celsius

\* Calculate temperature in Fahrenheit to Celsius *\


#include<stdio.h>
#include<conio.h>
void main()
{
float f,c;
clrscr();
printf(“Enter the value in Fahrenheit”);
scanf(“%f”,&f);
c=5.0/9.0 * [f – 32.0];
printf(“%f”,c);
getch();
}

/* Output:

Enter the value in Fahrenheit 32


0
*/
6-WAP to check whether the number is Even or not.
Coding:

\* Check whether the number is even or not.*\


#include<stdio.h>
#include<conio.h>
void main()
{
int n;
printf(“Enter the number”);
scanf(“%d”,&n);
if( n%2 == 0)
{
printf(“Number is Even”);
}
else
{
printf(“Number is Odd”);
}
getch();
}

/*Output

Enter the number 10


Number is Even
*/
7-WAP to check whether the triangle is Equilateral,Isoceles or Scalene.
Coding:

\*To check the type of a triangle*\


#include<stdio.h>
#include<conio.h>
void main()
{
float a,b,c;
printf(“Enter the sides of a triangle”);
scanf(%f%f%f”,&a,&b,&c);
if((a+b)>c)&&((b+c)>a)&&((c+a)>b)
{
if((a==b)&&(b==c))
{
printf(“Equilateral”);
}
else
{
if((a==b)||(b==c)||(c==a))
{
printf(“Isoceles”);
}
else
{
printf(“Scalene”);
}}}
else
{
printf(“Triangle is not possible”);
}
getch();
}

/*Output:

7
7
7
Equilateral
*/
8-WAP to print the days of a week using Switch Case.
Coding:

\* To print week days using Switch case.*\


#include<stdio.h>
#include<conio.h>
void main()
{
int n;
clrscr();
printf(“Enter your choice”);
scanf(“%d”,&n);
switch(n)
{
case 1:
printf(“Sunday”);
break;
case 2:
printf(“Monday”);
break;
case 3:
printf(“Tuesday”);
break;
case 4:
printf(“Wednesday”);
break;
case 5:
printf(“Thursday”);
break;
case 6:
printf(“Friday”);
break;
case 7:
printf(“Saturday”);
break;
default
printf(“Input is wrong”);
}
getch();
}

/* Output:
Enter your choice 4
Wednesday*/
9- WAP to print the number of days in a Month.
Coding: \* Print the number of days in a month.*\
#include<conio.h>
#include<stdio.h>
void main()
{
int n;
clrscr();
printf(“Enter your choice”);
scanf(“%d”,&n);
switch(n)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
{
printf(“31”);
break;
}
case 4:
case 6:
case 9:
case 11:
{
printf(“30”);
break;
}
case 2:
{
printf(“29”);
break;
}
default
{
printf(“Wrong Input”);
break;
}}
getch();
}
/* Output:
Enter your choice 2
29*/
10-WAP to print the number from 1 to n using FOR loop.
Coding:

\* Print the number from 1 to n using for loop*\


#include<stdio.h>
#include<conio.h>
void main()
{
int i,n;
clrscr();
printf(“Enter the value of n”);
scanf(“%d”,&n);
for(i=1;i<=n;i++)
{
printf(“%d\n”,i);
}
getch();
}

/*Output:
Enter the value of n 3
1
2
3
*/
11-WAP to print the number from 1 to n using WHILE.
Coding:

\* Print the number from 1 to n using while.*\


#include<stdio.h>
#include<conio.h>
void main()
{
int i,n;
clrscr();
printf(“Enter the value of n”);
scanf(“%d”,&n);
i = 1;
while(i<=n)
{
printf(“%d\n”,i);
i=i+1;
}
getch();
}

/* Output:

Enter the value of n 5


1
2
3
4
5
*/
12- WAP to print the table of a number.
Coding:

\* Print the table of a number from 1 to 5.*\


#include<stdio.h>
#include<conio.h>
void main()
{
int i,n,t=0;
clrscr();
printf(“Enter the value of number”);
scanf(“%d”,&n);
for(i=1;i<=5;i++)
{
t=n*i;
printf(“%d*%d=%d\n”,n,i,t);
}
getch();
}

/*Output:

Enter the value of number 5


5*1=5
5*2=10
5*3=15
5*4=20
5*5=25
*/
13-WAP to print the average of n numbers.
Coding:

\*Print the average of n numbers*\


#include<stdio.h>
#include<conio.h>
void main()
{
float avg;
int i,n,sum=0;
clrscr();
printf(“Enter the value of n”);
scanf(“%d\n”,&n);
for(i=1;i<=n;i++)
{
scanf(“%d”,&i);
sum = sum + i;
}
avg = sum/n;
printf(“Average is %f”,avg);
getch();
}

/*Output:

Enter the value of n 5


1
2
3
4
5
Average is 3
*/
14-WAP to calculate the factorial of a number.
Coding:

\*Calculate the factorial of a number*\


#include<stdio.h>
#include<conio.h>
void main()
{
int i,n,fact;
clrscr();
printf(“Enter the number”);
scanf(“%d”,&n);
if(n<0)
{
printf(“Factorial is not possible”);
}
else
{
fact =1;
for(i=1;i<=n;i++)
{
fact=fact*i;
}
printf(“Factorial is %d”,fact);
}
getch();
}

/*Output:

Enter the number


5
Factorial is 120
*/
15- WAP to find the sum of digits of a number.
Coding:

\* Sum of all the digits of a number*\


#include<stdio.h>
#include<conio.h>
void main()
{
int n,sum=0,a;
clrscr();
printf(“Enter a number”);
scanf(“%d”,&n);
while(n!=0)
{
a=n%10;
sum=sum+a;
n=n/10;
}
printf(“%d”,sum);
getch();
}

/*Output:

Enter a number
123
6
*/
16- WP to check whether the given number is Armstrong or not.
Coding:

\* Check whether the number is Armstrong or not.*\


#include<stdio.h>
#include<conio.h>
void main()
{
int n,sum=0,a,num;
clrscr();
printf(“Enter a number”);
scanf(“%d”,&n);
num=n;
while(n!=0)
{
a=n%10;
sum=sum+a*a*a;
n=n/10;
}
if(num==sum)
printf(“Number is Armstrong”);
else
printf(“Number is not Armstrong”);
getch();
}

/*Output:

Enter a number
153
Number is Armtrong.
*/
17-WAP to find the revere of a number.
Coding:

\* Reverse a number*\
#include<stdio.h>
#include<conio.h>
void main()
{
int n,r=0,a;
clrscr();
printf(“Enter a number”);
scanf(“%d”,&n);
while(n!=0)
{
a=n%10;
r=(r*10)+a;
n=n/10;
}
printf(“%d”,r);
getch();
}

/*Output:

Enter a number
153
351
*/
18-WAP to check whether the given number is Palindrome or not.
Coding:

\* Check whether the given number is Palindrome or not.*\


#include<stdio.h>
#include<conio.h>
void main()
{
it n,r=0,a,num;
clrscr();
printf(“Enter the number”);
scanf(“%d”,&n);
num=n;
while(n!=0)
{
a=n%10;
r=(r*10)+a;
n=n/10;
}
if(num==r)
printf(“Palindrome”);
else
printf(“Not Palindrome”);
getch();
}

/*Output:

Enter a number
151
Palindrome
*/
19-WAP to check whether the given number is prime or not.
Coding:

\* Check whether the given number is Prime or not. *\


#include<stdio.h>
#include<conio.h>
void main()
{
int i,n=1,num;
clrscr();
printf(“Enter a number”);
scanf(“%d”,&num);
for(i=2;i<=n/2;i++)
{
if(num%i==0)
{
n=0;
break;
}
else
n=1;
}
if(n==1)
printf(“Number is prime”);
else
printf(“Number is not prime”);
getch();
}

/*Output:

Enter a number
7
Number is prime
*/
20-WAP to find the Fibonacci series up to a fixed number.
Coding:

\* To print the Fibonacci series upto a fixed number.*\


#include<stdio.h>
#include<conio.h>
void main()
{
int num,i,a,b,c;
clrscr();
printf(“Enter the number”);
scanf(“%d”,&num);
a=0;
b=1;
printf(“%d\t%d”,a,b);
for(i=1;i<=num-2;i++)
{
c=a+b;
printf(“\t%d”,c);
a=b;
b=c;
}
getch();
}

/*Output:

Enter the number


5
0 1 1 2 3
*/
21-WAP to form the pyramid as shown: *
**
***
****
Coding:

\*To form a pyramid.*\


#include<stdio.h>
#include<conio.h>
void main()
{
int num,i,j;
clrscr();
printf(“Enter the number”);
scanf(“%d”,&num);
for(i=1;i<=num;i++)
{
for(j=1;j<=i;j++)
{
printf(“*”);
}
printf(“\n”);
}
getch();
}

/*Output:
Enter the number
4
*
**
***
****

*/
22-WAP to form a pyramid as shown: 1234
123
12
1
Coding:

\* To form a pyramid*\
#include<stdio.h>
#include<conio.h>
void main()
{
int num,i,j;
clrscr();
printf(“Enter a number”);
scanf(“%d”,&num);
for(i=1;i<=num;i++)
{
for(j=1;j<=num-i+1;j++)
{
printf(“%d”,j);
}
printf(“\n”);
}
getch();
}

/*Output:

Enter a number
4
1234
123
12
1
*/
23-WAP to form a pyramid as shown: 4
43
432
4321
Coding:

\* To form a pyramid.*\
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,j,k;
clrscr();
printf(“Enter a number”);
scanf(“%d”,&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=n-i;j++)
{
printf(“ “);
}
for(k=n;k>=n-i+1;k--)
{
printf(“%d”,k);
}
printf(“\n”);
}
getch();
}

/*Output:

Enter a number
4
4
43
432
4321
*/
24-WAP to form the pyramid.
Coding:

\*To form a pyramid.*\


#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,n;
clrscr();
printf(“Enter a number”);
scanf(“%d”,&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=n-i+1;j++)
{
printf(“%d”,j);
}
printf(“\n”);
}
for(i=1;i<=n-1;i++)
{
for(j=1;j<=i+1;j++)
{
printf(“%d”,j);
}
printf(“\n”);
}
getch();
}

/*Output:

Enter a number
4
1234
13
12
1
12
123
1234

*/
25-WAP to print the sum of the given series.
Coding:

\* To print the sum of the following series


1 – ½ + 1/3 – ¼ . . . n *\
#include<stdio.h>
#include<conio.h>
void main()
{
int n,si=1,num=1,den=1,c=1;
float t,sum=0;
clrscr();
printf(“Enter the number of terms”);
scanf(“%d”,&n);
do
{
t=(num/den)*si;
sum=sum+t;
den=den+1;
si=si*(-1);
c=c+1;
}
while(c<=n);
printf(“%f”,sum);
getch();
}

/*Output:

Enter the number of terms


4
0.583333
*/
26-WAP to calculate the sum of the sin x series.
Coding:

\*Calculate the sum of the ‘sin x’ series up to n terms.


x - x³/3! + x /5! . . . n*\
#include<stdio.h>
#include<conio.h>
void main()
{
int n,si=1,c,k=2;
float sum=0,xd,xr,pi=3.142;
float num,den=1,t;
printf(“Enter the number of terms”);
scanf(“%d”,&n);
scanf(“%f”,&xd);
xr=xd*(pi/180.0);
num=xr;
for(c=1;c<=n;c++)
{
t=(num/den)*si;
sum=sum+t;
num=num*xr*xr;
den=den*(k)*(k+1);
si=si*(-1)
k=k+2;
}
printf(“%f”,sum);
getch();
}

/*Output:

Enter the number of terms


3
1
*/
27-WAP to calculate the roots of a Quadratic equation.
Coding:
\* Calculate the roots of the equation*\
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float a,b,c,r1,r2,img1,img2,d;
clrscr();
printf(“Enter the values of a,b and c”);
scanf(“%f%f%f”,&a,&b,&c);
if(a==0.0)
{if(b==0.0)
{printf(“Equation is degenerate”);
}
else
{r1=-c/b;
printf(“Single root %f”,r1);
}}
else
{d=b*b-4*a*c;
if(d>0.0)
{r1=(-b + (sqrt(d)))/2*a;
r2=(-b – (sqrt(d)))/2*a;
printf(“%f%f”,r1,r2);
}
else
{if(d==0.0)
{r1=r2=-b/2*a;
printf(“%f%f”,r1,r2); }
else
{r1=r2=-b/2*a;
img1=sqrt(-d)/2*a;
img2=-img1;
printf(“Real part is%f and imaginary part is %f”,r1,img1);
printf(“Real part is%f and imaginary part is %f”,r2,img2);
}}}
getch();
}
/*Output:
Enter the values of a,b and c 1
5
6
-2,-3
*/
28-WAP to search an element in an array.
Coding:

\* Search an element in an array*\


#include<stdio.h>
#include<conio.h>
void main()
{
int i,n,a[20],s,found=0;
clrscr();
printf(“Enter the number of elements”);
scanf(“%d”,&n);
printf(“Enter the element to search”);
scanf(“%d”,&s);
for(i=0;i<=n-1;i++)
{
scanf(“%d”,&a[i]);
}
for(i=0;i<=n-1;i++)
{
if(a[i]==s)
{
found=1;
break;
}}
if(found==1)
{
printf(“Element is present at %d position”;i+1);
}
else
{
printf(“Element is not present”);
}
getch();
}

/*Output:
Enter the number of elements 5
Enter the element to search 4
3
4
5
6
7 */
Element is present at 2 position
29-WAP to sort an array in Ascending order.
Coding:
\* Sort an array in Ascending order.*\
#include<stdio.h>
#include<conio.h>
void main()
{
int i,a[20],n,j,t;
clrscr();
printf(“Enter no. of elements”);
scanf(“%d”,&n);
for(i=0;i<=n-1;i++)
{
scanf(“%d”,&a[i]);
}
for(i=0;i<n-1;i++)
{
for(j=i+1;j<=n-1;i++)
{
if(a[i]>a[j])
{
t=a[i];
a[i]=a[j];
a[j]=t;
}}}
for(i=0;i<=n-1;i++)
{
printf(“%d\n”,a[i]);
}
getch();
}

/*Output:
Enter no. of elements 5
4
6
8
2
1

1
2
4
6
8
*/
30-WAP of multiplication of a Double Dimensional array.
Coding:
\* Multiplication of a Matrix.*\
#include<stdio.h>
#include<conio.h>
void main()
{
int a[3][3],b[3][3],c[3][3],i,j;
clrscr();
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
scanf(“%d”,&a[i][j]);
}
}
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]=0;
(for(k=0;k<=2;k++)
{
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
}
}
for (i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
printf(“%d”,c[i][j]);
}
printf(“\n”);
}
getch();
}
/*Output:

1
0
0
0
1
0
0
0
1

1
0
0
0
1
0
0
0
1

100
010
001
*/
31-WAP to concatenate two strings.
Coding:

\* To concatenate two strings using function.*\


#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char a[20],b[10];
clrscr();
gets(a);
gets(b);
strcat(a,b);
puts(a);
getch();
}

/*Output:

My name
Is Pankaj
My name is Pankaj
*/
32-WAP to copy a string to another string.
Coding:

\* Copying a string to another string.*\


#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char a[10],b[10];
clrscr();
gets(a);
strcpy(b,a);
puts(b);
getch();
}

/*Output:

Pankaj
Pankaj

*/
33 -WAP to compare two strings.
Coding:

\* To compare two strings*\


#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int i;
char a[10],b[10];
clrscr();
gets(a);
gets(b);
i=strcmp(a,b);
if(i==0)
{
printf(“Identical”);
}
else
{
if(i<0)
printf(“a comes before b”);
else
printf(“b comes before a”);
}
getch();
}

/*Output:

Tom
Tom
Identical
*/
34 -WAP to reverse a string.
Coding:

\* Reverse a string using function*\


#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char a[10];
clrscr();
gets(a);
strrev(a);
puts(a);
getch();
}

/*Output:

Nitin
Nitin*/

Вам также может понравиться