Diploma Programme
SECOND SEMESTER SECOND SEMESTER
Common to E&C, CS&E & IS&E
1 WAP to swap the values of two variables without using third variable
Program code:
#include<stdio.h>
#include<conio.h>
void main()
{
int a, b;
clrscr();
printf(“enter the values of a and b\n ”);
scanf(“%d%d”,&a,&b);
printf(“the numbers before swapping: \n”);
printf(“a=%d \n”,a);
printf(“b=%d \n”,b);
a=a+b;
b=a-b;
a=a-b;
printf(“the numbers after swapping: \n”);
printf(“a=%d \n”,a);
printf(“b=%d \n”,b);
getch();
}
Output:
Enter the values of a and b
45
67
The numbers before swapping
a=45 b=67
the numbers after swapping
a=67 b=45
2 WAP to find the largest/smallest of 3 numbers (using conditional operator)
a.Program code:
#include<stdio.h>
#include<conio.h>
void main()
{
int a, b, c,large;
clrscr();
printf(“enter the values of the three numbers : \n”);
scanf(“%d%d%d”,&a,&b,&c);
large=(a>b)?a:b;
large=(large>c)?large:c;
printf(“the largest value of three numbers %d, %d and %d is %d\n”,a, b, c, large);
getch();
}
Output:
Enter the values of the three numbers:
23
45
67
The largest value of the numbers 23 ,45 and 67 is 67
b.Program code:
#include<stdio.h>
#include<conio.h>
void main()
{
int a, b, c,small;
clrscr();
printf(“enter the values of the three numbers : \n”);
scanf(“%d%d%d”,&a,&b,&c);
small=(a<b)?a:b;
small=(large<c)?small:c;
printf(“the largest value of three numbers %d, %d and %d is %d\n”,a, b, c, small);
getch();
}
Output:
Enter the values of the three numbers:
23
45
67
The smallest value of the numbers 23 ,45 and 67 is 23
3 WAP to calculate the total interset based on the following
principal amount (Rs) rate of interset
>=10000 20%
>=8000 && <=9999 18%
<8000 16%
include<stdio.h>#include<conio.h>Void main(){int p,t,r;float ti;
printf(“enter principalamount and time\ n”);
scanf(“%d%d”,&p,&t);
if(p>=10000)
r=20;
else if((p>=8000)&&(p<=9999))
r=18;
else if(p>8000)
r=16;
ti=(p*t*r)/100;
printf(“total interest=%f \ n”,ti);
getch();
}
OUT PUT:
Enter principal amount and time
5000
2
Total interest=33.000000
Enter principal amount and time
9000
18
Total interest=287.000000
4 WAP to find whether the given character is vowel or not (Use Switch)
#include<stdio.h>
int main()
{
char ch;
printf("Enter a character to check :: ");
scanf("%c",&ch);
switch(ch)
{
case 'a':
printf("\nThe entered character %c is a vowel",ch);
break;
case 'e':
printf("\nThe entered character %c is a vowel",ch);
break;
case 'i':
printf("\nThe entered character %c is a vowel",ch);
break;
case 'o':
printf("\nThe entered character %c is a vowel",ch);
break;
case 'u':
printf("\nThe entered character %c is a vowel",ch);
break;
default:
printf("\nThe entered character %c is a consonant",ch);
}
return 0;
}
OUTPUT:
Enter a character to check :: s
The entered character s is a consonant
Enter a character to check :: i
The entered character i is a vowel
Enter a character to check :: a
The entered character a is a vowel
5 WAP to calculate the roots of a quadratic equation(consider all
possible cases) ( Using Nested If and Switch)
#include<stdio.h>
#include<math.h>
int main()
{
int a, b, c, d;
int root1, root2, realPart, imaginaryPart;
printf("Enter cofficients (a, b, and c): ");
scanf("%d %d %d", &a, &b, &c);
printf("The quadratic equation: %dx²%+dx%+d \n",a,b,c);
d = (b*b) - 4*a*c; //discriminant
if( d>1 )
{
root1 = (-b+sqrt(d)) / 2*a;
root2 = (-b-sqrt(d)) / 2*a;
printf("Roots are %d and %d\n", root1, root2);
}
else if(d==0)
{
root1 = root2 = -b/2*a;
printf("Roots are %d and %d\n", root1, root2);
}
else
{
realPart = -b/2*a;
imaginaryPart = sqrt(d)/2*a;
printf("root1 = %d + i(%d)\n", realPart, imaginaryPart);
printf("root1 = %d - i(%d)\n", realPart, imaginaryPart);
}
return 0;
}
Output for different test cas
Enter cofficients (a, b, and c): 1 -1 -6
The quadratic equation: 1x²-1x-6
Roots are 3 and -2
Enter cofficients (a, b, and c): 1 -12 36
The quadratic equation: 1x²-12x+36
Roots are 6 and 6
Enter cofficients (a, b, and c): 1 4 5
The quadratic equation: 1x²+4x+5
root1 = -2 + i(-2147483648)
root1 = -2 – i(-2147483648)
6 WAP to sum & reverse a given integer
#include<stdio.h>
int main(){
int num,sum=0,reverse=0,r;
printf("Enter a number: ");
scanf("%d",&num);
while(num)
{
r=num%10;
reverse = reverse * 10 + remainder;
num=num/10;
sum=sum+r;
}
printf("Sum of digits of number: %d",sum);
printf("Reversed number = %d", reverse);
return 0;
}
Output
Enter a number: 123
Reversed number 321
Sum of digits of number: 6
7 WAP to check whether the given number is prime or not
#include<stdio.h>
#include<conio.h>
void main()
{
int num, i ,count=0;
clrscr();
printf(“enter the number :\n”);
scanf(“%d”,&num);
for(i=2;i<=num/2;i++)
{
if(num%i==0)
{
count=count+1;
break;
}
}
if(count==0 && num!=0)
printf(“%d is a prime number\n”,num);
else
printf(“%d is not a prime number \n”,num);
getch();
}
Output:
Enter the number
33
33 is a prime number
8 WAP to find the number of and sum of all integers greater than 100 and less
than 200 that are divisible by 7.
9 WAP to detect the Armstrong numbers in three digitss from 100 to 999.
note:- if sum of the digits of the number is equal to number itself, then the
no. is called armstrong no
#include<stdio.h>
int main()
{
int number, temp, rem, sum=0;
printf("Enter number: ");
scanf("%d",&number);
temp=number;
while( number!=0 )
{
rem = number%10;
sum += (rem*rem*rem);
number /= 10; //number=number/10
}
if(temp == sum)
printf("%d is an Armstrong number of order 3.\n",temp);
else
printf("%d is not an Armstrong number of order 3.\n",temp);
return 0;
}
Output:-
Enter number: 150
150 is not an Armstrong number of order 3.
10 WAP to evaluate sine series
#include<stdio.h>
#include<conio.h>
void main()
{
int i, n;
float x, sum, t;
clrscr();
printf(" Enter the value for x : ");
scanf("%f",&x);
printf(" Enter the value for n : ");
scanf("%d",&n);
x=x*3.14159/180;
t=x;
sum=x;
/* Loop to calculate the value of Sine */
for(i=1;i<=n;i++)
{
t=(t*(-1)*x*x)/(2*i*(2*i+1));
sum=sum+t;
}
printf(" The value of Sin(%f) = %.4f",x,sum);
getch();
}
11 WAP to generate fibanocci series
#include<stdio.h>
#include<conio.h>
void main()
{
int n,f1=0,f2=1,next,I;
clrscr();
printf(“enter the number of terms for series \n”);
scanf(“%d”,&n);
printf(“print %d terms of Fibonacci series are\n”,n);
for(i=0;i<n;i++)
{
if(i<=1)
next=i;
else
{
next=f1+f2;
f1=f2;
f2=next;
}
printf(“%d\n”, next);
}
getch ();
}
Output:
enter the number of terms for series
5
5 terms of Fibonacci series are
0 1 1 2 3
12 WAP to convert binary to decimal & vice versa
#include<stdio.h>
#include<conio.h>
void main()
{
int n, i=1, bin=0, rem;
clrscr();
printf(“enter an integer in decimal numbers system \n”);
scanf(“%d”,&n);
printf(“%d in binary numbers system is:\n”);
while(n!=0)
{
rem=n%2;
n=n/2;
bin=bin+(rem*i);
i=i*10;
}
printf(“bin=%d\n”, bin);
getch();
}
Output:
enter an integer in decimal numbers system
5
5 in binary numbers system is 101
#include<stdio.h>
#include<math.h>
int main()
{
int bin, dec=0, rem, i=0;
printf("Enter Binary number: ");
scanf("%d",&bin);
while(bin!=0)
{
rem = bin % 10;
dec += (rem * pow(2,i));
bin /= 10;
i++;
}
printf("Decimal = %d ",dec);
return 0;
}
Output:-
Enter binary number: 11101
Decimal value=29
13 WAP to calculate factorial of a given number using function
#include<stdio.h>
#include<conio.h>
void main()
{
int fact=1, n,i ;
clrscr();
printf(“enter the number: \n”);
scanf(“%d”,&n);
for(i=0;i<=n;i++)
{
fact=fact*i;
}
printf(“factorial of a number %d is %d\n”,n,fact);
getch();
}
Output:
Enter the number
5
Factorial of a number 4 is 24
14 WAP to find GCD of two numbers using function
#include<stdio.h>
int gcd(int a, int b);
int main()
{
int num1, num2;
printf("Enter two numbers : ");
scanf("%d %d",&num1, &num2);
int result = gcd(num1, num2);
printf("The GCD of %d and %d = %d", num1, num2, result);
return 0;
}
// User-defined function to find gcd of two numbers
int gcd(int a, int b)
{
int hcf;
for(int i=1; i<=a && i<=b; i++)
{
if(a%i==0 && b%i==0)
{
hcf = i;
}
}
return hcf;
}
Output
Enter two numbers : 12 20
The GCD of 12 and 20 = 4
Enter two numbers: 20 100
The GCD of 20 and 100 = 20
15 WAP to find sum and count of positive and negative numbers in an array
16 WAP to search for a given number in an array
#include<stdio.h>
#include<conio.h>
void main()
{
int a[50],i,n,search;
clrscr();
printf(“enter the number of elements in an array\n”);
scanf(“%d”,&n);
printf(“enter the elements of the array\n”);
for(i=0;i<n;i++)
scanf(“%d”,&a[i]);
printf(“enter the number to be search\n”);
scanf(“%d”,&search);
for(i=0;i<n;i++)
{
if(a[i]==search)
{
printf(“%d is present at %d location\n”,search,i+1);
break;
}
}
if(i==n)
printf(“%d is not present in an given array\n”,search);
getch();
}
Output:
Enter the number of elements in an array
4
Enter the elements of the array
7
5
8
9
Enter the number to be search
5
5 is present at location 2
17 WAP to find the transponse of a given matrix
#include <stdio.h>
int main() {
int a[10][10], transpose[10][10], r, c;
printf("Enter rows and columns: ");
scanf("%d %d", &r, &c);
// asssigning elements to the matrix
printf("\nEnter matrix elements:\n");
for (int i = 0; i < r; ++i)
for (int j = 0; j < c; ++j) {
printf("Enter element a%d%d: ", i + 1, j + 1);
scanf("%d", &a[i][j]);
}
// printing the matrix a[][]
printf("\nEntered matrix: \n");
for (int i = 0; i < r; ++i)
for (int j = 0; j < c; ++j) {
printf("%d ", a[i][j]);
if (j == c - 1)
printf("\n");
}
// computing the transpose
for (int i = 0; i < r; ++i)
for (int j = 0; j < c; ++j) {
transpose[j][i] = a[i][j];
}
// printing the transpose
printf("\nTranspose of the matrix:\n");
for (int i = 0; i < c; ++i)
for (int j = 0; j < r; ++j) {
printf("%d ", transpose[i][j]);
if (j == r - 1)
printf("\n");
}
return 0;
}
OUTPUT:
Enter rows and columns: 2
3
Enter matrix elements:
Enter element a11: 1
Enter element a12: 4
Enter element a13: 0
Enter element a21: -5
Enter element a22: 2
Enter element a23: 7
Entered matrix:
1 4 0
-5 2 7
Transpose of the matrix:
1 -5
4 2
0 7
18 WAP to addition two matrices
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10][10], b[10][10], c[10][10], i, j, m, n;
clrscr();
printf(“enter the size of row and colums :\n”);
scanf(“%d%d”, &m, &n);
printf(“enter the elements of first matrix :\n”);
for(i=0; i<m; i++)
{
for(j=0; j<n; j++)
{
scanf(“%d”,(*(a+i)+j));
}
}
printf(“enter the elements of second matrix :\n”);
for(i=0; i<m; i++)
{
for(j=0;j<n;j++)
{
scanf(“%d”,(*(b+i)+j));
}
}
printf(“the sum of two matrices is :\n”);
for(i=0; i<m; i++);
{
for(j=0; j<n; j++)
{
*(*(c+i)+j)=*(*(a+i)+j)*+(*(b+i)+j);
}
printf(“%d\t”,*(*(c+i)+j));
}
printf(“\n”);
getch();
}
Output:
enter the size of row and colums :
2
2
enter the elements of first matrix :
2
3
6
5
enter the elements of second matrix :
6
5
4
3
the sum of two matrices is :
8 8
10 8
19 WAP to mulitply two matrices
#include<stdio.h>
#include<stdlib.h>
int main(){
int a[10][10],b[10][10],mul[10][10],r,c,i,j,k;
system("cls");
printf("enter the number of row=");
scanf("%d",&r);
printf("enter the number of column=");
scanf("%d",&c);
printf("enter the first matrix element=\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("enter the second matrix element=\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&b[i][j]);
}
}
printf("multiply of the matrix=\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
mul[i][j]=0;
for(k=0;k<c;k++)
{
mul[i][j]+=a[i][k]*b[k][j];
}
}
}
//for printing result
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("%d\t",mul[i][j]);
}
printf("\n");
}
return 0;
}
Output:
enter the number of row=3
enter the number of column=3
enter the first matrix element=
111
222
333
enter the second matrix element=
111
222
333
multiply of the matrix=
666
12 12 12
18 18 18
20 WAP to sort a list of names
21 WAP to check whether the given string is palindrome or not
#include<stdio.h>
int main()
{
int n,r,sum=0,temp;
printf("enter the number=");
scanf("%d",&n);
temp=n;
while(n>0)
{
r=n%10;
sum=(sum*10)+r;
n=n/10;
}
if(temp==sum)
printf("palindrome number ");
else
printf("not palindrome");
return 0;
}
Output:
enter the number=151
palindrome number
enter the number=5621
not palindrome number
22 WAP to create a structure with employee details and display the same
#include <stdio.h>
#include <stdlib.h>
typedef struct{
char name[30];
int id;
double salary;
} Employee;
int main()
{
//number of employees
int n=2;
//array to store structure values of all employees
Employee employees[n];
//Taking each employee detail as input
printf("Enter %d Employee Details \n \n",n);
for(int i=0; i<n; i++){
printf("Employee %d:- \n",i+1);
//Name
printf("Name: ");
scanf("%[^\n]s",employees[i].name);
//ID
printf("Id: ");
scanf("%d",&employees[i].id);
//Salary
printf("Salary: ");
scanf("%lf",&employees[i].salary);
//to consume extra '\n' input
char ch = getchar();
printf("\n");
}
//Displaying Employee details
printf("-------------- All Employees Details ---------------\n");
for(int i=0; i<n; i++){
printf("Name \t: ");
printf("%s \n",employees[i].name);
printf("Id \t: ");
printf("%d \n",employees[i].id);
printf("Salary \t: ");
printf("%.2lf \n",employees[i].salary);
printf("\n");
}
return 0;
}
The output of the Program
23WAP to process student structure containing roll number, class and age as
members. The program must read 5 student record in an array of structure
and display the details of a student who is eldest. Use a function to find the
eldest for which array of structure is an argument.
24 WAP to count the number of constants, vowels, digits, white space &
special characters in a given sentence.
#include <stdio.h>
int main() {
char line[150];
int vowels, consonant, digit, space;
// initialize all variables to 0
vowels = consonant = digit = space = 0;
// get full line of string input
printf("Enter a line of string: ");
fgets(line, sizeof(line), stdin);
// loop through each character of the string
for (int i = 0; line[i] != '\0'; ++i) {
// convert character to lowercase
line[i] = tolower(line[i]);
// check if the character is a vowel
if (line[i] == 'a' || line[i] == 'e' || line[i] == 'i' ||
line[i] == 'o' || line[i] == 'u') {
// increment value of vowels by 1
++vowels;
}
// if it is not a vowel and if it is an alphabet, it is a consonant
else if ((line[i] >= 'a' && line[i] <= 'z')) {
++consonant;
}
// check if the character is a digit
else if (line[i] >= '0' && line[i] <= '9') {
++digit;
}
// check if the character is an empty space
else if (line[i] == ' ') {
++space;
}
}
printf("Vowels: %d", vowels);
printf("\nConsonants: %d", consonant);
printf("\nDigits: %d", digit);
printf("\nWhite spaces: %d", space);
getch();
}
OUTPUT:
Enter a line of string: C++ 20 is the latest version of C++ yet.
Vowels: 9
Consonants: 16
Digits: 2
White spaces: 8
25 WAP to demonstrate # define function.
#include <stdio.h>
#define PI 3.14
main() {
printf("%f",PI);
}
OUTPUT:
3.14000
Scheme of evaluation
1. Record 05
2. Writing two programs 30
3. Entering one program 10
4. Executing one program 25
5. Print out with results 10
6. Viva voce 20
Total 100 marks
Reference Books:
1. Programming in C - E.Balaguruswamy
2. Let Us 'C' - Yashavant Kanetkar
3. Programming with ANSI and Turbo C --- Ashok N. Kamthane
4. The Spirit of 'C' - MULLISH COOPER
5. Basics of Computers and Applications with C programming - Rajesh
Hongal
6. A computer Laboratory Referral for Diploma and Engineering Students,
Universities Press, T R Jagadeesh et al