C Programs (1-13)
C Programs (1-13)
2.Write a C program to find the GCD and LCM of two integer numbers.
//program to find the GCD and LCM of given two numbers
#include<stdio.h>
void main()
{
int a,b,gcd,lcm,x,y;
clrscr();
printf("\nEnter the first positive integer : ");
scanf("%d",&a);
print("\nEnter the second positive integer : ");
scanf("%d",&b);
if((a==0)||(b==0))
{
printf("\nGCD and LCM is computed for positive integers");
}
else
{
x=a;
y=b;
while(x!=y)
{
if(x>y)
x=x-y;
else
y=y-x;
}
gcd=x;
printf("\nGCD of %d and %d is %d",a,b,gcd);
lcm=(a*b)/gcd;
printf("\nLCM of %d and %d is %d",a,b,lcm);
}
getch();
}
3.Write a C program that reverses given integer number and checks whether
the number is palindrome or not.
//program to check whether a given number is palindrome or not
#include<stdio.h>
void main()
{
int m,n,s=0,r;
clrscr();
printf("\nEnter the number : ");
scanf("%d",&n);
m=n;
while(n>0)
{
r=n%10;
n=n/10;
s=s*10+r;
}
if(s==m)
printf("\n %d is a palindrome number",m);
else
printf("\n %d is not a palindrome number",m);
getch();
}
4.Write a C program to find whether a given number is prime number or not.
//program to check whether a given number is prime or not
#include<stdio.h>
void main()
{
int i,n,m,flag=0;
clrscr();
printf("\n Enter a positive integer greater than 1 : ");
scanf("%d",&n);
if(n<=1)
{
printf("\nThe number should be greater than 1");
getch();
exit();
}
else
{
for(i=2;i<n;i++)
{
m=n%i;
if(m==0)
{
flag=1;
break;
}
}
if(flag==0)
printf("\n The number %d is a prime number",n);
else
printf("\n The number %d is not a prime number",n);
getch();
}
}
6.Write a C program to compute the sum of even numbers and the sum of odd
numbers using function.
//program to find the sum of odd and even numbers
#include<stdio.h>
void main()
{
int n,i,a[100];
void oddevensum(int a[],int);
clrscr();
printf("\nEnter the number count : ");
scanf("%d",&n);
printf("\nEnter the numbers\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("The entered elements\n");
for(i=0;i<n;i++)
{
printf("%d\t",a[i]);
}
if(n>0)
oddevensum(a,n);
else
printf("\nThe count should be greater than zero");
getch();
}
void oddevensum(int a[],int n)
{
int i,esum=0,osum=0;
for(i=0;i<n;i++)
{
if(a[i]%2==0)
esum=esum+a[i];
else
osum=osum+a[i];
}
printf("\n\nThe sum of even numbers is %d",esum);
printf("\n\nThe sum of odd numbers is %d",osum);
}
#include<stdio.h>
void main()
{
int a[10][10],b[10][10],c[10][10],d[10][10];
int i,j,r1,c1,r2,c2;
clrscr();
printf("\nEnter the number of rows in the matrix A : ");
scanf("%d",&r1);
printf("\nEnter the number of columns in the matrix A : ");
scanf("%d",&c1);
printf("\nEnter the number of rows in the matrix B : ");
scanf("%d",&r2);
printf("\nEnter the number of columns in the matrix B : ");
scanf("%d",&c2);
if((r1!=r2)||(c1!=c2))
{
printf(" \nOperation not possible - Matrices are not of the
same order");
getch();
exit(0);
}
else
{
printf("\nEnter the matrix A elements\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("\nEnter the matrix B elements\n");
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
{
scanf("%d",&b[i][j]);
}
}
//performing addition and subtraction
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
c[i][j]=a[i][j]+b[i][j];
d[i][j]=a[i][j]-b[i][j];
}
}
printf("\nMatrix A\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
printf("\t%d",a[i][j]);
}
printf("\n");
}
printf("\nMatrix B\n");
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
{
printf("\t%d",b[i][j]);
}
printf("\n");
}
printf("\nMatrix C=A+B\n");
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
{
printf("\t%d",c[i][j]);
}
printf("\n");
}
printf("\nMatrix D=A-B\n");
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
{
printf("\t%d",d[i][j]);
}
printf("\n");
}
}
getch();
}
if((str[i]=='a')||(str[i]=='e')||(str[i]=='i')||(str[i]=='o')||(str[i]=='u
'))
vcount++;
else
ccount++;
}
else
{
if(isdigit(str[i]))
dcount++;
else if(isspace(str[i]))
wcount++;
else
scount++;
}
}
printf("\nVowels - %d",vcount);
printf("\nConsonants - %d",ccount);
printf("\nDigits - %d",dcount);
printf("\nSpecial characters - %d",scount);
printf("\nWhite spces - %d",wcount);
getch();
}
11. Write a C program to accept different goods with the number, price and
date of purchase and display them using
structure.
#include<stdio.h>
struct date
{
int dd;
int mm;
int yy;
};
struct product
{
int number;
int price;
struct date dt;
};
void main()
{
struct product p[5];
int i;
clrscr();
printf("\nEnter the details of 3 products\n");
for(i=0;i<3;i++)
{
clrscr();
printf("\nPRODUCT %d DETAILS\n",i+1);
printf("\nEnter the product number -");
scanf("%d",&p[i].number);
printf("\nEnter the price of the product -");
scanf("%d",&p[i].price);
printf("\nEnter the date of purchase in DD MM YY format\n");
printf("\nEnter the day of purchase\n");
scanf("%d",&p[i].dt.dd);
printf("\nEnter the month of purchase\n");
scanf("%d",&p[i].dt.mm);
printf("\nEnter the year of purchase\n");
scanf("%d",&p[i].dt.yy);
}
clrscr();
printf("\nPRODUCT DETAILS\n");
printf("\nNUMBER COST DATE\n");
for(i=0;i<3;i++)
{
printf("\n%d\t%d\t%d-%d-%d\n",p[i].number,p[i].price,
p[i].dt.dd,p[i].dt.mm,p[i].dt.yy);
}
getch();
}
{
char s1[200],s2[100];
void copy(char *p1,char *p2);
clrscr();
printf("\nEnter the string\n");
gets(s1);
copy(s1,s2);
printf("\nThe original string is %s ",s1);
printf("\nThe copied string is %s ",s2);
getch();
}
void copy(char *p1,char *p2)
{
while(*p1!='\0')
{
*p2=*p1;
p1++;
p2++;
}
*p2='\0';
}
if(fin==NULL)
{
printf("\nFile cannot be opened");
getch();
exit(0);
}
fout=fopen("output.dat","w");
if(fout==NULL)
{
printf("\nFile cannot be opened\n");
getch();
exit(0);
}
fprintf(fout,"\nRoll.No Name\n\n");
while(fscanf(fin,"%d%s",&rollno,&name)!=EOF)
{
fprintf(fout,"%d\t%s\n",rollno,name);
}
fclose(fout);
}