C Lab Programs
C Lab Programs
c = a+b;
printf("a+b = %d \n",c);
c = a-b;
printf("a-b = %d \n",c);
c = a*b;
printf("a*b = %d \n",c);
c = a/b;
printf("a/b = %d \n",c);
c = a%b;
printf("Remainder when a divided by b = %d \n",c);
return 0;
}
Write programs simple control statements :
a)Roots of a Quadratic Equation,
b)extractingnumbers ofintegers,
c)reversing of a given number ,
d)finding sum of individual numbers ,
e)printing multiplication tables,
f)Armstrongnumber,
g)checking for prime,
h)magicnumberr,
2.
a) Roots of a Quadratic Equation
Ax2+bx+c=0
#include <math.h>
#include <stdio.h>
int main() {
int a, b, c, d, r1, r2;
printf("Enter coefficients a, b and c: ");
scanf("%d %d %d", &a, &b, &c);
d = b * b - 4 * a * c;
if (d > 0) {
r1 = (-b + sqrt(d)) / (2 * a);
r2 = (-b - sqrt(d)) / (2 * a);
printf("r1 = %d and r2 = %d", r1, r2);
}
else if (d == 0) {
r1 = r2 = -b / (2 * a);
printf("r1 = r2 = %d;", r1);
}
else {
printf(“Roots are imaginary roots”);
}
return 0;
}
b)extracting numbers of integers
#include <stdio.h>
#include<conio.h>
int main()
{
int n ;
printf(“Enter a number”);
scanf(“%d”,&n);
while(n != 0)
{
int r = n % 10;
n = n / 10;
printf("%d\n", r);
}
return 0;
}
c) reversing of a given number
#include<stdio.h>
int main()
{
int n, rev=0, r;
printf("Enter a number: ");
scanf("%d", &n);
while(n!=0)
{
r=n%10;
rev=rev*10+r;
n=n/10;
}
printf("Reverse of a Number: %d",rev);
return 0;
}
d) finding sum of individual numbers
#include<stdio.h>
int main()
{
int n,sum=0,r;
printf("Enter a number:");
scanf("%d",&n);
while(n>0)
{
r=n%10;
sum=sum+r;
n=n/10;
}
printf("Sum is=%d",sum);
return 0;
}
e)Multiplication table
#include<stdio.h>
int main()
{
int i,n;
printf(“enter n multiplication table :”):
scanf(“%d”,&n);
for(i=1;i<=10;i++)
printf(“%d * %d = %d. \n “,n,i,(n*i));
}
f)Armstrong Number
153 = (1*1*1)+(5*5*5)+(3*3*3)
where:
(1*1*1)=1
(5*5*5)=125
(3*3*3)=27
So:
1+125+27=153
371 = (3*3*3)+(7*7*7)+(1*1*1)
where:
(3*3*3)=27
(7*7*7)=343
(1*1*1)=1
So:
27+343+1=371
#include<stdio.h>
int main()
{
int n,r,sum=0,temp;
printf("enter the 3 digit number only");
scanf("%d",&n);
temp=n;
while(n>0)
{
r=n%10;
sum=sum+(r*r*r);
n=n/10;
}
if(temp==sum)
printf("armstrong number ");
else
printf("not armstrong number");
return 0;
}
g)prime or not
#include<stdio.h>
int main(){
int n,i,m=0,flag=0;
printf("Enter the number to check prime:");
scanf("%d",&n);
m=n/2;
for(i=2;i<=m;i++)
{
if(n%i==0)
{
printf("Number is not prime");
flag=1;
break;
}
}
if(flag==0)
printf("Number is prime");
return 0;
}
h) //Magic number
Suppose we have a 1729 number and we need to validate whether the
number is a magic number or not.
So, first we need to get the sum of all digits of the number which is
19 (1 + 7 + 2 + 9 = 19).
Reverse the sum of the number is 91,
and then get the product of the sum of original digits with the reverse of
that sum as
19 * 91 = 1729.
So, it is the magic number.
#include <stdio.h>
#include <conio.h>
int main ()
{
int n, temp, rev = 0, digit, sum= 0;
printf (" Enter a Number: \n");
scanf (" %d", &n); // get the number
temp = n; // assign the number to temp variable
while ( temp > 0)
{
sum= sum+ temp % 10;
temp = temp / 10;
}
temp = sum; // assign the sumto temp variable
printf (" \n The sum of the digits = %d", temp);
}
3. Sinx and Cosx values using series expansion
// sinx series
#include <stdio.h>
intmain(){
int i, j, n, fact, sign = - 1;
float x, p, sum = 0;
printf("Enter the value of x : ");
scanf("%f", &x);
printf("Enter the value of n : ");
scanf("%d", &n);
for (i = 1; i <= n; i += 2)
{
p = 1;
fact = 1;
for (j = 1; j <= i; j++)
{
p = p * x;
fact = fact * j;
}
sign = - 1 * sign;
sum += sign * p / fact;
}
printf("sin %0.2f = %f", x, sum);
return0;
}
Enter the value of x : 2
Enter the value of n : 3
sin 2.00 = 0.666667
// cos x series
#include <stdio.h>
intmain()
{
int i, j, n, fact, sign = - 1;
float x, p, sum = 0;
printf("Enter the value of x : ");
scanf("%f", &x);
printf("Enter the value of n : ");
scanf("%d", &n);
for (i = 2; i <= n; i += 2)
{
p = 1;
fact = 1;
for (j = 1; j <= i; j++)
{
p = p * x;
fact = fact * j;
}
sum += sign * p / fact;
sign = - 1 * sign;
}
printf("cos %0.2f = %f", x, 1+sum);
return0;
}
Enter the value of x : 4
Enter the value of n : 3
cos 4.00 = -7.000000
// function prototype
int convert(long long);
int main() {
long long n;
printf("Enter a binary number: ");
scanf("%lld", &n);
printf("%lld in binary = %d in decimal", n, convert(n));
return 0;
}
// function definition
int convert(long long n) {
int dec = 0, i = 0, rem;
while (n!=0) {
rem = n % 10;
n /= 10;
dec += rem * pow(2, i);
++i;
}
return dec;
}
Enter a binary number: 1101
1101 in binary = 13 in decimal
int main() {
int n, bin;
printf("Enter a decimal number: ");
scanf("%d", &n);
bin = convert(n);
printf("%d in decimal = %lld in binary", n, bin);
return 0;
}
return bin;
}
Enter a decimal number: 13
13 in decimal = 1101 in binary
//Binary to octal
#include <math.h>
#include <stdio.h>
int convert(long long bin);
int main() {
long long bin;
printf("Enter a binary number: ");
scanf("%lld", &bin);
printf("%lld in binary = %d in octal", bin, convert(bin));
return 0;
}
//Binary to hexadecimal
#include<stdio.h>
int main(){
longint binaryval, hexadecimalval =0, i =1, remainder;
printf("Enter the binary number: ");
scanf("%ld",&binaryval);
while(binaryval !=0){
remainder = binaryval %10;
hexadecimalval = hexadecimalval + remainder * i;
i = i *2;
binaryval = binaryval /10;
}
printf("Equivalent hexadecimal value: %lX",
hexadecimalval);
return0;
}
Output: Enter the binary number:11100
Equivalent hexadecimal value:1C
5. a) pyramid of numbers
#include <stdio.h>
int main() {
int i, j, n, k = 0;
printf("Enter the number of n: ");
scanf("%d", &n);
for (i = 1; i <= n; ++i, k = 0) {
for (j = 1; j <= n - i; ++j) {
printf(" ");
}
while (k != 2 * i - 1) {
printf("%d ",i);
++k;
}
printf("\n");
}
return 0;
}
b) pascal triangle
#include <stdio.h>
void main()
{
int n,c=1,b,i,j;
printf("Input number of rows: ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
for(b=1;b<=n-i;b++)
printf(" ");
for(j=0;j<=i;j++)
{
if (j==0||i==0)
c=1;
else
c=c*(i-j+1)/j;
printf("% 4d",c);
}
printf("\n");
}
}
6. a) factorial using recursion
#include<stdio.h>
long int multiplyNumbers(int n);
int main() {
int n;
printf("Enter a positive integer: ");
scanf("%d",&n);
printf("Factorial of %d = %ld", n, multiplyNumbers(n));
return 0;
}
int main()
{
int n = 9;
printf("%d", fib(n));
getchar();
return 0;
}
7. Finding the maximum, minimum, average and standard
deviation of given set of numbers using arrays
#include <stdio.h>
#include<math.h>
int main()
{
int a[1000],i,n,min,max,sum=0;
float avg=0.0,sd=0.0;
printf("Enter size of the array : ");
scanf("%d",&n);
printf("Enter elements in array : ");
for(i=0; i<n; i++)
scanf("%d",&a[i]);
min=max=a[0];
for(i=0; i<n; i++)
{
sum+=a[i];
if(min>a[i])
min=a[i];
if(max<a[i])
max=a[i];
}
for(i=0; i<n; i++)
{
sum=sum+a[i];
avg=sum/n;
sd += pow(a[i] - avg, 2);
}
printf("minimum of array is : %d",min);
printf("\nmaximum of array is : %d",max);
printf("\naverage of array is : %f",avg);
printf("\nsd of array is : %f",sqrt(sd/(float)n));
return 0;
}
9. a) Matrix addition
#include<stdio.h>
int main()
{
int a[2][2], b[2][2], i, j, c[2][2];
printf("Enter 2*2 matrix 1 elements :");
for(i=0; i<2; i++)
{
for(j=0; j<2; j++)
scanf("%d",&a[i][j]);
}
printf("Enter 2*2 matrix 2 elements :");
for(i=0; i<2; i++)
{
for(j=0; j<2; j++)
scanf("%d",&b[i][j]);
}
printf("\nAdding the two matrix.....");
for(i=0; i<2; i++)
{
for(j=0; j<2; j++)
c[i][j]=a[i][j]+b[i][j];
}
printf("\nBoth matrices added successfully!");
printf("\nHere is the new matrix:\n");
for(i=0; i<2; i++)
{
for(j=0; j<2; j++)
printf("%d ",c[i][j]);
printf("\n");
}
return 0;
}
b)matrix multiplication
#include<stdio.h>
int main()
{
int a[2][2], b[2][2], i, j, c[2][2];
printf("Enter 2*2 matrix 1 elements :");
for(i=0; i<2; i++)
{
for(j=0; j<2; j++)
scanf("%d",&a[i][j]);
}
printf("Enter 2*2 matrix 2 elements :");
for(i=0; i<2; i++)
{
for(j=0; j<2; j++)
scanf("%d",&b[i][j]);
}
printf("\nAdding the two matrix.....");
for(i=0; i<2; i++)
{
for(j=0; j<2; j++)
{
c[i][j]=0;
for(k=0;k<2;k++)
{
c[i][j]=c[i][j]+a[i][k]+b[k][j];
}
}
}
printf("\nHere is the multiplication matrix:\n");
for(i=0; i<2; i++)
{
for(j=0; j<2; j++)
printf("%d ",c[i][j]);
printf("\n");
}
return 0;
}
c) transpose of a matrix
#include <stdio.h>
int main() {
int i, j;
int a[3][3], t[3][3];
}
10.string functions
#include <stdio.h>
#include <string.h>
int main() {
char string1[20] = "Annie Besant Women’s College",string2;
printf("Length of string1: %d \n", strlen(string1));
printf("String copy %s", strcpy(string2,string1));
printf("String Lower %s", strlwr(string2,string1));
printf("String upper%s", strupr(string2,string1));
printf("String rev %s", strrev(string2));
printf("String concatenation %s", strcat(string2,string1));
printf("String compare %s", strcmp(string2,string1);
}
11. strings without using string functions
#include <stdio.h>
int main()
{
char str1[100],str2[100];
int i;
printf("Enter the first string:\n");
scanf("%s", &str1);
printf("Enter the second string:\n");
scanf("%s", &str2);
for(i = 0; str1[i] != '\0'; ++i);
printf("Length of first string: %d", i);
for(i = 0; str2[i] != '\0'; ++i);
printf("Length of second string: %d", i);
for(i=0;str1[i]!='\0' || str2[i]!='\0';i++)
{
if(str1[i]!=str2[i])
{
printf("Given strings are not identical\n");
return;
}
}
printf("Given strings are identical\n");
return 0;
}
12. FindingtheNo.ofcharacters,wordsandlinesofgiventextfile
#include <stdio.h>
#include <stdlib.h>
int main(){
FILE * file;
char path[100];
char ch;
int characters, words, lines;
file=fopen("counting.txt","w");
printf("enter the text.press cntrl Z:");
while((ch = getchar())!=EOF){
putc(ch,file);
}
fclose(file);
printf("Enter source file path: ");
scanf("%s", path);
file = fopen(path, "r");
if (file == NULL){
printf("Unable to open file.");
exit(EXIT_FAILURE);
}
characters = words = lines = 0;
while ((ch = fgetc(file)) != EOF){
characters++;
if (ch == '\n' || ch == '\0')
lines++;
if (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\0')
words++;
}
if (characters > 0){
words++;
lines++;
}
printf("Total characters = %d \n", characters);
printf("Total words = %d \n", words);
printf("Total lines = %d \n", lines);
fclose(file);
return 0;
}
fclose(fptr);
return 0;
}