0% found this document useful (0 votes)
247 views33 pages

C Programming Exercises

The program reads a file and converts all lowercase characters to uppercase. It then copies the modified text to a new output file. It uses functions like fopen(), fgetc(), fputc(), toupper(), fclose() to perform the file operations and character conversions. The while loop reads each character from the input file, converts it to uppercase if lowercase, and writes it to the output file. Finally it closes both files.

Uploaded by

Madhav Aggarwal
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
247 views33 pages

C Programming Exercises

The program reads a file and converts all lowercase characters to uppercase. It then copies the modified text to a new output file. It uses functions like fopen(), fgetc(), fputc(), toupper(), fclose() to perform the file operations and character conversions. The while loop reads each character from the input file, converts it to uppercase if lowercase, and writes it to the output file. Finally it closes both files.

Uploaded by

Madhav Aggarwal
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 33

Program 11

Write a Program to find number of vowels in


the entered string
#include<stdio.h>
#include<conio.h>
int count_vowels(char []);
int check_vowel(char);
void main()
{
char array[100];
int c;
printf("Enter a string\n");
gets(array);
c = count_vowels(array);
printf("Number of vowels: %d\n", c);
getch();
}
int count_vowels(char a[])
{
int count = 0, c = 0, flag;
char d;
do
{d = a[c];
flag = check_vowel(d);
if ( flag == 1 )
count++;

c++;
}while( d != '\0' );
return count;
}
int check_vowel(char a)
{
if(a>='A'&&a<='Z')
a=a+'a'-'A';

/* Converting to lower case */

if ( a == 'a' || a == 'e' || a == 'i' || a == 'o' || a == 'u')


return 1;
return 0;

OUTPUT -11

Program 12

Write a Program to check weather the


entered string is palindrome or not using
pointers
#include<string.h>
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
char str[30],*ptr1,*ptr2;
int a=0;
printf("Enter the string: ");
scanf("%s",str);
ptr1=str;
ptr2=str;
for(int len=0;*ptr1!=NULL;ptr1++,len++);
--ptr1;

for(int i=0;i<(len/2);i++,--ptr1,++ptr2)
{
if(*ptr1!=*ptr2)
{
printf("It is not palindrome!!!");
++a;
break;

}
}

if(a==0)
printf("Hurry..It is palindrome");

getch();
}

OUTPUT-12

Program 13

Write a Program to convert uppercase to


lowercase and vice versa

#include<stdio.h>
#include<conio.h>
#include<ctype.h>
void main()
{
char ch;
int i=0;
clrscr();
printf("\n\nEnter a character (uppercase or lowercase):\t");
scanf("%c",&ch);
if(ch>=65&&ch<=90)
printf("\nYou entered an UPPER CASE character: %c
\nEquivalent in LOWER CASE: %c\t",ch,tolower(ch));
if(ch>=97&&ch<=122)
printf("\nYou entered a LOWER CASE character: %c
\nEquivalent in UPPER CASE:%c\t",ch,toupper(ch));
getch();
}

OUTPUT-13

PROGRAM- 14

Write a program to covert decimal number to


binary number and vice versa.
#include<stdio.h>
#include<conio.h>
void main()
{
int num,numb,rem,i=1,j=0;
clrscr();
printf("Enter a decimal integer:-");
scanf("%d",&num);
while(num>0)
{

rem=num%2;
j=j+rem*i;
num=num/2;
i=i*10;

}
printf("\nBinary Equivalent= %d\n",j);
j=0;
i=1;
printf("\nEnter a binary number(1s and 0s):-");
scanf("%ld",&numb);
while(numb>0)
{

rem=numb%10;
j=j+rem*i;
numb=numb/10;
i=i*2;

}
printf("\nDecimal equlvalent= %ld",j);
getch();
}

OUTPUT-14

PROGRAM-15
WAP to sort an array using bubble sort

#include<stdio.h>
#include<conio.h>

void bubble(int [],int);


void main()
{
clrscr();
int arr[20],num;
printf("Enter the no of elements of array: ");
scanf("%d",&num);
printf("Enter the array: ");
for(int i=0;i<num;i++)
scanf("%d",&arr[i]);
bubble(arr,num);
getch();
}
void bubble(int arr[],int num)
{
int temp;
for(int i=0;i<num;i++)
{
for(int j=0,k=0;j<(num-1);j++,k++)
{
if(arr[k+1]<arr[j])
{
temp=arr[j];
arr[j]=arr[k+1];

arr[k+1]=temp;
}
}
}
printf("\nThe sorted array in acending order is: ");
for(int h=0;h<num;h++)
printf(" %d ",arr[h]);
}

OUTPUT-15

Program 16
Write a Program to print the following:

1
1
1
1
1

#include<stdio.h>
#include<conio.h>
void main()
{
int i,j;
clrscr();
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
printf(" %d ",j);
}
printf("\n");
}
getch();
}

2
23
234
2345

OUTPUT 16

Program 17
Write a Program for the addition of two 3*3
matrices

#include<stdio.h>
#include<conio.h>
void main()
{
int A[3][3],B[3][3],C[3][3],i,j;
clrscr();
printf("\nEnter MATRIX A:\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
scanf("%d",&A[i][j]);
}
printf("\nMATRIX A: ");
for(i=0;i<3;i++)
{
printf("\n");
for(j=0;j<3;j++)
printf(" %d ",A[i][j]);
}
printf("\nEnter MATRIX B:\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)

scanf("%d",&B[i][j]);
}
printf("\nMATRIX B: ");
for(i=0;i<3;i++)
{
printf("\n");
for(j=0;j<3;j++)
printf(" %d ",B[i][j]);
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
C[i][j]=A[i][j]+B[i][j];
}
printf("\nSum of MATRICES is:\n");
for(i=0;i<3;i++)
{
printf("\n");
for(j=0;j<3;j++)
printf(" %d ",C[i][j]);
}
getch();
}

OUTPUT-17

Program 18
Write a Program to find the multiplication of
two 3*3 matrices

#include<stdio.h>
#include<conio.h>
void main()
{
int A[3][3],B[3][3],C[3][3],i,j,k;
clrscr();
printf("\nEnter MATRIX A:\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
scanf("%d",&A[i][j]);
}
printf("\nMATRIX A: ");
for(i=0;i<3;i++)
{
printf("\n");
for(j=0;j<3;j++)
printf(" %d ",A[i][j]);
}
printf("\nEnter MATRIX B:\n");
for(i=0;i<3;i++)

{
for(j=0;j<3;j++)
scanf("%d",&B[i][j]);
}
printf("\nMATRIX B: ");
for(i=0;i<3;i++)
{
printf("\n");
for(j=0;j<3;j++)
printf(" %d ",B[i][j]);
}
printf("\nProduct of MATRICES is:\n");
for(i=0;i<3;i++)
{
printf("\n");
for(j=0;j<3;j++)
{
C[i][j]=0;
for(k=0;k<3;k++)
C[i][j]=C[i][j]+A[i][k]*B[k][j];
printf(" %d ",C[i][j]);
}
}
getch();
}

OUTPUT- 18

Program 19

Write a Program to search for a number from


an array of numbers
#include<stdio.h>
#include<conio.h>
void main()
{
int A[100],n,s,i,flag=0;
clrscr();
printf("\nHow many elements? (less than 100):\t");
scanf("%d",&n);
printf("\nEnter elements\n");
for(i=0;i<n;i++)
scanf("%d",&A[i]);
printf("\nEnter a number to search for:\t");
scanf("%d",&s);
for(i=0;i<n;i++)
{
if(s==A[i])
{
printf("\nElement found at position %d",i+1);
flag=1;
break;

}
}
if(flag==0)
printf("\nElement not found.\n");
getch();}

OUTPUT- 19

PROGRAM - 20

WAP to generate employee details using


structures.

#include<stdio.h>
#include<conio.h>
struct add
{
int h_no,pin;
char city[20],block[5];
};
struct emp
{
float sal,age,code;
add a1;
char name[50];
};
void main()
{
int i,j;
emp e[20];
clrscr();
printf("\nEnter number of employees:");
scanf("%d",&j);
for(i=0;i<j;i++)
{
printf("\nEnter %d Employee detail",i+1);

printf("\nEnter Employee name:");


scanf("%s",e[i].name);
printf("Enter Employee code:");
scanf("%d",&e[i].code);
printf("Enter age of Employee:");
scanf("%d",&e[i].age);
printf("Enter Employee address\nEnter house number-");
scanf("%d",&e[i].a1.h_no);
printf("Enter block-");
scanf("%s",e[i].a1.block);
printf("Enter city-");
scanf("%s",e[i].a1.city);
printf("Enter pincode-");
scanf("%d",&e[i].a1.pin);
printf("Enter Employee salary:");
scanf("%d",&e[i].sal);
}
getch();
}

OUTPUT- 20

PROGRAM-21

WAP TO SORT AN ARRAY USING SELECTION


SORT
#include<stdio.h>
#include<conio.h>
void selection(int [],int);
int count=0,temp;
void main()
{
clrscr();
int arr[20],num;
printf("Enter the no of elements of array: ");
scanf("%d",&num);
printf("Enter the array: ");
for(int i=0;i<num;i++)
scanf("%d",&arr[i]);
selection(arr,num);
getch();
}
void selection(int arr [],int num)
{
for(int i=0;i<num;i++)
{
for(int j=i;j<num;j++)
{
for(int k=i,count=0;k<num;k++)
{
if(arr[j]<arr[k])

count++;
}
if(count==0)
{
temp=arr[j];
arr[j]=arr[i];
arr[i]=temp;
break;
}
}

}
for(int h=0;h<num;h++)
printf(" %d ",arr[h]);
}

OUTPUT-21

PROGRAM-22

WAP TO PASS AND RETURN A POINTER TO


FUNCTION
#include<stdio.h>
#include<conio.h>
int *add(int *,int *);
void main()
{
clrscr();
int a,b,*c,*d,*e;
printf("Enter the two nos to be added: ");
scanf("%d%d",&a,&b);
d=&a;
e=&b;
c=add(d,e);
printf("The sum of two nos is: %d ",*c);
getch();
}
int *add(int *d,int *e)
{
int a,*b;
a=*d+*e;
b=&a;
return b;
}

OUTPUT-22

PROGRAM -23

WAP TO PASS ARRAY TO FUNCTION CALL AS


POINTER TO CALCULATE SUM OF ELEMENTS
OF ARRAY
#include<stdio.h>
#include<conio.h>
void array(int *,int );
void main()
{
clrscr();
int arr[30],num,*ptr;
printf("Enter the no of elements in array: ");
scanf("%d",&num);
printf("\nEnter the elements of array: ");
for(int i=0;i<num;i++)
scanf("%d",&arr[i]);
ptr=arr;
array(ptr,num);
getch();
}
void array(int *ptr,int num)
{
int sum=0;
for(int i=0;i<num;i++)
sum+=*ptr;
printf("Sum of elements of array is: %d",sum);}

OUTPUT-23

PROGRAM -24

WAP TO READ A FILE AFTER CONVERTING


ALL LOWER CASE CHARACTER TO UPPER
CASE AND THEN COPY TO OTHE FILE
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
#include<process.h>
void main()
{
clrscr();
FILE *fp,*ft;
char ch;
fp=fopen("f","r");
if(fp==NULL)
{
puts("cannot open file");
exit(0);
}
ft=fopen("z","w");
if(ft==NULL)
{
puts("cannot open file");
exit(1);
}
while(1)

{
ch=fgetc(fp);
if(islower(ch)==1)
toupper(ch);
fputc(ch,fp);
}
fclose(fp);
fclose(ft);
getch();
}

PROGRAM-25

WAPTO FIND LENGTH OF STRING WITHOUT


USING STRING FUNC
#include<stdio.h>
#include<conio.h>
void slen(char []);
void main()
{
clrscr();
char str[30];
printf("Enter the string");
scanf("%d",str);
slen(str);
getch();
}
void slen(char str[])
{
for(int i=0;str[i]!=NULL;i++);
printf("\nLength of string is: %d",i);
}

OUTPUT 25

You might also like