/*C Program to Display "Hello, World!“ */ #include <stdio.h> int main() { // printf() displays the string inside quotation printf("Hello, World!"); return 0; }
n is for vertical tab . It takes the cursor to next line. t is for horizontal tab. #include<stdio.h> int main() { printf("how are youn I m fine. t ThankU"); }
/*C Program to Print an Integer (Entered by the User)*/ #include <stdio.h> int main() { int num; printf("Enter an integer: "); // reads and stores input scanf("%d", &num); // displays output printf("You entered: %d", num); return 0; }
/*Program to add two integers*/ #include <stdio.h> int main() { int num1, num2, sum; printf("Enter two integers: "); scanf("%d %d", &num1, &num2); // calculating sum sum = num1 + num2; printf("%d + %d = %d", num1, num2, sum); return 0; }
/*Program to Print ASCII Value*/ include <stdio.h> int main() { char c; printf("Enter a character: "); scanf("%c", &c); // %d displays the integer value of a character // %c displays the actual character printf("ASCII value of %c = %d", c, c); return 0; }
Example 2 • Write an algorithm and draw a flowchart to convert the length in feet to centimeter. Pseudocode: • Input the length in feet (Lft) • Calculate the length in cm (Lcm) by multiplying LFT with 30 • Print length in cm (LCM)
Example 2 Algorithm • Step 1: Input Lft • Step 2: Lcm  Lft x 30 • Step 3: Print Lcm START Input Lft Lcm  Lft x 30 Print Lcm STOP Flowchart
Example 3 Write an algorithm and draw a flowchart that will read the two sides of a rectangle and calculate its area. Pseudocode • Input the width (W) and Length (L) of a rectangle • Calculate the area (A) by multiplying L with W • Print A
Example 3 Algorithm • Step 1: Input W,L • Step 2: A  L x W • Step 3: Print A START Input W, L A  L x W Print A STOP
1. Area of Square 2. Area of Rectangle 3. Area and Circumference of a circle. 4. Finding simple interest. 5. Conversion of temperature from degree centigrade to Fahrenheit. 6. Calculate sum of 5 subjects and find percentage. 7. Arithmetic operations(+,-,*,/,%)
Hint: Use printf() function along with escape sequences to do the following programs. Write a C program to print “ “sun” rises in the ‘east’ ”
 void main() { int x; x = (200+200) * 20; printf("%d", x); }  What is the output of the following code? void main() { int x; x = 200*20 / 200; printf("%d", x); }  void main() { int x; x = 200+200*10; printf("%d", x); }
// C program to demonstrate working of binary arithmetic operators #include <stdio.h> int main() { int a = 10, b = 4, res; // printing a and b printf("a is %d and b is %dn", a, b); res = a + b; // addition printf("a+b is %dn", res); res = a - b; // subtraction printf("a-b is %dn", res); res = a * b; // multiplication printf("a*b is %dn", res); res = a / b; // division printf("a/b is %dn", res); res = a % b; // modulus printf("a%%b is %dn", res); return 0; }
16
// C Program for conditional operator (max of two numbers) #include<stdio.h> int main() { float num1, num2, max; printf("Enter two numbers: "); scanf("%f %f", &num1, &num2); max = (num1 > num2) ? num1 : num2; printf("Maximum of %.2f and %.2f = %.2f", num1, num2, max); return 0; }
// C Program for conditional operator (max of three numbers) #include<stdio.h> int main() { int a,b,c,d; printf(“Enter three numbers:”); scanf("%d%d%d",&a,&b,&c); d=a>b?(a>c?a:c):(b>c?b:c); printf("Max value is %d",d); return 0; }
//Write a C program to swap two numbers using temporary variable. #include<stdio.h> int main() { int x, y, temp; printf("Enter the value of x and y: "); scanf("%d %d", &x, &y); printf("Before swapping x=%d, y=%d ", x, y); /*Swapping logic */ temp = x; x = y; y = temp; printf("After swapping x=%d, b=%d", x, y); return 0; }
C program to swap two numbers without using the third or a temporary variable Step 1: Declare 2 variables x and y. Step 2: Read two numbers from keyboard. Step 3: Swap numbers. //Apply addition and subtraction operations to swap the numbers. i. x=x+y ii. y=x-y iii. x=x-y Step 4: Print x and y values.
#include<stdio.h> int main() { int x,y; printf("enter x and y values:"); scanf("%d%d",&x,&y); x=x+y; y=x-y; x=x-y; printf("After swap x=%d and y=%d",x,y); return 0; }
//Multiplication table. #include <stdio.h> int main() { int i, num; /* Input a number to print table */ printf("Enter number to print table: "); scanf("%d", &num); for(i=1; i<=10; i++) { printf("%d * %d = %d", num, i, (num*i)); } return 0; }
Sum of digits algorithm To get sum of each digits by c program, use the following algorithm: •Step 1: Get number by user •Step 2: Get the modulus/remainder of the number •Step 3: sum the remainder of the number •Step 4: Divide the number by 10 •Step 5: Repeat the step 2 while number is greater than 0.
#include<stdio.h> int main() { int n,sum=0,m; printf("Enter a number:"); scanf("%d",&n); while(n>0) { m=n%10; sum=sum+m; n=n/10; } printf("Sum is=%d",sum); return 0; }
Perfect Number In mathematics, a perfect number is a positive integer that is equal to the sum of its positive divisors, excluding the number itself. For example, 6 is a positive number that is completely divisible by 1, 2, and 3. We know that the number is also divisible by itself but we will include it in the addition of divisors. When we add these divisors (1 + 2 + 3 = 6), it produces 6, which is equal to the number that we have considered. So, we can say that 6 is a perfect number.
/ *Create a C Program to find the perfect number using while loop*/ #include<stdio.h> #include<conio.h> void main() { int i = 1, num, Sum = 0; printf(" Enter any number to check Perfect Number n"); scanf("%d", &num); while(i < num ) { if(num % i == 0) Sum = Sum + i; i++; } if(Sum == num) printf("n %d is Perfect Number", num); else printf("n %d is not a Perfect Number", num);
Armstrong Number is a number that is equal to the sum of cubes of its digits. For example 0, 1, 153, 370, 371 and 407 are the Armstrong numbers. 153 = (1*1*1)+(5*5*5)+(3*3*3) where: (1*1*1)=1 (5*5*5)=125 (3*3*3)=27 So: 1+125+27=153
#include<stdio.h> int main() { int n,r,sum=0,temp; printf("enter the number="); scanf("%d",&n); temp=n; while(n>0) { r=n%10; sum=sum+(r*r*r); n=n/10; } if(temp==sum) printf("armstrong number "); else
#include<stdio.h> int main() { int n, reverse=0, rem; printf("Enter a number: "); scanf("%d", &n); while(n!=0) { rem=n%10; reverse=reverse*10+rem; n/=10; } printf("Reversed Number: %d",reverse); return 0; }
Palindrome Number Algorithm •Get the number from user •Hold the number in temporary variable •Reverse the number •Compare the temporary number with reversed number •If both numbers are same, print palindrome number •Else print not palindrome number
#include<stdio.h> int main() { int n,r,sum=0,temp; printf("enter the number="); scanf("%d",&n); temp=n; while(n>0) { r=n%10; sum=(sum*10)+r; n=n/10; } if(temp==sum) printf("palindrome number "); else printf("not palindrome"); return 0;
#include<stdio.h> int main() { int i,fact=1,number; printf("Enter a number: "); scanf("%d",&number); for(i=1;i<=number;i++) { fact=fact*i; } printf("Factorial of %d is: %d",number,fact); return 0; }
Fibonacci Series In case of fibonacci series, next number is the sum of previous two numbers for example 0, 1, 1, 2, 3, 5, 8, 13, 21 etc. The first two numbers of fibonacci series are 0 and 1. Enter the number of elements:15 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377
#include<stdio.h> int main() { int n1=0,n2=1,n3,i,number; printf("Enter the number of elements:"); scanf("%d",&number); printf("n%d %d",n1,n2); //printing 0 and 1 for(i=2;i<number;++i) //loop starts from 2 because 0 and 1 are already printed { n3=n1+n2; printf(" %d",n3); n1=n2; n2=n3; } return 0; }
#include<stdio.h> int main() { int n,i,m=0,flag=0; printf("Enter the number to check prime:"); scanf("%d", &n); m=n/2; for(i=2;i<=m;i++) { if(n%i==0) { printf("Number is not prime"); flag=1; break; } } if(flag==0) printf("Number is prime"); return 0; }
To print a prime numbers up to 1 to n Prime number is a number which is exactly divisible by one and itself only Ex: 2, 3,5,7,……… Step 1: start Step 2: read n Step 3: initialize i=1,c=0 Step 4:if i<=n goto step 5 If not goto step 10 Step 5: initialize j=1 Step 6: if j<=i do the following. If no goto step 7 i)if i%j==0 increment c ii) increment j iii) goto Step 6 Step 7: if c== 2 print i Step 8: increment i Step 9: goto step 4 Step 10: stop
#include<stdio.h> void main() { int n,i,fact,j; printf("enter the number:"); scanf("%d",&n); for(i=1;i<=n;i++) { fact=0; //THIS LOOP WILL CHECK A NO TO BE PRIME NO. OR NOT. for(j=1;j<=i;j++) { if(i%j==0) fact++; } if(fact==2) printf("n %d",i); } } enter the number:21 2 3 5 7 11 13 17 19
#include <stdio.h> int main() { int a[10], number, i, j; printf("n Please Enter the Number You want to Convert : "); scanf("%d", &number); for(i = 0; number > 0; i++) { a[i] = number % 2; number = number / 2; } printf("n Binary Number of a Given Number = "); for(j = i - 1; j >= 0; j--) { printf(" %d ", a[j]); } printf("n"); return 0; } Please Enter the Number You want to Convert : 5 Binary Number of a Given Number = 1 0 1
#include <stdio.h> int main(void) { int i,max,min,N,x; printf("Enter N : "); scanf("%d",&N); max=0; min=999; for(i=1;i<=N;i++) { printf("Enter x-%d : ",i); scanf("%d",&x); if(max < x) max = x; if(min > x) min = x; } printf("n max = %d",max); printf("n min = %d",min); return 0; } OUTPUT Enter N : 4 Enter x-1 : 12 Enter x-2 : 43 Enter x-3 : 2 Enter x-4 : 54 max = 54 min = 2
#include<stdio.h> int main() { int rows; printf("Enter the number of rows"); scanf("%d",&rows); for(int i=1;i<=rows;i++) { for(int space=1;space<=(rows-i);space++) { printf(" "); } for(int k=1;k<=i;k++) { printf("* "); } printf("n"); } return 0; }
#include<stdio.h> int main() { int n,m; printf("Enter the number of rows"); scanf("%d",&n); m=n; for(int i=1;i<=n;i++) { for(int j=1;j<=m-1;j++) { printf(" "); } for(int k=1;k<=2*i-1;k++) { printf("*"); } m--; printf("n"); } return 0;
1.#include < stdio.h > 2.#include < math.h > 3. 4.int main() 5.{ 6. float degree, radian; 7. const float PI = 3.14159; 8. 9. printf("Enter angle in degreen"); 10. scanf("%f", °ree); 11. 12. radian = degree * (PI / 180.0); 13. 14. printf("Sin(%f) = %fn", degree, sin(radian)); 15. printf("Cos(%f) = %fn", degree, cos(radian)); 16. printf("Tan(%f) = %fn", degree, tan(radian)); 17. printf("Cosec(%f) = %fn", degree, 1/ sin(radian)); 18. printf("Sec(%f) = %fn", degree, 1/ cos(radian)); 19. printf("Cot(%f) = %fn", degree, 1/ tan(radian)); 20. 21. return 0;
#include<stdio.h> #include<conio.h> int main() { int n, i, j; printf("Enter the number of rows: "); scanf("%d",&n); for(i = 1; i <= n; i++) { for(j = 1; j <= i; j++) { printf("%d",j); } printf("n"); } return 0; }
0 row =1 1 row = (0+1), (1+0) = 1, 1 2 row = (0+1), (1+1), (1+0) = 1, 2, 1 3 row = (0+1), (1+2), (2+1), (1+0) = 1, 3, 3, 1 4 row = (0+1), (1+3), (3+3), (3+1), (1+0) = 1, 4, 6, 4, 1
#include<stdio.h> void main() { int x,y,n,a,z,s; /* x, y are for loop control and n is to store the input limit*/ printf("Enter the limit: "); /*limit implies number second from edge in Pascal's tringle*/ scanf("%d",&n); printf("nn"); s=n; for(x=0; x<=n; x++) { a=1; for(z=s; z>=0; z--) printf(" "); s--; /* s is for printing the space*/ for(y=0; y<=x; y++) { printf("%d ",a); a=(a*(x-y)/(y+1)); } printf("n"); } }
Operations on arrays An array is an Abstract Data Type (ADT). An ADT is a collection of data and operations on that data. As an ADT, the data an array includes is the collection of data items (integers, decimal points, characters or strings). The operations on these data items include:  Traversing: Visiting each element at least once.  Reversing: Changing the positions of elements.  Insertion: Inserting a new data item into existing list of data items  Deletion: Deleting a data item from existing list of data items.  Searching: Searching for a particular data item in the list of data items.  Sorting: Arranging the list of data items in ascending order.  Merging: Combining sorted arrays into one sorted array.
/* A program to traverse an array*/ #include<stdio.h> int main() { int a[10],i,n,LB,UB; printf(“n Enter the array size:”); scanf(“%d”,&n); LB=0; UB=n-1; printf(“n Enter array elements:”); for(i=LB;i<UB;i++) scanf(“%d”,&a[i]); printf(“n The array elements are:”); for(i=LB;i<UB;i++) printf(“%dt”,a[i]); } Enter the array size:5 Enter array elements:4 3 2 6 The array elements are:4 3 2 6
/* A program to reverse an array*/ #include<stdio.h> int main() { int a[10],n,i,j,temp; printf(“n Enter the array size:”); scanf(“%d”,&n); printf(“n Enter array elements:”); for(i=0;i<n;i++) scanf(“%d”,&a[i]); for(i=0,j=n-1;i<j;i++,j--) { temp=a[i]; a[i]=a[j]; a[j]=temp; } printf(“n The reversed array is:”); for(i=0;i<n;i++) printf(“%dt”,a[i]); } Enter the array size:5 Enter array elements:3 4 5 7 1 The reversed array is:1 7 5 4 3
//Inserting an element into an array #include<stdio.h> int main() { int a[10],n,i,pos,ele; printf(“n Enter the array size:”); scanf(“%d”,&n); printf(“n Enter the array elements:”); for(i=0;i<n;i++) scanf(“%d”,&a[i]); printf(“n Enter the element to insert:”); scanf(“%d”,&ele); printf(“n Enter the position:”); scanf(“%d”,&pos); n=n+1; //step 1 for(i=n-1;i>=pos-1;i- -) //step 2 a[i]=a[i-1]; a[pos-1]=ele; //step3 printf(“n New Arrayn”); for(i=0;i<n;i++) printf(“%dt”,a[i]); } Enter the array size:5 Enter the array elements:3 4 6 1 7 Enter the element to insert:8 Enter the position:2 New Array 3 8 4 6 1 7
//The following program demonstrates the process of deletion #include<stdio.h> int main() { int a[10],n,ele,pos,i; printf(“nEnter the array size:”); scanf(“%d”,&n); printf(“n Enter the array elements:”); for(i=0;i<n;i++) scanf(“%d”,&a[i]); printf(“n Enter the position:”); scanf(“%d”,&pos); printf(“n The deleted element=%d”,a[pos-1]);//step-1 for(i=pos-1;i<n;i++) //step-2 a[i]=a[i+1]; n=n-1; printf(“n New array:”); for(i=0;i<n;i++) printf(“%dt”,a[i]); Enter the array size:5 Enter the array elements:4 5 6 7 2 Enter the position of element to delete:3 The deleted element=6 New array:4 5 7 2
The following program demonstrates Linear Search #include<stdio.h> main() { int a[10],n,i,ele,pos,flag; printf(“n Enter the array size:”); scanf(“%d”,&n); printf(“n Enter the array elements:”); for(i=0;i<n;i++) scanf(“%d”,&a[i]); printf(“n Enter the element to be searched:”); scanf(“%d”,&ele); flag=0; for(i=0;i<n;i++) { if(a[i]= =ele) { flag=1; pos=i+1; break; } } if(flag= =1) printf(“n The given element is found at %d position”,pos); else printf(“n The given element is not found”); }
The following program demonstrates iterative process of binary search: #include<stdio.h> main() { int a[10],n,low,high,mid,ele,pos,i,flag; printf(“n Enter the array size:”); scanf(“%d”,&n); printf(“n Enter the array elements:”); for(i=0;i<n;i++) scanf(“%d”,&a[i]); printf(“n Enter the element to be searched:”); scanf(“%d”,&ele); low=0; high=n-1; flag=0; while(low<=high) { mid=(low+high)/2; if(ele= =a[mid]) { flag=1; pos=mid+1; break; } else if(ele>a[mid]) low=mid+1; else high=mid-1; } if(flag= =1) printf(“n The element is found at %d position”,pos); else printf(“n The element is not found”); }
The following program demonstrates the selection sort technique: #include<stdio.h> main() { int a[10],i,j,temp,minpos,n; printf("n Enter the array size:"); scanf("%d",&n); printf("n Enter the array elements:"); for(i=0;i<n;i++) scanf("%d",&a[i]); for(i=0;i<n-1;i++) { minpos=i; for(j=i+1;j<n;j++) if(a[j]<a[minpos]) minpos=j; if(minpos!=i) { temp=a[i]; a[i]=a[minpos]; a[minpos]=temp; } } printf("n Array elements after sorting (Selection sort):"); for(i=0;i<n;i++) printf("%dt",a[i]); }
The following code demonstrates the bubble sort technique: #include<stdio.h> main() { int a[10],n,i,j,temp; printf("n Enter the array size:"); scanf("%d",&n); printf("n Enter the array elements:"); for(i=0;i<n;i++) scanf("%d",&a[i]); for(i=0;i<n;i++) { for(j=0;j<n-i-1;j++) { if(a[j]>a[j+1]) { temp=a[j]; a[j]=a[j+1]; a[j+1]=temp; } } } printf("n Array elements after sort (Bubble sort):"); for(i=0;i<n;i++) printf("%dt",a[i]); }
/* Merging two arrays into one array*/ #include<stdio.h> main() { int a[10],n,b[10],m,c[20],i,j,k,l; printf(“n Enter the first array size:”); scanf(“%d”,&n); printf(“n Enter the first array elements in sorted order:”); for(i=0;i<n;i++) scanf(“%d”,&a[i]); printf(“n Enter the second array size:”); scanf(“%d”,&m); printf(“n Enter the second array elements in sorted order:”); for(j=0;j<m;j++) scanf(“%d”,&b[j]); i=j=k=0; while(i<n&&j<m) { if(a[i]<b[j]) { c[k]=a[i]; i++; k++; } else if(a[i]>b[j]) { c[k]=b[j]; j++; k++; } else { c[k]=a[i]; i++;j++;k++; } } if(i<n) { for(l=0;l<n;l++) { c[k]=a[l]; k++; } } if(j<m) { for(l=0;l<m;l++) { c[k]=b[l]; k++; } } printf(“n The merged list:”); for(i=0;i<k;i++) printf(“%dt”,c[i]);’ }
/* Program to read and print the contents of a two-dimensional array in the form of a matrix*/ #include<stdio.h> main() { int a[10][10],m,n,i,j; printf(“n Enter the matrix row and column values:”); scanf(“%d%d”,&m,&n); printf(“n Enter the matrix elements:”); for(i=0;i<m;i++) for(j=0;j<n;j++) scanf(“%d”,&a[i][j]); printf(“n The Given Matrixn”); for(i=0;i<m;i++) { for(j=0;j<n;j++) printf(“%dt”,a[i][j]); printf(“n”); } }
/*Program to add two matrices*/ #include<stdio.h> main() { int a[10][10],b[10][10],c[10][10],i,j; int m,n,p,q; printf(“n Enter the first matrix order:”); scanf(“%d%d”,&m,&n); printf(“n Enter first matrix elements:”); for(i=0;i<m;i++) for(j=0;j<n;j++) scanf(“%d”,&a[i][j]); printf(“n Enter the second matrix order:”); scanf(“%d%d”,&p,&q); printf(“n Enter second matrix elements:”); for(i=0;i<p;i++) for(j=0;j<q;j++) scanf(“%d”,&b[i][j]); if(m==p&&n==q) { for(i=0;i<m;i++) for(j=0;j<n;j++) c[i][j]=a[i][j]+b[i][j]; printf(“n The resultant matrix n”); for(i=0;i<m;i++) { for(j=0;j<n;j++) printf(“%dt”,c[i][j]); printf(“n”); } } else printf(“n Matrix addition is not possible”); }
/*Program to multiply two matrices*/ #include<stdio.h> main() { int a[10][10],b[10][10],c[10][10],i,j; int m,n,p,q; printf(“n Enter the first matrix order:”); scanf(“%d%d”,&m,&n); printf(“n Enter first matrix elements:”); for(i=0;i<m;i++) for(j=0;j<n;j++) scanf(“%d”,&a[i][j]); printf(“n Enter the second matrix order:”); scanf(“%d%d”,&p,&q); printf(“n Enter second matrix elements:”); for(i=0;i<p;i++) for(j=0;j<q;j++) scanf(“%d”,&b[i][j]); if(n==p) { for(i=0;i<m;i++) { for(j=0;j<q;j++) c[i][j]=0; for(k=0;k<n;k++) c[i][j]=c[i][j]+a[i][k]*b[k][j]; } printf(“n The resultant matrix n”); for(i=0;i<m;i++) { for(j=0;j<n;j++) printf(“%dt”,c[i][j]); printf(“n”); } } else printf(“n Matrix multiplication is not possible”); }
String-handling functions All of these built-in functions are defined in the header file string.h. Therefore, whenever we use one of these string handling functions, we should add the preprocessor statement #include<string.h> to our program. Some of the string- handling functions are: strlen() strrev() strcpy() strcat() strcmp() strlwr() strupr() strncpy() strncat() strncmp()

c programming lab for first year student

  • 1.
    /*C Program toDisplay "Hello, World!“ */ #include <stdio.h> int main() { // printf() displays the string inside quotation printf("Hello, World!"); return 0; }
  • 2.
    n is forvertical tab . It takes the cursor to next line. t is for horizontal tab. #include<stdio.h> int main() { printf("how are youn I m fine. t ThankU"); }
  • 3.
    /*C Program toPrint an Integer (Entered by the User)*/ #include <stdio.h> int main() { int num; printf("Enter an integer: "); // reads and stores input scanf("%d", &num); // displays output printf("You entered: %d", num); return 0; }
  • 4.
    /*Program to addtwo integers*/ #include <stdio.h> int main() { int num1, num2, sum; printf("Enter two integers: "); scanf("%d %d", &num1, &num2); // calculating sum sum = num1 + num2; printf("%d + %d = %d", num1, num2, sum); return 0; }
  • 5.
    /*Program to PrintASCII Value*/ include <stdio.h> int main() { char c; printf("Enter a character: "); scanf("%c", &c); // %d displays the integer value of a character // %c displays the actual character printf("ASCII value of %c = %d", c, c); return 0; }
  • 7.
    Example 2 • Writean algorithm and draw a flowchart to convert the length in feet to centimeter. Pseudocode: • Input the length in feet (Lft) • Calculate the length in cm (Lcm) by multiplying LFT with 30 • Print length in cm (LCM)
  • 8.
    Example 2 Algorithm • Step1: Input Lft • Step 2: Lcm  Lft x 30 • Step 3: Print Lcm START Input Lft Lcm  Lft x 30 Print Lcm STOP Flowchart
  • 9.
    Example 3 Write analgorithm and draw a flowchart that will read the two sides of a rectangle and calculate its area. Pseudocode • Input the width (W) and Length (L) of a rectangle • Calculate the area (A) by multiplying L with W • Print A
  • 10.
    Example 3 Algorithm • Step1: Input W,L • Step 2: A  L x W • Step 3: Print A START Input W, L A  L x W Print A STOP
  • 11.
    1. Area ofSquare 2. Area of Rectangle 3. Area and Circumference of a circle. 4. Finding simple interest. 5. Conversion of temperature from degree centigrade to Fahrenheit. 6. Calculate sum of 5 subjects and find percentage. 7. Arithmetic operations(+,-,*,/,%)
  • 12.
    Hint: Use printf()function along with escape sequences to do the following programs. Write a C program to print “ “sun” rises in the ‘east’ ”
  • 13.
     void main() { intx; x = (200+200) * 20; printf("%d", x); }  What is the output of the following code? void main() { int x; x = 200*20 / 200; printf("%d", x); }  void main() { int x; x = 200+200*10; printf("%d", x); }
  • 14.
    // C programto demonstrate working of binary arithmetic operators #include <stdio.h> int main() { int a = 10, b = 4, res; // printing a and b printf("a is %d and b is %dn", a, b); res = a + b; // addition printf("a+b is %dn", res); res = a - b; // subtraction printf("a-b is %dn", res); res = a * b; // multiplication printf("a*b is %dn", res); res = a / b; // division printf("a/b is %dn", res); res = a % b; // modulus printf("a%%b is %dn", res); return 0; }
  • 16.
  • 17.
    // C Programfor conditional operator (max of two numbers) #include<stdio.h> int main() { float num1, num2, max; printf("Enter two numbers: "); scanf("%f %f", &num1, &num2); max = (num1 > num2) ? num1 : num2; printf("Maximum of %.2f and %.2f = %.2f", num1, num2, max); return 0; }
  • 18.
    // C Programfor conditional operator (max of three numbers) #include<stdio.h> int main() { int a,b,c,d; printf(“Enter three numbers:”); scanf("%d%d%d",&a,&b,&c); d=a>b?(a>c?a:c):(b>c?b:c); printf("Max value is %d",d); return 0; }
  • 19.
    //Write a Cprogram to swap two numbers using temporary variable. #include<stdio.h> int main() { int x, y, temp; printf("Enter the value of x and y: "); scanf("%d %d", &x, &y); printf("Before swapping x=%d, y=%d ", x, y); /*Swapping logic */ temp = x; x = y; y = temp; printf("After swapping x=%d, b=%d", x, y); return 0; }
  • 20.
    C program toswap two numbers without using the third or a temporary variable Step 1: Declare 2 variables x and y. Step 2: Read two numbers from keyboard. Step 3: Swap numbers. //Apply addition and subtraction operations to swap the numbers. i. x=x+y ii. y=x-y iii. x=x-y Step 4: Print x and y values.
  • 21.
    #include<stdio.h> int main() { int x,y; printf("enterx and y values:"); scanf("%d%d",&x,&y); x=x+y; y=x-y; x=x-y; printf("After swap x=%d and y=%d",x,y); return 0; }
  • 22.
    //Multiplication table. #include <stdio.h> intmain() { int i, num; /* Input a number to print table */ printf("Enter number to print table: "); scanf("%d", &num); for(i=1; i<=10; i++) { printf("%d * %d = %d", num, i, (num*i)); } return 0; }
  • 23.
    Sum of digitsalgorithm To get sum of each digits by c program, use the following algorithm: •Step 1: Get number by user •Step 2: Get the modulus/remainder of the number •Step 3: sum the remainder of the number •Step 4: Divide the number by 10 •Step 5: Repeat the step 2 while number is greater than 0.
  • 24.
    #include<stdio.h> int main() { int n,sum=0,m; printf("Entera number:"); scanf("%d",&n); while(n>0) { m=n%10; sum=sum+m; n=n/10; } printf("Sum is=%d",sum); return 0; }
  • 25.
    Perfect Number In mathematics,a perfect number is a positive integer that is equal to the sum of its positive divisors, excluding the number itself. For example, 6 is a positive number that is completely divisible by 1, 2, and 3. We know that the number is also divisible by itself but we will include it in the addition of divisors. When we add these divisors (1 + 2 + 3 = 6), it produces 6, which is equal to the number that we have considered. So, we can say that 6 is a perfect number.
  • 26.
    / *Create a CProgram to find the perfect number using while loop*/ #include<stdio.h> #include<conio.h> void main() { int i = 1, num, Sum = 0; printf(" Enter any number to check Perfect Number n"); scanf("%d", &num); while(i < num ) { if(num % i == 0) Sum = Sum + i; i++; } if(Sum == num) printf("n %d is Perfect Number", num); else printf("n %d is not a Perfect Number", num);
  • 27.
    Armstrong Number isa number that is equal to the sum of cubes of its digits. For example 0, 1, 153, 370, 371 and 407 are the Armstrong numbers. 153 = (1*1*1)+(5*5*5)+(3*3*3) where: (1*1*1)=1 (5*5*5)=125 (3*3*3)=27 So: 1+125+27=153
  • 28.
    #include<stdio.h> int main() { int n,r,sum=0,temp; printf("enterthe number="); scanf("%d",&n); temp=n; while(n>0) { r=n%10; sum=sum+(r*r*r); n=n/10; } if(temp==sum) printf("armstrong number "); else
  • 29.
    #include<stdio.h> int main() { int n,reverse=0, rem; printf("Enter a number: "); scanf("%d", &n); while(n!=0) { rem=n%10; reverse=reverse*10+rem; n/=10; } printf("Reversed Number: %d",reverse); return 0; }
  • 30.
    Palindrome Number Algorithm •Getthe number from user •Hold the number in temporary variable •Reverse the number •Compare the temporary number with reversed number •If both numbers are same, print palindrome number •Else print not palindrome number
  • 31.
    #include<stdio.h> int main() { int n,r,sum=0,temp; printf("enterthe number="); scanf("%d",&n); temp=n; while(n>0) { r=n%10; sum=(sum*10)+r; n=n/10; } if(temp==sum) printf("palindrome number "); else printf("not palindrome"); return 0;
  • 32.
    #include<stdio.h> int main() { int i,fact=1,number; printf("Entera number: "); scanf("%d",&number); for(i=1;i<=number;i++) { fact=fact*i; } printf("Factorial of %d is: %d",number,fact); return 0; }
  • 33.
    Fibonacci Series In caseof fibonacci series, next number is the sum of previous two numbers for example 0, 1, 1, 2, 3, 5, 8, 13, 21 etc. The first two numbers of fibonacci series are 0 and 1. Enter the number of elements:15 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377
  • 34.
    #include<stdio.h> int main() { int n1=0,n2=1,n3,i,number; printf("Enterthe number of elements:"); scanf("%d",&number); printf("n%d %d",n1,n2); //printing 0 and 1 for(i=2;i<number;++i) //loop starts from 2 because 0 and 1 are already printed { n3=n1+n2; printf(" %d",n3); n1=n2; n2=n3; } return 0; }
  • 35.
    #include<stdio.h> int main() { intn,i,m=0,flag=0; printf("Enter the number to check prime:"); scanf("%d", &n); m=n/2; for(i=2;i<=m;i++) { if(n%i==0) { printf("Number is not prime"); flag=1; break; } } if(flag==0) printf("Number is prime"); return 0; }
  • 36.
    To print aprime numbers up to 1 to n Prime number is a number which is exactly divisible by one and itself only Ex: 2, 3,5,7,……… Step 1: start Step 2: read n Step 3: initialize i=1,c=0 Step 4:if i<=n goto step 5 If not goto step 10 Step 5: initialize j=1 Step 6: if j<=i do the following. If no goto step 7 i)if i%j==0 increment c ii) increment j iii) goto Step 6 Step 7: if c== 2 print i Step 8: increment i Step 9: goto step 4 Step 10: stop
  • 37.
    #include<stdio.h> void main() { int n,i,fact,j; printf("enterthe number:"); scanf("%d",&n); for(i=1;i<=n;i++) { fact=0; //THIS LOOP WILL CHECK A NO TO BE PRIME NO. OR NOT. for(j=1;j<=i;j++) { if(i%j==0) fact++; } if(fact==2) printf("n %d",i); } } enter the number:21 2 3 5 7 11 13 17 19
  • 38.
    #include <stdio.h> int main() { inta[10], number, i, j; printf("n Please Enter the Number You want to Convert : "); scanf("%d", &number); for(i = 0; number > 0; i++) { a[i] = number % 2; number = number / 2; } printf("n Binary Number of a Given Number = "); for(j = i - 1; j >= 0; j--) { printf(" %d ", a[j]); } printf("n"); return 0; } Please Enter the Number You want to Convert : 5 Binary Number of a Given Number = 1 0 1
  • 39.
    #include <stdio.h> int main(void){ int i,max,min,N,x; printf("Enter N : "); scanf("%d",&N); max=0; min=999; for(i=1;i<=N;i++) { printf("Enter x-%d : ",i); scanf("%d",&x); if(max < x) max = x; if(min > x) min = x; } printf("n max = %d",max); printf("n min = %d",min); return 0; } OUTPUT Enter N : 4 Enter x-1 : 12 Enter x-2 : 43 Enter x-3 : 2 Enter x-4 : 54 max = 54 min = 2
  • 40.
    #include<stdio.h> int main() { int rows; printf("Enterthe number of rows"); scanf("%d",&rows); for(int i=1;i<=rows;i++) { for(int space=1;space<=(rows-i);space++) { printf(" "); } for(int k=1;k<=i;k++) { printf("* "); } printf("n"); } return 0; }
  • 41.
    #include<stdio.h> int main() { int n,m; printf("Enterthe number of rows"); scanf("%d",&n); m=n; for(int i=1;i<=n;i++) { for(int j=1;j<=m-1;j++) { printf(" "); } for(int k=1;k<=2*i-1;k++) { printf("*"); } m--; printf("n"); } return 0;
  • 42.
    1.#include < stdio.h> 2.#include < math.h > 3. 4.int main() 5.{ 6. float degree, radian; 7. const float PI = 3.14159; 8. 9. printf("Enter angle in degreen"); 10. scanf("%f", °ree); 11. 12. radian = degree * (PI / 180.0); 13. 14. printf("Sin(%f) = %fn", degree, sin(radian)); 15. printf("Cos(%f) = %fn", degree, cos(radian)); 16. printf("Tan(%f) = %fn", degree, tan(radian)); 17. printf("Cosec(%f) = %fn", degree, 1/ sin(radian)); 18. printf("Sec(%f) = %fn", degree, 1/ cos(radian)); 19. printf("Cot(%f) = %fn", degree, 1/ tan(radian)); 20. 21. return 0;
  • 43.
    #include<stdio.h> #include<conio.h> int main() { int n,i, j; printf("Enter the number of rows: "); scanf("%d",&n); for(i = 1; i <= n; i++) { for(j = 1; j <= i; j++) { printf("%d",j); } printf("n"); } return 0; }
  • 44.
    0 row =1 1row = (0+1), (1+0) = 1, 1 2 row = (0+1), (1+1), (1+0) = 1, 2, 1 3 row = (0+1), (1+2), (2+1), (1+0) = 1, 3, 3, 1 4 row = (0+1), (1+3), (3+3), (3+1), (1+0) = 1, 4, 6, 4, 1
  • 45.
    #include<stdio.h> void main() { int x,y,n,a,z,s; /*x, y are for loop control and n is to store the input limit*/ printf("Enter the limit: "); /*limit implies number second from edge in Pascal's tringle*/ scanf("%d",&n); printf("nn"); s=n; for(x=0; x<=n; x++) { a=1; for(z=s; z>=0; z--) printf(" "); s--; /* s is for printing the space*/ for(y=0; y<=x; y++) { printf("%d ",a); a=(a*(x-y)/(y+1)); } printf("n"); } }
  • 46.
    Operations on arrays Anarray is an Abstract Data Type (ADT). An ADT is a collection of data and operations on that data. As an ADT, the data an array includes is the collection of data items (integers, decimal points, characters or strings). The operations on these data items include:  Traversing: Visiting each element at least once.  Reversing: Changing the positions of elements.  Insertion: Inserting a new data item into existing list of data items  Deletion: Deleting a data item from existing list of data items.  Searching: Searching for a particular data item in the list of data items.  Sorting: Arranging the list of data items in ascending order.  Merging: Combining sorted arrays into one sorted array.
  • 47.
    /* A programto traverse an array*/ #include<stdio.h> int main() { int a[10],i,n,LB,UB; printf(“n Enter the array size:”); scanf(“%d”,&n); LB=0; UB=n-1; printf(“n Enter array elements:”); for(i=LB;i<UB;i++) scanf(“%d”,&a[i]); printf(“n The array elements are:”); for(i=LB;i<UB;i++) printf(“%dt”,a[i]); } Enter the array size:5 Enter array elements:4 3 2 6 The array elements are:4 3 2 6
  • 48.
    /* A programto reverse an array*/ #include<stdio.h> int main() { int a[10],n,i,j,temp; printf(“n Enter the array size:”); scanf(“%d”,&n); printf(“n Enter array elements:”); for(i=0;i<n;i++) scanf(“%d”,&a[i]); for(i=0,j=n-1;i<j;i++,j--) { temp=a[i]; a[i]=a[j]; a[j]=temp; } printf(“n The reversed array is:”); for(i=0;i<n;i++) printf(“%dt”,a[i]); } Enter the array size:5 Enter array elements:3 4 5 7 1 The reversed array is:1 7 5 4 3
  • 49.
    //Inserting an elementinto an array #include<stdio.h> int main() { int a[10],n,i,pos,ele; printf(“n Enter the array size:”); scanf(“%d”,&n); printf(“n Enter the array elements:”); for(i=0;i<n;i++) scanf(“%d”,&a[i]); printf(“n Enter the element to insert:”); scanf(“%d”,&ele); printf(“n Enter the position:”); scanf(“%d”,&pos); n=n+1; //step 1 for(i=n-1;i>=pos-1;i- -) //step 2 a[i]=a[i-1]; a[pos-1]=ele; //step3 printf(“n New Arrayn”); for(i=0;i<n;i++) printf(“%dt”,a[i]); } Enter the array size:5 Enter the array elements:3 4 6 1 7 Enter the element to insert:8 Enter the position:2 New Array 3 8 4 6 1 7
  • 50.
    //The following programdemonstrates the process of deletion #include<stdio.h> int main() { int a[10],n,ele,pos,i; printf(“nEnter the array size:”); scanf(“%d”,&n); printf(“n Enter the array elements:”); for(i=0;i<n;i++) scanf(“%d”,&a[i]); printf(“n Enter the position:”); scanf(“%d”,&pos); printf(“n The deleted element=%d”,a[pos-1]);//step-1 for(i=pos-1;i<n;i++) //step-2 a[i]=a[i+1]; n=n-1; printf(“n New array:”); for(i=0;i<n;i++) printf(“%dt”,a[i]); Enter the array size:5 Enter the array elements:4 5 6 7 2 Enter the position of element to delete:3 The deleted element=6 New array:4 5 7 2
  • 52.
    The following programdemonstrates Linear Search #include<stdio.h> main() { int a[10],n,i,ele,pos,flag; printf(“n Enter the array size:”); scanf(“%d”,&n); printf(“n Enter the array elements:”); for(i=0;i<n;i++) scanf(“%d”,&a[i]); printf(“n Enter the element to be searched:”); scanf(“%d”,&ele); flag=0; for(i=0;i<n;i++) { if(a[i]= =ele) { flag=1; pos=i+1; break; } } if(flag= =1) printf(“n The given element is found at %d position”,pos); else printf(“n The given element is not found”); }
  • 54.
    The following programdemonstrates iterative process of binary search: #include<stdio.h> main() { int a[10],n,low,high,mid,ele,pos,i,flag; printf(“n Enter the array size:”); scanf(“%d”,&n); printf(“n Enter the array elements:”); for(i=0;i<n;i++) scanf(“%d”,&a[i]); printf(“n Enter the element to be searched:”); scanf(“%d”,&ele); low=0; high=n-1; flag=0; while(low<=high) { mid=(low+high)/2; if(ele= =a[mid]) { flag=1; pos=mid+1; break; } else if(ele>a[mid]) low=mid+1; else high=mid-1; } if(flag= =1) printf(“n The element is found at %d position”,pos); else printf(“n The element is not found”); }
  • 56.
    The following programdemonstrates the selection sort technique: #include<stdio.h> main() { int a[10],i,j,temp,minpos,n; printf("n Enter the array size:"); scanf("%d",&n); printf("n Enter the array elements:"); for(i=0;i<n;i++) scanf("%d",&a[i]); for(i=0;i<n-1;i++) { minpos=i; for(j=i+1;j<n;j++) if(a[j]<a[minpos]) minpos=j; if(minpos!=i) { temp=a[i]; a[i]=a[minpos]; a[minpos]=temp; } } printf("n Array elements after sorting (Selection sort):"); for(i=0;i<n;i++) printf("%dt",a[i]); }
  • 58.
    The following codedemonstrates the bubble sort technique: #include<stdio.h> main() { int a[10],n,i,j,temp; printf("n Enter the array size:"); scanf("%d",&n); printf("n Enter the array elements:"); for(i=0;i<n;i++) scanf("%d",&a[i]); for(i=0;i<n;i++) { for(j=0;j<n-i-1;j++) { if(a[j]>a[j+1]) { temp=a[j]; a[j]=a[j+1]; a[j+1]=temp; } } } printf("n Array elements after sort (Bubble sort):"); for(i=0;i<n;i++) printf("%dt",a[i]); }
  • 59.
    /* Merging twoarrays into one array*/ #include<stdio.h> main() { int a[10],n,b[10],m,c[20],i,j,k,l; printf(“n Enter the first array size:”); scanf(“%d”,&n); printf(“n Enter the first array elements in sorted order:”); for(i=0;i<n;i++) scanf(“%d”,&a[i]); printf(“n Enter the second array size:”); scanf(“%d”,&m); printf(“n Enter the second array elements in sorted order:”); for(j=0;j<m;j++) scanf(“%d”,&b[j]); i=j=k=0; while(i<n&&j<m) { if(a[i]<b[j]) { c[k]=a[i]; i++; k++; } else if(a[i]>b[j]) { c[k]=b[j]; j++; k++; } else { c[k]=a[i]; i++;j++;k++; } } if(i<n) { for(l=0;l<n;l++) { c[k]=a[l]; k++; } } if(j<m) { for(l=0;l<m;l++) { c[k]=b[l]; k++; } } printf(“n The merged list:”); for(i=0;i<k;i++) printf(“%dt”,c[i]);’ }
  • 60.
    /* Program toread and print the contents of a two-dimensional array in the form of a matrix*/ #include<stdio.h> main() { int a[10][10],m,n,i,j; printf(“n Enter the matrix row and column values:”); scanf(“%d%d”,&m,&n); printf(“n Enter the matrix elements:”); for(i=0;i<m;i++) for(j=0;j<n;j++) scanf(“%d”,&a[i][j]); printf(“n The Given Matrixn”); for(i=0;i<m;i++) { for(j=0;j<n;j++) printf(“%dt”,a[i][j]); printf(“n”); } }
  • 61.
    /*Program to addtwo matrices*/ #include<stdio.h> main() { int a[10][10],b[10][10],c[10][10],i,j; int m,n,p,q; printf(“n Enter the first matrix order:”); scanf(“%d%d”,&m,&n); printf(“n Enter first matrix elements:”); for(i=0;i<m;i++) for(j=0;j<n;j++) scanf(“%d”,&a[i][j]); printf(“n Enter the second matrix order:”); scanf(“%d%d”,&p,&q); printf(“n Enter second matrix elements:”); for(i=0;i<p;i++) for(j=0;j<q;j++) scanf(“%d”,&b[i][j]); if(m==p&&n==q) { for(i=0;i<m;i++) for(j=0;j<n;j++) c[i][j]=a[i][j]+b[i][j]; printf(“n The resultant matrix n”); for(i=0;i<m;i++) { for(j=0;j<n;j++) printf(“%dt”,c[i][j]); printf(“n”); } } else printf(“n Matrix addition is not possible”); }
  • 62.
    /*Program to multiplytwo matrices*/ #include<stdio.h> main() { int a[10][10],b[10][10],c[10][10],i,j; int m,n,p,q; printf(“n Enter the first matrix order:”); scanf(“%d%d”,&m,&n); printf(“n Enter first matrix elements:”); for(i=0;i<m;i++) for(j=0;j<n;j++) scanf(“%d”,&a[i][j]); printf(“n Enter the second matrix order:”); scanf(“%d%d”,&p,&q); printf(“n Enter second matrix elements:”); for(i=0;i<p;i++) for(j=0;j<q;j++) scanf(“%d”,&b[i][j]); if(n==p) { for(i=0;i<m;i++) { for(j=0;j<q;j++) c[i][j]=0; for(k=0;k<n;k++) c[i][j]=c[i][j]+a[i][k]*b[k][j]; } printf(“n The resultant matrix n”); for(i=0;i<m;i++) { for(j=0;j<n;j++) printf(“%dt”,c[i][j]); printf(“n”); } } else printf(“n Matrix multiplication is not possible”); }
  • 63.
    String-handling functions All ofthese built-in functions are defined in the header file string.h. Therefore, whenever we use one of these string handling functions, we should add the preprocessor statement #include<string.h> to our program. Some of the string- handling functions are: strlen() strrev() strcpy() strcat() strcmp() strlwr() strupr() strncpy() strncat() strncmp()