C Programming
C Programming
C Programming
Aim:
To write C program to calculate simple interest.
Algorithm:
Step 1: Start
Step 2: Declare Variables for Principal amount, Rate of interest, time
Step 3: Read Principal amount, Rate of interest, time
Step 4: Calculate interest amount using the expression amt=(p*r*t)/100
Step 5: Print amt
Step 6: Stop
Program:
#include<stdio.h>
int main()
{
int p,r,t;
float amt;
printf("Enter Principle amount, Rate of interest & time to find simple interest: \n");
scanf("%d%d%d",&p,&r,&t);
amt=(p*r*t)/100;
printf("Simple interest = %f",amt);
return 0;
}
Output:
Enter Principle amount, Rate of interest & time to find simple interest:
20000
6
2
Simple interest = 2400.000
Result:
Thus, the C program to calculate Simple Interest was written executed and
the output was verified successfully.
Aim:
To find whether the given year is leap year or not.
Algorithm:
Step 1: Get the input year from the user to check for leap year.
Step 2 : if(year%400 ==0 || year%100 != 0 && year % 4 == 0)
printf(“ is a leap year.");
else
printf(“is not a leap year.");
Step 3 : Stop
Program:
#include <stdio.h>
int main()
{
int year;
printf("Enter a year: ");
scanf("%d",&year);
if(year%400 ==0 || year%100 != 0 && year % 4 == 0)
printf("%d is a leap year.", year);
else
printf("%d is not a leap year.", year);
return 0;
}
Output:
Trial 1:
Enter a year: 1900
1900 is not a leap year.
Trial 2:
Enter a year: 2400
2400 is a leap year.
Result:
Thus, the C program to find whether the given year is leap year or not was
written executed and the output was verified successfully.
Aim:
To check whether a given number is Armstrong number or not.
Algorithm:
Program:
#include <stdio.h>
int main()
{
int num, originalNum, rem,nod, sum= 0;
printf("Enter a integer: ");
scanf("%d", &originalNum);
num = originalNum;
nod=(int)log10(num) + 1;
while (num != 0)
{
rem = num%10; res
sum=sum+pow(nod,rem);
num /= 10;
}
if(sum == originalNum)
printf("%d is an Armstrong number.",originalNum);
else
printf("%d is not an Armstrong number.",originalNum);
return 0;
}
Output:
Enter a integer:
1634
1634 is an Armstrong number
Enter a integer:
153
153 is an Armstrong number
Result:
Thus, the C program check whether a given number is Armstrong number or not was
written executed and the output was verified successfully.
Aim:
To Generate following pattern in C Programing
1
1 2
1 2 3
Algorithm:
Step 1: Start
Step 2: Read Number of Rows to print
Step 3: Use outer loop for maintaining Number of rows
Step 4: Use Inner Loop appropriately for required Coolum output
Step 5: Print required numbers
Step 6: Stop
Program:
#include <stdio.h>
int main()
{
int N, i, j;
printf("Enter the number of rows: ");
scanf("%d", &N);
for (i = 1; i<= N; i++)
{
printf(“\n”);
for (j = 1; j <= i; j++)
{
printf(“%d\t”,j);
}
}
return 0;
}
Output:
Enter the number of rows: 4
1
1 2
1 2 3
1 2 3 4
Result:
Thus, the C program to generate mentioned pattern was written executed and the output
was verified successfully.
Aim:
Write C program to find sum of array elements.
Algorithm:
Step 1: Start
Step 2: Read Number of elements in the array as n
Step 3: Initialize a variable s=0 for summation purpose
Step 4: Read n elements and store it in a array called a, using for loop
Step 5: Add elements with s while reading.
Step 6: Print value of s
Step 7: Stop
Program:
#include<stdio.h>
void main()
{
int i,n, a[10],s=0;
printf("Enter the number of element :\n");
scanf("%d",&n);
printf("Enter element:\n");
for(i=0;i<n;i++)
{
printf("a[%d]=",i);
scanf("%d",&a[i]);
s=s+a[i];
}
printf("Sum of arrary element:%d",s);
}
Output:
Enter the number of element :
5
Enter element:
a[0]=10
a[1]=20
a[2]=30
a[3]=4
a[4]=7
Sum of arrary element:71
Result:
Thus, the C program to find sum of array elements was written executed
and the output was verified successfully.
Aim:
Write C program to Insert an Element in an Array.
Algorithm:
Step 1: Start
Step 2: Read Number of elements in the array as n
Step 3: Read n elements and store it in a array called a, using for loop
Step 4: Read element to be insert and position in num and pos
Step 5: From the end of the array, Using for loop start moving the elements one
index ahead till the required index reach.
Step 6: increase number of elements n by one
Step 7: Insert the element in the required index.
Step 8: Print all the elements.
Step 9: Stop
Program:
#include<stdio.h>
void main()
{
int i,n,pos,num, a[10];
printf("Enter the number of element :\n");
scanf("%d",&n);
printf("Enter element:\n");
for(i=0;i<n;i++)
{
printf("a[%d]=",i);
scanf("%d",&a[i]);
}
printf("\nEnter the pos where the no. is to be inserted :");
scanf("%d",&pos);
printf("\nEnter the the no. is to be inserted :");
scanf("%d",&num);
for(i=n-1;i>=pos;i--)
a[i+1]=a[i];
n=n+1;
a[pos]=num;
printf("\n Display array after insertion:\n");
for(i=0;i<n;i++)
{
printf("a[%d]=%d\n",i,a[i]);
}
}
Output:
Result:
Aim:
Algorithm:
Step 1: Start
Step 2: Declare variables m,n,p,q for storing number of rows and columns of two matrix.
Step 3: Read Number of rows and columns of first matrix as m,n
Step 4: Read Number of rows and columns of second matrix as p,q
Step 5: Check whether n==p if so go to step 6 else print matrix multiplication not
possible and stop the program.
Step 6: Using two nested for loop read elements of first matrix
Step 7: Using two nested for loop read elements of second matrix
Step 8: Using three nested for loop multiply first matrix element and second matrix
element and store it in third matrix.
Step 9: Using two nested for loop print third matrix.
Step 10: Stop.
Program:
#include<stdio.h>
#include <stdlib.h>
void main()
{
int i,j,m,n,p,q,k;
int a[10][10], b[10][10], c[10][10];
printf("\nEnter no of rows and column of matrixA:");
scanf("%d%d",&m,&n);
printf("\nEnter no of rows and column of matrixB:");
scanf("%d%d",&p,&q);
printf("\n Enter elements of matrix A:\n");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
printf("\n Enter elements of matrix B:\n");
for(i=0;i<p;i++)
for(j=0;j<q;j++)
scanf("%d",&b[i][j]);
if(n==p)
{
for(i=0;i<m;i++)
for(j=0;j<q;j++)
{
c[i][j]=0;
for(k=0;k<n;k++)
c[i][j]=c[i][j]+(a[i][k]*b[k][j]);
}
}
else
{
printf("\n Matrix cannot be multiplied");
exit(1);
}
printf("\n Display matrix A:\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
printf("%d\t",a[i][j]);
printf("\n");
}
printf("\n Display matrix B:\n");
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
printf("%d\t",b[i][j]);
printf("\n");
}
printf("\n Display Product:\n");
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
printf("%d\t",c[i][j]);
printf("\n");
}
}
Output:
4
5
6
Display matrix A:
1 2 3
4 5 6
Display matrix B:
7 8
9 10
11 12
Display Product:
58 64
139 154
Result:
Thus, the C program to Multiply two Matrix was written executed and the
output was verified successfully.
Aim:
Write C program to Find Length of a string with and without string function
Program:
Without String Funciton With String Function
#include <stdio.h> #include <stdio.h>
int main() #include<string.h>
{ int main()
char s[100]; {
int i; char s[100];
printf("Enter a string: "); printf("Enter a string: ");
scanf("%[^\n]", s); scanf("%[^\n]", s);
for(i = 0; s[i] != '\0'; ++i); printf("Length of string: %ld", strlen(s));
printf("Length of string: %d", i); return 0;
return 0; }
}
Output:
Result:
Thus, the C program to Find Length of a string with & without string function was
written executed and the output was verified successfully.
Aim:
Algorithm:
Step 1: Start
Step 2: Declare two character array named str, copystr
Step 3: Read a String and store it in str
Step 4: Using a for loop copy all the characters of str to copystr
Step 5: Add ‘\0’ at the end of Copystr
Step 6: Print copystr
Step 7: Stop
Program:
#include <stdio.h>
#include <string.h>
int main()
{
char Str[100], CopyStr[100];
int i;
printf("\n Please Enter any String : ");
gets(Str);
for (i = 0; Str[i]!='\0'; i++)
{
CopyStr[i] = Str[i];
}
CopyStr[i] = '\0';
printf("\n String that we coped into CopyStr = %s", CopyStr);
return 0;
}
Output:
Result:
Thus, the C program to copy a string without string function was written
executed and the output was verified successfully.
Aim:
Algorithm:
Step 1: Start
Step 2: Declare two character array named str, copystr
Step 3: Read a String and store it in str
Step 4: Using strcpy() function copy content of str to copystr
Step 5: Print copystr
Step 6: Stop
Program:
#include <stdio.h>
#include <string.h>
int main()
{
char Str[100], CopyStr[100];
int i;
printf("\n Please Enter any String : ");
gets(Str);
strcpy(CopyStr,Str);
printf("\n String that we coped into CopyStr = %s", CopyStr);
return 0;
}
Output:
Result:
Thus, the C program to copy a string with string function was written
executed and the output was verified successfully.
Aim:
Algorithm:
Step 1: Start
Step 2: Define a user defined function called compare it will take two input strings
and return 0 if both strings are same, return 1 if both strings are different.
Step 3: Inside compare function using a while loop check both the strings index by
index, if all index elements are same then return 0 or else return 1
Step 4: In Main function Declare two character array named str1, str2
Step 5: Read two Strings and store it in str1, str2
Step 6: Call user defined compare function by passing str1,str2 as input
Step 7: If the function return 0 then print both strings are same else print both strings
are different
Step 8: Stop
Program:
#include <stdio.h>
int compare(char[],char[]);
int main()
{
char str1[20];
char str2[20];
printf("Enter the first string : ");
gets(str1);
printf("Enter the second string : ");
gets(str2);
int c= compare(str1,str2);
if(c==0)
printf("strings are same");
else
printf("strings are not same");
return 0;
}
{
if(a[i]!=b[i])
{
flag=1;
break;
}
i++;
}
if(flag==0)
return 0;
else
return 1;
}
Output:
Run 1:
Run2:
Result:
Thus, the C program to compare two string without string function was
written executed and the output was verified successfully.
Aim:
Algorithm:
Step 1: Start
Step 2: Declare two character array named str1, str2
Step 3: Read two Strings and store it in str1, str2
Step 4: Call strcmp() function by passing str1,str2 as input
Step 5: If the function return 0 then print both strings are same else print both strings
are different
Step 6: Stop
Program:
#include <stdio.h>
#include<string.h>
int main()
{
char str1[20];
char str2[20];
int value;
printf("Enter the first string : ");
gets(str1);
printf("Enter the second string : ");
gets(str2);
value=strcmp(str1,str2);
if(value==0)
printf("strings are same");
else
printf("strings are not same");
return 0;
}
Output:
Enter the first string : C Programming
Enter the second string : C Language
strings are not same
Result:
Thus, the C program to compare two string with string function was written
executed and the output was verified successfully.
Aim:
Algorithm:
Step 1: Start
Step 2: Define a function named mySort(), It will take a integer array and number of
values in a array as input
Step 3: Inside mySort() function Using two nested for loops check elements and
swap elements to sort array.
Step 4: Print the sorted array
Step 5: In main function declare a array
Step 6: Read total number of elements in the array as n
Step 7: Using for loop read n elements one by one
Step 8: call mySort() function to sort array
Step 9: Stop
Program:
#include<stdio.h>
void mySort(int[],int);
void main ()
{
int arr[10],n,i;
printf("Enter Number of elements in the array\n");
scanf("%d",&n);
printf("Enter Elements one by one\n");
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
mySort(arr,n);
}
void mySort(int a[],int n)
{
int i, j,temp;
for(i = 0; i<n; i++)
{
for(j = i+1; j<n; j++)
{
if(a[j] < a[i])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
printf("Printing Sorted Element List ...\n");
for(i = 0; i<n; i++)
{
printf("%d\n",a[i]);
}
}
Output:
Result:
Aim:
Program:
#include<stdio.h>
int fibonacci(int);
int main()
{
int n, m= 0, i;
printf("Enter Total terms:\n");
scanf("%d", &n);
printf("Fibonacci series terms are:\n");
for(i = 1; i <= n; i++)
{
printf("%d\t", fibonacci(m));
m++;
}
return 0;
}
int fibonacci(int n)
{
if(n == 0 || n == 1)
return n;
else
return(fibonacci(n-1) + fibonacci(n-2));
}
Output:
Enter Total terms:
10
Fibonacci series terms are:
0 1 1 2 3 5 8 13 21 34
Result:
Aim:
Write C program to Swap Two Numbers using call by Reference Method.
Algorithm:
Step 1: Start
Step 2: Define a swap function such that it will accept two integer pointers
Step 3: Inside swap function interchange the values of both integers using pointer
Step 4: In main function Read two integers
Step 5: Call swap function using the address of integers read from user
Step 6: Print the value of both integers
Step 7: Stop
Program:
#include <stdio.h>
void swap(int *, int *);
int main()
{
int a,b;
printf("Enter Two Values:\n");
scanf("%d%d",&a,&b);
printf("Before swapping the values in main a = %d, b = %d\n",a,b);
swap(&a,&b);
printf("After swapping values in main a = %d, b = %d\n",a,b);
}
void swap (int *a, int *b)
{
int temp;
temp = *a;
*a=*b;
*b=temp;
printf("After swapping values in function a = %d, b = %d\n",*a,*b);
}
Output:
Enter Two Values:
10 20
Before swapping the values in main a = 10, b = 20
After swapping values in function a = 20, b = 10
After swapping values in main a = 20, b = 10
Result:
Thus, the C program to Swap Two Numbers using call by Reference Method
was written executed and the output was verified successfully.
Aim:
Step 1: Start
Step 2: Declare a character pointer named text
Step 3: Read number of character in the string from user as n
Step 4: Allocate n bytes in memory using malloc() function and store the starting
address in the pointer variable text
Step 5: Read and store the string in text
Step 6: print entered string and length of the string
Step 7: Delete the allocated memory using free() function
Step 8: Stop
Program:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n;
char *text;
printf("Enter limit of the text: ");
scanf("%d",&n);
text=(char*)malloc(n*sizeof(char));
printf("Enter text: ");
scanf(" ");
gets(text);
printf("Inputted text is: %s\n",text);
printf("\nLength is:%ld",strlen(text));
free(text);
return 0;
}
Output:
Enter limit of the text: 25
Enter text: This is C Programming Lab
Inputted text is: This is C Programming Lab
Length is : 25
Result:
Thus, the C program to Print Text using Dynamic Memory was written
executed and the output was verified successfully.
Aim:
Algorithm:
Step 1: Start
Step 2: Declare a integer pointer named arr
Step 3: Read total number of elements in the array from user as limit
Step 4: Allocate n*size(int) bytes in memory using malloc() function and store the
starting address in the pointer variable arr
Step 5: Check whether the memory is allocated or not, if not allocated print error
message and stop, if allocated then proceed to next step
Step 6: Using a for loop read elements and store in allocated memory using pointer
notation, also calculate sum of the elements
Step 7: Using for loop and pointer notation print the elements
Step 8: Print sum
Step 9: Stop
Program:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int *arr;
int limit,i;
int sum=0;
printf("Enter total number of elements: ");
scanf("%d",&limit);
arr=(int*)malloc(limit*sizeof(int));
if(arr==NULL)
{
printf("Insufficient Memory, Exiting... \n");
return 0;
}
printf("Enter %d elements:\n",limit);
for(i=0; i<limit; i++)
{
printf("Enter element %3d: ",i+1);
scanf("%d",(arr+i));
sum=sum + *(arr+i);
}
printf("Array elements are:");
for(i=0; i<limit; i++)
printf("%3d ",*(arr+i));
printf("\nSum of all elements: %d\n",sum);
return 0;
}
Output:
Result:
Aim:
Algorithm:
Step 1: Start
Step 2: Define a Employee structure with required fields using struct keyword
Step 3: Declare a employee structure pointer
Step 4: Read total number of employees as n
Step 5: Allocate memory of n*sizeof(emplyeestructure) using malloc() function
Step 6: If memory not allocated stop else proceed to next step
Step 7: Using a for loop read n number of employees details and store them in
respective structure variable using -> operator
Step 8: Calculate net pay of each employee by adding basic pay and allowance and
reducing deduction
Step 9: Using a for loop iterate each employee structure and print all data present in it.
Step 10: Stop
Program:
#include <stdio.h>
#include <stdlib.h>
struct emp
{
int empno ;
char name[10] ;
int bpay, allow, ded, npay ;
};
int main()
{
struct emp *pemp;
int n,i;
printf("Enter total number of Employes: ");
scanf("%d",&n);
pemp=(struct emp*)malloc(n*sizeof(struct emp));
if(pemp==NULL)
{
printf("Insufficient Memory, Exiting... \n");
return 0;
}
for(i = 0 ; i < n ; i++)
{
printf("\nEnter the employee number : ") ;
scanf("%d", &(pemp+i)->empno) ;
printf("\nEnter the name : ") ;
scanf("%s", (pemp+i)->name) ;
printf("\nEnter the basic pay, allowances & deductions : ") ;
scanf("%d %d %d", &(pemp+i)->bpay, &(pemp+i)->allow, &(pemp+i)->ded) ;
(pemp+i)->npay = (pemp+i)->bpay + (pemp+i)->allow - (pemp+i)->ded ;
}
printf("\nEmp. No. Name \t Bpay \t Allow \t Ded \t Npay \n\n") ;
for(i = 0 ; i < n ; i++)
{
printf("%d \t %s \t %d \t %d \t %d \t %d \n", (pemp+i)->empno,
(pemp+i)->name, (pemp+i)->bpay, (pemp+i)->allow, (pemp+i)->ded, (pemp+i)-
>npay) ;
}
}
Output:
Result:
Aim:
Algorithm:
Step 1: Start
Step 2: Declare a file pointer fp, sum=0 and i=0, average
Step 3: Open a file in read mode which consist of numbers
Step 4: Using a while loop, until end of file reach, read each number and add it with
sum variable and also increment i
Step 5: Calculate average=sum/i
Step 6: Print sum and average
Step 7: Stop
Program:
#include <stdio.h>
#include <stdlib.h>
#define DATAFILE "numbers.dat"
int main() {
FILE* fp;
int n[50], i = 0;
float sum = 0,average;
if ((fp = fopen(DATAFILE, "r")) == NULL) {
printf("Unable to open %s...\n", DATAFILE);
exit(0);
}
puts("Reading numbers from num.dat");
while (!feof(fp)) {
fscanf(fp, "%d ", &n[i]);
printf("%d : %d\n", i, n[i]);
sum += n[i];
i++;
}
fclose(fp);
if (i == 0)
printf("No data available in %s", DATAFILE);
average = sum / i;
printf("The Sum is %.3f for %d numbers\n", sum, i);
printf("The average is %.3f for %d numbers\n", average, i);
return 0;
}
Output:
Result:
Algorithm:
Step 1: Start
Step 2: Receive two strings through command line argument argv
Step 3: Use strcmp() function to compare two strings present in argv[1],argv[2]
Step 4: If strcmp() returns 0 then print both strings are same, otherwise print strings
are not same
Step 5: Stop
Program:
#include <stdio.h>
#include <string.h>
int main(int argc, char const *argv[]) {
if (strcmp(argv[1], argv[2]) == 0)
printf("Yes 2 strings are same\n");
else
printf("No, 2 strings are not same\n" );
return 0;
}
Output:
Run 1:
[root@vinu ~]# ./a.out Programming Programing
No, 2 strings are not same
Run 2:
[root@vinu ~]# ./a.out Programming Programming
Yes, 2 strings are same
Result:
Program:
#include<stdio.h>
#include<string.h>
int main(int argc,char*argv[])
{
FILE *sptr,*dptr; char ch;
if(argc!=3){
printf("Command Line Error. Need Two argument");
return;
}
sptr=fopen(argv[1],"r");
dptr=fopen(argv[2],"w");
if(sptr==NULL || dptr==NULL) {
printf("Error in opening file!");
return;
}
while((ch=fgetc(sptr))!=EOF){
fputc(ch,dptr);
}
printf("File Copied Successfully.");
fclose(sptr);fclose(dptr);
}
Output:
[root@vinu ~]# vi sample.txt
[root@vinu ~]# vi filecopy.c
[root@vinu ~]# ./a.out sample.txt samplecopy.txt
File Copied Successfully.
Result:
Thus, the C program to Copy Content of One File to Another File using
Command Line Argument was written executed and the output was verified
successfully.