0% found this document useful (0 votes)
54 views6 pages

BASIC Programs in C: 1. Largest of Three

The document contains 9 C programs that demonstrate basic programming concepts: 1. A program to find the largest of three numbers using a ternary operator. 2. A Fibonacci sequence program that prints the nth term. 3. A factorial program that calculates the factorial of a given number. 4. Two programs to swap the values of two variables, one using a temporary variable and one without. 5. A program to check if a number is prime or not. 6. A program to find the maximum and minimum of an array. 7. A program to copy one string to another without using string functions. 8. A linear search program to search an element in an array.

Uploaded by

preghiera
Copyright
© © All Rights Reserved
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)
54 views6 pages

BASIC Programs in C: 1. Largest of Three

The document contains 9 C programs that demonstrate basic programming concepts: 1. A program to find the largest of three numbers using a ternary operator. 2. A Fibonacci sequence program that prints the nth term. 3. A factorial program that calculates the factorial of a given number. 4. Two programs to swap the values of two variables, one using a temporary variable and one without. 5. A program to check if a number is prime or not. 6. A program to find the maximum and minimum of an array. 7. A program to copy one string to another without using string functions. 8. A linear search program to search an element in an array.

Uploaded by

preghiera
Copyright
© © All Rights Reserved
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/ 6

BASIC Programs in C

1. LARGEST OF THREE

#include<stdio.h>
#include<conio.h>
main()
{
int a,b,c,res;
clrscr();
printf("Enter the value of A,B and C: ");
scanf("%d %d %d",&a,&b,&c);
printf("The Largest Number is %d",res=( a>b ? (a>c?a:c) : (b>c?b:c) ) );
//ternary operator – cond ? true stmt : false stmt;
getch();
}

2. FIBNO

#include<stdio.h>
#include<conio.h>
main()
{
int i,f1,f2,f3,n;
clrscr();
printf("Enter the value of n: ");
scanf("%d",&n);
f1=0; f2=1;
for(i=1;i<=n;i++)
{
printf(" %d",f1);
f3=f1+f2;
f1=f2;
f2=f3;

}
getch();
}

3. FACTORIAL

#include<stdio.h>
#include<conio.h>
void main()
{
int i,n,fact=1;
printf(“Enter the number:”);
scanf(“%d”,&n);
for(i=1;i<=n;i++)
{
fact=fact*i;
}
printf(“The factorial of %d is %d ”,n,fact );
getch();
}
4. SWAPPING

#include<stdio.h>
#include<conio.h>
void main()
{
int x,y;

printf("\nEnter the value of A and B: ");


scanf("%d %d",&x,&y);
int temp; //swapping using temporary variable
temp=x;
x=y;
y=temp;
printf("\nThe Value of A and B after Swapping: %d %d",x,y);

x=x+y; //swapping without using temporary variable


y=x-y;
x=x-y;
printf("\nThe Value of A and B after swapping: %d %d",x,y);
getch();
}
5. PRIME OR NOT

#include<stdio.h>
#include<conio.h>
void main()
{
int i,n;
clrscr();
printf("\nEnter the value of n : ");
scanf("%d",&n);
if(n==1)
{ printf("it is neither prime nor composite");
}
else
{
for(i=2 ; i<=(n/2) ;i++)
{
if ( (n%i)==0)
{
break;
}
}
if ( i > (n/2))
printf ("\nPrime");
else
printf ("\nNot Prime");
}
getch();
}

6. MAX MIN

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

void main()
{
int a[50],i,n,max,min;
printf("Enter the number of elements:");
scanf("%d",&n);
printf("Enter the elements:");
for(i=0;i<n;i++) // getting the input array
{
scanf("%d",&a[i]);
}
min=a[0]; // initializing min with first value in the array
max=a[0]; // initializing max with first value in the array
for(i=0;i<n;i++)
{
if( a[i]>max ) // updating the max value if necessary
{
max=a[i];
}
if( a[i]<min ) // updating the min value if necessary
{
min=a[i];
}
}
printf("\n The max number is %d",max);
printf("\n The min number is %d",min);
getch();
}

7. STRING COPY WITHOUT STRCPY

#include<stdio.h>
#include<conio.h>
void main()
{
int i;
char a[20],b[20];
printf("Enter the string:");
scanf("%s",a);
for(i=0; a[i] != '\0' ; i++ ) // loop will be executed until it finds the null character
{
b[i]=a[i]; // copying the characters one by one from a to b
}
b[i]='\0';
printf("The new string is %s",b);
getch();
}

8. LINEAR SEARCH

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

void main()
{
int a[50],i,n,j,x,flag=0;
clrscr();
printf ("\nEnter the value of n : ");
scanf ("%d",&n);
printf ("\nENTER THE ARRAY : ");
for (i=1;i<=n;i++)
scanf ("%d",&a[i]);
printf ("\nENTER THE NUMBER TO BE SEARCH : ");
scanf ("%d",&x);
for (i=1;i<=n;i++)
{
if (x==a[i])
{
printf ("\n THE NUMBER IS FOUND AT POSITION: %d",i);
flag =1;
break;

}
}
if (flag==0)
printf ("\nTHE NUMBER IS NOT FOUND ");
getch();
}

9. PALINDROME

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

void main()
{
int i,n;
clrscr();
char a[20];
printf("Enter the string:");
scanf("%s",a);
n=0;
for(i=0; a[i]!='\0' ; i++ ) // finding the length of the string
{
n++;
}
n=n-1; // this statement can be omitted if n is initialized to -1
for(i=0; i<=n/2 ; i++)
{
if(a[i] != a[n-i]) //comparing the characters 0 and n , 1 and n-1 , 2 and n-2,..
{
printf("\n %s is not a palindrome",a );
break;
}
}
if(i>n/2) // checking whether the for loop has executed fully
{
printf("\n %s is a palindrome",a );
}
}
15. ARMSTRONG NUMBER

#include <stdio.h>

int main()
{
int x[10],a,b,n,i=0,j=0,temp,result;
printf("Enter the number:");
scanf("%d",&a);
b=a; //creating a copy of the input
n=0;
while(b>0) //finding the digits in the given number and storing them in array x.
{
n++;
x[i]=b%10;
i++;
b=b/10;
}
result=0;//initializing result to 0
for(i=0;i<n;i++) // loop to process digits
{
temp=1;
for(j=0;j<n;j++) // loop to find each digit raised to the power of no. of digits
{ // (eg.153 – 1^3, 5^3, 3^3 ; 1634 – 1^4 , 6^4 , 3^4 , 4^4 )
temp=temp*x[i];
}
result=result+temp;
}
if(result==a)
{
printf("%d is an Armstrong number",a);
}
else
{
printf("%d is not an Armstrong number",a);
}

return 0;
}

11. STRING COUNT

#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <ctype.h>
void main(void)
{
int i,s=0,c=0,w=0,v=0,ch=0,d=0;
char name[500];
clrscr();
printf ("\nENTER THE STRING : ");
gets(name);
for (i=0;name[i]!='\0';i++)
{
ch++;
if ((name[i]=='a'||name[i]=='e'||name[i]=='i'||name[i]=='o'||name[i]=='u')|| (name[i]=='A'||name[i]=='E'||
name[i]=='I'||name[i]=='O'||name[i]=='U'))
v++;

else if ((name[i]>='a' && name[i]<='z') || (name[i] >='A' && name[i]<='Z'))


c++;
else if(name[i]>='0'&&name[i]<='9')
d++;
else if (name[i]=='.')
s++;
else if (name[i]==' ' )
w++;
}
printf ("\nVowels : %d",v);
printf ("\nCons : %d",c);
printf ("\nDigit : %d",d);
printf ("\ntotal : %d",ch);
printf ("\nSentence : %d",s);
printf ("\nword : %d",w);
getch();
}

You might also like