Computer Programming - All Programs - WM
Computer Programming - All Programs - WM
DEPARTMENT OF CSIT
I YEAR – I SEMESTER
1. Write the algorithm and draw the flow chart to find the roots of a quadratic
equation
Step 1. Start
Step 2. Read the coefficients of the equation, a, b and c from the user.
Step 3. Calculate discriminant = (b * b) – (4 * a * c)
Step 4. If discriminant > 0:
4.1: Calculate root1 = ( -b + sqrt(discriminant)) / (2 * a)
4.2: Calculate root2 = ( -b - sqrt(discriminant)) / (2 * a)
4.3: Display “Roots are real and different”
4.4: Display root1 and root2
Step 5: Else if discriminant = 0:
5.1: Calculate root1 = -b / (2 *a)
5.2: root2 = root1
5.3: Display “Root are real and equal”
5.4: Display root1 and root2
Step 6. Else:
6.1: Calculate real = -b / (2 * a)
6.2:Calculate imaginary = sqrt(-discriminant) / (2 * a)
6.3: Display “Roots are imaginary”
6.4: Display real, “±” , imaginary, “i”
Step 7. Stop
2. Write the algorithm and draw the flow chart to find the sum of digits of a given
n digit number
Pseudo Code:
Input a Number
Initialize Sum to zero
While Number is not zero
Get Remainder by Number Mod 10
Add Remainder to Sum
Divide Number by 10
Print sum
Detailed Algorithm:
Step 1: Input N
Step 2: Sum = 0
Step 3: While (N != 0)
Rem = N % 10;
N = N / 10;
Flowchart:-
Program
#include <stdio.h>
main()
{
char ch = 'B';
printf("%c\n", ch); //printing character data
//print decimal or integer data with d and i
int x = 45, y = 90;
printf("%d\n", x);
printf("%i\n", y);
float f = 12.67;
printf("%f\n", f); //print float value
printf("%e\n", f); //print in scientific notation
// int a = 45;
// printf(“%u\n”, a); //print in unsigned format
// printf(“%llu\n” a); //print in unsigned long long form
int b=67;
printf("%o\n", b); //print in octal format
printf("%x\n", b); //print in hex format
Output
B
45
90
12.670000
1.267000e+001
103
43
Hello World
Hello World
Hello World
Hello
Hello
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
float celsius,fahrenheit;
clrscr();
getch();
}
Output:
Program:
#include<stdio.h >
int main()
{
int n, a;
printf("Enter number of rows: ");
scanf("%d",&n);
// print space
for(int j=i; j <= n; j++)
{
printf(" ");
}
// print digit
for(int k=1; k <= 2*i-1; k++)
{
printf("%d",a++);
}
// new line
printf("\n");
}
// print space
for(int j=n; j>=i; j--)
{
printf(" ");
}
// print digit
for(int k=1; k<=2*i-1; k++)
{
printf("%d",a++);
}
// new line
printf("\n");
}
return 0;
}
Output:
Enter N value: 5
1
123
12345
1234567
123456789
1234567
12345
123
1
Program 1:
#include <stdio.h>
int main()
{
char op;
float num1, num2, result=0.0f;
case '-':
result = num1 - num2;
break;
case '*':
result = num1 * num2;
break;
case '/':
result = num1 / num2;
break;
default:
printf("Invalid operator");
}
return 0;
}
Output
22 * 6
Program:
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main ()
{
int number = 0, digit = 0, sumOfDigits = 0;
clrscr();
printf("Enter any number\n ");
scanf("%d", &number);
while (number != 0)
{
digit = number % 10;
sumOfDigits = sumOfDigits + digit;
number = number / 10;
}
printf ("Sum of individual digits of a given number is %d", sumOfDigits);
getch();
}
Output:
1234
8. Write the calculator program given in question no 6 to run the operations until
user‘s choice is exit.
Program:
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
int main()
{
// declaration of local variable op;
int op, n1, n2;
float res;
char ch;
do
{
// displays the multiple operations of the C Calculator
printf (" Select an operation to perform the calculation in C Calculator: ");
printf (" \n 1 Addition \t \t 2 Subtraction \n 3 Multiplication \t 4 Division \n 5 Square
\t \t 6 Square Root \n 7 Exit \n \n Please, Make a choice ");
scanf ("%d", &op); // accepts a numeric input to choose the operation
case 2:
// Subtract two numbers
printf (" You chose: Subtraction");
printf ("\n Enter First Number: ");
scanf (" %d", &n1);
Dr. V. Gokula Krishnan, Assoc. Professor/CSIT 14 CVR College of Engineering
65131 – Computer Programming Lab Semester 1
case 3:
// Multiplication of the numbers
printf (" You chose: Multiplication");
printf ("\n Enter First Number: ");
scanf (" %d", &n1);
printf (" Enter Second Number: ");
scanf (" %d", &n2);
res = n1 * n2; // multiply two numbers
printf (" Multiplication of two numbers is: %.2f", res);
break; // break the function
case 4:
// Division of the numbers
printf (" You chose: Division");
printf ("\n Enter First Number: ");
scanf (" %d", &n1);
printf (" Enter Second Number: ");
scanf (" %d", &n2);
if (n2 == 0)
{
printf (" \n Divisor cannot be zero. Please enter another value ");
scanf ("%d", &n2);
}
res = n1 / n2; // divide two numbers
printf (" Division of two numbers is: %.2f", res);
break; // break the function
case 5:
// getting square of a number
printf (" You chose: Square");
printf ("\n Enter First Number: ");
scanf (" %d", &n1);
case 6:
// getting the square root of the number
printf (" You chose: Square Root");
printf ("\n Enter First Number: ");
scanf (" %d", &n1);
case 7:
printf (" You chose: Exit");
exit(0);
break; // break the function
default:
printf(" Something is wrong!! ");
break;
}
printf (" \n \n ********************************************** \n ");
} while (op != 7);
return 0;
}
Output:
9. Write a C program to generate all the prime numbers between 1 and n, where n
is a value supplied by the user.
Program 1:
#include<stdio.h>
#include<conio.h>
void main()
{
int n, i, j, count;
clrscr();
printf("Prime no.series\n");
printf("Enter any number\n");
scanf("%d", &n);
printf("The prime numbers between 1 to %d\n",n);
for(i = 1; i <= n; i++)
{
count = 0;
for(j = 1; j <=i; j++)
if(i % j == 0)
{
count++;
}
if(count == 2)
{
printf("%d\t", i);
}
}
getch();
}
Output:
Program 1:
#include <stdio.h> Program:
int main()
{ #include<stdio.h>
int num, originalNum, remainder, result = 0; int main()
printf("Enter a three-digit integer: "); {
scanf("%d", &num); int n,r,sum=0,temp;
originalNum = num; printf("enter the number=");
scanf("%d",&n);
while (originalNum != 0) temp=n;
{ while(n>0)
// remainder contains the last digit {
remainder = originalNum % 10; r=n%10;
sum=sum+(r*r*r);
result += remainder * remainder * remainder; n=n/10;
}
// removing last digit from the orignal number if(temp==sum)
originalNum /= 10; printf("armstrong number ");
} else
printf("not armstrong number");
if (result == num) return 0;
printf("%d is an Armstrong number.", num); }
else
printf("%d is not an Armstrong number.", num); Output:
return 0; enter the number=153
} armstrong number
11. Write a C program to define the macros SUM (a, b), SQUARE (a) and
SQUARE (SUM (a, b)) and print the results.
Program:
#include <stdio.h>
int main()
{
int num1, num2, num3;
return 0;
}
Output:
SUM(2, 5) = 7
SQUARE(4) = 16
SQUARE(SUM(2, 5) = 49
Program:
#include<stdio.h>
// Declaration
void Addition();
void main()
{
printf("\n ............. \n");
Addition();
}
void Addition()
{
int Sum, a = 10, b = 20;
Sum = a + b;
Output :
.............
Sum of a = 10 and b = 20 is = 30
#include<stdio.h>
int Multiplication();
int main()
{
Dr. V. Gokula Krishnan, Assoc. Professor/CSIT 22 CVR College of Engineering
65131 – Computer Programming Lab Semester 1
int Multi;
Multi = Multiplication();
printf("\n Multiplication of a and b is = %d \n", Multi );
return 0;
}
int Multiplication()
{
int Multi, a = 20, b = 40;
Multi = a * b;
return Multi;
}
Output :
#include<stdio.h>
void main()
{
int a, b;
Sum = a + b;
Output :
#include<stdio.h>
int main()
{
int a, b, Multi;
Multi = a * b;
return Multi;
}
Output :
13. Write a C function to calculate the sine series sum 1- x3/3! +x5//5!……… and
call the function.
Program:
#include<stdio.h>
#include<math.h>
double factorial(int);
void calc(float, float*);
int main()
{
int x;
float radian, result = 0;
calc(radian, &result);
return 0;
}
Output 1:
Enter value of x is degrees
0
Sin(0) = 0.000000
Output 2:
Enter value of x is degrees
30
Sin(30) = 0.500000
Output 3:
Enter value of x is degrees
45
Sin(45) = 0.707106
Output 4:
Enter value of x is degrees
60
Sin(60) = 0.866025
Output 5:
Enter value of x is degrees
90
Sin(90) = 1.000000
14. Write a C program in which a recursive and non-recursive functions are called
to compute factorial values based on user‘s choice
Program:
#include <stdio.h>
#include <conio.h>
void main()
{
int n, a, b;
clrscr();
printf("Enter any number\n");
scanf("%d", &n);
printf(“1. Factorial using Recursion\n”);
printf(“2. Factorial without using Recursion\n”);
printf(“Enter your Choice:\n”);
scanf(“%d”, &ch);
if(ch==1)
{
a = recfactorial(n);
printf("The factorial of a given number using recursion is %d \n", a);
}
else if(ch==2)
{
b = nonrecfactorial(n);
printf("The factorial of a given number using non-recursion is %d ", b);
}
else
printf(“Invalid Choice\n”);
getch();
}
int recfactorial(int x)
{
int f;
if(x == 0)
{
return(1);
}
else
{
f = x * recfactorial(x - 1);
return(f);
}
}
int nonrecfactorial(int x)
{
int i, f = 1;
for(i = 1;i <= x; i++)
{
f = f * i;
}
return(f);
}
Output 1:
Output 2:
15. Write a C program in which a recursive and non-recursive functions are called
to generate Fibonacci series based on user‘s choice
Program:
#include <stdio.h>
#include <conio.h>
#include <math.h>
#include <stdlib.h>
void fib(int n)
{
int a = 0, b = 1, c, count = 3;
if(n == 1)
printf("0");
else if(n == 2)
printf("0 1");
else
{
printf("0 1 ");
while(count <= n)
{
c = a + b;
printf("%d ", c);
a = b;
b = c;
count++;
}
}
}
int rfib(int n)
{
if(n == 1)
return 0;
else if(n == 2)
return 1;
else
{
return rfib(n - 1) + rfib(n - 2);
Dr. V. Gokula Krishnan, Assoc. Professor/CSIT 29 CVR College of Engineering
65131 – Computer Programming Lab Semester 1
}
}
Output:
Enter a number: 10
Program:
#include<stdio.h>
Output:
1. Without argument: When the above code is compiled and executed without passing any
argument, it produces following output.
$ ./a.out
Program Name Is: ./a.out
No Extra Command Line Argument Passed Other Than Program Name
2. Three arguments : When the above code is compiled and executed with a three
arguments, it produces the following output.
argv[1]: First
argv[2]: Second
argv[3]: Third
3. Single Argument : When the above code is compiled and executed with a single argument
separated by space but inside double quotes, it produces the following output.
4. Single argument in quotes separated by space : When the above code is compiled and
executed with a single argument separated by space but inside single quotes, it produces
the following output.
17. Write a C program to find the Sum of the Elements of a given List (Array).
Program:
#include<stdio.h>
int main()
{
//let's assume the maximum array size as 100.
//initialize sum as 0. Otherwise, it will take some garbage value.
int arr[100], size, i, sum = 0;
return 0;
}
Output:
18. Write a C program to implement two separate functions which return the
minimum and maximum values of a given array-list and call these functions.
Program:
#include <stdio.h>
int main()
{
int n;
printf("Enter number of elements in array: ");
scanf("%d",&n);
int numbers[n];
int i;
int min, max ;
printf("Enter %d numbers : ", n);
for (i = 0; i < n; i++)
{
scanf("%d", &numbers[i]);
}
min = minimum(numbers, n);
max= maximum(numbers, n);
printf("\nMinimum number in the array is : %d\n", min);
printf(“\nMaximum number is the array is %d\n”, max);
return 0;
}
Output:
Enter 6 numbers :
4
6
1
2
5
3
19. Write a C program to find the transpose of a given input matrix (read the
dimensions of matrix too as input).
Program:
#include <stdio.h>
int main()
{
int a[10][10], transpose[10][10], r, c;
printf("Enter rows and columns: ");
scanf("%d %d", &r, &c);
Output :
Entered matrix:
1 4 0
-5 2 7
20. Write a C program to implement two separate functions for finding the sum
and product of matrices and call these functions.
Program:
#include<stdio.h>
#include<stdlib.h>
// main function
int main()
{
// matrix
int a[][3] = { {5,6,7}, {8,9,10}, {3,1,2} };
int b[][3] = { {1,2,3}, {4,5,6}, {7,8,9} };
int c[3][3];
return 0;
}
Output :
First Matrix:
5 6 7
8 9 10
3 1 2
Second Matrix:
1 2 3
4 5 6
7 8 9
Sum of matrix:
6 8 10
12 14 16
10 9 11
Multiplication of matrix:
78 96 114
114 141 168
21 27 33
21. Write a C function to exchange the values of given two variables and call the
function (using pointers).
Program:
#include<stdio.h>
int main()
{
int a, b;
swap(&a, &b);
return 0;
}
temp = *x;
*x = *y;
*y = temp;
}
Output 1 :
Output 2 :
Program:
#include <stdio.h>
#include <stdlib.h>
int a[100];
int element, i, loc, size, n, j, choice;
int main()
{
printf("Enter the size of an array\n");
scanf("%d",&size);
printf("Enter %d array elements\n",size);
for(i=0;i<size;i++)
{
scanf("%d", &a[i]);
}
if(a[i]==n)
{
for(j=i;j<(size-1);j++)
{
a[j]=a[j+1];
}
break;
}
}
printf("List after deletion\n");
for(i=0;i<(size-1);i++)
{
printf("%d ",a[i]);
}
}
Output 1 :
Output 2 :
23. Write a C program to create a dynamic list of real numbers where the size of
the list is accepted as input, extend its size and release it (use dynamic memory
allocation functions).
Program:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i, max, newSize;
int *ptr;
// Reallocate memory
printf("\nEnter new size of the array: ");
scanf("%d", &newSize);
ptr = (int *) realloc(ptr, (newSize * sizeof(int)));
return 0;
}
Output :
Enter 5 elements:
10 20 30 40 50
Enter 2 elements: 60 70
24. Write a C program to accept string as input and find its length using a user-
defined string length function, reverse the string and check whether the string is
palindrome or not.
Program:
#include <stdio.h>
#include <string.h>
int main()
{
char str[100], tmp, rev[100];
int begin, end;
//input
printf("Enter string: ");
scanf("%s", str);
//reverse
begin = 0;
end = strlen(str) - 1; //-1 because last character is NULL \0
while(begin < end)
{
tmp = str[begin];
str[begin] = str[end];
str[end] = tmp;
begin++;
end--;
}
//output
printf("Reverse string: %s\n", str);
//checking palindrome
if(strcmp(rev,str) == 0)
printf("%s is palindrome!\n", rev);
else
return 0;
}
Output 1:
Output 2:
malayalam is Palindrome!
25. Write a C function to read a multi-word string and copy the input string to
other string (the destination string must be a dynamically allocated string).
Program:
#include <stdio.h>
#include<stdlib.h>
#include <string.h>
void main()
{
char str1[50], *str2;
int n;
free(str2);
}
Output:
String 1 is College
26. Write a C program to create a user defined data-type Complex and implement
addition, subtraction and multiplication operations on complex numbers.
Program:
#include <stdio.h>
#include <stdlib.h>
struct complex
{
int real, img;
};
int main()
{
int choice, x, y, z;
struct complex a, b, c;
while(1)
{
printf("\nPress 1 to add two complex numbers.\n");
printf("Press 2 to subtract two complex numbers.\n");
printf("Press 3 to multiply two complex numbers.\n");
printf("Press 4 to exit.\n");
printf("Enter your choice\n");
scanf("%d", &choice);
if (choice == 1)
{
c.real = a.real + b.real;
c.img = a.img + b.img;
if (c.img >= 0)
printf("Sum of the complex numbers = %d + %di",
c.real, c.img);
else
printf("Sum of the complex numbers = %d %di",
c.real, c.img);-
}
else if (choice == 2)
{
c.real = a.real - b.real;
c.img = a.img - b.img;
if (c.img >= 0)
printf("Difference of the complex numbers = %d +
%di", c.real, c.img);
else
printf("Difference of the complex numbers = %d
%di", c.real, c.img);
}
else if (choice == 3)
{
c.real = a.real*b.real - a.img*b.img;
c.img = a.img*b.real + a.real*b.img;
if (c.img >= 0)
printf("Multiplication of the complex numbers = %d +
%di", c.real, c.img);
else
printf("Multiplication of the complex numbers = %d
%di", c.real, c.img);
}
else if (choice == 4)
exit(0);
else
printf("Invalid choice.");
printf("\nPress any key to enter choice again...\n");
}
}
Output:
27. Write a C program to create a user defined data-type Student containing the
fields Roll No, name and date of birth (by creating a user defined type Date).
Implement C functions to read the details of a student and create an array of
students.
Program:
#include <stdio.h>
struct date
{
int dd, mm, yyyy;
};
struct student
{
int roll;
char name[50];
struct date dob;
};
int main()
{
int i, n;
printf("\nEnter the number of students:");
scanf("%d", &n);
// storing information
for (i = 0; i <n; i++)
{
printf("\n\nEnter the Information of student[%d]:", i+1);
printf("\nEnter Roll No:");
scanf("%d", &st[i].roll);
printf("Enter Name: ");
scanf("%s", st[i].name);
printf("Enter Date of Birth (dd, mm, yyyy): ");
scanf("%d%d%d", &st[i].dob.dd, &st[i].dob.mm, &st[i].dob.yyyy);
}
Dr. V. Gokula Krishnan, Assoc. Professor/CSIT 57 CVR College of Engineering
65131 – Computer Programming Lab Semester 1
printf("\n\nDisplaying Information:\n");
// displaying information
for (i = 0; i < n; i++)
{
printf("\n\nInformation of Student[%d]:”, i + 1);
printf("\nName:%s", st[i].name);
printf("\nRoll No:%d", st[i].roll);
printf("\nDate of Birth: %d/%d/%d", st[i].dob.dd, st[i].dob.mm, st[i].dob.yyyy);
}
return 0;
}
Output:
Displaying Information:
Information of Student[1]:
Name: abishek
Roll No: 10
Date of Birth: 10/2/1991
Information of Student[2]:
Name: balaji
Roll No: 20
Date of Birth: 12/5/2000
Program:
#include <stdio.h>
#include <string.h>
union student
{
char name[20];
char subject[20];
float percentage;
};
int main()
{
union student record1;
union student record2;
strcpy(record2.subject, "Physics");
printf(" Subject : %s \n", record2.subject);
record2.percentage = 99.50;
printf(" Percentage : %f \n", record2.percentage);
return 0;
}
Output:
29. Write a C program to read the content of a given text file and count the
number of characters, words and lines in it (Read the file name as command line
argument).
Program:
// testfile.c
#include<stdio.h>
#include<stdlib.h>
fclose(fp);
return 0;
}
Output:
$ gedit inputfile
This is Manish
I had worked in Wipro and Cisco
$ gcc testfile.c
$ a.out inputfile
No. of characters = 38
No. of words = 10
No. of lines = 2
30. Write a C program to read the content of a given text file, convert all lower
case letters into upper case and display it on the screen.
Program:
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
int main()
{
FILE *fp1, *fp2;
char ch;
while((ch=fgetc(fp1))!=EOF)
{
ch = toupper(ch);
fputc(ch,fp2);
}
rewind(fp2);
printf("\nContents of the Target File is:\n");
while((ch=getc(fp2))!=EOF)
{
printf("%c",ch);
}
printf("\n");
Dr. V. Gokula Krishnan, Assoc. Professor/CSIT 63 CVR College of Engineering
65131 – Computer Programming Lab Semester 1
fclose(fp1);
fclose(fp2);
return 0;
Output:
Source.txt File
Here is source code of the C Program to convert the content of file to Upper Case. The
C program is successfully compiled and run on a Linux system.
31. Write a C program to copy the contents of one file into another.
Program:
#include <stdio.h>
#include <stdlib.h> // For exit()
int main()
{
FILE *fptr1, *fptr2;
char file1[50], file2[50], c;
fclose(fptr1);
fclose(fptr2);
return 0;
}
Output:
32. Write a C program to write the record list of Student type into a binary file
student.dat. Re-open the file, read the records from the file and display on the
screen.
Program:
#include <stdio.h>
struct student
{
char name[50];
int rollno;
float avg;
};
int main()
{
struct student a[10], b[10];
FILE *fptr;
int i, n;
fptr=fopen("student.dat","wb");
printf(“\nEnter the Number of Students:”);
scanf(“%d”, &n);
fptr=fopen("student.dat","rb");
fread(b,sizeof(b),1,fptr);
printf(“\nThe Student Records present in the File are:”);
Dr. V. Gokula Krishnan, Assoc. Professor/CSIT 67 CVR College of Engineering
65131 – Computer Programming Lab Semester 1
Output: