UCEN R23 Computer Programming Lab Record
UCEN R23 Computer Programming Lab Record
Program 1: Write an algorithm, draw the flow chart, and write a C program to
calculate the sum and average of 3 numbers
Aim: To write an algorithm, draw the flow chart, and write a C program to calculate
the sum and average of 3 numbers
Algorithm:
Flow Chart:
Source Code:
include <stdio.h>
int main()
{
int a, b, c, sum;
float avg;
printf("Enter 3 numbers: \n");
scanf("%d %d %d", &a, &b, &c);
sum = a + b + c;
avg = sum / 3;
printf("Sum = %d \n", sum);
printf("Average = %.2f", avg);
return 0;
}
Program 2: Write an algorithm, draw the flow chart, and write a C program to
convert Fahrenheit to Celsius
Aim: To write an algorithm, draw the flow chart, and write a C program to convert
Fahrenheit to Celsius
Algorithm:
Flow Chart:
Source Code:
#include<stdio.h>
int main()
{
float fahrenheit, celsius;
printf("\nEnter Fahrenheit: ");
scanf("%f",&fahrenheit);
celsius = (fahrenheit - 32)*5/9;
printf("\nCelsius: %f ", celsius);
return 0;
}
Program 3: Write an algorithm, draw the flow chart, and write a C program to
calculate the simple interest
Aim: To write an algorithm, draw the flow chart, and write a C program to calculate
simple interest
Algorithm:
Flow Chart:
Source Code:
# include <stdio.h>
int main ()
{
int principal, rate, time, interest;
printf("Enter the principal: ");
scanf("%d", &principal);
printf("Enter the rate: ");
scanf("%d", &rate);
printf("Enter the time: ");
scanf("%d", &time);
interest = (principal * rate * time) / 100;
printf("\nThe Simple interest is %d", interest);
return 0;
}
Program 6: Write a C program to find the area of the triangle using Heron’s formula
Aim: To find the area of a triangle using Heron's formula
Source Code:
#include<stdio.h>
#include<math.h>
void main()
{
float a,b,c,s=0,area=0;
printf("\nEnter the length of sides of triangle:);
scanf("%f %f %f",&a,&b,&c);
s = (a+b+c)/2.0;
area = sqrt((s*(s-a)(s-b)(s-c));
printf("Area of triangle =\t %f",area);
getch();
}
{
printf("\n%lf is the 2nd largest number\n",c);
}
else if(a >= b)
{
printf("\n%lf is the 2nd largest number\n", a);
}
else
{
printf("\n%lf is the 2nd largest number\n", b);
}
return 0;
}
Program 10: Write a C program to find the total and average of 5 subjects
Aim: Tl take the marks of 5 subjects in integers, and find the total, average in float.
#include<stdio.h>
int main()
{
int i;
int arr[5];
double sum=0.0, average=0.0;
printf("\nEnter the marks of 5 subjects: ");
for (i = 0; i< 5; i++)
{
printf(“ Enter subject %d marks”, i+1);
scanf(“%d”, arr[i]);
}
for (i = 0; i< 5; i++)
{
sum += arr[i];
}
average = sum/n;
printf( "\n The Sum of the Elements of the Array is %lf: ", sum);
printf( "\n The Average of the Elements of the Array is %lf: ", average);
return 0;
}
Program 11: Write a C program to find the maximum and minimum of four numbers
using if-else
Aim: Find maximum and minimum of four numbers using if- else
Source Code:
#include <stdio.h>
int main()
{
int num1, num2, num3, num4;
int max, min;
printf("Enter four numbers: ");
scanf("%d %d %d %d", &num1, &num2, &num3, &num4);
if (num1 >= num2 && num1 >= num3 && num1 >= num4)
{
max = num1;
}
else if (num2 >= num1 && num2 >= num3 && num2 >= num4)
{
max = num2;
}
else if (num3 >= num1 && num3 >= num2 && num3 >= num4)
{
max = num3;
}
else
{
max = num4;
}
if (num1 <= num2 && num1 <= num3 && num1 <= num4)
{
min = num1;
}
else if (num2 <= num1 && num2 <= num3 && num2 <= num4)
{
min = num2;
}
else if (num3 <= num1 && num3 <= num2 && num3 <= num4)
{
min = num3;
}
else
{
min = num4;
}
printf("Maximum number: %d\n", max);
printf("Minimum number: %d\n", min);
return 0;
}
}
return 0;}
case '-':
printf(“\n Subtraction is: %f ”,num1-num2);
break;
case '*':
printf(“\n Multiplication is: %f ”,num1*num2);
break;
case '/':
printf(“\n Division is: %f ”,num1/num2);
Lab Record Computer Programming Lab Page 21 of 84
University College of Engineering Narasaraopeta
Jawaharlal Nehru Technological University Kakinada
===================================
break;
default:
printf("Error! operator is not correct");
break;
}
getch();
return 0;
}
Program 15: Write a C program to find whether the given year is leap year or not
Aim: To find whether the given year is a leap year or not
Source Code:
#include <stdio.h>
int main()
{
int year;
printf("\n Enter a year: ");
scanf("%d", &year);
if (year % 400 == 0)
{
printf("%d is a leap year.", year);
}
else if (year % 100 == 0)
{
printf("%d is not a leap year.", year);
}
else if (year % 4 == 0)
{
printf("%d is a leap year.", year);
}
else
{
printf("%d is not a leap year.", year);
}
return 0;
}
Program 16: Write a C program to find the factorial of a given number using loops
Aim: To find the factorial of a given number using loops
Source Code:
#include<stdio.h>
int main()
{
int num, count, fact = 1;
printf("\nEnter a number to find its factorial\n");
scanf("%d", &num);
for(count = 1; count <= num; count++)
{
fact = fact * count;
}
printf("Factorial of %d is %d\n", num, fact);
return 0;
}
Program 17: Write a C program to check whether a given number is prime or not
Aim: To check whether the given number is prime or not.
Source Code:
#include <stdio.h>
int main()
{
int n, i, flag = 0;
printf("Enter a positive integer: ");
scanf("%d", &n);
if (n == 0 || n == 1)
flag = 1;
for (i = 2; i<= n / 2; ++i)
{
if (n % i == 0)
{
flag = 1;
break;
}
}
if (flag == 0)
printf("%d is a prime number.", n);
else
printf("%d is not a prime number.", n);
return 0;
}
Program 20: Write a C program to find the minimum of maximum number in a 1-D
array
Aim: To find minimum and maximum in a 1-D array
Source Code:
#include <stdio.h>
int main()
{
int array[10];
int i, min, max, size;
printf(“\n Please enter 10 interger values: “);
for(i=0;i<10;i++)
{
scanf(“%d”, &array[i]);
}
size = sizeof(array) / sizeof(array[0]);
min = array[0];
max = array[0];
for (int i = 1; i< size; i++)
{
if (array[i] < min)
{
min = array[i];
}
if (array[i] > max)
{
max = array[i]
}
}
Program 21: Write a C program to find 2’s complemet of a given binary number
Aim: To find 2’s complemt of the given binary number
Source Code:
#include <stdio.h>
#include <string.h>
void findTwosComplement(char binaryNumber[])
{
int length = strlen(binaryNumber);
int i;
for (i = length - 1; i>= 0; i--)
{
if(binaryNumber[i] == '1')
{
break;
}
}
if(i == -1)
{
printf("2's complement: %s\n", binaryNumber);
return;
}
for (int j = i - 1; j >= 0; j--)
{
if(binaryNumber[j] == '0')
{
binaryNumber[j] = '1';
}
else
Lab Record Computer Programming Lab Page 30 of 84
University College of Engineering Narasaraopeta
Jawaharlal Nehru Technological University Kakinada
===================================
{
binaryNumber[j] = '0';
}
}
printf("2's complement: %s\n", binaryNumber);
}
int main()
{
char binaryNumber[100];
printf("\nEnter a binary number: ");
scanf("%s", &binaryNumber);
findTwosComplement(binaryNumber);
return 0;
}
int main()
{
int i, Array[10];
Program 23: Write a C program to illustrate the concept of returning arrays from
function
Aim: Illustrate the concept of returning arrays from function
Source Code:
#include<stdio.h>
int* getEvenNumbers(int N)
{
static int evenNumberArray[100];
int i, even = 2;
for(i=0; i<N; i++)
{
evenNumberArray[i] = even;
even += 2;
}
return evenNumberArray;
}
int main()
{
int *array, counter;
array = getEvenNumbers(10);
printf("Even Numbers");
for(counter=0; counter<5; counter++){
printf("%d", array[counter]);
getch();
return 0;
}
int main()
{
int firstMatrix[10][10], secondMatrix[10][10], resultMatrix[10][10];
int rows, cols;
printf("Enter the number of rows and columns for the matrices: ");
scanf("%d %d", &rows, &cols);
printf("Enter the elements of the first matrix:\n");
for (int i = 0; i< rows; ++i)
{
for (int j = 0; j < cols; ++j)
{
scanf("%d", &firstMatrix[i][j]);
}
}
int main()
{
int
firstMatrix[MAX_ROWS][MAX_COLS],secondMatrix[MAX_ROWS][MAX_COLS],
result[MAX_ROWS][MAX_COLS];
int rowsFirst, colsFirst, rowsSecond, colsSecond;
int i, j;
printf("Enter rows and columns for the first matrix: ");
scanf("%d %d", &rowsFirst, &colsFirst);
printf("Enter elements of the first matrix:\n");
for (i = 0; i<rowsFirst; ++i)
{
for (j = 0; j <colsFirst; ++j)
{
scanf("%d", &firstMatrix[i][j]);
}
}
Lab Record Computer Programming Lab Page 38 of 84
University College of Engineering Narasaraopeta
Jawaharlal Nehru Technological University Kakinada
===================================
int main()
{
Lab Record Computer Programming Lab Page 40 of 84
University College of Engineering Narasaraopeta
Jawaharlal Nehru Technological University Kakinada
===================================
int matrix[50][50];
int rows, cols;
printf("Enter the number of rows and columns of the matrix: ");
scanf("%d %d", &rows, &cols);
printf("Enter the elements of the matrix:\n");
for (int i = 0; i< rows; ++i)
{
for (int j = 0; j < cols; ++j)
{
scanf("%d", &matrix[i][j]);
}
}
transposeMatrix(rows, cols, matrix);
return 0;
}
Program 27: Write a C program to determine the lower triangular matrix of a given
matrix
Aim: To determine the lower triangular matrix of a given matrix
Source Code:
#include <stdio.h>
void lowerTriangular(int mat[10][10], int row, int col)
{
int i, j;
for (i = 0; i< row; i++)
{
for (j = 0; j < col; j++)
{
if (j > i)
{
printf("0 ");
}
else
{
printf("%d ", mat[i][j]);
}
}
printf("\n");
}
}
int main()
{
int matrix[10][10];
Program 28: Write a C program to determine the upper triangular matrix of a given
matrix
Aim: To determine the upper triangular matrix of a given matrix
Source Code:
#include <stdio.h>
void upperTriangular(int mat[10][10], int row, int col)
{
int i, j;
for (i = 0; i< row; i++)
{
for (j = 0; j < col; j++)
{
if (j <i)
{
printf("0 ");
}
else
{
printf("%d ", mat[i][j]);
}
}
printf("\n");
}
}
int main()
{
int matrix[10][10];
Program 29: Write a C program to find the sum of all diagonal elements in a matrix
Aim: To find the sum of all the diagonal elements in a matrix
Source Code:
#include <stdio.h>
int main()
{
int matrix[10][10];
int row, col, i, j;
int diagonalSum = 0;
printf("Enter the number of rows and columns of the matrix: ");
scanf("%d %d", &row, &col);
printf("Enter the elements of the matrix:\n");
for (int i = 0; i< row; i++)
{
for (int j = 0; j < col; j++)
{
scanf("%d", &matrix[i][j]);
}
}
for (i = 0; i< row; i++)
{
for (j = 0; j < col; j++)
{
if (i == j)
{
diagonalSum += matrix[i][j];
}
}
Lab Record Computer Programming Lab Page 46 of 84
University College of Engineering Narasaraopeta
Jawaharlal Nehru Technological University Kakinada
===================================
}
printf("Sum of diagonal elements: %d\n", diagonalSum);
return 0;
}
Program 30: Write a C program to concatenate two strings without using built-in
functions.
Aim: To concatenate two strings without using built-in functions
Source Code:
#include <stdio.h>
int main()
{
char str1[50],str2[50];
char result[100];
int i, j;
printf("\n Please enter str1:\n");
gets(str1);
printf("\n Please enter str2:\n");
gets(str2);
for (i = 0; str1[i] != '\0'; i++)
{
result[i] = str1[i];
}
for (j = 0; str2[j] != '\0'; j++)
{
result[i + j] = str2[j];
}
result[i + j] = '\0';
printf("Concatenated String: %s\n", result);
return 0;
}
Program 31: Write a C program to compare two strings without using built-in
functions
Aim: To compare two strings without using built-in functions
Source Code:
#include<stdio.h>
int main()
{
int a=0,i;
char s1[20],s2[20];
printf("\n Please enter String1:\n");
gets(s1);
printf("\n Please enter String2:\n");
gets(s2);
for(i=0;s1[i]!='\0'||s2[i]!='\0';i++)
{
if(s1[i]!=s2[i])
{
a=1;
break;
}
}
if(a==0)
{
printf("the two strings are same.\n");
}
else
{
Program 32: Write a C program to find the length of the string without using built-
in functions.
Aim: To find the length of the string without using built-in functions.
Source Code:
#include<stdio.h>
main()
{
int count=0,i=0;
char name[20];
printf("enter name:\n");
gets(name);
while(name[i]!='\0')
{
count ++;
i++;
}
printf("\n The length of the string is : %d",count);
}
Program 33: Write a C program to copy one string to another string without using
built-in functions.
Aim: To copy one sting to another string without using built-in functions
Source Code:
#include<stdio.h>
main()
{
char str1[20],str2[20];
int i;
printf("\n Please enter String1:");
gets(str1);
for(i=0;str1[i]!='\0';i++)
{
str2[i]=str1[i];
}
str2[i]='\0';
printf("\n Copied String is %s", str2);
getch();
}
Program 34: Write a C program to find the length of a string without using built-in
functions
Aim: To find the length of a string
Source Code:
#include<stdio.h>
int main()
{
char str[100];
int i, length=0;
printf("Enter a string:");
scanf("%s",str);
for(i=0;str[i]!='\0';i++)
{
length++;
}
printf("\n Length of the string is: %d",length);
return 0;
}
Program 35: Write a C program to find whether the string is palindrome or not
without using built-in funtions
Aim: To find whether the string is a palindrome or not without using built-in
functions.
Source Code:
#include <stdio.h>
int main()
{
char str[100];
int i, L, flag = 0;
printf("\n Enter a string: ");
scanf("%s", str);
L = 0;
while (str[L] != '\0')
{
L++;
}
for(i = 0; i < L / 2; i++)
{
if (str[i] != str[L - i - 1])
{
flag = 1;
break;
}
}
if (flag == 0)
{
printf("%s is a palindrome.\n", str);
}
Lab Record Computer Programming Lab Page 54 of 84
University College of Engineering Narasaraopeta
Jawaharlal Nehru Technological University Kakinada
===================================
else
{
printf("%s is not a palindrome.\n", str);
}
return 0;
}
Program 36: Write a C program to reverse a string without using built-in functions
Aim: To reverse a string without using built-in functions
Source Code:
#include<stdio.h>
main()
{
char s1[20],s2[20];
int i,l=0;
printf("\n Enter s1:\n");
gets(s1);
while(s1[l]!='\0')
{
l++;
}
for(i=0;s1[i]!='\0';i++)
{
s2[i]=s1[l-i-1];
}
s2[i]!='\0';
printf("\n The reversed string is :%s", s2);
}
return 0;
}
void print_complex(struct complex c3)
{
printf("%f+i%f",c3.real,c3.img);
}
void sum_complex(struct complex c3, struct complex c4)
{
struct complex c5;
c5.real=c3.real+c4.real;
c5.img=c3.img+c4.img;
print_complex(c5);
}
void mul_complex(struct complex c3, struct complex c4)
{
struct complex c6;
c6.real=c3.real*c4.real-c3.img*c4.img;
c6.img=c3.real*c4.img+c4.real*c3.img;
print_complex(c6);
}
c7=mul_complex(c1,c2);
print_complex(c7);
}
void print_complex(struct complex c3)
{
printf("%f+i%f",c3.real,c3.img);
}
void sum_complex(struct complex c3, struct complex c4)
{
struct complex c5;
c5.real=c3.real+c4.real;
c5.img=c3.img+c4.img;
print_complex(c5);
}
struct complex mul_complex(struct complex c3,struct complex c4)
{
struct complex c6;
c6.real=c3.real*c4.real-c3.img*c4.img;
c6.img=c3.real*c4.img+c4.real*c3.img;
return c6;
}
Program 40: Write a C program to illustrate the concept of a function without return
type and without parameter list
Aim: A function without return type and without parameter list
Source Code:
#include<stdio.h>
void avg( );
main( )
{
avg( );
}
void avg( )
{
int m1,m2,m3;
float avg ;
printf("\n Enter three subject marks :”);
scanf("%d %d %d",&m1,&m2,&m3);
avg=(m1+m2+m3)/3;
printf(“\nAverage = %f”, avg);
}
Program 41: Write a C program to illustrate the concept of function without return
type and with parameter list
Aim: A function without return type and with parameter list
Source Code:
#include<stdio.h>
void avg(int,int,int);
main()
{
int m1,m2,m3;
printf("\nEnter three subject marks:");
scanf("%d %d %d",&m1,&m2,&m3);
avg(m1,m2,m3);
}
void avg (int a,int b,int c)
{
float avg;
avg=(a+b+c)/3;
printf("\nAverage=%f",avg);
}
Program 42: Write a C program to illustrate the concept of function with return type
and without parameter list
Aim: A function with return type and without parameter list
Source Code:
#include<stdio.h>
float avg();
main()
{
float result;
result=avg();
printf(“\n Average = %f”, result);
}
float avg()
{
int m1,m2,m3;
float avg;
printf("Enter three subject marks:");
scanf("%d %d %d",&m1,&m2,&m3);
avg=(m1+m2+m3)/3;
return avg;
}
Program 43: Write a C program to illustrate the concept of function with return type
and with parameter list
Aim: A function with return type and with parameter list.
Source Code:
#include<stdio.h>
float avg(int,int,int);
main()
{
float res;
int m1,m2,m3;
printf("Enter three subject marks:");
scanf("%d %d %d",&m1,&m2,&m3);
res=avg(m1,m2,m3);
printf("\n Average=%f",res);
}
float avg (int a, int b, int c)
{
float x;
x=(a+b+c)/3;
return x;
}
Program 46: Write a C program to find the LCM of two numbers using recursion
Aim: Find the LCM of two numbers using recursion
Source Code:
#include <stdio.h>
int lcm(int, int);
int main()
{
int a, b, result;
printf("Enter two numbers: ");
scanf("%d%d", &a, &b);
result = lcm(a, b); //call the function lcm recursively.
printf("The LCM of %d and %d is %d\n", a, b, result);
return 0;
}
int lcm(int a, int b)
{
static int common = 1;
if (common % a == 0 && common % b == 0)
{
return common;
}
common++;
lcm(a, b);
}
Program 47: Write a C program to find the factorial of a number using recursion
Aim: Recursive function to find the factorial of a given number
Source Code:
#include<stdio.h>
int rec_fact(int);
main()
{
int n,fact;
printf("enter the value of n:\n");
scanf("%d",&n);
fact=rec_fact(n);
printf("\n Factorial of %d is %d",n,fact);
}
int rec_fact(int n)
{
if(n==0)
{
return 1;
}
else
{
return n*rec_fact(n-1);
}
}
Program 48: Write a C pgoram to find the sum of n number using recursion
Aim: Recursive function to find the sum of n numbers
Source Code:
#include<stdio.h>
int sum_rec(int);
main()
{
int num,sum;
printf("enter a number");
scanf("%d",&num);
sum=sum_rec(num);
printf("sum=%d",sum);
}
int sum_rec(int num)
{
if(num!=0)
{
return num+sum_rec(num-1);
}
else
{
return 0;
}
}
Program 49: Write a C program to swap two numbers using call by reference
Aim: To swap two numbers using call by reference
Source Code:
#include <stdio.h>
void swap(int *num1, int *num2)
{
int temp = *num1;
*num1 = *num2;
*num2 = temp;
}
int main()
{
int a, b;
printf("\nEnter two numbers:\n");
scanf("%d %d", &a, &b);
printf("\nBefore swapping: a = %d, b = %d\n", a, b);
swap(&a, &b);
printf("\nAfter swapping: a = %d, b = %d\n", a, b);
return 0;
}
Program 50: Write a C program to find number of lowercase, uppercase, digits and
other characters in strings using pointers
Aim: Find number of lowercase, uppercase, digits and other characters in strings
using pointers
Source Code:
#include <stdio.h>
void countCharacters(const char *str, int *lowercase, int *uppercase, int *digits, int
*others)
{
while (*str != '\0')
{
if ((*str >= 'a') && (*str <= 'z'))
{
(*lowercase)++;
}
else if ((*str >= 'A') && (*str <= 'Z'))
{
(*uppercase)++;
}
else if ((*str >= '0') && (*str <= '9'))
{
(*digits)++;
}
else
{
(*others)++;
}
str++;
}
}
int main()
{
char inputString[100];
int lowercase = 0, uppercase = 0, digits = 0, others = 0;
printf("Enter a string: ");
fgets(inputString, sizeof(inputString), stdin);
countCharacters(inputString, &lowercase, &uppercase, &digits, &others);
printf("Lowercase characters: %d\n", lowercase);
printf("Uppercase characters: %d\n", uppercase);
printf("Digits: %d\n", digits);
printf("Other characters: %d\n", others);
return 0;
}
Program 51: Write a C program to manipulate the array values using pointers
Aim: To manipulate the array values using pointers
Source Code:
#include <stdio.h>
int main()
{
int arr[] = {1, 2, 3, 4, 5};
int *ptr = arr;
printf("Original array values: ");
for (int i = 0; i < 5; i++)
{
printf("%d ", *(ptr + i));
}
printf("\n");
*(ptr + 0) = 10;
printf("Modified array values: ");
for (int i = 0; i < 5; i++)
{
printf("%d ", *(ptr + i));
}
printf("\n");
return 0;
}
Program 52: Write a C program to write and read text into a file
Aim: To write and read text into a file
Source Code:
#include <stdio.h>
int main()
{
FILE *filePointer;
char data[100];
filePointer = fopen("example.txt", "w");
if (filePointer == NULL)
{
printf("Could not open the file for writing.\n");
return 1;
}
printf("Enter text to write to the file: ");
fgets(data, sizeof(data), stdin);
fprintf(filePointer, "%s", data);
fclose(filePointer);
filePointer = fopen("example.txt", "r");
if (filePointer == NULL)
{
printf("Could not open the file for reading.\n");
return 1;
}
printf("\nReading from the file:\n");
while (fgets(data, sizeof(data), filePointer) != NULL)
{
printf("%s", data);
Lab Record Computer Programming Lab Page 76 of 84
University College of Engineering Narasaraopeta
Jawaharlal Nehru Technological University Kakinada
===================================
}
fclose(filePointer);
return 0;
}
Program 53: Write a C program to copy the contents of one file to another file
Aim: To copy the contents of one file to another file
Source Code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE *fptr1, *fptr2;
char filename[100], c;
printf("Enter the filename to open for reading \n");
scanf("%s", filename);
fptr1 = fopen(filename, "r");
if (fptr1 == NULL)
{
printf("Cannot open file %s \n", filename);
exit(0);
}
printf("Enter the filename to open for writing \n");
scanf("%s", filename);
fptr2 = fopen(filename, "w");
if (fptr2 == NULL)
{
printf("Cannot open file %s \n", filename);
exit(0);
}
c = fgetc(fptr1);
while (c != EOF)
{
Lab Record Computer Programming Lab Page 78 of 84
University College of Engineering Narasaraopeta
Jawaharlal Nehru Technological University Kakinada
===================================
fputc(c, fptr2);
c = fgetc(fptr1);
}
printf("\nContents copied to %s", filename);
fclose(fptr1);
fclose(fptr2);
return 0;
}
Program 54: Write a C program to find the number of lines, words and characters in
a file
Aim: To write a C program to find no. of lines, words and characters in a file
Source Code:
#include<stdio.h>
int main()
{
FILE *fp;
char ch;
int lines = 0, words = 0, characters = 0;
char filename[50];
printf("Enter the filename: ");
scanf("%s", filename);
fp = fopen(filename, "r");
if (fp == NULL)
{
printf("Error opening file!\n");
return 1;
}
while ((ch = fgetc(fp)) != EOF)
{
characters++;
if (ch == '\n')
{
lines++;
}
if (ch == ' ' || ch == '\t' || ch == '\n')
{
words++;
}
}
if (characters > 0 && ch != ' ' && ch != '\t' && ch != '\n')
{
words++;
}
fclose(fp);
printf("Lines: %d\n", lines);
printf("Words: %d\n", words);
printf("Characters: %d\n", characters);
return 0;
}
Program 55: Write a C program to read integer values from the user and store them
in even.txt if the number is even and in odd.txt if the number is odd.
Aim: To write a C program to read integer values from the user and store them
ineven.txt if the number is even and in odd.txt if the number is odd.
Source Code:
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
void main()
{
int num;
char ans[5];
FILE *po, *pe;
clrscr();
po = fopen("odd.txt","w+");
if(po==NULL)
{
printf("Something went wrong!");
exit(1);
}
pe = fopen("even.txt","w+");
if(pe==NULL)
{
printf("Something went wrong!");
exit(1);
}
do
{
Lab Record Computer Programming Lab Page 82 of 84
University College of Engineering Narasaraopeta
Jawaharlal Nehru Technological University Kakinada
===================================
printf("Enter number:\n");
scanf("%d",&num);
if(num%2!=0)
{
fprintf(po,"%d\t",num);
}
else
{
fprintf(pe,"%d\t",num);
}
printf("Enter another number? (no to terminate)\n");
fflush(stdin);
scanf("%s",ans);
strlwr(ans);
}while(strcmp(ans,"no")!=0);
rewind(po);
rewind(pe);
printf("\nContent read from even.txt:\n");
while(!feof(pe))
{
fscanf(pe,"%d\t",&num);
printf("%d\t", num);
}
printf("\nContent read from odd.txt:\n");
while(!feof(po))
{
fscanf(po,"%d\t",&num);
printf("%d\t", num);
Lab Record Computer Programming Lab Page 83 of 84
University College of Engineering Narasaraopeta
Jawaharlal Nehru Technological University Kakinada
===================================
fclose(pe);
fclose(po);
}