0% found this document useful (0 votes)
68 views

CP Assignment Rakshit

The document contains solutions to 15 programming problems in C language. The problems cover topics like finding the largest of three numbers, arithmetic operations, quadratic equation, GCD, prime number check, Fibonacci series, palindrome check, square root, polynomial evaluation, linear search, binary search, selection sort, bubble sort, and matrix multiplication. For each problem, the question, solution code and sample output are given.

Uploaded by

Nicoles Java
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
68 views

CP Assignment Rakshit

The document contains solutions to 15 programming problems in C language. The problems cover topics like finding the largest of three numbers, arithmetic operations, quadratic equation, GCD, prime number check, Fibonacci series, palindrome check, square root, polynomial evaluation, linear search, binary search, selection sort, bubble sort, and matrix multiplication. For each problem, the question, solution code and sample output are given.

Uploaded by

Nicoles Java
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 31

PART A

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;

printf("enter the three numbers\n");

scanf("%d %d %d", &x,&y,&z);

lar=x>y?(x>z?x:z):(y>z?y:z);

printf("the largest number is %d",large);

return 0;

OUTPUT

the three numbers are

4 5 98

the largest number is 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;

printf("the operator is \n");

scanf("%c",operator);

printf("the numbers are\n");


scanf("%d %d",&a,&b);

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:

printf("the operator is wrong");

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;

printf("enter the three coeffcients");

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);

printf("the 2 roots are %f %f",r1,r2);

else if(dis=0)

r1=r2= -b/(2*a);

printf(" the roots are %f",r1);

if(dis<0)

real= -b/(2*a);

img= sqrt(-dis)/(2*a);

printf("r1=%f+%f and r2=%f-%f\n",real,img,real,img);

return 0;

}
OUTPUT

enter the three coeffcients 6 -17 12

the 2 roots are 16.916666 17.083334

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;

printf("enter the two numbers");

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;

printf("%d is GCD and %d is LCM\n",gcd,lcm);

OUTPUT

enter the two numbers 5 9

1 is GCD and 45 is LCM

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;

printf("Enter a positive integer");

scanf("%d", &n);

if (n==0 || n==1)

flag = 1;

for (i = 2; i <= n / 2; ++i)

if (n % i == 0)

flag = 1;

break;

if (flag == 1)

printf("%d is a not a prime number.", n);

else

printf("%d is a prime number.", n);


return 0;

OUTPUT

Enter a positive integer 16

16 is a not a prime number.

6. Design, develop and execute a C program to display Fibonacci sequence.

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("The Fibonacci series is\n");

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

The Fibonacci series is


0 1 1 2 3 5

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;

printf("enter the number:");

scanf("%d",&a);

temp=a;

while(a>0)

r=a%10;

s=r+(s*10);

a=a/10;

if(temp==s)

printf("the number is palindrome");

else

printf(" the number is not a palindrome");

return 0;

OUTPUT

enter the number: 2002

the number is palindrome

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;

printf("enter the number\n");

scanf("%f",&n);

x=1;

for(i=1;i<=10;i++)

root=((x*x)+n)/(2*x);

x= root;

printf("the root is %f\n",x);

return 0;

OUTPUT

enter the number

256

the root is 16.000000

9.

ANS:

#include <stdio.h>

#include <stdlib.h>

#define MAXSIZE 10

void main()

int array[MAXSIZE];

int i, num, power;

float x, polySum;
printf("Enter the order of the polynomial \n");

scanf("%d", &num);

printf("Enter the value of x \n");

scanf("%f", &x);

printf("Enter %d coefficients \n", num + 1);

for (i = 0; i <= num; i++)

scanf("%d", &array[i]);

polySum = array[0];

for (i = 1; i <= num; i++)

polySum = polySum * x + array[i];

power = num;

printf("Given polynomial is: \n");

for (i = 0; i <= num; i++)

if (power < 0)

break;

if (array[i] > 0)

printf(" + ");

else if (array[i] < 0)

printf(" - ");

else
printf(" ");

printf("%dx^%d ", abs(array[i]), power--);

printf("\n Sum of the polynomial = %6.2f \n", polySum);

OUTPUT:

Enter the order of the polynomial

Enter the value of x

Enter 7 coefficients

1234567

Given polynomial is:

+ 1x^6 + 2x^5 + 3x^4 + 4x^3 + 5x^2 + 6x^1 + 7x^0

Sum of the polynomial = 1636.00

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;

printf("enter the number of elements");

scanf("%d",&n);

printf("enter the elements");

for(i=0;i<n;i++)

scanf("%d",&a[i]);

printf(" enter the key to be searched:\n");

scanf("%d",&key);

for(i=0;i<n;i++)
{

if(a[i]==key)

printf("the key is at %d position",i+1);

count++;

if(count==0)

printf("Invalid key element!");

OUTPUT

enter the number of elements 2

enter the elements 16 19

enter the key to be searched:

16

the key is at 1 position

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];

printf("enter user name\n");

printf(" enter the total units consumed\n");


scanf("%s" ,name);

scanf("%f",&units);

if(units<=200)

total=100+((units-200)*0.80);

else if(units>200 && units <=300)

total=100+ 160 +((units-300)*0.90);

else if(units>300)

total=100+160+90+((units-300)*1);

printf("Name of user %s\n",name);

printf("Total %f\n",total);

if(total>400)

fin=total+(total*0.15);

else

fin= total;

printf(" the final amount to be paid is %f", fin);

getchar();

OUTPUT

enter user name:

MICHELLE

enter the total units consumed

222

Name of user MICHELLE

Total 189.800003

the final amount to be paid is 189.800003


12.Design, develop and execute a C program to search for a given key element in an array of ‘n’
elements using binary search technique

ANS:

#include<stdio.h>

int main()

int i, first, last, middle, n, key, array[100];

printf("Enter number of elements\n");

scanf("%d", &n);

printf("Enter %d integers\n", n);

for (i = 0; i < n; i++)

scanf("%d", &array[i]);

printf("Enter value to find\n");

scanf("%d", &key);

first = 0;

last = n - 1;

middle = (first+last)/2;

while (first <= last) {

if (array[middle] < key)

first = middle + 1;

else if (array[middle] == key)

printf("%d found at location %d \n",key, middle+1);

break;

else

last = middle - 1;

middle = (first + last)/2;

if (first > last)


printf("No Match!!\n",key);

return 0;

OUTPUT

Enter number of elements

Enter 5 integers

14 24 16 19 54

Enter value to find

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;

printf("Enter number of elements\n");

scanf("%d", &n);

printf("Enter %d integers\n", n);

for (i = 0; i < n; i++)

scanf("%d", &a[i]);

for (i = 0 ; i < n - 1; i++)

for (j = 0 ; j < n - i - 1; j++)

if (a[j] > a[j+1])

temp = a[j];

a[j] = a[j+1];
a[j+1] = temp;

printf("Sorted list in ascending order is\n");

for (i=0;i<n;i++)

printf("%d\n", a[i]);

return 0;

OUTPUT

Enter number of elements

Enter 5 integers

19 16 24 25 26

Sorted list in ascending order is

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;

printf("Enter no. of elements \n");

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;

printf("The sorted numbers in ascending order is\n");

for(i=0;i<n;i++)

printf("%d\n", a[i]);

OUTPUT

Enter no. of elements

Enter elements

19 16 24

The sorted numbers in ascending order is

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;

int a[10][10], b[10][10],ans[10][10];

printf("Enter the number of rows and columns of matrix A\n");


scanf("%d %d", &m, &n);

printf("Enter the elements of matrix A\n");

for(i=0;i<m;i++)

for(j=0;j<n;j++)

scanf("%d", &a[i][j]);

printf("Enter the number of rows and columns of matrix B\n");

scanf("%d %d", &p, &q);

if(n!=p)

printf("multiplication is not possible. Enter valid matrix");

else

printf("Enter the elements of matrix B\n");

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++)

sum = sum + a[i][k]*b[k][j];

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

Enter the number of rows and columns of matrix A:

22

Enter the elements of matrix A:

23 2

12 14

Enter the number of rows and columns of matrix B:

22

Enter the elements of matrix B:

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

Enter the number of students in class(<=10):5

Enter student 1 marks :86

Enter student 2 marks :98

Enter student 3 marks :99

Enter student 4 marks :100

Enter student 5 marks :97

Average marks = 96.00

student 1 marks = 86 below average

student 2 marks = 98 above average

student 3 marks = 99 above average

student 4 marks = 100 above average

student 5 marks = 97 above average


17.. Design, develop and execute a C program with an appropriate structure definition and
variable declaration to store information about an employee using nested structures. Consider the
following fields like Ename, Empid, DOJ(Date, Month, Year) and Salary (Basic, DA, HRA).

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);

printf("\nEnter the Employee ID:");


scanf("%d", &ed.id);

printf("\nEnter the date of birth of the Employee DD MM YYYY:");


scanf("%d", &ed.d);
scanf("%d", &ed.m);
scanf("%d", &ed.y);

printf("\nEnter the Employee salary:");


scanf("%f", &salary);
hra=0, da=0, gs=0;
hra = salary*(0.2);
hra = salary*(0.4);
gs = salary + hra + da;
}
for(i=0;i<n;i++)
{
printf("\nEmployee %d\n", i+1);
printf("\nName:%s", ed.name);
printf("\nID:%d", ed.id);
printf("\nDate of birth:%d - %d - %d",ed.d, ed.m, ed.y);
printf("\nSalary:%f", gs);
printf("\n");
}
return 0;
}

OUTPUT

Enter the Employee name: Rohit

Enter the employee ID: 4321

Enter the date of birth of the employee DD MM YYY: 14 - 8 – 2003

Enter employee salary: 34000

Name: Rohit

ID:4321

Date of birth: 14 – 8 –2003

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>

int fac(int x);

void main()

float x,Q,sum=0;

int i,j,limit;

printf("Enter the value of x of sinx series: ");

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 value of x of sinx series: 30

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 ()

float a[20], sum1 = 0, sum2 = 0, mean, var, dev;

int i, n;

printf ("Enter no of elements:");


scanf ("%d", &n);

printf ("Enter array elements:");

for (i = 0; i < n; i++)

scanf ("%f", a + i);

sum1 = sum1 + * (a + i);

mean = sum1 / n;

for (i = 0; i < n; i++)

sum2 = sum2 + pow ((*(a + i) - mean), 2);

var = sum2 / n;

dev = sqrt (var);

printf ("Sum :%f\n", sum1);

printf ("Mean :%f\n", mean);

printf ("Standard Deviation :%f\n", dev);

OUTPUT

Enter no of elements: 3

Enter array elements:

16

19

Sum :38.000000

Mean :12.666667

Standard Deviation :6.944222

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 compare(char [ ],char [ ]);

void concat(char [ ],char [ ]);

void length(char *[ ]);

void main( )

int n,digit;

char str1[10],str2[10];

do

printf("press 1-compare 2-concatenate 3-length of string");

printf("\n enter your choice=");

scanf("%d",&n);

switch(n)

case 1: printf("enter first string=");

scanf("%s",str1);

printf("enter second string=");

scanf("%s",str2);

compare(str1,str2);

break;

case 2: printf("enter first string=");

scanf("%s",str1);

printf("enter second string=");

scanf("%s",str2);

concat(str1,str2);

break;

case 3: printf("enter string=");

scanf("%s",str1);

length(&str1);

break;
default:printf("wrong choice");

break;

printf("\n Do you want to continue(1/0)? ");

scanf("%d", &digit);

}while(digit==1);

void compare(char str1[ ],char str2[ ])

int i;

i=strcmp(str1,str2);

if(i==0)

printf("strings are equal\n ");

else

printf("string are not equal\n");

void concat(char str1[ ],char str2[ ])

strcat(str1,str2);

printf("concatenate string=%s",str1);

void length(char *str1[ ])

int len;

len=strlen(str1);

printf("the length of string=%d",len);

OUTPUT

press 1-compare 2-concatenate 3-length of string

enter your choice=1

enter first string=MICHELLE


enter second string=DAVID

string are not equal

Do you want to continue(1/0)? 1

press 1-compare 2-concatenate 3-length of string

enter your choice=2

enter first string=MICHELLE

enter second string=DAVID

concatenate string=MICHELLEDAVID

Do you want to continue(1/0)? 1

press 1-compare 2-concatenate 3-length of string

enter your choice=3

enter string=MICHELLE

the length of string=8

OPEN ENDED EXPERIMENTS


1. Alan Turing was an English computer scientist, mathematician, logician and cryptanalyst whose
work has led him to be considered the father of theoretical computer science and artificial
intelligence. It is said that Turing saved “millions of lives” by cracking the Enigma code during
Second World War. It is important to encrypt and decrypt messages in intelligence services. One of
the fundamental steps in encryption is to find the frequency of characters in a message. Write and
execute a C program to find the frequency of characters in a string.

ANS

#include<stdio.h>

int main()

char str[1000],ch;

int count=0;

printf("Enter a string:");

fgets(str,sizeof(str),stdin);

printf("Enter a character to find its frequency ");

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

Enter a string: snake

Enter a character to find its frequency: n

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;

printf("enter IP address c");

scanf("%d",&c);

if(c>1&&c<=127)

printf("IP address of class A");

else if(c>=128&&c<=191)

printf("IP address of class B");

else if(c>=192&&c<=223)
{

printf("IP address of class C");

else if(c>=224&&c<=239)

printf("IP address of class D");

else if(c>=240&&c<=254)

printf("IP address of class E");

return 0;

OUTPUT

enter IP address c 222

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;

extern void write_extern();

main()

count=5;

write_extern();

#include<stdio.h>

extern int count;


void write_extern(void)

printf("count is %d\n",count);

OUTPUT

count is 5

Process returned 0 (0x0) execution time : 0.047 s

Press any key to continue.

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;

printf("enter the numbers a and b ");

scanf("%d %d",&a,&b);

printf("before swapping a=%d b=%d\n",a,b);

a=a+b;

b=a-b;

a=a-b;

printf("after swapping a=%d and b=%d",a,b);

return 0;

OUTPUT

enter the numbers a and b 16 19

before swapping a=16 b=19

after swapping a=19 and b=16

Process returned 0 (0x0) execution time : 10.862 s

Press any key to continue.


5..An Ethical hacker is trying to breach a security area and is investigating the patterns of a
password given to login to a server. He has a serious of tools to check the patterns of the
password. Simulate a program that can check whether the character is in upper case, lower case
or non alphabetic.

ANS:

#include<stdio.h>

void check(char ch)

if(ch>='A'&&ch<='z')

printf("\n%c is an uppercase character",ch);

else if(ch>='a'&&ch<='z')

printf("\n%c is an lowercase character",ch);

else

printf("\n%c is a non alphabetic character",ch);

int main()

char ch;

//get the character

ch='A';

// check the character

check(ch);

//get the character

ch='a';

// check the character

check(ch);

// get the character

ch='0';

// check the character

check(ch);

return 0;

}
OUTPUT

A is an uppercase character

a is an uppercase character

0 is a non alphabetic character

Process returned 0 (0x0) execution time : 0.047 s

Press any key to continue.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy