0% found this document useful (0 votes)
63 views

A Program To Find The GCD/HCF of Two Numbers Entered Through The Keybouard

This document contains code for multiple C programs including: 1. A program to find the greatest common divisor (GCD) of two numbers entered by the user. 2. A program to calculate the Fibonacci sequence of the first N numbers. 3. A program to reverse the values of two variables entered by the user.
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 DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
63 views

A Program To Find The GCD/HCF of Two Numbers Entered Through The Keybouard

This document contains code for multiple C programs including: 1. A program to find the greatest common divisor (GCD) of two numbers entered by the user. 2. A program to calculate the Fibonacci sequence of the first N numbers. 3. A program to reverse the values of two variables entered by the user.
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 DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

A PROGRAM TO FIND THE GCD\HCF OF TWO NUMBERS ENTERED THROUGH

THE KEYBOUARD.
#include<stdio.h>/*code by siwalik*/
#include<conio.h>
main()
{
int m,n;
int gcd(int,int);
clrscr();
printf("enter m,n value:");
scanf("%d%d",&m,&n);
printf("the gcd is:%d",gcd(m,n));
getch();
}
int gcd(int m , int n)
{
if(m==0)
return n;
if(n==0)
return m;
return gcd(n, m%n);
}

A PROGRAM TO FIND THE FIBONACCI SEQUENCE OF FIRST N NUMBERS:


#include<stdio.h>/*code by siwalik*/
#include<conio.h>
void main()
{
int n;
printf("ENTER THE VALUE OF N");
scanf("%d",&n);
int i, fib[25];
fib[0] = 0;
fib[1] = 1;
for (i = 2; i < n; i++)
{
fib[i] = fib[i - 1] + fib[i - 2];
}
printf("The fibonacci series is as follows \n");
for (i = 0; i < n; i++) {
printf("%d \n", fib[i]);
}
getch();
}
A PROGRAM TO REVERSE THE VALUE OF TWO VARIABLES INPUT THROUGH
THE KEYBOARD
#include <stdio.h>/*code by siwalik*/
#include <conio.h>
void main()
{
int a,b,t;
clrscr();
printf("enter the value of a followed by b\n");
scanf("%d%d",&a,&b);
printf ("\n The value of a is %d, and value of b is %d.",a,b);
t=a;
a=b;
b=t;
printf ("\n reversed value of a is %d, and that of b is %d.",a,b);
getch();
}

A PROGRAM TO ACCEPT TWO STRING AND PERFORM FOLLOWING


OPERATIONS:
1. Count the number of characters in 1st string
2. Reverse the second string
3. Concatenate the two strings
4. Compare the two strings
5. Convert all lowercase letters to uppercase of the first string
#include<string.h>/*code by siwalik*/
#include<stdio.h>
#include<conio.h>
void main()
{
char string1[10],string2[10],string3[10],string4[10],string5[10];
int x,y;
clrscr();
printf("Enter the 2 strings\n");
scanf("\n %s %s",string1,string2);
strcpy(string3,string1);
strcpy(string4,string2);
strcpy(string5,string3);
x=strlen(string1);
y=strcmp(string1,string2);
printf("\nThe number of charecters in the 1st string %s is %d",string1,x);
printf("\nThe reverse order of the 2nd string %s is %s",string4,strrev(string2));
printf("\nThe concatinated result of the 2 strings %s and %s is
%s",string1,string4,strcat(string3,string4));
printf("\nThe differnce between the 2 strings %s and %s is %d",string1,string4,y);
printf("\nThe upper case of the 1st string %s is %s",string5,strupr(string1));
getch();
}
A MENU DRIVEN PROGRAM TO FIND WHETHER A NO. IS PRIME, ITS
FACTORIAL AND IF ODD\EVEN.
#include<stdio.h>/*code by siwalik*/
#include<conio.h>
void main()
{
int m;
clrscr();
printf ("Enter the value of menu between one to three");
printf ("\nPress 1 to find whether prime,2 for factorial,3 for odd or even");
scanf ("\n %d",&m);
switch (m)
{
case 1:
int num,z;
clrscr();
printf ("Enter a number");
scanf ("\n %d",& num);
z=2;
while (z<=num-1)
{
if (num%z==0)
{
printf ("The number %d not a prime number",num);
break;
}
z++;
}
if (z==num)
printf ("The number %d is not a prime number",num);
break;
case 2:
int rec(int);
int a,fact;
clrscr();
printf ("Enter any number");
scanf ("%d",&a);
fact=rec(a);
printf ("Factorial value of the number %d = %d",a,fact);
break;
case 3:
int b;
clrscr();
printf ("Enter a number");
scanf ("\n %d",&b);
if (b%2!=0)
{
printf ("\n The number %d is odd",b);
}
else
if (b%2==0)
{
printf ("\n The number %d is even",b);
}
break;
default:
printf ("\n EXIT");
}
getch();
}
rec(int x)
{
int f;
if (x==1)
return (1);
else
f=x*rec(x-1);
return (f);
}

A PROGRAM TO FIND THE RELATION BETWEEN THE STRINGS AND THEIR


MEMORY LOCATION:
#include<stdio.h>/*code by siwalik*/
#include<conio.h>
void main()
{
int array[5];
int *p,*q,*r,*s,*t;
clrscr();
printf("enter the 5 numbers");
scanf("%d%d%d%d%d",&p,&q,&r,&s,&t);
p=&array[0];
q=&array[1];
r=&array[2];
s=&array[3];
t=&array[4];
printf("The address of the 5 elements of the array -\n%u \n%u \n%u \n%u \n%u\n",p,q,r,s,t);
if(q-p==r-q==s-r==t-s)
printf("\nThe elements of the integer type array are in contigious memory location");
else
printf("\nThey elements of the integer type array are in no relation");
getch();
}
A PROGRAM TO CALCULATE PERMUTATION AND COMBINATION:
#include<stdio.h>/*code by siwalik*/
#include<conio.h>
void per(int,int);
void com(int,int);
int fact(int);
void main()
{       
clrscr();
int n,r;
int chk;
do
{
printf("\n Enter the value of n:");
scanf("%d",&n);
printf("\n Enter the value of r:");
scanf("%d",&r);
printf("\n Press 1 to calculate nPr:");
printf("\n Press 2 to calculate nCr:");
printf("\n Press 3 to EXIT:");
printf("\n Please enter your choice......");
scanf("%d",&chk);
printf("\n");
switch(chk)
{
case 1:
per(n,r);
break;
case 2:
com(n,r);
break;
case 3:
break;
default:
printf("\n You have enter a wrong choice!!!!!!!!\n");
break;
}
}
while(chk!=3);
}
void per(int n,int r)
{
int p;
p=fact(n)/fact(n-r);
printf("\n %dP%d is %d:",n,r,p);
getche();
}
void com(int n,int r)
{
int p;
p=fact(n)/(fact(n-r)*fact(r));
printf("\n %dC%d is %d:",n,r,p);
getche();
}
int fact(int n)
{
if(n==0)
return 1;
else
{
int act=1;
for(int i=1;i<=n;i++)
{
act=act*i;
}
return act;
}
}

A PROGRAM TO FIND NUMBER OF SUCCESIVE VOWELS IN A STRING:


#include<stdio.h>/*code by siwalik*/
#include<string.h>
#include<conio.h>
void main()
{
char str[80];
int count=0;
char *s=str;
clrscr();
printf("\nEnter the string");
gets(str);
while(*s)
{
if(*s=='a'||*s=='e'||*s=='i'||*s=='o'||*s=='u')
{
s++;
if(*s=='a'||*s=='e'||*s=='i'||*s=='o'||*s=='u')
count++;
}
s++;
}
printf("\nThe number of occurence of 2 successive vowel is %d",count);
getch();
}
A PROGRAM TO SIMULATE A SIMPLE CALCULATOR:
#include<stdio.h>/*code by siwalik*/
#include<conio.h>
void main()
{
int cal;
clrscr();
printf("Press 1 to add, 2 to substract, 3 to multiply and 4 to devide\n");
scanf("%d",&cal);
switch(cal)
{
case 1:
int a,b,c;
clrscr();
printf("Enter the numbers to be added\n");
scanf("%d %d",&a,&b);
c=a+b;
printf("Sum of %d and %d is %d",a,b,c);
getch();
break;
case 2:
int x,y,z;
clrscr();
printf("Enter the numbers to be substracted\n");
scanf("%d %d",&x,&y);
z=x-y;
printf("Difference of %d and %d is %d",x,y,z);
getch();
break;
case 3:
int m,n,o;
clrscr();
printf("Enter the numbers to be multiplied\n");
scanf("%d %d",&m,&n);
o=m*n;
printf("Product of %d and %d is %d",m,n,o);
getch();
break;
case 4:
int i,j;
float k;
clrscr();
printf("Enter the devidend and the devisor\n");
scanf("%d %d",&i,&j);
if(j==0)
{
printf("0 cannot b used devisor\n");
getch();
}
else
{
k=i/j;
printf("Quotient of %d and %d is %f",i,j,k);
}
getch();
break;
default:
printf("EXIT");      
}
getch();
}

A PROGRAM TO ACCEPT AND PRINT MARKS,NAME,ROLL OF STUDENTS USING


STRUCTURE:

#include<stdio.h>/*code by siwalik*/
#include<string.h>
#include<conio.h>
void main()
{
struct student
{
char name[20];
int roll;
int m1;
int m2;
int m3;
};
int i,j=0;
float avg;
struct student s[3];
for(i=0;i<=3;i++)
{
printf("Enter the name of student%d\n",j+1);
scanf("%s",(s[i].name));
printf("Enter the roll no. of %s\n",s[i].name);
scanf("\n%d",&s[i].roll);
printf("Enter the marks in 3 tests of %s\n",s[i].name);
scanf("%d%d%d",&s[i].m1,&s[i].m2,&s[i].m3);
avg=(s[i].m1+s[i].m2+s[i].m3)/3;
printf("\nName:%s \nRoll no:%d \nMarks in test 1:%d \nMarks in test 2:%d \nMarks in test 3:%d
\nAverage:%f\n",s[i].name,s[i].roll,s[i].m1,s[i].m2,s[i].m3,avg);
j++;
i++;
}
getch();
}
A PROGRAM TO CHECK PALINDROME OF A STRING:
#include<stdio.h>/*code by siwalik*/
#include<conio.h>
#include<string.h>
#define size 26
void main()
{
char strsrc[size];
char strtmp[size];
clrscr();
printf("\n Enter String:\t"); gets(strsrc);
strcpy(strtmp,strsrc);
strrev(strtmp);
if(strcmp(strsrc,strtmp)==0)
printf("\n Entered string \"%s\" is a palindrome",strsrc);
else
printf("\n Entered string \"%s\" is not a palindrome",strsrc);
getch();
}

A PROGRAM TO FIND THE PRODUCT OF INTEGER AND FLOAT VARIABLE


#include <stdio.h>/*code by siwalik*/
#include <conio.h>
void main()
{
float calpro(int ,float);
int i;
float p,product;
clrscr();
printf ("Enter two numbers one intger type and one float type");
scanf ("\n %d %f",&i,&p);
product=calpro(i,p);
printf ("The product is %f",product);
getch();
}
float calpro(int x,float y)
{
float d;
d=x*y;
return(d);
}
A PROGRAM TO FING THE ASCII VALUE OF EACH CHARECTER OF A STRING:
#include<stdio.h>/*code by siwalik*/
#include<conio.h>
#include<string.h>
void main()
{
int i=0;
char a[20];
char c;
int x;
clrscr();
printf("Enter your name\n");
gets(a);
x=strlen(a);
for(i=0;i<=x-1;i++)
{
c=a[i];
printf("\nThe ASCII equivalent of the character %c of the stirng %s is %d",a[i],a,c);
}
getch();
}

A PROGRAM TO FIND THE AVERAGE AND PERCENTAGE OF MARKS OF THREE


SUBJECTS:
#include <stdio.h>/*code by siwalik*/
#include <conio.h>
void main()
{
float calavg(int,int,int);
float calper(int,int,int);
int a,b,c;
float avg,per;
clrscr();
printf ("Enter the marks of three subjects");
scanf ("\n %d%d%d",&a,&b,&c);
avg=calavg(a,b,c);
per=calper(a,b,c);
printf ("\n The average marks of the student is %f",avg);
printf ("\n The percentage of the student is %f",per);
getch();
}
float calavg(int x, int y, int z)
{
float d;
d=(x+y+z)/3;
return(d);
}
float calper(int l, int m, int n)
{
float k;
k=((l+m+n)*100)/300;
return(k);
}
A C PROGRAM TO BUBBLE SORT 6 NUMBERS INPUT THROUGH THE
KEYBOARD:
#include<stdio.h>/*code by siwalik*/
#include<conio.h>
void main()
{
int a[6],i,j,t;
clrscr();
printf("Enter 6 nubers");
for(i=0;i<=5;i++)
scanf("%d",&a[i]);
for(i=0;i<=5;i++)
{
for(j=0;j<=5-i;j++)
{
if(a[j]>a[j+1])
{
t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
}
}
printf("\nSorted numbers are:\n");
for(i=0;i<=5;i++)
printf("\n%d",a[i]);
getch();
}

A STRING PROGRAM SHOWING UPPERCASE,LOWER CASE, STRING LENTH AND


CONCATINATION: 
#include <stdio.h>/*code by siwalik*/
#include <conio.h>
#include<string.h>
void main()
{
char name[30];
printf("ENTER A STRING\n");
scanf("%s",&name);
char cat[30];
printf("ENTER ANOTHER STRING FOR CONCATINATION\n");
scanf("%s",&cat);
;
char target[30];
int length;
length=strlen(name);
strcpy(target,name);
printf("STRING LENGTH OF %s IS %d",name,length);
printf("\nLOWER CASE OF %s IS %s",target,strlwr(name));
printf("\nUPPERCASE OF %s IS %s",target,strupr(name));
printf("\nUPPERCASE OF %s IS %s",cat,strupr(cat));
printf("\nCONCATINATION OF %s AND %s IS %s",target,cat,strcat(name,cat));
}
A PROGRAM TO FIND THE ASCII VALUE OF A CHARECTER AND VICE-VERSA.
#include<stdio.h>/*code by siwalik*/
#include<conio.h>
void siwalik();
void main()
{
char ch;
printf("enter charecter\n");
scanf("%c",&ch);
printf("ascii of %c is %d\n",ch,ch);
printf("PRESS ENTER\n");
getch();
siwalik();
}
void siwalik()
{
int x;
printf("enter ascii\n");
scanf("%d",&x);
printf("ascii of %d is %c",x,x);
getch();
}

You might also like