CP Assignment Rakshit
CP Assignment Rakshit
1. Design, develop and execute a C program to find the biggest of three numbers using
ternary operator
#incude<stdio.h>
int main()
int x,y,z,large;
lar=x>y?(x>z?x:z):(y>z?y:z);
return 0;
OUTPUT
4 5 98
2. Design, develop and execute a C program to find the biggest of three numbers using ternary
operator
ANS:
#include<stdio.h>
int main()
int a,b,answer;
char operator;
scanf("%c",operator);
switch(operator)
case'+':
answer=a+b;
printf("%d",answer);
break;
case'-':
answer=a-b;
printf("%d",answer);
break;
case'*':
answer=a*b;
printf("%d",answer);
break;
case'/':
answer=a/b;
printf("%d",answer);
break;
default:
return 0;
OUTPUT
The operator is
2 4
8
3.. Design, develop and execute a C program to Compute the roots of a quadratic equation by
accepting the coefficients. Print appropriate messages.
ANS:
#include<stdio.h>
int main()
float a,b,c,dis,real,img,r1,r2;
scanf("%f %f %f",&a,&b,&c);
dis=b*b-4*a*c;
if(dis>0)
r1= -b-sqrt(dis)/(2*a);
r2= -b+sqrt(dis)/(2*a);
else if(dis=0)
r1=r2= -b/(2*a);
if(dis<0)
real= -b/(2*a);
img= sqrt(-dis)/(2*a);
return 0;
}
OUTPUT
4. Design, develop and execute a C Program to find GCD and LCM of two numbers using Euclids
algorithm.
ANS:
#include<stdio.h>
int main()
int n1,n2,dr,nr,gcd,lcm,r;
scanf("%d %d",&n1,&n2);
if(n1>n2)
nr=n1;
dr=n2;
else
nr=n2;
dr=n1;
r=nr%dr;
while(r!=0)
nr=dr;
dr=r;
r=nr%dr;
gcd=dr;
lcm=n1*n2/gcd;
OUTPUT
5. .Design, develop and execute a C program to check whether a given number is prime or not
ANS:
#include<stdio.h>
int main()
int n, i, flag = 0;
scanf("%d", &n);
if (n==0 || n==1)
flag = 1;
if (n % i == 0)
flag = 1;
break;
if (flag == 1)
else
OUTPUT
ANS:
#include<stdio.h>
int main()
int n,i,n1,n2,fib,temp;
printf("enter no of terms");
scanf("%d",&n);
n1=0;
n2=1;
printf("%d\t",n1);
printf("%d\t",n2);
for(i=3;i<=n;i++)
temp = n1+n2;
printf("%d\t", temp);
n1= n2;
n2= temp;
OUTPUT
enter no of terms 6
7. Design, develop and execute a C program find the reverse of a positive integer and check for
palindrome or not
ANS:
#include<stdio.h>
int main()
int a,temp,r,s=0;
scanf("%d",&a);
temp=a;
while(a>0)
r=a%10;
s=r+(s*10);
a=a/10;
if(temp==s)
else
return 0;
OUTPUT
8.. Design, develop and execute a C program to find the square root of a given number N
ANS:
#include<stdio.h>
int main()
float x,n,i,root;
scanf("%f",&n);
x=1;
for(i=1;i<=10;i++)
root=((x*x)+n)/(2*x);
x= root;
return 0;
OUTPUT
256
9.
ANS:
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 10
void main()
int array[MAXSIZE];
float x, polySum;
printf("Enter the order of the polynomial \n");
scanf("%d", &num);
scanf("%f", &x);
scanf("%d", &array[i]);
polySum = array[0];
power = num;
if (power < 0)
break;
if (array[i] > 0)
printf(" + ");
printf(" - ");
else
printf(" ");
OUTPUT:
Enter 7 coefficients
1234567
10..Design, develop and execute a C program to search for a given key element in an array of ‘n’
elements using linear search technique
ANS:
#include<stdio.h>
int main()
int a[20],i,n,key,count=0;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
scanf("%d",&key);
for(i=0;i<n;i++)
{
if(a[i]==key)
count++;
if(count==0)
OUTPUT
16
PART B
11. An electricity board charges the following rates for the use of electricity: for the first 200 units
80 paise per unit: for the next 100 units 90 paise per unit: beyond 300 units Rs 1 per unit. All users
are charged a minimum of Rs. 100 as meter charge. If the total amount is more than Rs 400, then
an additional surcharge of 15% of total amount is charged. Write a program to read the name of
the user, number of units consumed and print out the charges.
Ans:
#include<stdio.h>
int main()
float units,total,fin;
char name[29];
scanf("%f",&units);
if(units<=200)
total=100+((units-200)*0.80);
else if(units>300)
total=100+160+90+((units-300)*1);
printf("Total %f\n",total);
if(total>400)
fin=total+(total*0.15);
else
fin= total;
getchar();
OUTPUT
MICHELLE
222
Total 189.800003
ANS:
#include<stdio.h>
int main()
scanf("%d", &n);
scanf("%d", &array[i]);
scanf("%d", &key);
first = 0;
last = n - 1;
middle = (first+last)/2;
first = middle + 1;
break;
else
last = middle - 1;
return 0;
OUTPUT
Enter 5 integers
14 24 16 19 54
16
16 found at location 3
13. 3.Design, develop and execute a C program to sort ‘n’ elements of an array using selection sort
technique
ANS:
int main()
int a[100],n,i,j,temp;
scanf("%d", &n);
scanf("%d", &a[i]);
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
for (i=0;i<n;i++)
printf("%d\n", a[i]);
return 0;
OUTPUT
Enter 5 integers
19 16 24 25 26
16 19 24 25 26
14..Design, develop and execute a C program to sort ‘n’ elements of an array using bubble sort
technique.
ANS:
#include<stdio.h>
int main()
int a[10],n,i,j,min,temp;
scanf("%d",&n);
printf("Enter elements\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n-1;i++)
{
min=i;
for(j=i+1;j<n;j++)
if(a[j]<a[min])
min=j;
temp=a[min];
a[min]=a[i];
a[i]=temp;
for(i=0;i<n;i++)
printf("%d\n", a[i]);
OUTPUT
Enter elements
19 16 24
16 19 24
15. Design, develop and execute a C program to Implement Matrix multiplication and validate the
rules of multiplication
ANS:
#include<stdio.h>
int main()
int m, n, p, q, i, j, k, sum=0;
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d", &a[i][j]);
if(n!=p)
else
for(i=0;i<p;i++)
for(j=0;j<q;j++)
scanf("%d", &b[i][j]);
for(i=0;i<m;i++)
for(j=0;j<q;j++)
for(k=0;k<p;k++)
ans[i][j] = sum;
sum = 0;
}
}
printf("Product of matrices:\n");
for(i=0;i<m;i++)
for(j=0;j<q;j++)
printf("%d\t", ans[i][j]);
printf("\n");
OUTPUT
22
23 2
12 14
22
12 45
32
Product of matrices:
6 4
42 28
16. Design, develop and execute a C program to implement structures to read, write and compute
average- marks and the students scoring above and below the average marks for a class of N
students
Ans:
# include<stdio.h>
#include<conio.h>
struct student{
int marks;
}st[10];
void main()
{
int i,n;
float total=0,avgmarks;
//clrscr();
printf("\nEnter the number of students in class(<=10):");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter student %d marks :",i+1);
scanf("%d",&st[i].marks);
}
for(i=0;i<n;i++)
{
total = total + st[i].marks;
}
avgmarks=total/n;
printf("\nAverage marks = %.2f",avgmarks);
for(i=0;i<n;i++)
{
if(st[i].marks>=avgmarks)
{
printf("\n student %d marks = %d above average",i+1,st[i].marks);
}
else
{
printf("\n student %d marks = %d below average",i+1,st[i].marks);
}
}
getch();
}
OUTPUT
ANS:
#include <stdio.h>
struct employeedetail
{
char name[20];
int id, d, m, y;
float salary, hra, da, gs;
};
struct employeedetail ed;
int main()
{
int n, name, i, d, m, y;
float salary, hra, da, gs;
printf("Enter the number of employees:");
scanf("%d", &n);
for(i=0;i<n;i++)q
{
printf("\nEmployee %d details", i+1);
printf("\nEnter the Employee name:");
scanf("%s", ed.name);
OUTPUT
Name: Rohit
ID:4321
Salary: 34000
18. Design, develop and execute a C program to compute sin(x) using Taylor series approximation.
Compare your result with the built-in library function. Print both the results with appropriate
inferences.
ANS:
#include<stdio.h>
#include<math.h>
#include<conio.h>
void main()
float x,Q,sum=0;
int i,j,limit;
scanf("%f",&x);
printf("Enter the limit upto which you want to expand the series");
scanf("%d",&limit);
Q=x;
x = x*(3.1415/180);
for(i=1,j=1;i<=limit;i++,j=j+2)
{
if(i%2!=0)
sum=sum+pow(x,j)/fac(j);
else
sum=sum-pow(x,j)/fac(j);
printf("Sin(%0.1f): %f",Q,sum);
getch();
int fac(int x)
int i,fac=1;
for(i=1;i<=x;i++)
fac=fac*i;
return fac;
OUTPUT
Enter the limit upto which you want to expand the series 5
Sin(30.0): 0.499987
19. Develop a C program using pointers to compute the sum, mean and standard deviation of all
elements stored in an array of N real numbers.
ANS:
#include<stdio.h>
#include<math.h>
void main ()
int i, n;
mean = sum1 / n;
var = sum2 / n;
OUTPUT
Enter no of elements: 3
16
19
Sum :38.000000
Mean :12.666667
20. Design, develop and execute a C program using functions to implement string operations such
as compare, concatenate, string length. Convince the parameter passing techniques. Do not use in-
built string functions
ANS:
#include<stdio.h>
#include<string.h>
void main( )
int n,digit;
char str1[10],str2[10];
do
scanf("%d",&n);
switch(n)
scanf("%s",str1);
scanf("%s",str2);
compare(str1,str2);
break;
scanf("%s",str1);
scanf("%s",str2);
concat(str1,str2);
break;
scanf("%s",str1);
length(&str1);
break;
default:printf("wrong choice");
break;
scanf("%d", &digit);
}while(digit==1);
int i;
i=strcmp(str1,str2);
if(i==0)
else
strcat(str1,str2);
printf("concatenate string=%s",str1);
int len;
len=strlen(str1);
OUTPUT
concatenate string=MICHELLEDAVID
enter string=MICHELLE
ANS
#include<stdio.h>
int main()
char str[1000],ch;
int count=0;
printf("Enter a string:");
fgets(str,sizeof(str),stdin);
scanf("%c",ch);
for(int i=o;str[i]!='\0';++i)
{
if(ch==str[i])
++count;
printf("frequency of %c=%d",ch,count);
return 0;
OUTPUT
Frequency of n = 1
2.. Every computer system connected to Internet, communicate with the network through
Internet protocol (IP) address. Design and execute a C program to find the class of IP Address.
ANS:
#include<stdio.h>
int main()
int c;
scanf("%d",&c);
if(c>1&&c<=127)
else if(c>=128&&c<=191)
else if(c>=192&&c<=223)
{
else if(c>=224&&c<=239)
else if(c>=240&&c<=254)
return 0;
OUTPUT
IP address of class C
3. A program (Software) can be split up into multiple files. This makes it easier to edit and
understand, especially in the case of large programs, it also allows the individual parts to be
compiled independently. The functions and global variables of a C program can be split across
multiple files. Each of the files can be compiled separately, into a *.o file. Later, all the *.o files can
be linked together into a running program, an executable object program. Write, execute and
demonstrate the use of storage class specifier extern to create a C program with multiple files.
ANS:
#include<stdio.h>
int count;
main()
count=5;
write_extern();
#include<stdio.h>
printf("count is %d\n",count);
OUTPUT
count is 5
4. Programming is an art, any program may be implemented using different approaches. Design,
Implement and execute a C program to swap two Integers without using an extra variable.
(Sometimes, thinking out of the box would solve the problem in much faster way).
ANS:
#include<stdio.h>
int main()
int a,b;
scanf("%d %d",&a,&b);
a=a+b;
b=a-b;
a=a-b;
return 0;
OUTPUT
ANS:
#include<stdio.h>
if(ch>='A'&&ch<='z')
else if(ch>='a'&&ch<='z')
else
int main()
char ch;
ch='A';
check(ch);
ch='a';
check(ch);
ch='0';
check(ch);
return 0;
}
OUTPUT
A is an uppercase character
a is an uppercase character