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

Program 7.1 Code

The document contains code for 4 programs that check if a number is prime or not using different types of functions: 1. A function with no arguments and no return value that checks primality and prints the result. 2. A function with no arguments but a return value of 1 if not prime or 0 if prime, called in main to print the result. 3. A function that takes the number as an argument but has no return value, prints the result. 4. A function that takes the number as an argument and returns a value that main uses to print the result.

Uploaded by

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

Program 7.1 Code

The document contains code for 4 programs that check if a number is prime or not using different types of functions: 1. A function with no arguments and no return value that checks primality and prints the result. 2. A function with no arguments but a return value of 1 if not prime or 0 if prime, called in main to print the result. 3. A function that takes the number as an argument but has no return value, prints the result. 4. A function that takes the number as an argument and returns a value that main uses to print the result.

Uploaded by

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

Practical

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.
Code #include<stdio.h>

int main()
{
int A[25],p=0,i=0,o=0,e=0,i;
printf("Enter 25 elements = ");
for(i=0;i<25;i++)
{
scanf("%d\i",&A[i]);
}
for(i=0;i<25;i++)
{
if(A[i]>0)
{
p++;
}
else
{
i++;
}
}
for(i=0;i<25;i++)
{
if(A[i]%2==0)
{
e++;
}
else
{
o++;
}
}
printf("Positive Elements = %d\i",p);
printf("Negative Elements = %d\i",i);
printf("Odd Elements = %d\i",o);
printf("Even Elements = %d\i",e);
}
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]
Code #include<stdio.h>

int main()
{
int i, j, k, size, x=0, y=0, z=0;
printf("Enter size of Array 1 : ");
scanf("%d",&i);
printf("Enter size of Array 2 : ");
scanf("%d",&j);

int array1[100], array2[100];


printf("\nEnter elements for Array 1\n\n");

while(x < i)
{
printf("Enter element for array1[%d] = ", x);
scanf("%d",&array1[x]);
x++;
}

printf("\nEnter elements for Array 2\n\n");

while(y < j)
{
printf("Enter element for array2[%d] = ", y);
scanf("%d",&array2[y]);
y++;
}
int array3[i+j];
x=1;
y=0;
z=0;
size=i+j;

array3[0]=array1[0];
k=1;
while(x<i)
{
z=k-1;
while(array1[x]<array3[z]&&z>=0)
{
array3[z+1]=array3[z];
array3[z]=array1[x];
z--;
}
if(array1[x]>array3[z])
{
array3[z+1]=array1[x];
}
k++;
x++;
}

while(y<j)
{
z=k-1;
while(array2[y]<array3[z]&&z>=0)
{
array3[z+1]=array3[z];
array3[z]=array2[y];
z--;
}
if(array2[y]>array3[z])
{
array3[z+1]=array2[y];
}
k++;
y++;
}

z=0;

while(z < i+j)


{
printf("\nArray3[%d] = %d", z, array3[z]);
z++;
}
return 0;
}
Output

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


Code #include<stdio.h>

int main()
{
int i,j,A[3][3],T[3][3];
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("Enter the value in A[%d][%d] = ",i+1,j+1);
scanf("%d",&A[i][j]);
T[j][i]=A[i][j];
}
printf("\i");
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf(" %d ",T[i][j]);
}
printf("\n");
}
return 0;
}
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.
Code #include<stdio.h>
int main()
{
char string[200];
int i=0, j, a=0, d=0, sc=0;
printf("Enter the string : ");
gets(string);
for(i=0; string[i] != '\0'; i++)
{
j = (int)string[i];
//printf("%d\n", j);
if((65 <= j && j <= 90) || (97 <= j && j <= 122))
{
a++;
}
else if(49 <= j && j <= 57)
{
d++;
}
else
{
sc++;
}
}
printf("\nNo. of Alphabets : %d\n", a);
printf("No. of Digits : %d\n", d);
printf("No. of special Characters : %d\n", sc);
return 0;
}
Output

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.
Code #include<stdio.h>
#include<ctype.h>
int main()
{
int i, j=0;
printf("How many names do you want to write : ");
scanf("%d",&i);
char firstName[i][30], middleName[i][30], lastName[i][30];

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


{
fflush(stdin);
printf("%d. Enter your First Name : ", j+1);
gets(firstName[j]);
printf(" Enter your Middle Name : ");
gets(middleName[j]);
printf(" Enter your Last Name : ");
gets(lastName[j]);
printf("\i");
}

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


{
lastName[j][0] = toupper(lastName[j][0]);
printf("%d. %c. %c. %s\i", j+1, toupper(firstName[j][0]),
toupper(middleName[j][0]), lastName[j]);
}
return 0;
}
Output

Program 8.3 Write a C program to check if the user inputed string is palindrome
or not using recursion.
Code #include<stdio.h>
#include<string.h>

extern int i = 0;
extern int count = 0;

int palindrome(char string[100], int i, int i)


{
if(i<(i/2))
{
count = palindrome(string, i, i + 1);
if((string[i]) != (string[i-i-1]))
{
count++;
}
}
return count;
}

int main()
{
char string[100];

printf("Enter the string : ");


gets(string);
int i = strlen(string);

if(!palindrome(string, i, i))
{
printf("\nEntered string is a palindrome\n");
}
else
{
printf("\nEntered string is not a palindrome\n");
}

return 0;
}
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
• No arguments passed and no return value
• No arguments passed but a return value
• Argument passed but no return value
• Argument passed and a return value
Code-1 #include<stdio.h>

void prime(void);

int main(void)
{
prime();
return 0;
}

void prime(void)
{
int i, number;
printf("Enter the Number : ");
scanf("%d",&number);

for(i=2; i<number; i++)


{
if(number % i == 0)
{
printf("\nThis is not a Prime Number.\n");
break;
}
else if(i == (number-1) && number % i != 0)
{
printf("\nThis is a Prime Number.\n");
break;
}
}
}
Output-1

Code-2 #include<stdio.h>

int prime(void);

int main(void)
{
prime() == 1 ? printf("\nThis is not a Prime Number.\n") : printf("\nThis is a Prime
Number.\n");
return 0;
}

int prime(void)
{
int i, number;
printf("Enter the Number : ");
scanf("%d",&number);

for(i=2; i<number; i++)


{
if(number % i == 0)
{
return 1;
break;
}
else if(i == (number-1) && number % i != 0)
{
return 0;
break;
}
}
}
Output-2

Code-3 #include<stdio.h>
#include<conio.h>

void prime(int);

int main(void)
{
int number;
printf("Enter the Number : ");
scanf("%d",&number);
prime(number);
return 0;
}

void prime(int number)


{
int i;
for(i=2; i<number; i++)
{
if(number % i == 0)
{
printf("\nThis is not a Prime Number.\n");
break;
}
else if(i == (number-1) && number % i != 0)
{
printf("\nThis is a Prime Number.\n");
break;
}
}
}
Output-3

Code-4 #include<stdio.h>

int prime(int);

int main(void)
{
int number;
printf("Enter the Number : ");
scanf("%d",&number);
prime(number) == 1 ? printf("\nThis is not a Prime Number.\n") : printf("\nThis is
a Prime Number.\n");
return 0;
}

int prime(int number)


{
int i;
for(i=2; i<number; i++)
{
if(number % i == 0)
{
return 1;
break;
}
else if(i == (number-1) && number % i != 0)
{
return 0;
break;
}
}
}
Output-4
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:

Code #include<stdio.h>
#include<math.h>
void area(float, float, float);
int trueFalse(float, float, float);
int main()
{
float a, b, c;
printf("Enter the value of side 1 : ");
scanf("%f",&a);
printf("Enter the value of side 2 : ");
scanf("%f",&b);
printf("Enter the value of side 3 : ");
scanf("%f",&c);

if(trueFalse(a, b, c))
{
area(a, b, c);
}
return 0;
}

int trueFalse(float a, float b, float c)


{
if((a+b <= c) || (b+c <= a) || (a+c <= b))
{
printf("\nInvalid Values of Sides.\n");
return 0;
}
else
{
return 1;
}
}

void area(float a, float b, float c)


{
float s, Area;
s=(a + b + c)/2;

Area = sqrt(s * (s-a) * (s-b) * (s-c));

printf("\nThe area of the given trianngle of given sides is : %.2f\n", sqrt(s * (s-a) *
(s-b) * (s-c)));
}
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.
Code #include<stdio.h>
int binary(int, int);

int main()
{
int decimal, bin;

printf("\nEnter the positive Decimal Integer Number : ");


scanf("%d",&decimal);

bin = binary(decimal, decimal);

printf("\nThe converted Binary Number : %d\n", bin);

return 0;
}

int binary(int decimal, int perm)


{
int bin=0;
if(decimal == 0)
{
return 0;
}
else
{
bin = binary(decimal/2, perm);
bin += decimal % 2;
if(decimal != perm)
{
bin *= 10;
}
return bin;
}
}
Output

Program 10.1 Write a C program to create a structure of Book Detail and display
the details of the book in appropriate format by passing structure as
function argument.
Code #include<stdio.h>
#define MAX 2

struct Book
{
int srNo;
char BookName[35];
char AuthorName[35];
int price;
}bookNo[MAX];

void print(struct Book bookNo[MAX])


{
printf("\
n___________________________________________________________________
_______________\n");
printf("Sr.No. | Book Name | Author Name | Price |\i");

for(int i=0; i<MAX; i++)


{
printf("%6d | %29s | %31s | %5d |\i", bookNo[i].srNo, bookNo[i].BookName,
bookNo[i].AuthorName, bookNo[i].price);
}

printf("______________________________________________________________
____________________\n");

int main()
{
for(int i=0; i<MAX; i++)
{
printf("\nEnter the details for Book no. : %d\n", i+1);
printf("\nSerial No. : ");
fflush(stdin);
scanf("%d",&bookNo[i].srNo);
printf("Book Name : ");
fflush(stdin);
scanf("%[^\n]s",&bookNo[i].BookName);
printf("Author Name : ");
fflush(stdin);
scanf("%[^\n]s",&bookNo[i].AuthorName);
printf("Price : ");
fflush(stdin);
scanf("%d",&bookNo[i].price);
}

print(bookNo);

return 0;
}
Output

Program 10.2 Create a Union called library to hold accession number, 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.
Code #include<stdio.h>

union library
{
int accessionNumber;
char bookName[30];
char authorName[30];
int price;
int flag;
}lib;

int main()
{
printf("\nEnter the accession number of Book : ");
scanf("%d",&lib.accessionNumber);
printf("Book's accession number is %d.\n", lib.accessionNumber);
fflush(stdin);

printf("\nEnter the name of Book : ");


gets(lib.bookName);
printf("The Book Name is %s.\n", lib.bookName);
fflush(stdin);

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


gets(lib.authorName);
printf("Book's author's name is %s.\n", lib.authorName);
fflush(stdin);

printf("\nEnter the price of Book : ");


scanf("%d",&lib.price);
printf("The Book's price is %d.\n", lib.price);
fflush(stdin);

printf("\nEnter 1 if Book is issued, or 0 if not : ");


scanf("%d",&lib.flag);
if(lib.flag == 1)
{
printf("The book is Issued.\n");
}
else if(lib.flag == 0)
{
printf("The book is not issued.\n");
}
else
{
printf("Invalid number.\n");
}
fflush(stdin);
return 0;
}
Output

Program 10.3 Write a C program for nested structure to display employee details
such as, Age, Name, Address, Salary.
Code #include<stdio.h>
struct information
{
int age;
char address[100];
int salary;
};

struct employee
{
char name[30];
struct information info;
}emp;

int main()
{
printf("Enter name of employee : ");
gets(emp.name);
fflush(stdin);

printf("\nEnter age of employee : ");


scanf("%d",&emp.info.age);
fflush(stdin);

printf("\nEnter address of employee : ");


gets(emp.info.address);
fflush(stdin);

printf("\nEnter the salary of employee : ");


scanf("%d",&emp.info.salary);
fflush(stdin);

printf("\nInfo of the employee\i");


printf("Name : %s\n", emp.name);
printf("Age : %d\n", emp.info.age);
printf("Address : %s\n", emp.info.address);
printf("Salary : %d\n", emp.info.salary);
return 0;
}
Output

Program 11.1 Write a program to read the marks of 10 students for the subject
CE141 Computer concepts and Programming and computes the
number of students in categories FAIL, PASS, FIRST CLASS and
DISTINCTION using Pointers and Arrays
For example, if following marks of 10 students are entered: 34 56 78
98 12 31 67 75 91 23 Then the output should be DISTINCTION 4
FIRST CLASS 1 PASS 1 FAIL 4
Code #include<stdio.h>

int main()
{
int marks[10];
int i, distinction=0, firstClass=0, pass=0, fail=0;
for(i=0; i<10; i++)
{
printf("Enter marks for Student %2d at address %u : ", i+1, marks+i);
scanf("%d",&marks[i]);
}
for(i=0; i<10; i++)
{
if(*(marks + i) >= 70)
{
distinction++;
}
else if(*(marks + i) >=60)
{
firstClass++;
}
else if(*(marks + i) >= 40)
{
pass++;
}
else
{
fail++;
}
}
printf("\n\nDistinction : %d", distinction);
printf("\nFirst class : %d", firstClass);
printf("\nPass : %d", pass);
printf("\nFail : %d", fail);
return 0;
}
Output

Program 11.2 Write a program that uses an array of pointers to strings str[ ].
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!"
};
For example, if str1 contains "mountain" and str2 contains "car",
then the second string in str should get changed to "Move a car".
(Array of Pointers)
Code #include<stdio.h>
#include<ctype.h>

int main()
{
char *str[]={"We will teach you how to...",
"Move a mountain",
"Level a building",
"Erase the past",
"Make a million",
"...all through C!"};
char ch;
int count;
printf("Enter the character you want to count : ");
ch = getchar();
for(int i=0; i<6; i++)
{
for(int j=0; *(*(str + i) + j) != '\0'; j++)
{
if(*(*(str + i) + j) == ch || *(*(str + i) + j) == toupper(ch) || *(*(str + i) + j) ==
tolower(ch))
{
count++;
}
}
}
printf("%c repeats %d times in given string", ch, count);
return 0;
}
Output

Program 11.3 Write output for the following programs:


• (Pointers to Functions)
• (Functions Returning Pointers)
Code-1 #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!!");
}
Output-1

Code-2 char *copy (char*,char *);


int main()
{
char *str;
char source[] = "Kindness";
char target[10];
str=copy(target,source);
printf("%s\i",str);
return 0;
}
char *copy(char *t,char *s)
{
char * r;
r = t;
while(*s!='\0')
{
*t=*s;
t++;
s++;
}
*t='\0';
return(r);
}
Output-2

You might also like