Download as pdf or txt
Download as pdf or txt
You are on page 1of 20

PPS

IMP PROGRAMS
Program 1: W.A.P. to calculate area of circle.
#include<stdio.h>
#include<conio.h>
#define PI 3.14 // definition section, PI is symbolic constant here
void main()
{
int r;
float area;
clrscr( );
printf("Enter radius of circle: ");
scanf("%d", &r);
area = PI * r * r;
printf("\nArea of circle: %f ",area);
getch( );
}
Output:
Enter radius of circle: 10
Area of circle: 314.000000
Similar Programs:
Example 1: W.A.P. to calculate area of rectangle (area = length * width).
Example 1: W.A.P. to calculate area of square (area = length * length).
Example 2: W.A.P. to calculate circumference of circle (circumference = 2 * pi *r).
Example 3: W.A.P. to compute Fahrenheit from centigrade (f=1.8*c +32).
Program 2: W.A.P. to swap value of 2 variables using extra variable.
#include<stdio.h>
#include<conio.h>
void main()
{
int x, y, temp;
clrscr( );
printf("Enter the value of x and y\n");
scanf("%d %d", &x, &y);
printf("Before Swapping\nx = %d \ny = %d\n", x, y);
temp = x;
x = y;
y = temp;
printf("After Swapping\nx = %d\ny = %d\n", x, y);
getch( );
}
Output:
Enter the value of x and y 10
20
Before Swapping
x = 10
y = 20
After Swapping
x = 20
y = 10
Program 3: W.A.P. to swap value of 2 variables without using extra variable.
#include<stdio.h>
#include<conio.h>
void main()
{
int x, y;
clrscr( );
printf("Enter the value of x and y\n");
scanf("%d %d", &x, &y);
printf("Before Swapping\nx = %d \ny = %d\n", x, y);
x = x + y;
y = x - y;
x = x - y;
printf("After Swapping\nx = %d\ny = %d\n", x, y);
getch( );
}
Output:
Enter the value of x and y
10
20
Before Swapping
x = 10
y = 20
After Swapping
x = 20
y = 10
Program 4: W.A.P. to find maximum from two numbers.
#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);
if(a > b)
{
printf("%d is largest", a);
}
else
{
printf("%d is largest", b);
}
getch( );
}
Output:
Enter Value of a: 10
Enter Value of b: 8
10 is maximum
Similar Programs:
Example 1: W.A.P. to find minimum from two numbers.
Example 2: W.A.P. to check whether a number is odd or even (Simple If Else).
Example 3: W.A.P. to check whether a number is positive or negative (Simple If Else).
Program 5: W.A.P. to find maximum from three numbers. (Nested if else)
#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(“\na is maximum“);
}
else
{
printf(“\nc is maximum“);
}
}
else
{
if(b>c)
{
printf(“\nb is maximum“);
}
else
{
printf(“\nc is maximum“);
}
}
getch( );
}
Output:
Enter Value of a: 10
Enter Value of b: 8
Enter Value of c: 15
c is maximum
Similar Programs:
Example 1: W.A.P. to find maximum / minimum from three numbers (If else Nested).
Example 2: WAP to check whether entered character is vowel or not.
Program 6: W.A.P. to find maximum from three numbers. (if else ladder)
#include<stdio.h>
#include<conio.h>
void main()
{
int a, b, c;
clrscr( );
printf("Enter Number a:");
scanf("%d", &a);
printf("Enter Number b:");
scanf("%d", &b);
printf("Enter Number c:");
scanf("%d", &c);
if(a>b && a>c)
{
printf(“\na is maximum");
}
else if(b>a && b>c)
{
printf(“\nb is maximum");
}
else
{
printf(“\nc is maximum");
}
getch( );
}
Output:
Enter Value of a: 10
Enter Value of b: 18
Enter Value of c: 15
b is maximum
Similar Programs:
Example 1: W.A.P. to find minimum from three numbers (If else Ladder).
Program 7: W.A.P. to check whether a number is positive, negative or zero.
#include<stdio.h>
#include<conio.h>
void main()
{
int a;
clrscr( );
printf("Enter Number:");
scanf("%d", &a);
if(a > 0)
printf(“\nPositive Number");
else if(a == 0)
printf(“\nZero");
else
printf(“\nNegative Number");
getch();
}
Output:
Enter Number: 10
Positive Number
Similar Programs:
Example 1: WAP to check whether the entered character is capital, small letter, digit or any
special character.
Example 2: WAP that accept year and shows, weather is leap year or not.
Program 8: WAP to read marks from keyboard and your program should display equivalent grade
according to following table (using switch case).
Marks 100 - 80 79 - 60 59 - 40 < 40
Grade Distinction First Class Second Class Fail
#include<stdio.h>
#include<conio.h>
void main()
{
int m, p, c, total, a;
float per;
clrscr( );
printf("Enter marks of Mathematics: ");
scanf("%d", &m);
printf("Enter marks of Physics: ");
scanf("%d", &p);
printf("Enter marks of Chemistry: ");
scanf("%d", &c);
total = m + p + c;
per = (float) (total*100) / 300;
printf("\nTotal = %d\n” , total);
printf(”Percentage = %f\n", per);
a = per/10;
switch(a)
{
case 10:
case 9:
case 8:
case 7:
printf("grade = Distinction");
break;
case 6:
printf("grade = 1st class");
break;
case 5:
printf("grade = 2nd class");
break;
case 4:
printf("grade = 3rd class");
break;
default:
printf("Student is failed");
}
}
getch( );
}
Output:
Enter marks of Mathematics: 80
Enter marks of Physics: 70
Enter marks of Chemistry: 90
Total = 270
Percentage = 90.000000
grade = Distinction
Program 9: W.A.P. to input an integer number and check the last digit of number is even or odd.
#include<stdio.h>
#include<conio.h>
void main()
{
int n, first, last;
clrscr( );
printf(“Enter an integer: ”);
scanf(“%d”, &n);
if(first%2==0)
{
printf(“\nFirst digit is even number”);
}
else
{
printf(“\nFirst digit is odd number”);
}

last=n%10;
first=n;
while(first>=10)
{
first=first/10;
}
if(last%2==0)
{
printf(“\nLast digit is even number”);
}
else
{
printf(“\nLast digit is odd number”);
}
getch( );
}
Output:
Enter an integer: 1201
First digit is odd number
Last digit is odd number
Program 10: W.A.P. to find factorial of a given number.
#include<stdio.h>
#include<conio.h>
void main()
{
int n, i;
clrscr( );
long factorial = 1;
printf("Enter an integer: ");
scanf("%d", &n);
for(i=1; i<=n; ++i)
{
factorial = factorial* i;
}
printf("Factorial of %d = %ld", n, factorial);
getch();
}
Output:
Enter an integer: 5
Factorial of 5 = 120
Similar Programs:
Example 1: Write a program to evaluate the series 1^2+2^2+3^2+……+n^2
Example 2: Write a C program to find 1+1/2+1/3+1/4+....+1/n.
Program 11: W.A.P. to reverse a number.
#include<stdio.h>
#include<conio.h>
void main()
{
int n, rem, rev=0;
clrscr( );
printf("Enter a no:");
scanf("%d", &n);
while(n>0)
{
rem = n % 10;
rev = rev * 10 + rem;
n = n / 10;
}
printf("Reversed number =%d", rev);
getch();
}
Output:
Enter a no: 123
Reversed number =321
Program 12: W.A.P. generate first n number of Fibonacci series.
#include<stdio.h>
#include<conio.h>
void main()
{
int n, first = 0, second = 1, next, i;
clrscr( );
printf("Enter the number of terms\n");
scanf("%d", &n);

printf("First %d terms of Fibonacci series are :-\n", n);


printf(“%d, %d, ”, first, second);
for ( i = 3 ; i<= n ; i++ )
{
next = first + second;
first = second;
second = next;
printf("%d, ", next);
}
getch();
}
Output:
Enter the number of terms : 5
First 5 terms of Fibonacci series are :-
0, 1, 1, 2, 3,
Program 13: W.A.P. check whether the given number is prime or not.
#include<stdio.h>
#include<conio.h>
void main()
{
int n, i, flag = 0;
clrscr( );
printf("Enter a positive integer: \n");
scanf("%d", &n);
if (n==0 || n==1)
{
flag = 1;
}

if (n>1)
{
for (i=2 ; i<= n/2 ; i++)
{
if ( n % i == 0 )
{
flag = 1;
break;
}
}
}

if ( flag == 0 )
{
printf(“%d is a prime number.", n);
}
else
{
printf(“%d is not a prime number.", n);
}
getch();
}
Output:
Enter a positive integer: 11
11 is a prime number.
Program 14: W.A.P. to print below pattern.
1
22
333
4444
#include<stdio.h>
#include<conio.h>
void main()
{
int i, j, n;
clrscr( );
printf(“Enter number of rows: ”);
scanf(“%d”, &n);
for( i = 1 ; i <= n ; i++)
{
for( j = 1 ; j <= i ; j++)
{
printf(“%d ”,i );
}
printf(“\n”);
}
getch( );
}
Output:
Enter number of rows: 4
1
22
333
4444
Similar Programs:
Example 1: WAP to print below pattern.
* 1 1
** 12 23
*** 123 456
**** 1234 7 8 9 10
Program 15: W.A.P. to print below pattern.
*
**
***
****
#include<stdio.h>
#include<conio.h>
void main()
{
int i, j, k, n;
clrscr( );
printf(“Enter number of rows: ”);
scanf(“%d”, &n);
for(i=1;i<=n;i++)
{
for(k=n ; k>i ; k--)
{
printf(“ ”);
}
for(j=1 ; j<=i ; j++)
{
printf(“* ”);
}
printf(“\n”);
}
getch();
}
Output:
Enter number of rows: 4
*
**
***
****
Similar Programs:
Example 1: WAP to print below pattern.
1 1 1
12 22 23
123 333 456
1234 4444 7 8 9 10
Program 16: W.A.P. to print below pattern.
****
***
**
*
#include<stdio.h>
#include<conio.h>
void main()
{
int i, j, n;
clrscr( );
printf(“enter number of rows : ”);
scanf(“%d”, &n);
for(i=1 ; i<=n ; i++)
{
for(j=n ; j>=i ; j--)
{
printf(“* ”);
}
printf(“\n”);
}
getch();
}
Output:
Enter number of rows: 4
****
***
**
*
Similar Programs:
Example 1: WAP to print below pattern.
1111 AAAA 4321
222 BBB 321
33 CC 21
4 D 1
Program 17: W.A.P. to print below pattern.
1234
123
12
1
#include<stdio.h>
#include<conio.h>
void main()
{
int i, j, n;
clrscr( );
printf(“enter number of rows : ”);
scanf(“%d”, &n);
for(i=n;i>=1;i--)
{
for(j=1;j<=i;j++)
{
printf(“%d ”,j);
}
printf(“\n”);
}
getch();
}
Output:
Enter number of rows: 4
1234
123
12
1
Similar Programs:
Example 1: WAP to print below pattern.
ABCD 4444
ABC 333
AB 22
A 1
Program 18: W.A.P. to store 10 elements in array given by user and to find maximum out of those
10 elements.
#include<stdio.h>
#include<conio.h>
void main()
{
int array[10], i, max, min;
clrscr( );
printf("Enter value of array elements: \n");
for( i=0 ; i<10 ; i++)
{
printf("Enter element value %d= “,i+1);
scanf("%d", &array[i]);
}
max=array[0];
min=array[0];
for( i=0 ; i<10 ; i++)
{
if(max < array[i])
{
max = array[i];
}
if(min > array[i])
{
min = array[i];
}
}
printf(“Max=%d, Min=%d", max, min);
getch();
}
Output:
Enter value of array elements:
Enter element value 1= 1
Enter element value 2= 6
Enter element value 3= 4
Enter element value 4= 8
Enter element value 5= 9
Enter element value 6= 2
Enter element value 7= 3
Enter element value 8= 4
Enter element value 9= 5
Enter element value 10= 7
Max=9, Min=1
Similar Programs:
Example 1: WAP to find out the largest or smallest of an array.
Example 2: WAP to printf sum of all array elements.
Example 3: WAP to read 10 numbers from user and store them in an array. Display Sum,
Minimum and Average of the numbers.
Program 19: W.A.P. in c for multiply two matrices A and B of dimensions pXq and qXr respectively
and store the result in third matrix C.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[3][3],b[3][3],c[3][3], i, j, sum=0, k;
int r1,c1,r2,c2;
clrscr( );
printf("enter details of array a\n");
printf("enter number of rows : ");
scanf("%d", &r1);
printf("enter number of column : ");
scanf("%d", &c1);
for( i=0 ; i<r1 ; i++ )
{
for( j=0 ; j<c1 ; j++ )
{
printf("Enter array element a[%d] [%d] = “, i, j );
scanf("%d", &a[i][j]);
}
}
printf("enter details of array b\n");
printf("enter number of rows : ");
scanf("%d",&r2);
printf("enter number of column : ");
scanf("%d",&c2);
for( i=0 ; i<r2 ; i++ )
{
for( j=0 ; j<c2 ; j++ )
{
printf("Enter array element b[%d] [%d] = “, i, j );
scanf("%d", &b[i][j]);
}
}
for( i=0 ; i<r1 ; i++ )
{
for( j=0 ; j<c2 ; j++ )
{
sum=0;
for(k=0;k<c1;k++)
{
sum = sum + a[i][k]*b[k][j];
}
c[i][j] = sum;
printf("%d\t", c[i][j]);
}
printf(“\n“);
}
getch();
}
Output:
Enter details of array a
Enter number of rows : 2
Enter number of column : 3
Enter array element a[0][0] = 1
Enter array element a[0][1] = 1
Enter array element a[0][2] = 1
Enter array element a[1][0] = 2
Enter array element a[1][1] = 2
Enter array element a[1][2] = 2
Enter details of array b
Enter number of rows : 3
Enter number of column : 2
Enter array element b[0][0] = 1
Enter array element b[0][1] = 1
Enter array element b[1][0] = 2
Enter array element b[1][1] = 2
Enter array element b[2][0] = 3
Enter array element b[2][1] = 3
6 6
12 12
Similar Programs:
Example 1: Write a program to display transpose of given 3*3 matrix.
Example 2: Develop a program to perform addition of two (3*3) matrix.
Program 20: Develop a menu-based program to perform addition, multiplication, subtraction and
division using user-defined function.
#include<stdio.h>
#include<conio.h>
void add(int a, int b);
void sub(int a, int b);
void mul(int a, int b);
void div(int a, int b);
void main( )
{
int x, y, choice;
clrscr( );
printf(“Enter first number : ");
scanf(“%d”, &x);
printf(“Enter second number : ");
scanf(“%d”, &y);
printf(“1 = add\n2 = sub\n3 = mul\n4 = div\n");
printf(“Enter choice : ");
scanf(“%d”, &choice);
switch(choice)
{
case 1:
add(x,y);
break;
case 2:
sub(x,y);
break;
case 3:
mul(x,y);
break;
case 4:
div(x,y);
break;
default:
printf("wrong choice");
break;
}
getch();
}
void add(int a, int b)
{
printf(“\nadd = %d“, a+b);
}
void sub(int a, int b)
{
printf(“\nsub = %d“, a-b);
}
void mul(int a, int b)
{
printf(“\nmul = %d“, a*b);
}
void div(int a, int b)
{
printf(“\ndiv = %f“, (float)a/b);
}
Output:
Enter first number : 10
Enter second number : 5
1 = add
2 = sub
3 = mul
4 = div
Enter choice : 4
div = 2.000000
Program 21: Create a function to check number is prime or not. If number is prime then function
return value 1 otherwise return 0. OR
WAP to check whether entered number is prime or not with the help of user-defined function check-
prime().
#include<stdio.h>
#include<conio.h>
int checkPrime(int num);
void main( )
{
int n, prime;
clrscr( );
printf(“Enter a number: ");
scanf(“%d”, &n);
prime = checkPrime(n); //Function Call
if( prime == 1 )
{
printf(“%d is prime number “, n);
}
else
{
printf(“%d is not prime number “, n);
}
getch();
}
int checkPrime (int num)
{
int i=2;
while ( i < num )
{
if(num%i == 0 )
{
return 0;
}
else
{
i++;
}
}
return 1;
}
Output:
Enter a number: 13
13 is prime number
Similar Programs:
Example 1: Create a function to swap the values of two variables.
Example 2: WAP to find Factorial of a Number using user defined function.
Example 3: W.A.P. find factorial of given number using Recursion.
Program 22: W.A.P. to print the address of variable using pointer. OR
W.A.P. to store and print the address of variable using pointer.
#include<stdio.h>
#include<conio.h>
void main()
{
int i=12;
int *ptr;
clrscr( );
printf(“\ni = %d”, i);
ptr = &i;
printf("\nAddress of i = %u”, &i);
printf("\nAddress of ptr = %u”, &ptr);
printf("\nValue contains by ptr = %d”, *ptr);
printf("\nValue of i = %d”, i);
getch( );
}
Output:
i = 12
Address of i = 6422044
Address of ptr = 6422032
Value contains by ptr = 12
Value of i = 12
Similar Programs:
Example 1: W.A.P. to add two numbers using pointer.
Example 2: W.A.P. to demonstrate pointer to pointer.
Program 23: Write a program in c using structure to enter rollno, marks of the three subject for 3
student and find total obtained by each student.
#include<stdio.h>
#include<conio.h>
struct marks
{
int rollno;
int sub1;
int sub2;
int sub3;
int total;
};
void main()
{
int i;
struct marks student[3];
clrscr( );
for( i=0 ; i<3 ; i++)
{
printf("enter student %d roll no : ", i+1);
scanf("%d", &student[i].rollno);
printf("enter student %d subject 1 marks : ", i+1);
scanf("%d", &student[i].sub1);
printf("enter student %d subject 2 marks : ", i+1);
scanf("%d", &student[i].sub2);
printf("enter student %d subject 3 marks : ", i+1);
scanf("%d", &student[i].sub3);
}
for(i=0;i<3;i++)
{
student[i].total = student[i].sub1 + student[i].sub2 + student[i].sub3;
}
printf("STUDENT \t TOTAL\n");
for(i=0; i<3; i++)
{
printf("Student[%d] \t %d\n", i+1, student[i].total);
}
getch();
}
Output:
enter student 1 roll no : 101
enter student 1 subject 1 marks : 50
enter student 1 subject 2 marks : 50
enter student 1 subject 3 marks : 50
enter student 2 roll no : 102
enter student 2 subject 1 marks : 60
enter student 2 subject 2 marks : 60
enter student 2 subject 3 marks : 60
enter student 3 roll no : 103
enter student 3 subject 1 marks : 70
enter student 3 subject 2 marks : 70
enter student 3 subject 3 marks : 70
STUDENT TOTAL
Student[1] 150
Student[2] 180
Student[3] 210
Program 24: Define a structure “personal” that would contain person name, date of joining and
salary. Using this structure read information of 5 people and print the same on screen. Also display
sum of salary of all 5 people.
#include<stdio.h>
#include<conio.h>
struct personal
{
char name[20];
char date[20];
float salary;
};
void main()
{
struct personal person[5];
int i;
clrscr( );
for( i=0 ; i<5 ; i++)

{
printf("enter details of person %d : \n", i+1);
printf("enter name : ");
gets(person[i].name);
printf("enter joining date (dd-month-year) : ");
gets(person[i].date);
printf("enter salary : ");
scanf("%f", &person[i].salary);
}
for(i=0;i<5;i++)
{
printf("\ndetails of %d person : ", i+1);
printf("name = %s\njoining date = %s\nsalary = %f\n" ,person[i].name,
person[i].date, person[i].salary);
}
getch();
}
Output:
enter details of 1 person :
enter name : donald
enter joining date (dd-month-year) : 10 jan 2022
enter salary : 50000
details of 1 person : name = donald
joining date = 10 jan 2022
salary = 50000.000000
Similar Programs:
Example 1: Define a structure data type called time_struct containing three member’s integer
hours, minutes, second. Develop a program that would assign values to individual member
and display the time in following format : HH:MM:SS.
Example 2: Define a structure cricket that will describe the following information: Player
name, Team name, Batting average. Using cricket, declare an array player with 50 elements
and WAP to read the information about all the 50 players and print team wise list containing
names of players with their batting average.
Program 25: W.A.P. to demonstrate use of malloc( ) and free( ).
#include<stdio.h>
#include<conio.h>
void main()
{
int *fp;
clrscr( );
fp = (int *)malloc(sizeof(int));
if(fp == NULL)
{
printf("Error! memory not allocated");
exit(0);
}
else
{
printf("Memory allocated : %u\n", fp);
}
*fp=25;
printf("value stored at address = %d", *fp);
free(fp);
getch( );
}
Output:
Memory allocated : 1599824
value stored at address = 25
Program 26: W.A.P. to use getc( ) and putc( ) function.
#include<stdio.h>
#include<conio.h>
void main()
{
char c;
FILE *fp;
clrscr( );
printf("\Data input");
fp = fopen("input.txt“ , "w");
printf("\nEnter data to write in file\n");
while((c=getchar())!='\n')
{
putc(c , fp);
}
fclose(fp);
printf("\nData output\n");
fp = fopen("input.txt“ , "r");
while((c=getc(fp))!=EOF)
{
printf("%c",c);
}
fclose(fp);
getch();
}
Output:
Data input
Enter data to write in file
this is demo of putc and getc function
Data output
this is demo of putc and getc function

Similar Programs:
Example 1: W.A.P. to use fscanf( ) and fprintf( ) function.
Example 2: W.A.P. to use fgetc( ) and fputc( ) function.
Example 3: W.A.P. to use fputs( ) function and fgets( ) in c program.

You might also like