WAP in C to display Hello World!
void main()
{
clrscr();
printf(" Hello, World! "); /* output statement */
getch();
}
WAP in C to display an integer .
void main()
{
int number;
printf(" Enter input data: ");
scanf("%d", &number); /* input statement */
printf(" The input value is: %d ", number);
getch();
}
Write a program that produces the following output:
#include <stdio.h>
int main()
printf("CCCCCCCCC\nCC\nCC\nCC\nCC\nCCCCCCCCC");
return 0;
}
WAP in C for addition of two integer numbers.
Void main()
{
Clrscr();
int num1, num2, sum;
printf(" Enter first integer number: ");
scanf("%d", &num1);
printf(" Enter second integer number: ");
scanf("%d", &num2);
sum = num1 + num2;
printf(" Sum of the entered numbers: %d ", sum);
getch();
}
WAP in C for addition ,subtraction, multiplication and
division of two integer numbers.
void main()
{
clrscr();
int num1, num2, sum , sub, mult;
float div;
printf(" Enter two integer number: ");
scanf("%d %d ", &num1,&num2);
sum = num1 + num2;
sub = num1-num2;
mult= num1*num2:
div = num1/(num2*1.0); /* multiplied with 1.0 for float value */
printf(" Sum of numbers: %d ", sum);
printf(" Subtraction of numbers: %d ", sub);
printf(" Multiplication of numbers: %d ", mult);
printf(" Division of numbers: %f ", div);
getch();
}
WAP in C for area and perimeter of a rectangle.
void main()
{
clrscr();
int side1,side2,area,perim ;
printf(" Enter data of a rectangle: ");
scanf(" %d %d ", &side1,&side2);
area=side1*side2;
perim= 2*(side1+side2) ;
printf(" Area of rectangle: %d ", area);
printf(" Perimeter of rectangle: %d ", perim);
getch();
}
WAP in C for area and circumference of a circle.
void main()
{
clrscr();
float radius, area, circum ,pi=3.14;
printf(" Please enter radius of a circle: ");
scanf(" %f ", &radius);
area=pi*radius*radius;
circum= 2*pi*radius;
printf(" The area of circle: %f ", area);
printf(" The circumference of circle: %f ", circum);
getch();
}
/ What's wrong with this program? /*
int main();
}
int a, b, c \* Three integers */
a = 3
b = 4
c = a + b
printf("The value of c is %d" + C);
return 0;
}
Corrected Code
int main()
{
int a, b, c; /* Three integers */
a = 3;
b = 4;
c = a + b;
printf("The value of c is %d", c);
return 0;
}
WAP to convert the temperature in Celsius into Fahrenheit
F = 9/5 C + 32
void main()
{
clrscr();
float celsius, fahrenheit;
printf("Enter the temperature in celcius: ");
scanf(" %f ", &celsius);
fahrenheit = 9.0 / 5 * celsius + 32;
printf("("Temperature in fahrenheit : %f", fahrenheit);
getch();
}
WAP in C for calculating average of five integer numbers.
void main()
{
int n1,n2,n3,n4,n5,sum;
float avg;
printf(" Enter five integer numbers: ");
scanf(" %d%d%d%d%d", &n1,&n2,&n3,&n4,&n5);
sum=n1+n2+n3+n4+n5;
avg=sum/5.0; /* Divided with 5.0 to get float value */
printf(" The average of five numbers: %f", avg);
getch();
}
WAP in C for finding larger number between two numbers.
Method A:
void main()
{
int n1, n2;
printf("Enter two numbers: ");
scanf("%d %d", &n1, &n2);
if( n1>=n2)
printf(" %d is the larger number ", n1);
if( n2>=n1)
printf(" %d is the largest number", n2);
getch();
}
Method B:
void main()
{
int n1, n2;
printf("Enter two numbers: ");
scanf("%d %d", &n1, &n2);
if( n1>=n2)
printf(" %d is the larger number ", n1);
else
printf(" %d is the largest number", n2);
getch();
}
Method C:
void main()
{
int n1, n2;
printf("Enter two numbers: ");
scanf("%d %d", &n1, &n2);
if( n1==n2)
printf(" The numbers are equal " );
else
if( n1>n2)
printf(" %d is the largest number", n1);
else
printf(" %d is the largest number", n2);
getch();
}
WAP in C for finding larger number among three
integer numbers.
Method A:
void main()
{
int n1, n2, n3;
printf(" Please input three integer numbers: ");
scanf("%d %d %d", &n1, &n2, &n3);
if( n1>=n2 && n1>=n3 )
printf("%d is the largest number.", n1);
if( n2>=n1 && n2>=n3 )
printf("%d is the largest number.", n2);
if( n3>=n1 && n3>=n2 )
printf("%d is the largest number.", n3);
getch();
}
Method B:
void main()
{
int n1, n2, n3;
printf(" Please input three integer numbers: ");
scanf("%d %d %d", &n1, &n2, &n3);
if( n1>=n2 && n1>=n3)
printf("%.d is the largest number.", n1);
else
if (n2>=n1 && n2>=n3)
printf("%d is the largest number.", n2);
else
printf("%d is the largest number.", n3);
getch();
}
Method C: ( by using nested if-else condition)
void main()
{
int n1, n2, n3;
printf(" Please input three integer numbers: ");
scanf("%d %d %d", &n1, &n2, &n3);
if (n1>=n2)
{
if(n1>=n3)
printf(" %d is the largest number ", n1);
else
printf(" %d is the largest number ", n3);
}
else
{
if(n2>=n3)
printf(" %d is the largest number ", n2);
else
printf(" %d is the largest number ",n3);
}
getch();
}
WAP in C to test whether a year is leap year or not
Definition : If a year is divisible by 400 OR if a year is divisible by 4 but
not divisible by 100 then it is a leap year .
Method A:
void main()
{
int year;
printf(" Please enter a year : ");
scanf("%d",&year);
if (year %100 ! = 0 && year%4==0 || year % 400 ==0 )
printf( " %d is a leap year.", year);
else
printf(" %d is not a leap year.", year);
getch();
}
Method B:
int main()
{
int year;
printf(" Please enter a year : ");
scanf("%d",&year);
if (year %400 ==0 )
printf( " %d is a leap year.", year);
else
if (year %100 ! = 0 && year%4==0 )
printf( " %d is a leap year.", year);
else
printf(" %d is not a leap year.", year);
getch();
}
WAP in c to test whether a number is even or not .
void main()
{
int num;
printf("Enter an integer: ");
scanf("%d",&num);
if ( num%2 == 0 )
printf("%d is an even number", num);
else
printf("%d is an odd number", num);
getch();
}
WAP in C for sum of natural number (Sum=1+2+3+…+n )
Method A : ( by using for loop)
void main()
{
int num, count, sum = 0; /* initialization must to avoid garbage value */
printf("Enter a positive integer: ");
scanf("%d", &num);
for(count = 1; count <= num; count=count+1)
sum =sum + count; /* sum += count */
printf(" The answer : %d", sum);
getch();
}
Method B: ( by using for loop)
void main()
{
int num, count, sum = 0; /* initialization must to avoid garbage value */
printf("Enter a positive integer: ");
scanf("%d", &num);
for(count = 1; count<num+1; count++)
{ /* braces optional if statement is only one */
sum =sum + count; /* sum+=count */
}
printf(" The answer : %d", sum);
getch();
}
Method C: ( by using for loop)
void main()
{
int num, count, sum = 0; /* initialization must to avoid garbage value */
printf("Enter a positive integer: ");
scanf("%d", &num);
for(count = 1; count <= num; )
sum =sum +count++; /*Note: sum=sum+count--make indefinite loop */
printf(" The answer : %d", sum);
getch();
}
Method D: ( by using for loop)
void main()
{
int num, count, sum = 0; /* initialization must to avoid garbage value */
printf("Enter a positive integer: ");
scanf("%d", &num);
count=1;
for( ; count <= num ; )
sum =sum + count++ ;
printf(" The answer : %d", sum);
getch();
}
WAP in C for summation of ODD numbers between 1 & n
(Sum=1+3+5. . .+n )
void main()
{
int num, count, sum = 0; /* initialization must to avoid garbage value */
printf("Enter a positive integer: ");
scanf("%d", &n);
for(count = 1; count <= n; count=count+2)
sum =sum + count; /* sum += count */
printf(" The answer : %d", sum);
getch();
}
WAP in C for summation of EVEN numbers between 2 & n
(Sum=2+4+6. . .+n )
void main()
{
int num, count, sum = 0; /* initialization must to avoid garbage value */
printf("Enter a positive integer: ");
scanf("%d", &n);
for(count = 2; count <= n; count=count+2)
sum =sum + count; /* sum += count */
printf(" The answer : %d", sum);
getch();
}
WAP in C for factorial a number (fact=1*2*3*. . .*num )
Method A: ( by using for loop)
void main()
{
int num, count, fact = 1; /* initialization must to avoid garbage value */
printf("Enter a positive integer: ");
scanf("%d", &num);
for(count = 1; count <= num; count++)
fact =fact* count; /* fact *= count */
printf(" The answer : %d", fact);
getch();
}
Method B: (fact=n*... 3*2*1 )
void main()
{
int num, count, fact = 1; /* initialization must to avoid garbage value */
printf("Enter a positive integer: ");
scanf("%d", &num);
for(count = num; count >=1 count--)
fact =fact * count; /* fact *= count */
printf(" The answer : %d", fact);
getch();
}
WAP in C for sum of natural number (Sum=1+2+3+…+num )
By using while loop ( Method A)
void main()
{
int num, count, sum = 0; /* initialization must to avoid garbage value */
printf("Enter a positive integer: ");
scanf("%d", &num);
count=1;
while(count <= num)
sum =sum + count++;
printf(" The answer : %d", sum);
getch();
}
By using while loop ( Method B)
void main()
{
int num, count, sum = 0; /* initialization must to avoid garbage value */
printf("Enter a positive integer: ");
scanf("%d", &num);
count=1;
while(count <= num)
{ /* braces MUST as the executing statement is more than ONE */
sum =sum + count;
count=count+1;
}
printf(" The answer : %d", sum);
getch();
}
WAP in C for sum of natural number (Sum=1+2+3+…+n )
By using do-while loop ( Method A)
void main()
{
int num, count, sum = 0; /* initialization must to avoid garbage value */
printf("Enter a positive integer: ");
scanf("%d", &num);
count=1;
do
sum =sum + count++;
while(count <= num);
printf(" The answer : %d", sum);
getch();
}
By using do-while loop ( Method B)
void main()
{
int num, count, sum = 0; /* initialization must to avoid garbage value */
printf("Enter a positive integer: ");
scanf("%d", &num);
count=1;
do
{ /* braces MUST as the executing statement is more than ONE */
sum =sum + count;
count=count+1;
} while(count <= num);
printf(" The answer : %d", sum);
getch();
}
WAP in C to test whether a number is prime number or not
(A prime number is a positive integer which is divisible by 1 & itself ONLY. e.g.1,3,5,7,11,13,17 etc )
void main()
{
int num, i ;
printf("Enter a positive integer: ");
scanf("%d", &num);
for(i = 2; i <num ; i++) /* the value of i varies from i=2 to num-1 */
if(num%i == 0) break; /* exit the loop only when the condition is true */
if (i == num)
printf(" %d is a prime number.", n);
else
printf(" %d is not a prime number.", n);
getch();