You are on page 1of 64

CE143:Computer Concepts & Programming Roll No

Practicals
Program: 7.1 Twenty five numbers are entered from the keyboard into an array. Write a program
to find out how many of them are positive, negative, how many are even and odd.

Flowchart

U & P U Patel Department of Computer Engineering Page 1


CE143:Computer Concepts & Programming Roll No

Algorithm

Code #include<stdio.h>
int main()
{
int i,array[25];
for(i=0;i<25;i++)
{
printf("Enter 25 numbers:");
scanf("%d",&array[i]);
}
for(i=0;i<25;i++)
{
if (array[i]<0)
printf("%d is negative\n",array[i]);

U & P U Patel Department of Computer Engineering Page 2


CE143:Computer Concepts & Programming Roll No

else
printf("%d is positive\n",array[i]);
if(array[i]%2==0)
printf("Even number\n");
else
printf("Odd number\n");
}
return 0;
}

Output

Program: 7.2 Write a program for creating two arrays of different size and merge both
arrays into one by sorting those arrays in ascending order. [Merge by sorting]

U & P U Patel Department of Computer Engineering Page 3


CE143:Computer Concepts & Programming Roll No

Flowchart

U & P U Patel Department of Computer Engineering Page 4


CE143:Computer Concepts & Programming Roll No

Algorithm

Code #include<stdio.h>
int main()
{
int size_arrayA[20],size_arrayB[20],size_arrayC[30],x,y,i,j,k=0;
printf("Enter the size of array A:");
scanf("%d",&x);

printf("\n Enter the sorted elements of array A:\n");


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

U & P U Patel Department of Computer Engineering Page 5


CE143:Computer Concepts & Programming Roll No

scanf("%d",&size_arrayA[i]);
}
printf("Enter the size of array B:");
scanf("%d",&y);

printf("\n Enter the sorted elements of array B:\n");


for(i=0;i<y;i++)
{
scanf("%d",&size_arrayB[j]);
}
i=0;
j=0;
while(i<size_arrayA && j<size_arrayB)
{
if(size_arrayA[i] < size_arrayB[j])
{
size_arrayC[k] = size_arrayA[i];
k++;
i++;
}
else
{
size_arrayC[k] = size_arrayB[j];
k++;
j++;
}
while(i<size_arrayA)

U & P U Patel Department of Computer Engineering Page 6


CE143:Computer Concepts & Programming Roll No

{
size_arrayC[k] = size_arrayA[i];
k++;
i++;
}
while(j<size_arrayB)
{
size_arrayC[k] =size_arrayB[j];
k++;
j++;
}
}
return 0;
}

Output

Program: 7.3 Write a Program to multiply any two 3*3 Matrices.

U & P U Patel Department of Computer Engineering Page 7


CE143:Computer Concepts & Programming Roll No

Flowchart

U & P U Patel Department of Computer Engineering Page 8


CE143:Computer Concepts & Programming Roll No

Algorithm

Code #include<stdio.h>
int main()
{
int a[3][3],b[3][3],c[3][3],i,j,k;
int sum=0;

printf("\nEnter first matrix:\n");


for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&a[i][j]);
}

U & P U Patel Department of Computer Engineering Page 9


CE143:Computer Concepts & Programming Roll No

}
printf("\nEnter second matrix:\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&b[i][j]);
}
}
printf("The first matrix is :\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d",a[i][j]);
}
printf("\n");
}
printf("The second matrix is :\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d",b[i][j]);
}
printf("\n");
}

U & P U Patel Department of Computer Engineering Page 10


CE143:Computer Concepts & Programming Roll No

for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
sum =0;
for(k=0;k<3;k++)
{
sum=sum+a[i][k]*b[j][k];
}
c[i][j]=sum;
}
}
printf("\nThe Multiplication of two matrix is :\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d",c[i][j]);
}
printf("\n");
}
return 0;
}

U & P U Patel Department of Computer Engineering Page 11


CE143:Computer Concepts & Programming Roll No

Output

Program: 8.1 Take a user input for a string and calculate the number of
alphabets, digits and special characters from the given input.
Flowchart

U & P U Patel Department of Computer Engineering Page 12


CE143:Computer Concepts & Programming Roll No

Algorithm

Code #include<stdio.h>
#include<string.h>
int main()
{
char str[20];
int i,alphabets=0,digits=0,specialcharacters=0;

printf("Enter the string:");


gets(str);

U & P U Patel Department of Computer Engineering Page 13


CE143:Computer Concepts & Programming Roll No

for(i=0;str[i];i++)
{
if((str[i]>=65 && str[i]<=90)|| (str[i]>=97 && str<=122))
alphabets++;
else if(str[i]>=48 && str[i]<=57)
digits++;
else
specialcharacters++;
}
printf("Alphabets =%d\n",alphabets);
printf("Digits =%d\n",digits);
printf("Specialcharacters =%d\n",specialcharacters);

return 0;
}

Output

U & P U Patel Department of Computer Engineering Page 14


CE143:Computer Concepts & Programming Roll No

Program: 8.2 Write a program that takes a set of names of individuals and
abbreviates the first, middle and other names except the last
name by their first letter.
Flowchart

U & P U Patel Department of Computer Engineering Page 15


CE143:Computer Concepts & Programming Roll No

Algorithm

Code #include<stdio.h>

int main()

int i,j,k;

char Firstname[20],Middlename[20],Lastname[20],Fullname[60];

printf("\nEnter Firstname:\n");

gets(Firstname);

printf("\nEnter Middlename:\n");

gets(Middlename);

printf("\nEnter Lastname:\n");

gets(Lastname);

for(i=0;Firstname[i]!='\0';i++)

U & P U Patel Department of Computer Engineering Page 16


CE143:Computer Concepts & Programming Roll No

Fullname[i]=Firstname[i];

Fullname[i]=' ';

for(j=0;Middlename[j]!='\0';j++)

Fullname[i+j+1]=Middlename[j];

Fullname[i+j+1]=' ';

for(k=0;Lastname[k]!='\0';k++)

Fullname[i+j+k+2]=Lastname[k];

Fullname[i+j+k+2]='\0';

printf("\n Fullname is:\n");

puts(Fullname);

U & P U Patel Department of Computer Engineering Page 17


CE143:Computer Concepts & Programming Roll No

Output

Program: 8.3 Write a C program to check if the user inputed string is


palindrome or not using recursion.
Flowchart

U & P U Patel Department of Computer Engineering Page 18


CE143:Computer Concepts & Programming Roll No

Algorithm

Code #include<stdio.h>

int isPalindrome(char *inputString, int leftIndex, int rightIndex);

int main()

char inputString[30];

printf("Enter a string for palindrome check\n");

scanf("%s", inputString);

if(isPalindrome(inputString, 0, strlen(inputString) - 1))

printf("%s is a Palindrome \n", inputString);

U & P U Patel Department of Computer Engineering Page 19


CE143:Computer Concepts & Programming Roll No

else

printf("%s is not a Palindrome \n", inputString);

return 0;

int isPalindrome(char *inputString, int leftIndex, int rightIndex)

if(leftIndex >= rightIndex)

return 1;

if(inputString[leftIndex] == inputString[rightIndex])

return isPalindrome(inputString, leftIndex + 1, rightIndex - 1);

return 0;

U & P U Patel Department of Computer Engineering Page 20


CE143:Computer Concepts & Programming Roll No

Output

Program: 9.1 Write a C program to check if the entered number is prime or


not by using types of user defined functions
(i) No arguments passed and no return value
(ii) No arguments passed but a return value
(iii) Argument passed but no return value
(iv) Argument passed and a return value
Code(i) #include <stdio.h>

int checkprime(int n);

int main()

int n,flag;

printf("Enter a positive number:");

scan("%d",&n);

U & P U Patel Department of Computer Engineering Page 21


CE143:Computer Concepts & Programming Roll No

flag=checkprime(n);

if(flag==1)

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

else

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

return 0;

int checkprime(int n)

int i;

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

if(n%i==0)

return 1;

return 0;

Output(i)

Code(ii) #include <stdio.h>

U & P U Patel Department of Computer Engineering Page 22


CE143:Computer Concepts & Programming Roll No

int checkprime();

int main()

int i,n,flag=0;

n=checkprime();

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

if(n%i==0)

flag=1;

break;

if(flag==1)

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

else

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

return 0;

int checkprime()

int n;

printf("Enter a positive number:");

scanf("%d",&n);

U & P U Patel Department of Computer Engineering Page 23


CE143:Computer Concepts & Programming Roll No

return n;

Output(ii)

Code(iii) #include <stdio.h>

void checkprime(int n);

int main()

int n;

printf("Enter a positive integer:",n);

scanf("%d",&n);

checkprime(n);

return 0;

void checkprime(int n)

int i,flag=0;

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

U & P U Patel Department of Computer Engineering Page 24


CE143:Computer Concepts & Programming Roll No

if(n%i==0)

flag=1;

break;

if (flag==1)

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

else

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

Output(iii)

Code(iv) #include <stdio.h>

int checkprime(int n);

int main()

int n,flag;

printf("Enter a positive number:");

U & P U Patel Department of Computer Engineering Page 25


CE143:Computer Concepts & Programming Roll No

scan("%d",&n);

flag=checkprime(n);

if(flag==1)

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

else

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

return 0;

int checkprime(int n)

int i;

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

if(n%i==0)

return 1;

return 0;

U & P U Patel Department of Computer Engineering Page 26


CE143:Computer Concepts & Programming Roll No

Output(iv)

Program: 9.2 If the length of the sides of a triangle are denoted by a, b and
c, then the area of triangle is given by:
S=a+b+c/2
Flowchart

U & P U Patel Department of Computer Engineering Page 27


CE143:Computer Concepts & Programming Roll No

Algorithm

Code #include<stdio.h>

int checkValidity(double a,double b,double c)

if(a+b<=c||a+c<=b||b+c<=a)

return 0;

else

return 1;

void area(double x,double y,double z)

double s,area;

s=(x+y+z)/2;

U & P U Patel Department of Computer Engineering Page 28


CE143:Computer Concepts & Programming Roll No

area=sqrt(s*(s-x)*(s-y)*(s-z));

printf("\nArea of Triangle calculated using Heron's formula is:%lf",area);

int main()

double a,b,c;

printf("\nEnter the sides of triangle:\n");

scanf("%lf%lf%lf",&a,&b,&c);

if(checkValidity(a,b,c))

area(a,b,c);

else

printf("Invalid Triangle");

U & P U Patel Department of Computer Engineering Page 29


CE143:Computer Concepts & Programming Roll No

Output

Program: 9.3 A positive integer is entered through the keyboard, write a


function to find the binary equivalent of this number using
recursion.

U & P U Patel Department of Computer Engineering Page 30


CE143:Computer Concepts & Programming Roll No

Flowchart

Algorithm

U & P U Patel Department of Computer Engineering Page 31


CE143:Computer Concepts & Programming Roll No

Code #include<stdio.h>

int main()

int number;

printf("\nEnter number to convert into binary:");

scanf("%d",&number);

binary(number);

return 0;

void binary(int num)

if(num==0)

return 0;

binary(num/2);

printf("%d",num%2);

U & P U Patel Department of Computer Engineering Page 32


CE143:Computer Concepts & Programming Roll No

Output

Program: Write a C program to create a structure of Book Detail and


10.1
display the details of the book in appropriate format by
passing structure as function argument.

U & P U Patel Department of Computer Engineering Page 33


CE143:Computer Concepts & Programming Roll No

Flowchart

U & P U Patel Department of Computer Engineering Page 34


CE143:Computer Concepts & Programming Roll No

Algorithm

Code #include<stdio.h>

struct BookDetail

char Acc_No[10];

char Author[20];

char Bookname[50];

float price;

int f;

book1,book2;

void main()

U & P U Patel Department of Computer Engineering Page 35


CE143:Computer Concepts & Programming Roll No

printf("\n Enter Book1 information:");

printf("\n Accession number:");

scanf("%s",book1.Acc_No);

printf("\n Author Name:");

fflush(stdin);

scanf("%[^\n]s",book1.Author);

printf("\n Book Name:");

fflush(stdin);

scanf("%[^\n]s",book1.Bookname);

printf("\n Enter Price:");

scanf("%f",&book1.price);

printf("\n Issued:");

scanf("%d",&book1.f);

printf("\n You entered the Book Details as:");

printf("\n Accession number:%s",book1.Acc_No);

printf("\nAuthor Name:%s",book1.Bookname);

printf("\nPrice:%f",book1.price);

printf("\nBook Issued?:");

if(book1.f==1)

printf("Issued");

U & P U Patel Department of Computer Engineering Page 36


CE143:Computer Concepts & Programming Roll No

else

printf("Not Issued");

printf("\n Enter Book2 information:");

printf("\n Accession number:");

scanf("%s",book2.Acc_No);

printf("\n Author Name:");

fflush(stdin);

scanf("%[^\n]s",book2.Author);

printf("\n Book Name:");

fflush(stdin);

scanf("%[^\n]s",book2.Bookname);

printf("\n Enter Price:");

scanf("%f",&book2.price);

printf("\n Issued:");

scanf("%d",&book2.f);

printf("\n You entered the Book Details as:");

printf("\n Accession number:%s",book2.Acc_No);

printf("\nAuthor Name:%s",book2.Bookname);

printf("\nPrice:%f",book2.price);

printf("\nBook Issued?:");

if(book2.f==1)

U & P U Patel Department of Computer Engineering Page 37


CE143:Computer Concepts & Programming Roll No

printf("Issued");

else

printf("Not Issued");

Output

Program: Create a Unioncalled library to hold accession number,


10.2
title of the book ,author name, price of the book and
flag indicating whether the book is issued or not.(flag =
1 if the book is issued , flag = 0 otherwise). Write a
program to enter data of one book and display the data.

U & P U Patel Department of Computer Engineering Page 38


CE143:Computer Concepts & Programming Roll No

Flowchart

Algorithm

Code #include<stdio.h>

U & P U Patel Department of Computer Engineering Page 39


CE143:Computer Concepts & Programming Roll No

union library

char title[30],author[20];

float price;

int flag;

long int accessionNo;

};

void main()

union library b1;

printf("\nEnter Details of Book 1:");

printf("\nEnter Book title:");

gets(b1.title);

printf("\nEnter the author name:");

gets(b1.author);

printf("\nEnter book price:");

scanf("%f",&b1.price);

fflush(stdin);

printf("\nEnter the AccessionNo:");

scanf("%ld",&b1.accessionNo);

fflush(stdin);

U & P U Patel Department of Computer Engineering Page 40


CE143:Computer Concepts & Programming Roll No

printf("\nEnter flag 1 for book issued \n 0 for book not issued: ");

scanf("%d",&b1.flag);

fflush(stdin);

printf("Book details are:\n\n");

printf("\nBook title:");

puts(b1.title);

printf("\nEnter the author name:");

gets(b1.author);

printf("\nAuthor name:");

puts(b1.author);

printf("\nEnter book price:");

scanf("%f",&b1.price);

fflush(stdin);

printf("\nBook price:=%f",b1.price);

printf("\nEnter the AccessionNo:");

scanf("%ld",&b1.accessionNo);

fflush(stdin);

printf("\nAccessionNo: =%ld",b1.accessionNo);

printf("\nEnter flag 1 for book issued \n 0 for book not issued: ");

U & P U Patel Department of Computer Engineering Page 41


CE143:Computer Concepts & Programming Roll No

scanf("%d",&b1.flag);

fflush(stdin);

if(b1.flag==1)

printf("\nBook Issued\n");

else if(b1.flag==0)

printf("\nBook not Issued");

printf("flag = %d",b1.flag);

Output

Program: Write a C program for nested structure to display employee

U & P U Patel Department of Computer Engineering Page 42


CE143:Computer Concepts & Programming Roll No

10.3 details such as, Age, Name, Address, Salary.


Flowchart

U & P U Patel Department of Computer Engineering Page 43


CE143:Computer Concepts & Programming Roll No

Algorithm

Code #include<stdio.h>

struct employee

char name[20];

char designation[20];

char address[50];

int age;

struct salary

float
basicsalary,dearnessallowance,houserentallowance,travellingallowance,totalsala
ry;

U & P U Patel Department of Computer Engineering Page 44


CE143:Computer Concepts & Programming Roll No

}s;

}e;

void main()

printf("\nEnter name:");

gets(e.name);

fflush(stdin);

printf("\nEnter age:");

scanf("%d",&e.age);

fflush(stdin);

printf("\nEnter designation:");

gets(e.designation);

fflush(stdin);

printf("\nEnter address:");

gets(e.address);

fflush(stdin);

printf("\nEnter Basicsalary:");

scanf("%f",&e.s.basicsalary);

fflush(stdin);

U & P U Patel Department of Computer Engineering Page 45


CE143:Computer Concepts & Programming Roll No

printf("\nEnter Dearness Allowance:");

scanf("%f",&e.s.dearnessallowance);

fflush(stdin);

printf("\nEnter House rent Allowance:");

scanf("%f",&e.s.houserentallowance);

fflush(stdin);

printf("\nEnter Travelling Allowance:");

scanf("%f",&e.s.travellingallowance);

fflush(stdin);

e.s.totalsalary =
e.s.basicsalary+e.s.dearnessallowance+e.s.houserentallowance+e.s.travellingallo
wance;

printf("\n-----Details are-----\n");

printf("\nName :");

puts(e.name);

printf("\nDesignation :");

puts(e.designation);

printf("\nAddress :");

puts(e.address);

U & P U Patel Department of Computer Engineering Page 46


CE143:Computer Concepts & Programming Roll No

printf("\nAge: %d",e.age);

printf("\nTotal salary =%f",e.s.totalsalary);

Output

Program: Write a program to read the marks of 10 students for


11.1
the subject CE141 Computer concepts
and Programming and computes the number of students in
categories FAIL, PASS, FIRSTCLASS and DISTINCTION
using Pointers and Arrays.MarksCategories70 or
AboveDISTINCTION69 to 60FIRST CLASS59 to
40PASSBelow 40FAILFor example, if following marks of 10

U & P U Patel Department of Computer Engineering Page 47


CE143:Computer Concepts & Programming Roll No

students are entered:34 56 78 98 12 31 67 75 91 23Then the


output should beDISTINCTION 4 FIRST CLASS 1 PASS 1
FAIL 4
Flowchart

U & P U Patel Department of Computer Engineering Page 48


CE143:Computer Concepts & Programming Roll No

Algorithm

Code #include<stdio.h>

int main()

int x[10],i,*p;

int Distinction=0,Firstclass=0,Pass=0,Fail=0;

printf("\nEnter marks of 10 students:%d\n");

p=x;

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

printf("\nEnter x[%d]=",i);

scanf("%d",p);

p++;

U & P U Patel Department of Computer Engineering Page 49


CE143:Computer Concepts & Programming Roll No

for(p=x;p<&x[10];p++)

if(*p>=70)

Distinction++;

else if(*p<=69&&*p>=60)

Firstclass++;

else if(*p<=59&&*p>=40)

Pass++;

else

Fail++;

printf("\n Number of students with Distinction=%d\n",Distinction);

printf("\n Number of students with Firstclass=%d\n",Firstclass);

printf("\n Number of students Passed=%d\n",Pass);

printf("\n Number of students Failed=%d\n",Fail);

U & P U Patel Department of Computer Engineering Page 50


CE143:Computer Concepts & Programming Roll No

Output

Program: Write a program that uses an array of pointers to strings str[ ].


11.2
Receive two strings str1 and str2 and check if str1 is
embedded in any of the strings in str[ ]. If str1 is found, then
replace it with str2. char *str[ ] = { "We will teach you how
to...", "Move a mountain", "Level a building", "Erase the past",
"Make a million", "...all through C!" } ;

U & P U Patel Department of Computer Engineering Page 51


CE143:Computer Concepts & Programming Roll No

Flowchart

Algorithm

U & P U Patel Department of Computer Engineering Page 52


CE143:Computer Concepts & Programming Roll No

Code #include<stdio.h>

void main()

char *s[]=

"We will teach you how to...",

"Move a mountain",

"Level a building",

"erase the past",

"Make a million",

"...all through C!"

};

char *p;

int i,count=0;

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

for(p=s[i];*p;p++)

if(*p=='e')

count++;

U & P U Patel Department of Computer Engineering Page 53


CE143:Computer Concepts & Programming Roll No

printf("total no. of 'e' is :%d\n",count);

Output

Program:
11.3(i)

Code #include<stdio.h>

void display();

int main()

void (*func_ptr)();

func_ptr=display;

printf("Address of functions display is %u\n",func_ptr);

(*func_ptr)();

return 0;

void display()

puts("By helping others, we help overselves!!");

U & P U Patel Department of Computer Engineering Page 54


CE143:Computer Concepts & Programming Roll No

Output

Program:11.
3(ii)

Code #include<stdio.h>

char *copy (char*,char *);

int main()

char *str;

char source[] = "Kindness";

char target[10];

str=copy(target,source);

printf("%s\n",str);

return 0;

char *copy(char *t,char *s)

char * r;

r = t;

while(*s!='\0')

U & P U Patel Department of Computer Engineering Page 55


CE143:Computer Concepts & Programming Roll No

*t=*s;

t++;

s++;

*t='\0';

return(r);

Output

Program: 1.1

U & P U Patel Department of Computer Engineering Page 56


CE143:Computer Concepts & Programming Roll No

Flowchart

Algorithm

U & P U Patel Department of Computer Engineering Page 57


CE143:Computer Concepts & Programming Roll No

Code

Output

Program: 1.2

U & P U Patel Department of Computer Engineering Page 58


CE143:Computer Concepts & Programming Roll No

Flowchart

Algorithm

U & P U Patel Department of Computer Engineering Page 59


CE143:Computer Concepts & Programming Roll No

Code

Output

Program: 1.3

U & P U Patel Department of Computer Engineering Page 60


CE143:Computer Concepts & Programming Roll No

Flowchart

Algorithm

U & P U Patel Department of Computer Engineering Page 61


CE143:Computer Concepts & Programming Roll No

Code

Output

NOTE : https://drive.google.com/file/d/1EndhAEDBVHXuVpbLrXtd0DjhUGjfsoIQ/
view?usp=sharing

PDF file of Experiment 2 to 6

U & P U Patel Department of Computer Engineering Page 62


CE143:Computer Concepts & Programming Roll No

Output

U & P U Patel Department of Computer Engineering Page 63


CE143:Computer Concepts & Programming Roll No

Code

Output

Sign: Grade:

U & P U Patel Department of Computer Engineering Page 64

You might also like