JNTUK R19 C Programming Lab
JNTUK R19 C Programming Lab
Exercise1:
1. Write a C program to print a block F using hash (#), where the F has a height of
six characters and width of five and four characters.
2. Write a C program to compute the perimeter and area of a rectangle with a
height of 7 inches and width of 5 inches.
3. Write a C program to display multiple variables.
Exercise2:
1. Write a C program to calculate the distance between the two points.
2. Write a C program that accepts 4 integers p, q, r, s from the user where r and s
are positive and p is even. If q is greater than r and s is greater than p and if the
sum of r and s is greater than the sum of p and q print "Correct values", otherwise
print "Wrong values".
Exercise3:
1. Write a C program to convert a string to a long integer.
2. Write a program in C which is a Menu-Driven Program to compute the area of
the various geometrical shape.
3. Write a C program to calculate the factorial of a given number.
Exercise4:
1. Write a program in C to display the n terms of even natural number and their
sum.
2. Write a program in C to display the n terms of harmonic series and their sum. 1
+ 1/2 + 1/3 + 1/4 + 1/5 ... 1/n terms.
3. Write a C program to check whether a given number is an Armstrong number or
not.
Exercise5:
1. Write a program in C to print all unique elements in an array.
2. Write a program in C to separate odd and even integers in separate arrays.
3. Write a program in C to sort elements of array in ascending order.
Exercise6:
1. Write a program in C for multiplication of two square Matrices.
2. Write a program in C to find transpose of a given matrix.
Exercise7:
1. Write a program in C to search an element in a row wise and column wise sorted
matrix.
2. Write a program in C to print individual characters of string in reverse order.
Exercise8:
1. Write a program in C to compare two strings without using string library
functions.
2. Write a program in C to copy one string to another string.
Exercise9:
1. Write a C Program to Store Information Using Structures with Dynamically
Memory Allocation
2. Write a program in C to demonstrate how to handle the pointers in the program.
Exercise10:
1. Write a program in C to demonstrate the use of & (address of) and *(value at
address) operator.
2. Write a program in C to add two numbers using pointers.
Exercise11:
1. Write a program in C to add numbers using call by reference.
2. Write a program in C to find the largest element using Dynamic Memory
Allocation.
Exercise12:
1. Write a program in C to swap elements using call by reference.
2. Write a program in C to count the number of vowels and consonants in a string
using a pointer.
Exercise13:
1. Write a program in C to show how a function returning pointer.
2. Write a C program to find sum of n elements entered by user. To perform this
program, allocate memory dynamically using malloc( ) function.
Exercise14:
1. Write a C program to find sum of n elements entered by user. To perform this
program, allocate memory dynamically using calloc( ) function. Understand the
difference between the above two programs
2. Write a program in C to convert decimal number to binary number using the
function.
Exercise15:
1. Write a program in C to check whether a number is a prime number or not using
the function.
2. Write a program in C to get the largest element of an array using the function.
Exercise16:
1. Write a program in C to append multiple lines at the end of a text file.
2. Write a program in C to copy a file in another name.
3. Write a program in C to remove a file from the disk.
EXERCISE-1
1. Write a C program to print a block F using hash (#), where the F has a height of
six characters and width of five and four characters.
SOURCE CODE:
#include<stdio.h>
int main(){
printf("#####\n");
printf("#\n");
printf("#\n");
printf("####\n");
printf("#\n");
printf("#\n");
return 0;
}
OUTPUT:
#####
#
#
####
#
#
2. Write a C program that accepts 4 integers p, q, r, s from the user where r and s
are positive and p is even. If q is greater than r and s is greater than p and if the
sum of r and s is greater than the sum of p and q print Correct values, otherwise
print Wrong values.
SOURCE CODE:
#include<stdio.h>
int main(){
int p,q,r,s;
printf("Enter p,q,r,s values:");
scanf("%d%d%d%d",&p,&q,&r,&s);
if(r>0 && s>0 && p%2==0 && q>r && s>p && r+s>p+q)
printf("Correct values");
else
printf("Wrong values");
return 0;
}
OUTPUT-1:
Enter p,q,r,s values:4 8 2 13
Correct values
OUTPUT-2:
Enter p,q,r,s values:9 3 1 45
Wrong values
EXERCISE-3
1. Write a C program to convert a string to a long integer.
SOURCE CODE:
#include<stdio.h>
#include<stdlib.h>
int main(){
long int l;
char str[30];
printf("Enter a string:\n");
scanf("%s",str);
l=atol(str);
printf("String to long integer is : %ld",l);
return 0;
}
OUTPUT:
Enter a string:
123456789
String to long integer is : 123456789
EXERCISE-4
1. Write a program in C to display the n terms of even natural number and their
sum.
SOURCE CODE:
#include<stdio.h>
int main(){
int n,i=1,c=1,sum=0;
printf("Enter number of terms : ");
scanf("%d",&n);
printf("Even natural numbers are : \n");
while(c<=n){
if(i%2==0){
printf("%d ",i);
sum=sum+i;
c++;
}
i++;
}
printf("\nSum of even natural numbers is: %d\n",sum);
return 0;
}
OUTPUT:
Enter number of terms : 5
Even natural numbers are :
2 4 6 8 10
Sum of even natural numbers is: 30
2. Write a program in C to display the n terms of harmonic series and their sum. 1
+ 1/2 + 1/3 + 1/4 + 1/5 ... 1/n terms.
SOURCE CODE:
#include<stdio.h>
int main(){
int n,i;
float sum=0;
printf("Enter range:");
scanf("%d",&n);
printf("Series is:\n");
for(i=1;i<=n;i++){
printf("1/%d",i);
if(i<n)
printf("+");
sum = sum + (float)1/i;
}
printf("\nSum of series is: %f",sum);
return 0;
}
OUTPUT:
Enter range:5
Series is:
1/1+1/2+1/3+1/4+1/5
Sum of series is: 2.283334
3. Write a C program to check whether a given number is an Armstrong number or
not.
SOURCE CODE:
/*Program to check whether the given number is Armstrong number or not.*/
#include<stdio.h>
#include<math.h>
int main(){
int n,temp,arm=0,c=0;
printf("Enter number:");
scanf("%d",&n);
temp=n;
while(temp>0){
temp=temp/10;
c++;
}
temp=n;
while(temp>0){
arm=arm+pow((temp%10),c);
temp=temp/10;
}
if(n==arm)
printf("Given number is Armstrong.");
else
printf("Given number is not Armstrong.");
return 0;
}
OUTPUT-1:
Enter number:371
Given number is Armstrong.
OUTPUT-2:
Enter number:151
Given number is not Armstrong.
EXERCISE-5
1. Write a program in C to print all unique elements in an array.
SOURCE CODE:
#include<stdio.h>
int main(){
int a[20],n,i,j,k;
printf("Enter array size:");
scanf("%d",&n);
printf("Enter array elements:\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++){
for(j=i+1;j<n;j++){
if(a[i]==a[j]){
for(k=j;k<n-1;k++)
a[k]=a[k+1];
n--;
j--;
}
}
}
printf("\nUnique elements in array are:\n");
for(i=0;i<n;i++)
printf("%d\t",a[i]);
return 0;
}
OUTPUT:
Enter array size:5
Enter array elements:
12321
Result matrix is
30 24 18
84 69 54
138 114 90
OUTPUT-2:
Enter matrix sizes:3 2
Matrix multiplication is not possible
2. Write a program in C to find transpose of a given matrix.
SOURCE CODE:
#include<stdio.h>
int main(){
int mtrx[10][10],r,c,i,j,temp;
printf("Enter matrix size:\n");
scanf("%d %d",&r,&c);
printf("Enter matrix elements:\n");
for(i=0;i<r;i++)
for(j=0;j<c;j++)
scanf("%d",&mtrx[i][j]);
if(r>=c){
for(i=0;i<c;i++){
for(j=0;j<r;j++){
if(j>i){
temp = mtrx[i][j];
mtrx[i][j] = mtrx[j][i];
mtrx[j][i] = temp;
}
}
}
}
else{
for(i=0;i<r;i++){
for(j=0;j<c;j++){
if(j>i){
temp = mtrx[i][j];
mtrx[i][j] = mtrx[j][i];
mtrx[j][i] = temp;
}
}
}
}
printf("Transpose of the given matrix is\n");
for(i=0;i<c;i++){
for(j=0;j<r;j++)
printf("%d ",mtrx[i][j]);
printf("\n");
}
return 0;
}
OUTPUT-1:
Enter matrix size:
23
Enter matrix elements:
123
456
Transpose of the given matrix is
14
25
36
OUTPUT-2:
Enter matrix size:
32
Enter matrix elements:
12
34
56
Transpose of the given matrix is
135
246
EXERCISE-7
1. Write a program in C to search an element in a row wise and column wise sorted
matrix.
SOURCE CODE:
#include<stdio.h>
int main(){
int mtrx[10][10], r, c, i, j, ele;
printf("Enter matrix size:");
scanf("%d%d",&r,&c);
printf("Enter %d X %d matrix elements:\n",r,c);
for(i=0;i<r;i++)
for(j=0;j<c;j++)
scanf("%d",&mtrx[i][j]);
printf("Enter search element:");
scanf("%d",&ele);
for(i=0;i<r;i++){
for(j=0;j<c;j++){
if(mtrx[i][j]==ele)
break;
}
if(j!=c)
break;
}
if(i!=r && j!=c)
printf("%d element is found at %d-row and %d-column.\n",ele,i,j);
else
printf("%d element is not found.\n",ele);
}
OUTPUT:
Enter matrix size:3 3
Enter 3 X 3 matrix elements:
123
456
789
Enter search element:5
5 element is found at 1-row and 1-column.
EXERCISE-8
1. Write a program in C to compare two strings without using string library
functions.
SOURCE CODE:
#include<stdio.h>
int main(){
char str1[50],str2[50];
int i;
printf("Enter string1:\n");
gets(str1);
printf("Enter string2:\n");
gets(str2);
for(i=0;str1[i]!='\0' || str2[i]!='\0';i++)
if(str1[i]!=str2[i])
break;
if(str1[i]-str2[i]==0)
printf("Both strings are equal.");
else if(str1[i]-str2[i]>0)
printf("String1 is greater than String2.");
else
printf("String1 is lesser than String2.");
return 0;
}
OUTPUT-1:
Enter string1:
there
Enter string2:
there
Both strings are equal.
OUTPUT-2:
Enter string1:
their
Enter string2:
there
String1 is lesser than String2.
OUTPUT-3:
Enter string1:
there
Enter string2:
their
String1 is greater than String2.
EXERCISE-10
1. Write a program in C to demonstrate the use of & (address of) and *(value at
address) operator.
SOURCE CODE:
#include<stdio.h>
int main() {
int num1, *p1;
float num2, *p2;
char ch1, *p3;
printf("Enter int, float and char values : ");
scanf("%d %f %c",&num1, &num2, &ch1 );
p1=&num1;
p2=&num2;
p3=&ch1;
printf("Given int value : %d\n", *p1);
printf("Given float value : %f\n", *p2);
printf("Given char value : %c\n", *p3);
return 0;
}
OUTPUT:
Enter int, float and char values : 97 3.24 I
Given int value : 97
Given float value : 3.240000
Given char value : I
EXERCISE-11
1. Write a program in C to add numbers using call by reference.
SOURCE CODE:
#include<stdio.h>
#include<stdlib.h>
int main(){
int n, *eles, i, sum=0;
printf("Enter number of elements : ");
scanf("%d",&n);
eles = (int*)malloc(sizeof(int)*n);
printf("Enter %d elements : ",n);
for(i=0;i<n;i++)
scanf("%d",&eles[i]);
sum = add(eles, n);
printf("Sum of all elements is : %d\n",sum);
return 0;
}
int add(int *eles, int n){
int i, sum=0;
for(i=0;i<n;i++)
sum = sum + eles[i];
return sum;
}
OUTPUT:
Enter number of elements : 4
Enter 4 elements : 2 6 1 5
Sum of all elements is : 14
EXERCISE-12
1. Write a program in C to swap elements using call by reference.
SOURCE CODE:
#include<stdio.h>
int main() {
int num1, num2;
printf("Enter two integer values : ");
scanf("%d %d", &num1, &num2);
printf("Before swapping in main : num1 = %d \t num2 = %d\n", num1, num2);
swap(&num1, &num2);
printf("After swapping in main : num1 = %d \t num2 = %d\n", num1, num2);
return 0;
}
EXERCISE-13
1. Write a program in C to show how a function returning pointer.
SOURCE CODE:
#include<stdio.h>
int* findLarger(int*, int*);
int main() {
int num1, num2;
int *result;
printf("Enter two numbers : ");
scanf("%d %d", &num1, &num2);
result = findLarger(&num1, &num2);
printf("Largest number : %d\n" , *result);
return 0;
}
EXERCISE-14
1. Write a C program to find sum of n elements entered by user. To perform this
program, allocate memory dynamically using calloc( ) function. Understand the
difference between the above two programs.
SOURCE CODE:
#include<stdio.h>
#include<stdlib.h>
int main(){
int n, *eles, i, sum=0;
printf("Enter number of elements : ");
scanf("%d",&n);
eles = (int*)calloc(n,sizeof(int));
printf("Enter %d elements : ",n);
for(i=0;i<n;i++)
scanf("%d",&eles[i]);
for(i=0;i<n;i++)
sum = sum + eles[i];
printf("Sum of all elements is : %d\n",sum);
return 0;
}
OUTPUT:
Enter number of elements : 4
Enter 4 elements : 5 4 3 2
Sum of all elements is : 14
EXERCISE-15
1. Write a program in C to check whether a number is a prime number or not using
the function.
SOURCE CODE:
#include<stdio.h>
void isPrime(int);
int main(){
int n;
printf("Enter number : ");
scanf("%d",&n);
isPrime(n);
return 0;
}
void isPrime(int n){
int i, c=0;
for(i=1;i<=n/2;i++){
if(n%i==0)
c++;
}
if(c==1)
printf("Given number %d is a prime number.\n",n);
else
printf("Given number %d is not a prime number.\n",n);
}
OUTPUT-1:
Enter number : 5
Given number 5 is a prime number.
OUTPUT-2:
Enter number : 25
Given number 25 is not a prime number.
2. Write a program in C to get the largest element of an array using the function.
SOURCE CODE:
#include <stdio.h>
int large(int[], int);
int main() {
int a[10], i, n;
printf("Enter number of elements to be insert : ");
scanf("%d", &n);
printf("Enter %d elements : ",n);
for (i = 0; i < n; i++)
scanf("%d", &a[i]);
printf("The largest element of the array = %d\n", large(a, n));
return 0;
}
int large(int ary[], int n){
int i, max;
max=ary[0];
for(i=1;i<n;i++){
if(ary[i]>max)
max=ary[i];
}
return max;
}
OUTPUT:
Enter number of elements to be insert : 4
Enter 4 elements : 8 3 4 2
The largest element of the array = 8
EXERCISE-16
1. Write a program in C to append multiple lines at the end of a text file.
SOURCE CODE:
#include<stdio.h>
int main() {
FILE *fp;
char ch;
fp = fopen("Sample.txt", "w");
printf("Enter the text press ctrl+z for end of file: \n");
while ((ch=getchar())!=EOF) {
putc(ch,fp);
}
fclose(fp);
fp = fopen("Sample.txt", "a");
printf("Enter the text to append to a file press ctrl+z for end of file: \n");
while ((ch=getchar())!=EOF) {
putc(ch,fp);
}
fclose(fp);
fp = fopen("Sample.txt", "r");
printf("File content after appending : \n");
while ((ch=fgetc(fp))!=EOF) {
putchar(ch);
}
printf("\n");
fclose(fp);
return 0;
}
OUTPUT:
Enter the text press ctrl+z for end of file:
Hello
^Z
Enter the text to append to a file press ctrl+z for end of file:
Welcome
here you learn file concepts.
^Z
File content after appending :
Hello
Welcome
here you learn file concepts.