Professional Documents
Culture Documents
LUCKNOW
FACULTY-
NIHARIKA SRIVASTAVA
ASSISTANT PROFESSOR
M.TECH (SOFTWARE ENGINEERING)
1|Page
CENTRAL INSTITUTE OF PLASTICS ENGINEERING AND TECHNOLOGY,
LUCKNOW
Program 1
WAP that accepts the marks of 5 subjects and finds the sum and percentage marks obtained by
the student.
#include<stdio.h>
int main() {
int s1, s2, s3, s4, s5, sum, total = 500;
float per;
printf("\nEnter marks of 5 subjects : ");
scanf("%d %d %d %d %d", &s1, &s2, &s3, &s4, &s5);
sum = s1 + s2 + s3 + s4 + s5;
printf("\nSum : %d", sum);
per = (sum * 100) / total;
printf("\nPercentage : %f", per);
return (0);
}
Program 2
WAP that calculates the Simple Interest and Compound Interest. The Principal , Amount, Rate of
Interest and Time are entered through the keyboard.
#include<stdio.h>
void main()
{
float p,r,t,i;
clrscr();
printf("Enter principal amount : ");
scanf("%f",&p);
printf("\nEnter Rate of Interest : ");
scanf("%f",&r);
printf("\nEnter time period : ");
scanf("%f",&t);
i=(p*r*t)/100;
printf("\nInterest calculated is %f",i);
2|Page
CENTRAL INSTITUTE OF PLASTICS ENGINEERING AND TECHNOLOGY,
LUCKNOW
getch();
}
Program 3
WAP to calculate the area and circumference of a circle.
#include<stdio.h>
int main()
{
int rad;
float PI = 3.14, area, ci;
printf("\nEnter radius of circle: ");
scanf("%d", &rad);
area = PI * rad * rad;
printf("\nArea of circle : %f ", area);
ci = 2 * PI * rad;
printf("\nCircumference : %f ", ci);
return (0);
}
Program 4
WAP that accepts the temperature in Centigrade and converts into Fahrenheit using the formula
C/5=(F-32)/9.
#include<stdio.h>
int main()
{
float celsius, fahrenheit;
printf("\nEnter temp in Celsius : ");
scanf("%f", &celsius);
fahrenheit = (1.8 * celsius) + 32;
printf("\nTemperature in Fahrenheit : %f ", fahrenheit);
return (0);
}
3|Page
CENTRAL INSTITUTE OF PLASTICS ENGINEERING AND TECHNOLOGY,
LUCKNOW
Program 5
#include<stdio.h>
#include<conio.h>
int main()
{
int a,b,temp;
printf("enter two values\n");
printf("\na = ");
scanf("%d",&a);
printf("\nb = ");
scanf("%d",&b);
temp=a;
a=b;
b=temp;
printf("\na = %d\tb = %d ", a, b);
getch();
}
Program 6
WAP that checks whether the two numbers entered by the user are equal or not.
#include <stdio.h>
void main()
{
int m,n;
printf("Enter the values for M and N\n");
scanf("%d %d", &m,&n);
if(m == n )
printf("M and N are equal\n");
else
printf("M and N are not equal\n");
4|Page
CENTRAL INSTITUTE OF PLASTICS ENGINEERING AND TECHNOLOGY,
LUCKNOW
Program 7
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
Program 8
5|Page
CENTRAL INSTITUTE OF PLASTICS ENGINEERING AND TECHNOLOGY,
LUCKNOW
Program 9
Program 10
WAP that accepts marks of five subjects and finds percentage and prints grades according to
the following criteria:
Between 90-100%--------------Print ‘A’
80-90%----------------------------Print ‘B’
60-80%---------------------------Print ‘C’
Below 60%----------------------Print ‘D’
#include<stdio.h>
#include<conio.h>
void main()
{
float m1,m2,m3,m4,m5,percent;
clrscr();
printf("Enter the marks of the student in 5 subjects:\n");
scanf("%f%f%f%f%f",&m1,&m2,&m3,&m4,&m5);
percent=(m1+m2+m3+m4+m5)/5;
printf("\nPercentage=%f",percent);
if(percent>90.0 && percent<=100.0)
printf("\nGrade:A");
else if(percent>80.0 && percent<=90.0)
printf("\nGrade:B");
else if(percent>60.0 && percent<=80.0)
printf("\nGrade:C");
else
6|Page
CENTRAL INSTITUTE OF PLASTICS ENGINEERING AND TECHNOLOGY,
LUCKNOW
printf("\nGrade:D");
getch();
}
Program 11
WAP that takes two operands and one operator from the user and perform the operation and
prints the result by using Switch statement.
#include <stdio.h>
#include <conio.h>
void main()
{
int a, b, c;
char ch;
clrscr() ;
printf("Enter your operator(+, -, /, *, %)\n");
scanf("%c", &ch);
printf("Enter the values of a and b\n");
scanf("%d%d", &a, &b);
switch(ch)
{
case '+': c = a + b;
printf("addition of two numbers is %d", c);
break;
case '-': c = a - b;
printf("substraction of two numbers is %d", c);
break;
case '*': c = a * b;
printf("multiplication of two numbers is %d", c);
break;
case '/': c = a / b;
printf("remainder of two numbers is %d", c);
break;
case '%': c = a % b;
printf("quotient of two numbers is %d", c);
break;
default: printf("Invalid operator");
break;
}
getch();
}
7|Page
CENTRAL INSTITUTE OF PLASTICS ENGINEERING AND TECHNOLOGY,
LUCKNOW
Program 12
Program 13
8|Page
CENTRAL INSTITUTE OF PLASTICS ENGINEERING AND TECHNOLOGY,
LUCKNOW
Program 14
#include <stdio.h>
void main()
{
int i, num, odd_sum = 0, even_sum = 0;
printf("Enter the value of num\n");
scanf("%d", &num);
for (i = 1; i <= num; i++)
{
if (i % 2 == 0)
even_sum = even_sum + i;
else
odd_sum = odd_sum + i;
}
printf("Sum of all odd numbers = %d\n", odd_sum);
printf("Sum of all even numbers = %d\n", even_sum);
}
Program 15
#include<stdio.h>
int main() {
int first, second, sum, num, counter = 0;
printf("Enter the term : ");
scanf("%d", &num);
printf("\nEnter First Number : ");
scanf("%d", &first);
printf("\nEnter Second Number : ");
scanf("%d", &second);
printf("\nFibonacci Series : %d %d ", first, second);
while (counter < num) {
sum = first + second;
printf("%d ", sum);
first = second;
second = sum;
counter++;
}
return (0);
}
9|Page
CENTRAL INSTITUTE OF PLASTICS ENGINEERING AND TECHNOLOGY,
LUCKNOW
Program 16
WAP to check whether the entered number is prime or not.
#include <stdio.h>
int main()
{
int n, i, flag=0;
printf("Enter a positive integer: ");
scanf("%d",&n);
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);
return 0;
}
Program 17
WAP to find the sum of digits of the entered number
#include<stdio.h>
int main(){
int num,sum=0,r;
printf("Enter a number: ");
scanf("%d",&num);
while(num){
r=num%10;
num=num/10;
sum=sum+r;
}
printf("Sum of digits of number: %d",sum);
return 0;
}
10 | P a g e
CENTRAL INSTITUTE OF PLASTICS ENGINEERING AND TECHNOLOGY,
LUCKNOW
Program 18
Program 19
WAP to print Armstrong numbers from 1 to 100.
#include <stdio.h>
void main()
{
int number, temp, digit1, digit2, digit3;
11 | P a g e
CENTRAL INSTITUTE OF PLASTICS ENGINEERING AND TECHNOLOGY,
LUCKNOW
Program 20
WAP to convert binary number into decimal number and vice versa.
#include <stdio.h>
#include <math.h>
int binary_decimal(int n);
int decimal_binary(int n);
int main()
{
int n;
char c;
printf("Instructions:\n");
printf("1. Enter alphabet 'd' to convert binary to decimal.\n");
printf("2. Enter alphabet 'b' to convert decimal to binary.\n");
scanf("%c",&c);
if (c =='d' || c == 'D')
{
printf("Enter a binary number: ");
scanf("%d", &n);
printf("%d in binary = %d in decimal", n, binary_decimal(n));
}
if (c =='b' || c == 'B')
{
printf("Enter a decimal number: ");
scanf("%d", &n);
printf("%d in decimal = %d in binary", n, decimal_binary(n));
}
return 0;
}
{
int decimal=0, i=0, rem;
while (n!=0)
{
rem = n%10;
n/=10;
decimal += rem*pow(2,i);
++i;
}
return decimal;
}
Program 21
WAP that simply takes elements of the array from the user and finds the sum of these
elements.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[5],i,sum;
float avg=0.0;
printf("Enter the elements of array");
for(i=0;i<5;i++)
scanf("%d" ,&a[i]);
sum=a[0]+a[1]+a[2]+a[3]+a[4];
printf("\n The sum of array is %d " ,sum);
getch();
}
Program 22
WAP that inputs two arrays and saves sum of corresponding elements of these arrays in a third
array and prints them.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10];
13 | P a g e
CENTRAL INSTITUTE OF PLASTICS ENGINEERING AND TECHNOLOGY,
LUCKNOW
int b[10];
int sum[10];
int i , n;
clrscr();
printf(" Enter the size of Array A and B\n");
scanf("%d" , &n);
printf(" Enter the elements of Array A\n");
for(i=0 ; i<n ; i++)
{
scanf("%d" , &a[i]);
}
printf(" Enter the elements of Array B\n");
for(i=0 ; i<n ; i++)
{
scanf("%d" , &b[i]);
}
for(i=0 ; i<n ; i++)
sum[i] = a[i] + b[i];
printf(" Sum of elements of A and B\n");
{
printf("%d\n" , sum[i] );
}
}
Program 23
#include<stdio.h>
int main() {
int a[30], i, num, largest, smallest;
printf("\nEnter no of elements :");
scanf("%d", &num);
for (i = 0; i < num; i++)
scanf("%d", &a[i]);
largest = a[0];
for (i = 0; i < num; i++) {
if (a[i] > largest) {
largest = a[i];
}
}
printf("\nLargest Element : %d", largest);
smallest = a[0];
14 | P a g e
CENTRAL INSTITUTE OF PLASTICS ENGINEERING AND TECHNOLOGY,
LUCKNOW
Program 24
#include <stdio.h>
int main()
{
int array[100], search, c, n;
return 0;
}
Program 25
15 | P a g e
CENTRAL INSTITUTE OF PLASTICS ENGINEERING AND TECHNOLOGY,
LUCKNOW
WAP to sort the elements of the array inascending order using Bubble Sort technique
#include <stdio.h>
#define MAXSIZE 10
void main()
{ int array[MAXSIZE];
int i, j, num, temp;
16 | P a g e
CENTRAL INSTITUTE OF PLASTICS ENGINEERING AND TECHNOLOGY,
LUCKNOW
Program 26
.
WAP to add and multiply two matrices of order nxn.
#include<stdio.h>
int main() {
int i, j, mat1[10][10], mat2[10][10], mat3[10][10];
int row1, col1, row2, col2;
17 | P a g e
CENTRAL INSTITUTE OF PLASTICS ENGINEERING AND TECHNOLOGY,
LUCKNOW
return (0);
}
Program 27
WAP that finds the sum of diagonal elements of a mxn matrix.
#include<stdio.h>
int main() {
18 | P a g e
CENTRAL INSTITUTE OF PLASTICS ENGINEERING AND TECHNOLOGY,
LUCKNOW
return (0);
}
Program 28
WAP to implement strlen (), strcat (),strcpy () using the concept of Functions
strlen(string);
It calculates the length of the string and returns its length. For example:
#include<string.h>
string = "Mumbai";
printf("Length = %d",strlen(string));
The above code displays 5, because Mumbai consists of 5 characters. Note: it does not count null
character.
2. Joining two strings (strcat)
The syntax of strcat is
strcat(string1,string2);
Now it removes the null character from string1 and joins the first character of string2 at that
position. Now string1 consists of both string1 and string2 in joined form. Example:
#include<string.h>
19 | P a g e
CENTRAL INSTITUTE OF PLASTICS ENGINEERING AND TECHNOLOGY,
LUCKNOW
strcat(string1,string2);
strcmp(string1,string2);
It returns 0 if string1 is same as string2 and returns 1 if they are not same. Example:
#include<string.h>
if(strcmp(string1,string2)==0){
}else{
}
4. Copying one string to another (strcpy)
The syntax of strcpy is
strcpy(destination_string, source_string);
It copies the content of source_string to destination_string. Example:
#include<string.h>
strcpy(destination,source);
20 | P a g e
CENTRAL INSTITUTE OF PLASTICS ENGINEERING AND TECHNOLOGY,
LUCKNOW
These are some of the functions in string.h for string operation. To use these functions you must
include header file <string.h>. But we can make our own functions to perform above task
without including string,h. Here is the complete source code that has own functions find_length
(like strlen) to find length, join_strings( like strcat) for joining strings, compare_strings(like
strcmp) for comparing two strings and copy_string(like strcpy) to copy one string from another.
Observer carefully the code, if you are beginner, you will learn a lot of things about string
operation.
Program 30
WAP to swap two elements using the concept of pointers.
#include<stdio.h>
void swap(int *num1, int *num2) {
int temp;
temp = *num1;
*num1 = *num2;
*num2 = temp;
}
int main() {
int num1, num2;
printf("\nEnter the first number : ");
scanf("%d", &num1);
printf("\nEnter the Second number : ");
scanf("%d", &num2);
swap(&num1, &num2);
printf("\nFirst number : %d", num1);
printf("\nSecond number : %d", num2);
return (0);
}
Program 31
WAP to compare the contents of two files and determine whether they are same or not.
#include<stdio.h>
int main() {
FILE *fp1, *fp2;
int ch1, ch2;
char fname1[40], fname2[40];
21 | P a g e
CENTRAL INSTITUTE OF PLASTICS ENGINEERING AND TECHNOLOGY,
LUCKNOW
if (fp1 == NULL) {
printf("Cannot open %s for reading ", fname1);
exit(1);
} else if (fp2 == NULL) {
printf("Cannot open %s for reading ", fname2);
exit(1);
} else {
ch1 = getc(fp1);
ch2 = getc(fp2);
if (ch1 == ch2)
printf("Files are identical n");
else if (ch1 != ch2)
printf("Files are Not identical n");
fclose(fp1);
fclose(fp2);
}
return (0);
}
Program 32
WAP to check whether a given word exists in a file or not. If yes then find the number of times it
occurs.
#include "stdafx.h"
# include <stdio.h>
22 | P a g e
CENTRAL INSTITUTE OF PLASTICS ENGINEERING AND TECHNOLOGY,
LUCKNOW
# include <conio.h>
# include<string.h>
# include <stdlib.h>
# include <conio.h>
# include <stdlib.h>
checkRepeats(sentence, text);
while(sentence[i]!='\0')
{
if(sentence[i]==' ')
{
23 | P a g e
CENTRAL INSTITUTE OF PLASTICS ENGINEERING AND TECHNOLOGY,
LUCKNOW
words++;
i++;
wrd[i] = sentence[i];
if(strcmp(wrd,text)==1)
{
again++;
}
else
printf("The search word does not exist in the sentence");
}
if (len==0)
{
printf("\n\nRESULTS:\n\nNumber of words in the text: %d",words);
printf("\n\nCharacters:%d \n",len);
}
else
{
printf("\n\nRESULTS:\n\nNumber of words: %d",words+1);
printf("\nCharacters:%d \n",len-1);
}
24 | P a g e