0% found this document useful (0 votes)
253 views134 pages

Advanced C Lab Record

The document describes 14 experiments involving writing C programs to perform various mathematical calculations and analyze input values, with each experiment providing the aim, code sample, and expected output for programs to find additions/subtractions of two numbers, area calculations, ASCII values, GCD/LCM, vowel/consonant identification, capitalization, even/odd determination, maximum of two numbers, minimum of five numbers, digit sum and product, and the Fibonacci series.

Uploaded by

bhanuchandra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
253 views134 pages

Advanced C Lab Record

The document describes 14 experiments involving writing C programs to perform various mathematical calculations and analyze input values, with each experiment providing the aim, code sample, and expected output for programs to find additions/subtractions of two numbers, area calculations, ASCII values, GCD/LCM, vowel/consonant identification, capitalization, even/odd determination, maximum of two numbers, minimum of five numbers, digit sum and product, and the Fibonacci series.

Uploaded by

bhanuchandra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 134

Experiment No: 01 Date: 04/12/2018

AIM: Write a program to read two numbers and find the addition, subtraction, multiplication and division of
them.

Code:
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
int op;
//clrscr();
printf(" 1.Addition\n 2.Subtraction\n 3.Multiplication\n 4.Division\n");
printf("Enter the values of a & b: ");
scanf("%d %d",&a,&b);
printf("Enter your Choice : ");
scanf("%d",&op);
switch(op)
{
case 1 :
printf("Sum of %d and %d is : %d",a,b,a+b);
break;
case 2 :
printf("Difference of %d and %d is : %d",a,b,a-b);
break;
case 3 :
printf("Multiplication of %d and %d is : %d",a,b,a*b);
break;
case 4 :
printf("Division of Two Numbers is %d : ",a/b);
break;
default :
printf(" Enter Your Correct Choice.");
break;
}

Page | 1
getch();
}
Output:

Page | 2
Experiment No: 02 Date: 04/12/2018

AIM: Write a program to find area of circle, square and rectangle.

Code:
#include <stdio.h>
void main()
{
int op;
float side, base, length, breadth, height, area, radius;
printf("-------------------------\n");
printf(" 1 --> Circle\n");
printf(" 2 --> Rectangle\n");
printf(" 3 --> Triangle\n");
printf(" 4 --> Square\n");
printf("-------------------------\n");
printf("Enter the option\n");
scanf("%d", &op);
switch(op)
{
case 1:
printf("Enter the radius\n");
scanf("%f", &radius);
area = 3.142 * radius * radius;
printf("Area of a circle = %f\n", area);
break;
case 2:
printf("Enter the breadth and length\n");
scanf("%f %f", &breadth, &length);
area = breadth * length;
printf("Area of a Reactangle = %f\n", area);
break;
case 3:
printf("Enter the side\n");
scanf("%f", &side);
area = side * side;
Page | 3
printf("Area of a Square=%f\n", area);
break;
default:
printf("Error\n");
break;
}
}
Output:

Page | 4
Experiment No: 03 Date: 04/12/2018

AIM: Write a program to find the ASCII value of a character.

Code:
#include <stdio.h>
int main()
{
char c;
printf("Enter a character: ");
scanf("%c", &c);
printf("ASCII value of %c = %d", c, c);
return 0;
}
Output:

Page | 5
Experiment No: 04 Date:06/12/2018

AIM: Write a program to find the GCD of two numbers.

Code:
#include <stdio.h>
int main()
{
int n1, n2, i, gcd;
printf("Enter two integers: ");
scanf("%d %d", &n1, &n2);
for(i=1; i <= n1 && i <= n2; ++i)
{
if(n1%i==0 && n2%i==0)
gcd = i;
}
printf("G.C.D of %d and %d is %d", n1, n2, gcd);
return 0;
}
Output:

Page | 6
Experiment No: 05 Date: 06/12/2018

AIM: Write a program to find the LCM of two numbers.

Code:
#include <stdio.h>
int main()
{
int n1, n2, i, gcd, lcm;
printf("Enter two positive integers: ");
scanf("%d %d",&n1,&n2);
for (i=1; i <= n1 && i <= n2; ++i)
{
if (n1%i==0 && n2%i==0)
gcd = i;
}
lcm = (n1*n2)/gcd;
printf("The LCM of two numbers %d and %d is %d.", n1, n2, lcm);
return 0;
}
Output:

Page | 7
Experiment No: 06 Date: 06/12/2018

AIM: Write a program to find whether the character is vowel or consonant.

Code:
#include <stdio.h>
int main()
{
char ch;
printf("Enter any character: ");
scanf("%c", &ch);
if(ch=='a' || ch=='e' || ch=='i' || ch=='o' || ch=='u' || ch=='A' || ch=='E' || ch=='I' || ch=='O' || ch=='U')
{
printf("'%c' is Vowel.", ch);
}
else if((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))
{
printf("'%c' is Consonant.", ch);
}
else
{
printf("'%c' is not an alphabet.", ch);
}
return 0;
}
Output:

Page | 8
Experiment No: 07 Date:07/12/2018

AIM: Write a program to find capital letter of a small letter if the input is small letter and vice versa.

Code:
#include <stdio.h>
int main()
{
char ch;
printf("enter a alphabet : \n");
scanf("%c", &ch);
if((ch>='A' && ch<='Z') || (ch>='a' && ch<='z'))
{
if(ch>='A' && ch<='Z')
ch=ch+32;
else if(ch>='a' && ch<='z')
ch=ch-32;
else
printf("converted character is : %c",ch);
}
else
{
printf("Entered character is not a valid alphabet!!!");
}
return 0;
}
Output:

Page | 9
Experiment No: 08 Date: 07/12/2018

AIM: Write a program to find the type of input. Ex1: input : ‘i’ ,output : oval Ex2: input : ‘%’ ,output :
special character

Code:
#include<stdio.h>
int main()
{
char k;
printf("Enter : ");
scanf("%c",&k);
if(k>=65 && k<=90)
{
if(k=='A' || k=='E' || k=='I' || k=='O' || k=='U')
{
printf("%c is Vowel and capital letter", k);
}
else
{
printf("%c is consonant and capital letter",k);
}
}
else if(k>=97 && k<=122)
{
if(k=='a' || k=='e' || k=='i' || k=='o' || k=='u')
{
printf("%c is Vowel and small letter", k);
}
else
{
printf("%c is consonant and small letter",k);
}
}
else if(k>=48 && k<=57)
{
Page | 10
printf("digit");
}
else
{
printf("special character");
}
return 0;
}
Output:

Page | 11
Experiment No: 09 Date: 07/12/2018

AIM: Write a program to find whether the number is even or odd

Code:
#include <stdio.h>
int main()
{
int number;
printf("Enter an integer: ");
scanf("%d", &number);
if(number % 2 == 0)
printf("%d is even.", number);
else
printf("%d is odd.", number);
return 0;
}
Output:

Page | 12
Experiment No: 10 Date:11/12/2018

AIM: Write a program to find the largest number among two numbers.

Code:
#include<stdio.h>
#include<conio.h>
void main()
{
int a, b, big;
printf("Enter two number : ");
scanf("%d%d",&a,&b);
if(a>b)
{ big=a; }
else
{
big=b;
}
printf("Biggest of the two number is %d",big);
getch();
}

Output:

Page | 13
Experiment No: 11 Date: 11/12/2018

AIM: Write a program to find the smallest number among five numbers.

Code:
#include<stdio.h>
int main()
{
int i, n, sm;
printf ("Enter first number \n");
scanf ("%d", &n);
sm=n;
for (i=1; i<= 5-1 ; i++)
{
printf ("\n Enter another number \n");
scanf ("%d",&n);
if (n<sm)
sm=n;
}
printf ("\n The smallest number is %d", sm);
return 0;
}
Output:

Page | 14
Experiment No: 12 Date: 11/12/2018

AIM: Write a program to find the sum of digits in a number. Ex: input : 562 output : 13 because 5+6+2=13

Code:
#include <stdio.h>
void main()
{ long num, temp, digit, sum = 0;
printf("Enter the number \n");
scanf("%ld", &num);
temp = num;
while (num > 0)
{ digit = num % 10;
sum = sum + digit;
num /= 10; }
printf("Given number = %ld\n", temp);
printf("Sum of the digits %ld = %ld\n", temp, sum);
}
Output:

Page | 15
Experiment No: 13 Date:13/12/2018

AIM: Write a program to find the product of digits in a number. Ex: input : 562 output : 60 because
5*6*2=60

Code:
#include<stdio.h>
int main()
{
int num, rem, prod = 1;
printf("Enter a number: ");
scanf("%d", &num);
while(num != 0)
{ rem = num % 10;
prod *= rem;
num /= 10; }
printf("%d", prod);
return 0; }

Output:

Page | 16
Experiment No: 14 Date: 13/12/2018

AIM: Write a program to find the Fibonacci series.

Code:
#include <stdio.h>
int main()
{
int i, n, t1 = 0, t2 = 1, nextTerm;
printf("Enter the number of terms: ");
scanf("%d", &n);
printf("Fibonacci Series: ");

for (i = 1; i <= n; ++i)


{ printf("%d, ", t1);
nextTerm = t1 + t2;
t1 = t2;
t2 = nextTerm; }
return 0;
}

Output:

Page | 17
Experiment No: 15 Date: 13/12/2018

AIM: Write a program to find whether the Armstrong number or not.

Code:
#include <stdio.h>
int main()
{ int n, sum = 0, temp, remainder, digits = 0;
printf("Input an integer\n");
scanf("%d", &n);
temp = n;
while (temp != 0) {
digits++;
temp = temp/10; }
temp = n;
while (temp != 0) {
remainder = temp%10;
int p=1;
for (int c = 1; c <= digits; c++)
{
p = p*remainder;
}
sum = sum + p;
temp = temp/10;
}
if (n == sum)
printf("%d is an Armstrong number.\n", n);
else
printf("%d isn't an Armstrong number.\n", n);
return 0;
}
Output:

Page | 18
Experiment No: 16 Date: 17/12/2018

AIM: Write a program to find whether the number is a magic number or not

Code:
#include<stdio.h>
#include<conio.h>
int main () {
int num, digitsum, reverse=0,sum=0,number;
printf("Enter a number\n");
scanf("%d", &num);
number=num;
while(num != 0){
sum = sum + num%10;
num = num/10; }
digitsum=sum;
while (sum > 0) {
reverse = (reverse * 10) + (sum % 10);
sum = sum / 10;
}
if ((digitsum * reverse) == number) {
printf("%d is a Magic Number\n", number);
} else {
printf("%d is not a Magic Number\n", number);
}
return 0;
}

Output:

Page | 19
Experiment No: 17 Date: 17/12/2018

AIM: Write a program to find whether the number is a perfect number or not.

Code:
#include <stdio.h>
int main()
{
int i, num, sum = 0;
printf("Enter any number to check perfect number: ");
scanf("%d", &num);
for(i=1; i<num; i++)
{
if(num%i == 0)
{ sum += i; }
}
if(sum == num)
{
printf("%d is PERFECT NUMBER", num);
}
else
{
printf("%d is NOT PERFECT NUMBER", num);
}
return 0;
}

Output:

Page | 20
Experiment No: 18 Date: 17/12/2018

AIM: Write a program to find the number is a prime number or not

Code:
#include <stdio.h>
int main()
{
int n, i, flag = 0;
printf("Enter a positive integer: ");
scanf("%d", &n);
for(i = 2; i <= n/2; ++i)
{
if(n%i == 0)
{
flag = 1;
break;
}
}
if (n == 1)
{
printf("1 is neither a prime nor a composite number.");
}
else
{
if (flag == 0)
printf("%d is a prime number.", n);
else

Page | 21
printf("%d is not a prime number.", n);
}
return 0;
}
Output:

Experiment No: 19 Date: 20/12/2018

AIM: Write a program to find the root of quadratic equation.

Code:
#include <stdio.h>
#include <math.h>
int main()
{
int a, b, c, d;
double root1, root2;
printf("Enter a, b and c where a*x*x + b*x + c = 0\n");
scanf("%d%d%d", &a, &b, &c);
d = b*b - 4*a*c;
if (d < 0) { //complex roots
printf("First root = %.2lf + j%.2lf\n", -b/(double)(2*a), sqrt(-d)/(2*a));
printf("Second root = %.2lf - j%.2lf\n", -b/(double)(2*a), sqrt(-d)/(2*a));
}
else {
root1 = (-b + sqrt(d))/(2*a);
root2 = (-b - sqrt(d))/(2*a);
printf("First root = %.2lf\n", root1);
printf("Second root = %.2lf\n", root2);
}
}
Output:

Page | 22
Experiment No: 20 Date: 20/12/2018

AIM: Write a program to find the sum of individual number till input number. Ex: input: 5, output: 15
because 1+2+3+4+5=15

Code:
#include<stdio.h>
int main()
{ int n,sum=0;
printf("enter a no. : ");
scanf("%d",&n);
for(int i=1;i<=n;i++)
{ sum=sum+i; }
printf("sum = %d",sum);
}

Output:

Page | 23
Experiment No: 21 Date: 20/12/2018

AIM: Write a program to find the factorial of a number.

Code:
#include <stdio.h>
int main()
{ int c, n, fact = 1;
printf("Enter a number to calculate its factorial\n");
scanf("%d", &n);
for (c = 1; c <= n; c++)
fact = fact * c;
printf("Factorial of %d = %d\n", n, fact);
return 0;
}
Output:

Page | 24
Experiment No: 22 Date: 24/12/2018

AIM: Write a program to find the factors of a number.

Code:
#include <stdio.h>
int main()
{ int number, i;
printf("Enter a positive integer: ");
scanf("%d",&number);
printf("Factors of %d are: ", number);
for(i=1; i <= number; ++i)
{
if (number%i == 0)
{
printf("%d ",i);
}
}
return 0;
}
Output:

Page | 25
Experiment No: 23 Date: 24/12/2018

AIM: Write a program to list out the number of even factors, odd factors and prime factors of a number

Code:
#include <stdio.h>
int main()
{ int number, i,e=0,o=0;
printf("Enter a positive integer: ");
scanf("%d",&number);
printf("Factors of %d are: \n", number);
for(i=1; i <= number; ++i)
{
if (number%i == 0)
{
if(i%2==0)
{ printf("even factor = %d\n",i);
e++;
}
else
{ printf("odd factor = %d\n",i);
o++;
} } }
printf("\neven factors are %d and odd factors are %d",e,o);
return 0;
}

Page | 26
Output:

Experiment No: 24 Date: 24/12/2018

AIM: Write a program to find the addition of all the factors of a number.

Code:
#include <stdio.h>
int main()
{ int number, i,sum=0;
printf("Enter a positive integer: ");
scanf("%d",&number);
for(i=1; i <= number; ++i)
{ if (number%i == 0)
{
sum=sum+i;
}
}
printf("\nsum of factors = %d",sum);
return 0;
}

Output:

Page | 27
Experiment No: 25 Date: 26/12/2018

AIM: Write a program to find the accurate average of 5 numbers (float).

Code:
#include<stdio.h>
int main()
{ float a,b,c,d,e;
double avg;
printf("enter 5 float values : \n");
scanf("%f%f%f%f%f",&a,&b,&c,&d,&e);
avg=(double)(a+b+c+d+e)/5;
printf("avg = %lf",avg);
return 0;
}
Output:

Page | 28
Experiment No: 26 Date: 26/12/2018

AIM: Write a program with CUTM has a grading system .If one’s total marks in a paper are x, then: WAP
using switch statement to accomplish the task of assigning a grade, the input x. 90<=x<=100: grade=’O’
80<=x<=90: grade=’E’ 70<=x<=80: grade=’A’ 60<=x<=70: grade=’B’ 50<=x<=60: grade=’C’
35<=x<=50: grade=’D’ 0<=x<=35: grade=’F’

Code:
#include<stdio.h>
#include<conio.h>
int main()
{ int marks;
printf("Enter the marks of the students");
scanf("%d",&marks);
marks=marks/10;
switch(marks)
{
case 10:
Page | 29
case 9:

printf("grade='O'");
break;
case 8:
printf("grade='E'");
break;
case 7:
printf("grade='A'");
break;
case 6:
printf("grade='B'");
break;
case 5:
printf("grade='C'");
break;
case 4:
printf("grade='D'");
break;
default:
printf("grade='F'");
break;
}
getch();
return(0);
}
Output:

Page | 30
Experiment No: 27 Date: 26/12/2018

AIM: Write a program to find the count of digits present in a numbers.

Code:
#include <stdio.h>
int main()
{ long long n;
int count = 0;
printf("Enter an integer: ");
scanf("%lld", &n);
while(n != 0)
{ n /= 10;
++count; }
printf("Number of digits: %d", count);
}

Page | 31
Output:

Experiment No: 28 Date: 28/12/2018

AIM: Write a program to find the sum of first and last digit of a number.

Code:
#include <stdio.h>
int main()
{ int num, sum=0, firstDigit, lastDigit;
printf("Enter any number: ");
scanf("%d", &num);
lastDigit = num % 10;
while(num >= 10)
{
num = num / 10;
}
firstDigit = num;
sum = firstDigit + lastDigit;
Page | 32
printf("Sum of first and last digit: %d", sum);
return 0; }
Output:

Experiment No: 29 Date: 28/12/2018

AIM: Write a program to exchange/swap the first digit and the last digit.

Code:
#include <stdio.h>
#include<math.h>
int main()
{ int num, last, first, temp, swap, count = 0;
printf("Enter any number: ");
scanf("%d", &num);
temp = num;
last = temp % 10;
count = (int)log10(temp);
while(temp>=10)
{ temp /= 10; }
first = temp;

Page | 33
swap = (last pow(10, count) + first) + (num - (first * pow(10, count) + last));
printf("Last Digit: %d\n", last);
printf("First Digit: %d\n", first);
printf("%d is swapped to %d\n", num, swap);
return 0;}

Output:

Experiment No: 30 Date: 28/12/2018

AIM: Write a program to reverse a number.

Code:
#include <stdio.h>
int main()
{ int n, reverse = 0;
printf("Enter a number to reverse\n");
scanf("%d", &n);
while (n != 0)
{ reverse = reverse * 10;
reverse = reverse + n%10;
n = n/10; }
printf("Reverse of entered number is = %d\n", reverse);
return 0; }

Page | 34
Output:

Experiment No: 31 Date:31/12/2018

AIM: Write a program to find whether the number is palindrome or not.

Code:
#include <stdio.h>
int main()
{ int n, reverse = 0,num;
printf("Enter a number to reverse\n");
scanf("%d", &n);
num=n;
while (n != 0)
{ reverse = reverse * 10;
reverse = reverse + n%10;
n = n/10; }
if(num==reverse)
{ printf("Palindrome"); }

Page | 35
else
{
printf("not a Palindrome");
}
return 0;
}

Output:

Experiment No: 32 Date: 31/12/2018

AIM: Write a program to find the power of number using loops.

Code:
#include <stdio.h>
int main ()
{ int base, exponent;
Long int power = 1;
int i;
printf("Enter base: ");
scanf("%d", &base);
printf("Enter exponent: ");
scanf("%d", &exponent);
for(i=1; i<=exponent; i++)
{
power = power * base;

Page | 36
}
printf("%d ^ %d = %lld", base, exponent, power);
return 0;
}

Output:

Experiment No: 33 Date: 31/12/2018

AIM: AAAAA
BBBBB
CCCCC
DDDDD
EEEEE
Code:
#include<stdio.h>
void main()
{
int i,j;
for(i=65;i<=69;i++){
for(j=65;j<=69;j++){
printf("%c",i);
}

Page | 37
printf("\n");}
}
Output:

Experiment No: 34 Date:02/01/2019

AIM: 55555
44444
33333
22222
11111
Code:
#include<stdio.h>
void main()
{
int i,j;
for(i=5;i>=1;i--){
for(j=5;j>=1;j--){
printf("%d",i);
}
printf("\n");
}

Page | 38
Output:

Experiment No: 35 Date: 02/01/2019

AIM : EDCBA
EDCBA
EDCBA
EDCBA
EDCBA
Code:
#include<stdio.h>
void main()
{
int i,j;
for(i=69;i>=65;i--){
for(j=69;j>=65;j--){
printf("%c",j);
}
printf("\n");
}
}
Output:
Page | 39
Experiment No: 36 Date: 02/01/2019

AIM : 1
12
123
1234
12345
Code :
#include<stdio.h>
void main()
{
int i,j;
for(i=1;i<=5;i++){
for(j=1;j<=5;j++){
if(j>=1&&j<=i){
printf("%d",j);
}
}
printf("\n");
}

Page | 40
}

Output:

Experiment No: 37 Date: 04/01/2019

AIM: * * * * *
****
***
**
*
Code:
#include<stdio.h>
void main()
{
int i,j;
for(i=1;i<=5;i++){
for(j=1;j<=5;j++){
if(j>=1&&j<=6-i){
printf("*");
}
}
printf("\n");
}

Page | 41
}
Output:

Experiment No: 38 Date: 04/01/2019

AIM: 54321
5432
543
54
5
Code:
#include<stdio.h>
void main()
{
int i,j;
for(i=5;i>=1;i--){
for(j=5;j>=1;j--){
if(j>=1&&j>=6-i){
printf("%d",j);
}
}
printf("\n");
}

Page | 42
}
Output:

Experiment No: 39 Date: 04/01/2019

AIM: * * * * *
****
***
**
*
Code:
#include<stdio.h>
void main()
{int i,j;
for(i=1;i<=5;i++){
for(j=1;j<=5;j++){
if(j>=i&&j<=5){
printf("*");
}else{

Page | 43
printf(" ");
}} printf("\n");}}
Output:

Experiment No: 40 Date: 07/01/2019

AIM: *
* * *
* * * **
* * * * * **
* * * * * * * **
Code:
#include<stdio.h>
void main()
{
int i,j;
for(i=1;i<=5;i++){
for(j=1;j<=9;j++){
if(j>=6-i&&j<=4+i){
printf("*");
}
else
{
printf(" ");
Page | 44
}
}
printf("\n");}}
Output:

Experiment No: 41 Date: 07/01/2019

AIM: A
CCC
EEEEE
G GGGGGG
I I I I I I I I I
Code:
#include<stdio.h>
void main()
{
int i,j;
char k =65;
for(i=1;i<=5;i++){
for(j=1;j<=9;j++){
if(j>=6-i&&j<=4+i){
printf("%c",k);
}
else{
printf(" ");
Page | 45
}
}
k=k+2;
printf("\n");}}
Output:

Experiment No: 42 Date: 07/01/2019

AIM: 1
321
54 321
76 54 321
9 876 5 4321
Code:
#include<stdio.h>
void main()
{
int i,k,j,z=1;
for(i=1;i<=5;i++)
{
for(j=5-1;j>=i;j--)

Page | 46
{
printf(" ");
}
for(k=0;k<z;k++)
{
printf("%d",z-k);
}
z+=2;
printf("\n");
}
}
Output:

Experiment No: 43 Date: 11/01/2019

AIM: A
C B A
E D C B A
GF E D C B A
I HG F E D C BA

Code:
#include<stdio.h>
void main()
{
int i,k,j,z=0;
for(i=1;i<=5;i++)
{

Page | 47
for(j=5-1;j>=i;j--)
{
printf(" ");
}
for(k=0;k<=z;k++)
{
printf("%c", z-k+65);
}
z+=2;
printf("\n");
}
}

Output:

Experiment No: 44 Date: 11/01/2019

AIM : A
B A B
C B A B C
D C B A B C D
E D C B A B C D E
Code:
#include<stdio.h>
void main()
{
int i,j,k,k1;
for(i=1;i<=5;i++){
k=64+i;

Page | 48
k1=66;
for(j=1;j<=5;j++){
if(j>=6-i&&j<=5)
{
printf("%c",k);
k--;
}
else
{
printf(" ");
}
}//printf("\n");
for(j=6;j<=9;j++){
if(j>=6&&j<=4+i)
{
printf("%c",k1);
k1++;
}
else
{
printf(" ");
}
}
printf("\n");
}
}
Output:

Page | 49
Experiment No: 45 Date: 11/01/2019

AIM: 1
1 2 1
1 2 3 2 1
1 2 3 4 3 2 1
Code:
#include<stdio.h>
void main()
{
int i,j,k,k1;
for(i=1;i<=5;i++){
k=1;
k1=i-1;
for(j=1;j<=5;j++){
Page | 50
if(j>=6-i&&j<=5)
{
printf("%d",k);
k++;
}
else
{
printf(" ");
}
}//printf("\n");
for(j=6;j<=9;j++){
if(j>=6&&j<=4+i)
{
printf("%d",k1);
k1--;
}
else
{
printf(" ");
}
}printf("\n");
}
}
Output:

Page | 51
Experiment No: 46 Date: 18/01/2019

AIM: A B C D E F G
A B C D E
A B C
A
Code:
#include<stdio.h>
void main()
{
int i,j;
char k ;
for(i=1;i<=4;i++){
k=65;
for(j=1;j<=7;j++){
if(j>=i&&j<=8-i){
printf("%c",k);
k++;
}
Page | 52
else{
printf(" ");
}
}
printf("\n");
}
}

Output:

Experiment No: 47 Date: 18/01/2019

AIM: *
* *
* * *
* * * *
* * *
* *
*
Code:
#include<stdio.h>
void main()
{
int i,j,k=0;
for(i=1;i<=7;i++){
i<=4?k++:k--;
for(j=1;j<=4;j++){
if(j<=k){
printf("*");
}
else{
printf(" ");
Page | 53
}
}
printf("\n");
}
}

Output:

Experiment No: 48 Date: 18/01/2019

AIM: *
* *
* * *
* * * *
* * * * *
* * * *
* * *
* *
*
Code:
#include<stdio.h>
void main()
{
int i,j,k;
for(i=1;i<=5;i++){
k=1;
for(j=1;j<=9;j++){
if(j>=6-i&&j<=4+i&&k){
printf("*");
k=0;
Page | 54
}
else{
printf(" ");
k=1;
}
}
printf("\n");
}
for(i=1;i<=4;i++){
k=1;
for(j=1;j<=8;j++){
if(j>=i+1&&j<=9-i&&k){
printf("*");
k=0;
}
else{
printf(" ");
k=1;
}
}
printf("\n");
}

Output:

Page | 55
Experiment No: 49 Date: 21/01/2019

AIM: A
A B
A B C
A B C D
A B C D E
B C D E
C D E
D E
E
Code:
#include<stdio.h>
void main()
{
Page | 56
int i,j,k;
char al;
for(i=1;i<=5;i++){
k=1;
al=65;
for(j=1;j<=9;j++){
if(j>=6-i&&j<=4+i&&k){
printf("%c",al);
al++;
k=0;
}
else{
printf(" ");
k=1;
}
}
printf("\n");
}
for(i=1;i<=4;i++){
k=1;
al=65+i;
for(j=1;j<=8;j++){
if(j>=i+1&&j<=9-i&&k){
printf("%c",al);
al++;
k=0;
}
else{
printf(" ");
k=1;
}
}
printf("\n");
}

Page | 57
}
Output:

Experiment No: 50 Date: 21/01/2019

AIM: * *
* *
* *
* *
*
Code:
#include<stdio.h>
void main()
{
int i,j;
for(i=1;i<=5;i++){
for(j=1;j<=9;j++)
if(j==i||j==10-i){
printf("*");
}
else{
Page | 58
printf(" ");
}
printf("\n");
}
}

Output:

Experiment No: 51 Date: 21/01/2019

AIM: E E
D D
C C
B B
A
Code:
#include<stdio.h>
void main()
{
int i,j;
char k=69;
for(i=1;i<=5;i++){
for(j=1;j<=9;j++)
if(j==i||j==10-i){
printf("%c",k);
}
else{
Page | 59
printf(" ");
}
k--;
printf("\n");
}
}
Output:

Experiment No: 52 Date: 28/01/2019

AIM: *
* *
* *
* *
* *
* *
* * * * * * * *
Code:
#include<stdio.h>
void main()
{
int i,j;
for(i=1;i<=7;i++){
for(j=1;j<=7;j++){
if(j==1||i==7||i==j){
printf("*");
Page | 60
}
else{
printf(" ");
}}
printf("\n");}}
Output:

Experiment No: 53 Date: 28/01/2019

AIM: Write a program to print even numbers between 1 to n.

Code:
#include<stdio.h>
int main()
{
int n;
printf("enter the value of N\n");
scanf("%d",&n);
evennum(n);
}
int evennum(int N)
{
static int i=1;
Page | 61
if(i%2==0)
{
printf("%d\n",i);
}i++;
if(i==N+1)
{
return 0;
}
evennum(N);
}
Output:

Experiment No: 54 Date: 28/01/2019

AIM: Write a program to find the sum of individual number till the input number.
Code:
#include<stdio.h>
int main()
{
int n,sum;
printf("enter the value of N\n");
scanf("%d",&n);
sum=indivnum(n);
printf("%d",sum);
}
int indivnum(int N)
{
static int i=0, j=1;
i=i+j;
j++;
if(j==N+1)
{
return i;
}
Page | 62
indivnum(N);

}
Output:

Experiment No: 55 Date: 01/02/2019

AIM: Write a program to find whether a number is prime number or not.


Code:
#include<stdio.h>
int main()
{
int n,m;
printf("enter the value of N\n");
scanf("%d",&n);
m=primenum(n);
if(m==2)
{
printf("it a prime");
}
else
{
printf("it is not a prime");
}
}
int primenum(int N)
{
Page | 63
static int i=1,j=0;
if(N%i==0)
{
j++;
}i++;
if(i==N+1)
{
return j;
}
primenum(N);

Output:

Experiment No: 56 Date: 01/02/2019

AIM: Write a program to find the count of digits in a number.


Code:
#include<stdio.h>
int main()
{
int n,m;
printf("enter the value of N\n");
scanf("%d",&n);
m=digitnum(n);
printf("%d",m);

}
int digitnum(int N)
{
static int i=0;
if(N%10==0)
{
return i;
}
i++;

Page | 64
N=N/10;
digitnum(N);
}
Output:

Experiment No: 57 Date: 01/02/2019

AIM: Write a program to find the sum of digits in a number.


Code:
#include<stdio.h>
int main()
{
int n,m;
printf("enter the value of N\n");
scanf("%d",&n);
m=digitsum(n);
printf("sum of the digits %d",m);

}
int digitsum(int N)
{
static int i=0,j=0;
j=j+N%10;

if(N%10==0)
{
return j;
Page | 65
}

N=N/10;
digitsum(N);
}

Output:

Experiment No: 58 Date: 04/02/2019

AIM: Write a program to find the GCD of two numbers.


Code:
#include<stdio.h>
int main()
{
int a,b,r;
printf("enter two number\n");
scanf("%d%d",&a,&b);
r=gcd(a,b);
printf("GCD of %d and %d is %d" ,a,b,r);
}
int gcd(int a,int b)
{
static int i=0,g=0;
i++;
if(a%i==0 && b%i==0)
{
g=i;
}
Page | 66
if(i==a || i==b)
{
return g;
}
gcd(a,b);
}

Output:

Experiment No: 59 Date: 04/02/2019

AIM: Write a program to find the biggest number among three numbers.
Code:
#include<stdio.h>
int main()
{
int big;
big=biggest();
printf("biggest number is %d",big);
}
int biggest()
{
static int a,i=1,big=0;
printf("number %d\n",i);
scanf("%d",&a);
i++;
if(a>big)
{
big=a;
}
Page | 67
if(i<=3)
{
biggest();
}
return big;
}

Output:

Experiment No: 60 Date: 04/02/2019

AIM: Write a program to find the factorial of a numbers.


Code:
#include<stdio.h>
int main()
{
int a,r;
printf("enter a number\n");
scanf("%d",&a);
r=fac(a);
printf("factorial of %d is %d" ,a,r);
}
int fac(int a)
{
static int i=1;
i=i*a;
a=a-1;
if(a>0)
{

Page | 68
fac(a);
}
return i;
}

Output:

Experiment No: 61 Date: 07/02/2019

AIM: Write a program to print Fibonacci series.


Code:
#include<stdio.h>
int fib(int);
int main ()
{
int n,i=0,j;
printf ("enter the length of series\n");
scanf("%d",&n);

printf ("Fibonacci series\n");

for (j=1; j<=n; j++)


{
printf ("%d\t", fib(i));
i++;
}

Page | 69
return 0;
}
int fib (int a)
{
if(a==0)
{
return 0;
}
else if(a==1)
{
return 1;
}
else
return (fib(a-1) + fib(a-2));
}

Output:

Experiment No: 62 Date: 07/02/2019

AIM: Write a program to find the power of a number.


Code:
#include<stdio.h>
int main()
{
int base,expo,result;
printf("enter the base value\t");
scanf("%d",&base);
printf("enter the exponent value\t");
scanf("%d",&expo);
result=power(base,expo);
printf("value of %d^%d is %d ",base,expo,result);
}
int power(int a,int b)
{
static int i=1;

Page | 70
if(b!=0)
{
i=i*a;
b--;
power(a,b);
}
return i;}

Output:

Experiment No: 63 Date: 07/02/2019

AIM: Write a program to read and display elements in an array


Code:
#include<stdio.h>
int main()
{
int i,n;
int a[30];
printf("enter the size");
scanf("%d",&n);
printf("enter the elements");
for(i=0;i<n-1;i++)
Page | 71
{
scanf("%d",&a[i]);
}
printf("array elements are : ");
for(i=0;i<n-1;i++)
{
printf("%d, ",a[i]);
}
return 0;

}
Output:

Experiment No: 64 Date: 11/02/2019

AIM: Write a program to find even numbers in an array.


Code:
#include<stdio.h>
int main()
{
int a[20], i,n;
printf("enter the size of array");
scanf("%d",&n);
printf(" enter the elements in the array");
for(i=0;i<n;i++)
{
scanf("%d\n",&a[i]);
}
printf(" even nums in array");
for(i=0;i<n;i++)

Page | 72
{
if(a[i]%2==0)
{
printf("%d\n",a[i]);
}

}
printf(" odd nums in array");
for(i=0;i<n;i++)
{
if(a[i]%2!=0)
{
printf("%d",a[i]);
}

}
printf("\n");
return 0;

}
Output:

Experiment No: 65 Date: 11/02/2019

AIM: Write a program to reverse an array.


Code:
#include <stdio.h>

int main()
{
int n, c, d, a[100], b[100];

printf("Enter the number of elements in array\n");


scanf("%d", &n);

printf("Enter array elements\n");

for (c = 0; c < n ; c++)

Page | 73
scanf("%d", &a[c]);

for (c = n - 1, d = 0; c >= 0; c--, d++)


b[d] = a[c];

for (c = 0; c < n; c++)


a[c] = b[c];

printf("Reverse array is\n");

for (c = 0; c < n; c++)


printf("%d\n", a[c]);

return 0;
}
Output:

Experiment No: 66 Date: 11/02/2019

AIM: program to find the biggest element in the array.


Code:
#include <stdio.h>

int main()
{
int array[50], size, i, largest;
printf("\n Enter the size of the array: ");
scanf("%d", &size);
printf("\n Enter %d elements of the array: ", size);

for (i = 0; i < size; i++)


scanf("%d", &array[i]);

Page | 74
largest = array[i];

for (i = 1; i < size; i++)


{
if (largest < array[i])
largest = array[i];
}

printf("\n largest element present in the given array is : %d", largest);

return 0;}
Output:

Experiment No: 67 Date: 13/02/2019

AIM: Write a program to find the second biggest number in the array.
Code:
#include <stdio.h>
int main() {
int a[10], n;
int largest1, largest2, i;

printf("enter number of elements you want in array");


scanf("%d", &n);
printf("enter elements");
for (i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
Page | 75
largest1 = a[0];
for (i = 0; i < n; i++) {
if (a[i] > largest1) {
largest1 = a[i];
}
}
largest2 = a[0];
for (i = 1; i < n; i++) {
if (a[i] > largest2 && a[i] < largest1)
largest2 = a[i];
}
printf("First and second largest number is %d and %d ", largest1, largest2);
}

Output:

Experiment No: 68 Date: 13/02/2019

AIM: Write a program to find the number of prime numbers in the array.
Code:
#include <stdio.h>

int isPrime(int num)


{
int i;
int flag=0;

for(i=2; i<num/2; i++)


{
if(num%i ==0)
{
Page | 76
flag =1;
break;
}
}
if(flag==1)
return 0;
else
return 1;
}

int main()
{
int loop;
int arr[]={100, 200, 31, 13, 97, 10, 20, 11};
//calculate length of the array
int len = sizeof(arr)/sizeof(arr[0]);

for(loop=0; loop<len; loop++)


{
printf("%3d - %s\n",arr[loop],(isPrime(arr[loop])?"Prime":"Not Prime"));
}

printf("\n");

return 0;
}
Output:

Experiment No: 69 Date: 13/02/2019

AIM: Write a program to reverse an array using swapping technique.


Code:
#include <stdio.h>
int main() {
int array[100], n, c, t, end;

scanf("%d", &n);
end = n - 1;

for (c = 0; c < n; c++) {


scanf("%d", &array[c]);
}

Page | 77
for (c = 0; c < n/2; c++) {
t = array[c];
array[c] = array[end];
array[end] = t;

end--;
}

printf("Reversed array elements are:\n");

for (c = 0; c < n; c++) {


printf("%d\n", array[c]);
}

return 0;
}
Output:

Experiment No: 70 Date: 15/02/2019

AIM: program to move each element to the next position and the last element to the first position
Code:
#include<stdio.h>
int show(int *k,int s)
{
int i,b[50];
b[0]=k[s-1];
for(i=0;i<s;i++)
{
if(i<s)
Page | 78
{
b[i+1]=k[i];
}
}
for(i=0;i<s;i++)
{
printf("%d\n",b[i]);
}
}
int main()
{
int a[50],n,i,flag;
printf("enter the size of array:\n");
scanf("%d",&n);
for(i=0;i<=n-1;i++)
{
scanf("%d",&a[i]);
}
printf("updated array :\n");
show(a,n);
}

Output:

Page | 79
Experiment No: 71 Date: 15/02/2019

AIM: program to find the conjugate even pairs in an array.


Code:
#include<stdio.h>
int show(int *k,int s)
{ int i,count;
count=0;
for(i=0;i<s-1;i++)
{
if(k[i]%2==0&&k[i+1]%2==0)
{
Page | 80
count++;
}
}
return count;
}
int main()
{
int a[50],n,i,big;
printf("enter the size of array:\n");
scanf("%d",&n);
for(i=0;i<=n-1;i++)
{
scanf("%d",&a[i]);
}
big=show(a,n);
printf("number of consecutive even pairs in the arrray is %d\n",big);
}
Output:

Experiment No: 72 Date: 15/02/2019

AIM: C program that takes two arrays of integers and print the smallest number in the first array which is
not present in the second array.
Code:
#include<stdio.h>
void main()
{
int a[30],a1[30];
int i,j,k,n,c=1;
printf("enter the size of array:\n");
scanf("%d",&n);

Page | 81
printf("first array elements:\n");
for(i=0;i<=n;i++){
scanf("%d",&a[i]);
}
printf("second array elements:\n");
for(i=0;i<=n;i++){
scanf("%d",&a1[i]);
}//code for sorting the elements in asscending order
for(i=0;i<=n;i++){
for(j=i+1;j<=n;j++){
if(a[i]>a[j]){
k=a[i];
a[i]=a[j];
a[j]=k;
} } }
for(i=0;i<=n;i++){
c=1;
for(j=0;j<=n;j++){
if(a[i]==a1[j]){
c=c+1;
} }
if(c==1){
printf("shortest number %d\n",a[i]);
break;
}
}
}
Output:

Page | 82
Experiment No: 73 Date: 18/02/2019

AIM: program to read and display elements in a matrix.


Code:
#include<stdio.h>
void main()
{
int a[50][50],b[50][40];
int i,j,r,c,sum;
printf("enter number of rows and coloumns of matrix a:\n");
scanf("%d%d",&r,&c);
for(i=1;i<=r;i++){
Page | 83
for(j=1;j<=c;j++){
scanf("%d",&a[i][j]);
}
}
printf("matrix :\n");
for(i=1;i<=r;i++){
for(j=1;j<=c;j++){
printf("%d ",a[i][j]);
}
printf("\n");
}
}
Output:

Experiment No: 74 Date: 18/02/2019

AIM : program to perform Matrix addition.


Code:
#include<stdio.h>
void main()
{
int a[50][50],b[50][50],sum[50][50];
int i,j,r,c,k,rr,cc;
printf("enter number of rows");
scanf("%d",&r);
printf("enter number of coloumns");

Page | 84
scanf("%d",&c);
printf("enter number of rows");
scanf("%d",&rr);
printf("enter number of coloumns");
scanf("%d",&cc);
for(i=1;i<=r;i++){
for(j=1;j<=c;j++){
scanf("%d",&a[i][j]);
}
}
for(i=1;i<=rr;i++){
for(j=1;j<=cc;j++){
scanf("%d",&b[i][j]);
}
}
printf("matrix a:\n");
for(i=1;i<=r;i++){
for(j=1;j<=c;j++){
printf("%d ",a[i][j]);
}
printf("\n");
}
printf("matrix b :\n");
for(i=1;i<=rr;i++){
for(j=1;j<=cc;j++){
printf("%d ",b[i][j]);
}
printf("\n");
}
printf("sum of matrix is:\n");
for(i=1;i<=r;i++){
for(j=1;j<=c;j++){
sum[i][j]= a[i][j]+b[i][j];
printf("%d ",sum[i][j]);

Page | 85
}
printf("\n");
}
}
Output:

Experiment No: 75 Date: 18/02/2019

AIM : program to perform Matrix multiplication.


Code:
#include<stdio.h>
void main()
{
int a[50][50],b[50][50],mul1[50][50];
int i,j,r,c,k,mul=0,rr,cc;
printf("enter number of rows");
scanf("%d",&r);
printf("enter number of coloumns");
Page | 86
scanf("%d",&c);
printf("enter number of rows");
scanf("%d",&rr);
printf("enter number of coloumns");
scanf("%d",&cc);
for(i=1;i<=r;i++){
for(j=1;j<=c;j++){
scanf("%d",&a[i][j]);
}
}
for(i=1;i<=rr;i++){
for(j=1;j<=cc;j++){
scanf("%d",&b[i][j]);
}
}
printf("matrix a:\n");
for(i=1;i<=r;i++){
for(j=1;j<=c;j++){
printf("%d ",a[i][j]);
}
printf("\n");
}
printf("matrix b :\n");
for(i=1;i<=rr;i++){
for(j=1;j<=cc;j++){
printf("%d ",b[i][j]);
}
printf("\n");
}
if(c==rr)
{
printf("mul of matrix is:\n");
for(i=1;i<=r;i++){
for(j=1;j<=c;j++){

Page | 87
for(k=1;k<=r;k++){
mul= mul+a[i][k]*b[k][j];
}
mul1[i][j]=mul;
printf("%d ",mul1[i][j]);
mul=0;
}
printf("\n");
}
}
else{
printf("matrix multiplication is not possible hence number of rows of matrix a is not equal to number
of coloumn of matrix b ");
}
}
Output:

Experiment No: 76 Date: 26/02/2019

AIM: Program to find that matrix is symmetric or not using functions


Code:
#include<stdio.h>
void display(int m,int n,int arr[10][10])
{
int i,j,b[30][30],count=0;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)

Page | 88
{
printf("%d",arr[i][j]);
}
printf("\n");
}
printf("Transpose matrix :\n");
for(i=0;i<m;i++){
for(j=0;j<n;j++){
b[i][j]=arr[j][i];
printf("%d",b[i][j]);
}printf("\n");
}
for(i=0;i<m;i++){
for(j=0;j<n;j++){
if(b[i][j]!=arr[i][j])
{
count=1;
break;
}
}
}if(count==0)
{
printf("sym");
}else
{
printf("not sym");
}
}
int main()
{
int arr[10][10];
int i,j,m,n;
printf("enter the number of rows and coloumns:");
scanf("%d%d",&m,&n);

Page | 89
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&arr[i][j]);
}
}
display(m,n,arr);
return 0;
}
Output:

Experiment No: 77 Date: 26/02/2019

AIM : Program to read a matrix and display the sum of odd numbers of the diagonal elements.
Code:
#include<stdio.h>
int main()
{
int a[50][50];
int i,j,r,c,sum;
printf("enter number of rows and coloumns of matrix a:\n");

Page | 90
scanf("%d%d",&r,&c);
for(i=1;i<=r;i++){
for(j=1;j<=c;j++){
scanf("%d",&a[i][j]);
}
}
printf("matrix :\n");
for(i=1;i<=r;i++){
for(j=1;j<=c;j++){
printf("%d",a[i][j]);
}printf("\n");
}
printf("sum of odd elements in diagonal matrix :\n");
sum=0;
for(i=1;i<=r;i++){
for(j=1;j<=c;j++){
if(i==j)
{
if(a[i][j]%2!=0)
{
sum = sum+a[i][j];
}
}
}
}
printf("%d",sum);
printf("\n");
}
Output:

Page | 91
Experiment No: 78 Date: 26/02/2019

AIM : program to read and display elements in a cross order in the Matrix.
Code:
Page | 92
#include<stdio.h>
void main()
{
int a[30][30];
int i,j,r,c,k,s;
printf("enter number of rows and columns:\n");
scanf("%d%d",&r,&c);
for(i=1;i<=r;i++){
for(j=1;j<=c;j++){
scanf("%d",&a[i][j]);
}
}
for(i=1;i<=r;i++){
for(j=1;j<=c;j++){
printf("%d ",a[i][j]);
}printf("\n");
}
printf("cross order matrix is :\n");
for(i=1;i<=r;i++){
k=i;
for(j=1;j<=i;j++){
printf("%d ",a[k][j]);
k--;
}printf("\n");
}
s=1;
for(i=r+1;i<=r*2-1;i++){
k=i-s;
s++;
for(j=s;j<=r;j++){
printf("%d ",a[k][j]);
k--;
}
printf("\n");

Page | 93
}
}
Output:

Experiment No: 79 Date: 01/03/2019

Aim: Program to concatenate two strings without using predefined functions

Page | 94
Code:
#include<stdio.h>
int* show(char k[],char k1[],int m,int o)
{ int i=m,j=0;
while(k1[j]!='\0')
{ k[i]=k1[j];
i++;
j++;
} k[i+1]='\0';
return k;
}
int main()
{
char str[30],str2[56];
int i,n,n1;
printf("enter string1\n");
gets(str);
printf("enter string2\n");
gets(str2);
n = strlen(str);
n1 = strlen(str2);
int *z=show(str,str2,n,n1);
printf(" %s",z);
}
Output:

Experiment No: 80 Date: 01/03/2019

Aim: Program to find string palindrome using functions. (Without using predefined functions).
Page | 95
Code:
#include<stdio.h>
void main()
{
char str[30],str1[30];
int i,l=0,c=0;
printf("enter string:\n");
gets(str);
for(i=0;str[i]!='\0';i++)
{
l++;
}
for(i=0;i<l/2;i++)
{
if(str[i]!=str[l-1])
{
c=1;
break;
}
l=l-1;
}
if(c==1)
{
printf("not paliindrome\n");
}
else
{
printf("palindrome");
}
}

Output:

Page | 96
Experiment No: 81 Date: 01/03/2019

Page | 97
AIM: Program to find the longest word in the string and display its length.
Code:
#include<stdio.h>
#include<string.h>
void main()
{
char a[50],b[50],c[50];
int i,j=0,l=0;
printf("enter a string : ");
gets(a);
for(i=0;i<=strlen(a);i++)
{
if(a[i]!=32 && a[i]!='\0')
b[j++]=a[i];
else
{
b[j]='\0';
if(strlen(b)>l)
{
strcpy(c,b);
l=strlen(b);
}
j=0;
}
}
printf("\nlongest word : %s",c);
}
Output:

Experiment No: 82 Date: 05/03/2019

Page | 98
AIM: Program to reverse the individual words with in the string.
cse it mech and chem.
esc ti hcem dna mehc.
Code:
#include<stdio.h>

int main()
{
char string[100],str[100];
int i,j=0,c=0,k=0,m=0;
printf("enter the string\n");
gets(string);
for(i=0;string[i]!='\0';i++)
{
if(string[i] !=' ')
{
str[j]=string[i];
j++;
}
else
{
str[j]='\0';
for(c=j-1;c>=0;c--)
{
printf("%c",str[c]);
}
printf(" ");
j=0;
}
}
str[j]='\0';
for(c=j-1;c>=0;c--)
{
printf("%c",str[c]);

Page | 99
}
}
Output:

Experiment No: 83 Date: 05/03/2019

Page | 100
AIM: Program to find the occurrence of spaces and number of words.

Code:
#include<stdio.h>
int display(char *k)
{
int i,j=0,m=0,l,n=0;
l=strlen(k);
for(i=0;i<=l;i++)
{
if((k[i]==' '))
{
m++;
}
}
for(i=0;i<=l;i++)
{
if((k[i]==' ')&&((k[i+1]!=' ')&&(k[i+1]!='\0')))
{
j=j+1;
}
}
printf("num of wrds %d\n",j+1);
printf("%d\n",m);
}
int main()
{
char str[3000];
printf("enter string:");
gets(str);
display(str);
}

Output:

Page | 101
Experiment No: 84 Date: 05/03/2019

Page | 102
AIM: We say that a string 's' is an anagram of another string 't' if the letters in 's' can be rearranged to form
't'. For example, "butterfly" is an anagram of "flutterby", since a rearrangement of the first word results in
the second.
Code:
#include<stdio.h>
void main()
{
char str1[30],str2[30],k,k1;
int i,j,l1=0,l2=0,c=1;
printf("enter string 1:\n");
gets(str1);
printf("enter string 2:\n");
gets(str2);
l1=strlen(str1);
l2=strlen(str2);
for(i=0;i<l1;i++){
for(j=i+1;j<l1;j++){
if(str1[i]>str1[j]){
k=str1[i];
str1[i]=str1[j];
str1[j]=k;
}
}
}
for(i=0;i<l2;i++){
for(j=i+1;j<l2;j++){
if(str2[i]>str2[j]){
k1=str2[i];
str2[i]=str2[j];
str2[j]=k1;
}
}
}
if(l1==l2)
{

Page | 103
for(i=0;str1[i]!='\0';i++)
{
if(str1[i]!=str2[i]){
c=c+1;
}
}
if(c==1)
{
printf("the given two strings are anagrams\n");
}
else
{
printf("the given strings are not anagrams\n");
}
}
else
{
printf("the given strings are not anagrams\n");
}
}

Output:

Experiment No: 85 Date: 07/03/2019

Page | 104
AIM : Program to read a string and display the number of times the occurrence of each letter from A to Z by
using pointers and functions Ex: WELCOME TO CUTM O/p: A 0 TIMES B 0 TIMES C 2 TIMES
Code:
#include<stdio.h>
int main()
{
char str[100];
printf("enter the string from A to Z:");
gets(str);
anagram(str);
}
int anagram( char k[])
{
int i,l;
int j;
for(j='A';j<='Z';j++)
{
l=0;
for(i=0;k[i]!='\0';i++)
{
if(k[i]==j)
l++;
}
printf("%c %d TIMES \t\t",j,l);
}
}
Output:

Experiment No: 86 Date: 07/03/2019

Page | 105
AIM: c program to read a string and replace existing characters with new characters and then check the
resultant string is palindrome or not.
Code:
#include<stdio.h>
int main()
{
char str[100];
printf("enter the string:\n");
gets(str);
alphabet(str);
pallin(str);
}
int alphabet(char k[])
{
int i,l=0,f[26];
for(i=0;k[i]!='\0';i++)
{
l++;
}
for(i=0;i<26;i++)
{
f[i]=0;
}
for(i=0;i<l;i++)
{
if(k[i]>='a' && k[i]<='z')
{
k[i]++;
}
} }
int pallin(char l[])
{ char str2[20];
int i,c=0, m = 0;
for (i = 0; l[i] != '\0'; i++)
Page | 106
{ m++; }
for (i = m - 1; i >= 0; i--)
{
str2[m - i - 1] = l[i];
}
for ( i = 0; i < m; i++)
{
if (str2[i] != l[i])
c = 1;
break;
}
if (c == 0)
printf("%s is a palindrome.", l);
else
printf("%s is not a palindrome", l);
return 0;
}
Output:

Experiment No: 87 Date: 07/03/2019

Page | 107
AIM: program to replace the existing substring with new string and display the reverse of the resultant
string using functions.
Code:
#include<stdio.h>
int main()
{ char str[100];
printf("enter the string:\n");
gets(str);
alphabet(str);
// printf("%s",str);
sub(str);}
int alphabet(char k[])
{ int i,l=0,f[26];
for(i=0;k[i]!='\0';i++)
{ l++; }
for(i=0;i<26;i++)
{ f[i]=0; }
for(i=0;i<l;i++)
{
if(k[i]>='a' && k[i]<='z')
{ k[i]++; }
}
}
int sub(char l[])
{ char str2[20];
int i,c=0, m = 0;
for (i = 0; l[i] != '\0'; i++)
{
m++;
}
for (i = m - 1; i >= 0; i--)
{
str2[m - i - 1] = l[i];
}
printf(str2);
Page | 108
}

Output:

Experiment No: 88 Date: 08/03/2019

Page | 109
AIM: Program to create a structure for student(with fields name,id_no,gender and age). Read the details of
student and display it.
Code:
#include<stdio.h>
struct student
{
char name[30],gender[30];
int id_no,age;
};
void main()
{
struct student s;
printf("enter student details:\n");
scanf("%s%d%s%d",&s.name,&s.id_no,&s.gender,&s.age);
printf("details of a student:\nstudent name:%s\nstudent id_no:%d\nstudent gender:%s\nstudent age:
%d",s.name,s.id_no,s.gender,s.age);
}

Output:

Experiment No: 89 Date: 08/03/2019

Page | 110
AIM: Program to create a structure for bank customer(with fields name, accno, and balance). Read the
details of n customers and display it.
Code:
#include<stdio.h>
struct customer
{
char name[30];
int Acc_no,balance;
};
void main()
{
int n,i;
struct customer s[30];
printf("enter number of customers: ");
scanf("%d",&n);
printf("enter the details of bank customer:\n");
for(i=1;i<=n;i++)
{
scanf("%s%d%d",&s[i].name,&s[i].Acc_no,&s[i].balance);
}
for(i=1;i<=n;i++)
{
printf("details of a customer %d:\n",i);
printf("customer name:%s\ncustomer Acc_no:%d\ncustomer balance:
%d\n\n",s[i].name,s[i].Acc_no,s[i].balance);
}
}

Output:

Page | 111
Experiment No: 90 Date: 08/03/2019
Page | 112
AIM: Program to create a structure called employee having members fields name, gender and salary. WAP
to that accepts the details of employees. Display all employees in sorted order according to their salary.
Code:
#include<stdio.h>
struct employee
{
char name[30], gender[56];
int salary;
} s[56], k;
void main()
{
int n,i,j;
printf("enter number of employees: ");
scanf("%d",&n);
printf("enter the details of employee:\n");
for(i=1;i<=n;i++)
{
scanf("%s%s%d",&s[i].name,&s[i].gender,&s[i].salary);
}
for(i=1;i<=n;i++){
for(j=i+1;j<=n;j++){
if(s[i].salary>s[j].salary){
k=s[i];
s[i]=s[j];
s[j]=k;
}
}
}
for(i=1;i<=n;i++)
{
printf("details of a employees %d:\n",i);
printf("employee name:%s\nemployee gender:%s\nemployee salary:
%d\n\n",s[i].name,s[i].gender,s[i].salary);
Page | 113
}
}

Output:

Experiment No: 91 Date: 11/03/2019

Page | 114
AIM: Program to create a structure for student (with members fields name, id_no , gender and age). Read
the details of student and Display it in sorted order by using their names.
Code:
#include<stdio.h>
#include<string.h>
struct stud
{
char name[50];
char gender[50];
int id,age;
}s[200];
int main()
{
struct stud t;
int i=0,j=0,n;
printf("How many student records you want to enter ?");
printf("\nEnter Limit : ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter Student-%d Details",i+1);
printf("\n--------------------------\n");
printf("Enter Name : ");
scanf("%s",s[i].name);
printf("Gender : ");
scanf("%s",s[i].gender);
printf("Id Number : ");
scanf("%d",&s[i].id);
printf("Age : ");
scanf("%d",&s[i].age);
}
printf("\n\tData before rearrangement");
printf("\n-----------------------------------");

Page | 115
printf("\nStudent Name\tGender\tId\tAge");
printf("\n-----------------------------------");
for(i=0;i<n;i++)
{
printf("\n%-10s\t%3s\t%d\t%d\n",s[i].name,s[i].gender,s[i].id,s[i].age);
}
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(strcmp(s[i].name,s[j].name)>0)
{
t=s[i];
s[i]=s[j];
s[j]=t;
}
}
}
printf("\n\tData after rearrangement");
printf("\n-----------------------------------");
printf("\nStudent Name\tGender\tId\tAge\n");
printf("-------------------------------------");
for(i=0;i<n;i++)
{
printf("\n%-10s\t%3s\t%d\t%d\n",s[i].name,s[i].gender,s[i].id,s[i].age);
}
return 0;
}

Output:
Page | 116
Experiment No: 92 Date: 11/03/2019
Page | 117
AIM: program to create a single linked list.
Code:
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
void main()
{
struct node
{
int num;
struct node *ptr;
};
typedef struct node NODE;
NODE *head, *first, *temp = 0;
int count = 0;
int choice = 1;
first = 0;
while (choice)
{
head = (NODE *)malloc(sizeof(NODE));
printf("Enter the data item\n");
scanf("%d", &head-> num);
if (first != 0)
{
temp->ptr = head;
temp = head;
}
else
{
first = temp = head;
}
fflush(stdin);
Page | 118
printf("Do you want to continue(Type 0 or 1)?\n");
scanf("%d", &choice);
}
temp->ptr = 0;
temp = first;
printf("\n status of the linked list is\n");
while (temp != 0)
{
printf("%d=>", temp->num);
count++;
temp = temp -> ptr;
}
printf("NULL\n");
printf("No. of nodes in the list = %d\n", count);
}
Output:

Experiment No: 93 Date: 11/03/2019

Page | 119
AIM: program to create a double linked list:
Code:
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node * prev;
struct node * next;
}*head, *last;
void createList(int n);
void displayListFromFirst();
void displayListFromEnd();
int main()
{
int n, choice;
head = NULL;
last = NULL;
printf("Enter the number of nodes you want to create: ");
scanf("%d", &n);
createList(n); // Create list of n nodes
printf("\nPress 1 to display list from First");
printf("\nPress 2 to display list from End : ");
scanf("%d", &choice);
if(choice==1)
{
displayListFromFirst();
}
else if(choice == 2)
{
displayListFromEnd();
}
return 0;
}
Page | 120
void createList(int n)
{
int i, data;
struct node *newNode;
if(n >= 1)
{
head = (struct node *)malloc(sizeof(struct node));
if(head != NULL)
{
printf("Enter data of 1 node: ");
scanf("%d", &data);
head->data = data;
head->prev = NULL;
head->next = NULL;
last = head;
for(i=2; i<=n; i++)
{
newNode = (struct node *)malloc(sizeof(struct node));
if(newNode != NULL)
{
printf("Enter data of %d node: ", i);
scanf("%d", &data);
newNode->data = data;
newNode->prev = last; // Link new node with the previous node
newNode->next = NULL;
last->next = newNode; // Link previous node with the new node
last = newNode; // Make new node as last/previous node
}
else
{
printf("Unable to allocate memory.");
break;
}
Page | 121
}
printf("\nDOUBLY LINKED LIST CREATED SUCCESSFULLY\n");
}
else
{
printf("Unable to allocate memory");
}
}
}
void displayListFromFirst()
{
struct node * temp;
int n = 1;

if(head == NULL)
{
printf("List is empty.");
}
else
{
temp = head;
printf("\n\nDATA IN THE LIST:\n");

while(temp != NULL)
{
printf("DATA of %d node = %d\n", n, temp->data);
n++;
temp = temp->next;
}
}
}
void displayListFromEnd()
{
Page | 122
struct node * temp;
int n = 0;
if(last == NULL)
{
printf("List is empty.");
}
else
{
temp = last;
printf("\n\nDATA IN THE LIST:\n");
while(temp != NULL)
{
printf("DATA of last-%d node = %d\n", n, temp->data);
n++;
temp = temp->prev;
}
}
}
Output:

Experiment No: 94 Date: 12/03/2019

AIM: Program to create a file and display the content within the file.
Page | 123
Code:
#include<stdio.h>
void main()
{
FILE *fp;
char ch;
fp=fopen("prc.c","r");
while(1)
{
ch =fgetc(fp);
if(ch==EOF)
{
break;
}
printf("%c",ch);
}
fclose(fp);
}
Output:

Experiment No: 95 Date: 12/03/2019

AIM: Write a C program which copies one file to another.

Page | 124
Code:
#include<stdio.h>
void main()
{
FILE *fs,*ft;
char ch;

fs=fopen("prc.c","r");
if(fs==NULL)
{
puts("Cannot open source file");
exit(1);
}
ft=fopen("pr1.c","w");
if(ft==NULL)
{
puts("cannot open target file");
fclose(fs);
exit(2);
}
while(1){
ch=fgetc(fs);
if(ch==EOF)
break;
else
fputc(ch,ft);
}
printf(“file copy is success”);
fclose(fs);
fclose(ft);
}

Output:

Page | 125
Experiment No: 96 Date: 12/03/2019

Page | 126
AIM: Write a C program to reverse the first n character in a file.
Code:
#include <stdio.h>
int main()
{
FILE *s,*t;
char c,temp[100],temp2[100];
int n,i;
printf("enter n value");
scanf("%d",&n);
s = fopen("file.txt", "r");
t = fopen("copy.txt", "w");
for(i=0;i<n;i++)
{
c=fgetc(s);
temp[i]=c;
}
temp[i]='\0';
for(i=0;i<n;i++)
{
temp2[i]=temp[n-1-i];
}
temp2[i]='\0';
for(i=0;i<n;i++)
{
fputc(temp2[i],t);
}
while(1){
c = fgetc(s);

if(c == EOF)
{
Page | 127
printf("Successfully copied");
break;
}
else
{
fputc(c,t);
}
}
fclose(s);
fclose(t);
return 0;
}
Output:

Experiment No: 97 Date: 13/03/2019

Page | 128
AIM: Program to merge two files.
Code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE *fp1 = fopen("file1.c", "r");
FILE *fp2 = fopen("file2.c", "r");
FILE *fp3 = fopen("file3.c", "w");
char c;
if (fp1 == NULL || fp2 == NULL || fp3 == NULL)
{
puts("Could not open files");
exit(0);
}
while ((c = fgetc(fp1)) != EOF)
fputc(c, fp3);
while ((c = fgetc(fp2)) != EOF)
fputc(c, fp3);
printf("Merged file1.c and file2.c into file3.c");
fclose(fp1);
fclose(fp2);
fclose(fp3);
return 0;
}

Output:

Page | 129
Experiment No: 98 Date: 13/03/2019

Page | 130
AIM: Program to read a number, if it is even number store in even file (even) else store in odd file (odd).
Code:
#include<stdio.h>
#include<conio.h>
void main()
{
FILE *f1,*f2,*f3;
int number,i,n;
printf("Enter n value");
scanf("%d",&n);
printf("Contents of DATA file\n\n");
f1 = fopen("DATA","w"); /* create a data file */
for(i=1;i<=n;i++)
{
scanf("%d",&number);
putw(number,f1);
}
fclose(f1);
f1 = fopen("DATA","r");
f2 = fopen("ODD","w");
f3 = fopen("EVEN","w");
while((number = getw(f1)) != EOF) /* Read from Data file */
{
if(number%2==0)
putw(number,f3);
else
putw(number,f2);
}
fclose(f1);
fclose(f2);
fclose(f3);
f2 = fopen("ODD","r");
Page | 131
f3 = fopen("EVEN","r");
printf("\n\nContents of ODD file \n\n");
while((number = getw(f2)) != EOF)
printf("%4d",number);
printf("\n\nContents of EVEN file \n\n");
while((number = getw(f3)) != EOF)
printf("%4d",number);
fclose(f2);
fclose(f3);
getch();
}

Output:

Experiment No: 99 Date: 13/03/2019


Page | 132
AIM: Program to count the number of lines, words and spaces in a file.
Code:
#include<stdio.h>
void main()
{
FILE *fp;
char ch;
int nol=0,nota=0,nob=0,noc=0;
fp=fopen("prc.c","r");
while(1)
{
ch=fgetc(fp);
if(ch==EOF)
break;
noc++;
if(ch==' ')
nob++;
if(ch=='\n')
nol++;
if(ch=='\t')
nota++;
}
fclose(fp);
printf("\n Number of characters=%d",noc);
printf("\n Number of blanks=%d",nob);
printf("\n Number of tabs=%d",nota);
printf("\n Number of lines=%d",nol);
}

Page | 133
Output:

Page | 134

You might also like