0% found this document useful (0 votes)
81 views9 pages

C Programming Basic Algorithms Guide

This document provides code snippets for various technical programming problems and their solutions in C language. The problems covered include: 1. Swapping two numbers without a third variable. 2. Checking if a year is a leap year. 3. Finding the greatest among three numbers. It then continues to provide code solutions for several other common programming problems such as checking prime numbers, calculating sum of digits, palindrome checks, Armstrong numbers, factorials, Fibonacci series, perfect numbers, GCD, LCM and arithmetic operations with menus.

Uploaded by

Rahul Singh
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)
81 views9 pages

C Programming Basic Algorithms Guide

This document provides code snippets for various technical programming problems and their solutions in C language. The problems covered include: 1. Swapping two numbers without a third variable. 2. Checking if a year is a leap year. 3. Finding the greatest among three numbers. It then continues to provide code solutions for several other common programming problems such as checking prime numbers, calculating sum of digits, palindrome checks, Armstrong numbers, factorials, Fibonacci series, perfect numbers, GCD, LCM and arithmetic operations with menus.

Uploaded by

Rahul Singh
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

CAREER ADVISORY & AUGMENTATION SERVICES (CAAS)

Technical (C)
1. Swapping Two Number without using third Variable. printf("%d is Greater",c);
#include<stdio.h> }
void main() 4. Largest among Three Numbers.(using Conditional
{ Operator)
int a,b; #include<stdio.h>
printf("\n Enter value for A="); void main()
scanf("%d",&a); {
printf("\n Enter value for B="); int a,b,c,max;
scanf("%d",&b); printf("Enter Three Number.\n");
a=b-a; scanf("%d %d %d",&a,&b,&c);
b=b-a; max=(a>b && a>c?a:b>c?b:c);
a=a+b; printf("Large number= %d",max);
printf("New values ...."); }
printf("A=%d\n",a); 5. Check whether a number is Prime or Not.
printf("B=%d\n",b); #include<stdio.h>
} void main()
2. Check whether a year is Leap or not. {
#include<stdio.h> int num,counter,countf=0;
void main() printf("Enter a Number.\n");
{ scanf("%d",&num);
int year; counter=1;
printf("Enter Year."); while(counter<=num)
scanf("%d",&year); {
if(year%4==0 && year%100!=0 || year%400==0) if(num%counter==0)
printf("Leap year.\n"); countf=countf+1;
else counter++;
printf("Not a Leap Year.\n"); }
} if(countf==2)
3. Greatest among 3 Numbers. printf("Prime.\n");
#include<stdio.h> else
void main() printf("Not Prime.");
{ }
int a,b,c; 6. Sum of Digits of a Number.
printf("Enter Three Numbers.\n"); #include<stdio.h>
scanf("%d %d %d",&a,&b,&c); void main()
if(a>b && a>c) {
printf("%d is Greater",a); int num,ld,sum=0;
else printf("Enter a Number.\n");
if(b>c && b>a) scanf("%d",&num);
printf("%d is Greater",b); while(num>0)
else {
ld=num%10;
Page 1
Technical Assignment.
sum=sum+ld; void main()
num=num/10; {
} int num;
printf("Sum of Digits=%d",sum); long int fact=1;
} printf("Enter a Number.\n");
7. Check whether a number is Palindrome or not. scanf("%d",&num);
#include<stdio.h> while(num>1)
void main() {
{ fact=fact*num;
int num,ld,rev=0,num2; num--;
printf("Enter A Number.\n"); }
scanf("%d",&num); printf("Factorial=%ld",fact);
num2=num; }
while(num>0) 10. Print Fibonacci series upto N Terms.
{ #include<stdio.h>
ld=num%10; void main()
rev=rev*10+ld; {
num=num/10; int fnum,snum,tnum,n,counter=1;
} printf("Enter the number of Terms.");
if(rev==num2) scanf("%d",&n);
printf("Palidrome.\n"); fnum=0;snum=1;tnum=0;
else while(counter<=n)
printf("Not Palidrome.\n"); {
} printf("%d \t",tnum);
8. Check whether a number is Armstrong number or not. fnum=snum;
#include<stdio.h> snum=tnum;
void main() tnum=fnum+snum;
{ counter++;
int num,ld,sum=0,num2; }
printf("Enter A Number.\n"); }
scanf("%d",&num); 11. Check whether a Number is perfect or not. (Sum of the
num2=num; divisors (Excluding the number itself) is Equivalent to
while(num>0) that number.)
{ #include<stdio.h>
ld=num%10; void main()
sum=sum+ld*ld*ld; {
num=num/10; int num,counter,fsum=0;
} printf("Enter a Number.\n");
if(sum==num2) scanf("%d",&num);
printf("Armstrong.\n"); counter=1;
else while(counter<num)
printf("Not Armstrong.\n"); {
} if(num%counter==0)
9. Factorial of a number. fsum+=counter;
#include<stdio.h> counter++;

Page 2
Technical Assignment.
} printf("Enter Number.\n");
if(fsum==num) scanf("%d",&nterm);
printf("Perfect Number.\n"); do
else {
printf("Not a Perfect Number."); y=x;
} rev=0;
12. GCF of two numbers. do
#include<stdio.h> {
void main() ld=y%10;
{ rev=rev*10+ld;
int n1,n2,x,y; y=y/10;
printf("\nEnter two numbers:"); } while(y!=0);
scanf("%d %d",&n1,&n2); if(x==rev)
x=n1,y=n2; printf("%d\t",x);
while(n1!=n2) x++;
{ }while(x<=nterm);
if(n1>n2) }
n1=n1-n2; 15. Arithmetic operation between 2 number.( Menu
else driven using switch case )
n2=n2-n1; #include <stdio.h>
} #include<stdlib.h>
printf("G.C.F=%d",n1); void main ()
} {
13. LCM of two numbers. int num1, num2, result;
#include<stdio.h> char choice;
void main() printf("1.Addition.\n");
{ printf("2.Subtraction.\n");
int n1,n2,x,y; printf("3.Multiplication.\n");
printf("\nEnter two numbers:"); printf("4.Division.\n");
scanf("%d %d",&n1,&n2); printf ("\nEnter two numbers");
x=n1,y=n2; scanf ("%d %d", &num1, &num2);
while(n1!=n2) printf ("Enter Your Choice.\n");
{ fflush(stdin);
if(n1>n2) scanf ("%c", &choice);
n1=n1-n2; switch (choice)
else {
n2=n2-n1; case '1':
} result = num1 + num2;
printf("L.C.M=%d",x*y/n1); break;
} case '2':
14. Print all the Palindrome number from 1 to N. result = num1 - num2;
#include<stdio.h> break;
void main() case '3':
{ result = num1 * num2;
int nterm,ld,rev,x=1,y; break;
Page 3
Technical Assignment.
case '4': }
result = num1 / num2; 18. Find out the maximum and minimum number from an
break; array.
default: #include<stdio.h>
printf ("\n unknown operator"); void main()
exit (0); {
} int num[10],i;
printf ("%d", result); int min,max;
} printf("Enter 10 no :\n");
16. Sum of Elements of an array (1D). for(i=0;i<10;i++)
#include<stdio.h> scanf("%d",&num[i]);
void main() min=max=num[0];
{ for(i=0;i<10;i++)
int n[10],i,total=0; {
printf("Enter 10 no :\n"); if(num[i]<min)
for(i=0;i<10;i++) min=num[i];
scanf("%d",&n[i]); if(num[i]>max)
for(i=0;i<10;i++) max=num[i];
{ }
total=total+n[i]; printf("Minimum Numbers=%d\n",min);
} printf("Maximum Number=%d\n",max);
printf("Sum Of 10 Elements In An Array = %d\n",total); }
} 19. Remove duplicate elements from an array.
17. Reverse of an Array. #include<stdio.h>
#include<stdio.h> void main()
void main() {
{ int arr[20], i, j, k, size;
int arr[20], i, j, num, temp; printf("\nEnter array size : ");
printf("\nEnter no of elements : "); scanf("%d", &size);
scanf("%d", &num); printf("\nEnter Numbers : ");
for (i = 0; i < num; i++) for (i = 0; i < size; i++)
scanf("%d", &arr[i]); scanf("%d", &arr[i]);
j = i - 1; printf("\nArray with Unique list : ");
i = 0; for (i = 0; i < size; i++)
while (i < j) {
{ for (j = i + 1; j < size;)
temp = arr[i]; {
arr[i] = arr[j]; if (arr[j] == arr[i])
arr[j] = temp; {
i++; for (k = j; k < size; k++)
j--; {
} arr[k] = arr[k + 1];
printf("Result after reversal\n : "); }
for (i = 0; i < num; i++) size--;
printf("%d \t", arr[i]); }
Page 4
Technical Assignment.
else key = arr[j];
j++; i = j-1;
} while (i >= 0 && arr[i] > key)
} {
for (i = 0; i < size; i++) arr[i+1] = arr[i];
{ i = i-1;
printf("%d ", arr[i]); }
} arr[i+1] = key;
} }
20. Bubble sort. printf("In ascending order: ");
#include<stdio.h> for(i=0;i<n;++i)
void main() printf("%d ",arr[i]);
{ }
int arr[20],i,n,pass,temp; 22. Selection Sort.
printf("Enter the number of elements to be sorted: "); #include <stdio.h>
scanf("%d",&n); void main()
for(i=0;i<n;++i) {
scanf("%d",&arr[i]); int data[20],i,n,steps,temp;
for(pass=0;pass<n-1;pass++) printf("Enter the number of elements to be sorted: ");
{ scanf("%d",&n);
for(i=0;i<n-pass-1;i++) for(i=0;i<n;++i)
{ scanf("%d",&data[i]);
if(arr[i]>arr[i+1]) for(steps=0;steps<n;++steps)
{ {
temp=arr[i]; for(i=steps+1;i<n;++i)
arr[i]=arr[i+1]; {
arr[i+1]=temp; if(data[steps]>data[i])
} {
} temp=data[steps];
} data[steps]=data[i];
printf("In ascending order: "); data[i]=temp;
for(i=0;i<n;++i) }
printf("%d ",arr[i]); }
} }
21. Insertion sort. printf("In ascending order: ");
#include<stdio.h> for(i=0;i<n;++i)
void main() printf("%d ",data[i]);
{ }
int arr[20],i,j,n,key; 23. Binary Search.
printf("Enter the number of elements to be sorted: "); #include<stdio.h>
scanf("%d",&n); void main()
for(i=0;i<n;++i) {
scanf("%d",&arr[i]); int a[10],i,n,num,c=0,lb,ub,mid;
for (j = 1; j < n; j++) printf("Enter the size of an array: ");
{ scanf("%d",&n);
Page 5
Technical Assignment.
printf("Enter the elements in ascending order: "); }
for(i=0;i<n;i++) return(fa);
scanf("%d",&a[i]); }
printf("Enter the number to be search: "); 25. Passing Array to Function (Sum of elements of an
scanf("%d",&num); array).
l=0,u=n-1; #include<stdio.h>
while(l<=u) int arrsum(int arr[],int n);
{ void main()
mid=(l+u)/2; {
if(num==a[mid]) int num[5],i;
{ printf("Enter Elements into an Array.\n");
c=1; for(i=0;i<5;i++)
break; scanf("%d",&num[i]);
} printf("Array sum=%d",arrsum(num,5));
else }
if(num<a[mid]) int arrsum (int arr[],int n)
u=mid-1; {
else int sum=0,i;
l=mid+1; for(i=0;i<n;i++)
} sum=sum+arr[i];
if(c==0) return (sum);
printf("The number is not found."); }
else 26. Bubble sort using Function.
printf("The number is found."); #include<stdio.h>
} void bubblesort(int a[],int s);
void main()
24. Factorial of a Number using Function. {
#include<stdio.h> int arr[20],i,n;
long int factorial(int); printf("Enter the number of elements to be sorted: ");
void main() scanf("%d",&n);
{ for(i=0;i<n;++i)
int n; scanf("%d",&arr[i]);
long int fact; bubblesort(arr,n);
printf("\nEnter a Number:\n"); printf("In ascending order: ");
scanf("%d",&n); for(i=0;i<n;++i)
fact=factorial(n); printf("%d ",arr[i]);
printf("Factorial Of a Number:%ld",fact); }
} void bubblesort(int a[],int n)
long int factorial(int num) {
{ int pass,i,temp;
int a; for(pass=0;pass<n-1;pass++)
long int fa=1; {
for(a=1;a<=num;a++) for(i=0;i<n-pass-1;i++)
{ { if(a[i]>a[i+1])
fa=fa*a; {
Page 6
Technical Assignment.
temp=a[i]; printf("String 1-%s\n",str1);
a[i]=a[i+1]; printf("String 2-%s\n",str2);
a[i+1]=temp; scopy(str1,str2);
} printf("After Copy.\n\n");
} printf("String 1-%s\n",str1);
} printf("String 2-%s\n",str2);
} }
27. String copy program without using Library function. // Array Version.//
#include<stdio.h> void scopy(char str1[],char str2[])
#include<string.h> {
void main() int i=0;
{ while(str2[i]!='\0')
char str1[30],str2[30]; {
int i; str1[i]=str2[i];
printf("Enter First String:\n"); i++;
gets(str1); }
printf("Enter Second String:\n"); str1[i]='\0';
gets(str2); }
printf("Before copy:\n"); //Pointer Version//
printf("String 1-%s\n",str1); void scopy(char *s1, char *s2)
printf("String 2-%s\n",str2); {
i=0; while(*s2!='\0')
while(str2[i]!='\0') {
{ *s1=*s2;
str1[i]=str2[i]; s1++;
i++; s2++;
} }
str1[i]='\0'; *s1 = '\0';
printf("After Copy.\n\n"); }
printf("String 1-%s\n",str1); 29. String Reverse
printf("String 2-%s\n",str2); #include<stdio.h>
} #include<string.h>
28. String Copy using user-defined function(Array void main()
Version). {
#include<stdio.h> char str[50];
#include<string.h> char rev[50];
void scopy(char str1[],char str2[]); int i=0,j=0;
void main() printf("Enter any string : ");
{ gets(str);
char str1[30],str2[30]; while(str[i]!='\0')
printf("Enter First String:\n"); {
gets(str1); i++;
printf("Enter Second String:\n"); }
gets(str2); i=i-1;
printf("Before copy:\n"); while(i>=0)
Page 7
Technical Assignment.
{ puts(str1);
rev[j] = str[i]; puts(str2);
j++; while(str1[i]!='\0')
i--; {
} i++;
rev[j]='\0'; }
printf("Reverse of string is : %s",rev); while(str2[j]!='\0')
} {
30. Check a string is palindrome or not without using str1[i++]=str2[j++];
Library function. }
#include<stdio.h> str1[i]='\0';
#include<string.h> printf("After concatenation the string Is:\n");
void main () puts(str1);
{ }
char a[10]; 32. Factorial of a number using recursion.
int i,j,f=0; #include<stdio.h>
printf("Enter String.\n"); #include<conio.h>
gets(a); long int fact(int num);
for (i=0;a[i]!='\0';i++) void main()
{ } {
i--; int n;
for (j=0;a[j]!='\0';j++) long int f;
{ if(a[i]!=a[j]) printf("Enter Number.\n");
{ scanf("%d",&n);
f=1; f=fact(n);
break; printf("Factorial of %d =%ld",n,f);
} }
i--; long int fact(int n)
} {
if (f==0) if(n==0)
printf("string is palindrome"); return (1);
else else
printf("string is not palindrome"); return (n*fact(n-1));
} }
31. Concatenate Two string without using library function. 33. Print Fibonacci Series using Recursion.
#include<stdio.h> #include<stdio.h>
void main() #include<conio.h>
{ int fib(int);
int i=0,j=0; void main()
char str1[20],str2[10]; {
puts("Enter first string:\n"); int n,i;
gets(str1); printf("Enter No. of Terms.\n");
puts("Enter second string:\n"); scanf("%d",&n);
gets(str2); for(i=0;i<n;i++)
printf("Before concatenation the strings are:\n"); printf("%d \t",fib(i));
Page 8
Technical Assignment.
} int sum(int n)
int fib(int n) {
{ if(n==1)
if(n==0||n==1) return 1;
return(1); else
else return n+sum(n-1);
return(fib(n-1)+fib(n-2)); }
}
34. GCF using Recursion.
#include<stdio.h>
int gcf(int n1,int n2);
int main()
{
int n1,n2;
printf("\nEnter two numbers:");
scanf("%d %d",&n1,&n2);
printf("G.C.F=%d",gcf(n1,n2));
return 0;
}
int gcf(int n1, int n2)
{
if(n1==n2)
return(n1);
else
{
if(n1>n2)
n1=n1-n2;
else
n2=n2-n1;
return gcf(n1,n2);
}
}

35. Sum of first n natural numbers using recursion.


#include <stdio.h>
int sum(int n);
void main()
{
int num,add;
printf("Enter End Term:\n");
scanf("%d",&num);
add=sum(num);
printf("sum=%d",add);
}

Page 9

You might also like