Program 7.1 Code
Program 7.1 Code
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);
while(x < i)
{
printf("Enter element for array1[%d] = ", x);
scanf("%d",&array1[x]);
x++;
}
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;
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];
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 main()
{
char string[100];
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);
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);
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;
}
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;
}
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;
}
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;
return 0;
}
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];
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);
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);
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
void display()
{
puts("By helping others, we help overselves!!");
}
Output-1