(XI) C Program Collections

1. Write C Program to display "Hello World".
#include <stdio.h> int main() { printf("Hello World"); return 0; }
2. Write C Program to display your name.
#include <stdio.h> int main() { printf(" Alex Lal Karn "); return 0; }
3. Write C Program to find sum of two numbers.
#include <stdio.h> int main() { int num1,num2,sum; printf(" Enter First number : "); scanf("%d%d",&num1,&num2); printf(" Enter Second number : "); scanf("%d",&num2); sum=num1+num2; printf("Sum of two numbers is %d ", sum); return 0; }
4. Write C Program to calculate area and perimeter of a rectagle.
#include <stdio.h> int main() { int length,breadth,area,perimeter; printf(" Enter Length of rectanlge : "); scanf("%d",&length); printf(" Enter Breadth of rectanlge : "); scanf("%d",&breadth); area=length*breadth; perimeter=2*(length+breadth); printf("Area of rectangle is %d ", area); printf(" \n Perimeter of rectangle is %d ", perimeter); return 0; }
5. Example programs of type conversion.
Example of Implicit type conversion
#include<stdio.h>
main()
{
float c;
int a=5,b=2;
c=a/b;
printf("Output is %0.2f" ,c);
}
Output is 2.00 }
Example of Explicit type conversion
#include<stdio.h>
main()
{
float c;
int a=5,b=2;
c=(float)a/b;
printf("Output is %0.2f" ,c);
}
Output is 2.50
6. Write C Program to calculate simple interest.
#include <stdio.h> int main() { float p,t,r,si; printf(" Enter principal : "); scanf("%f",&p); printf(" Enter time : "); scanf("%f",&t); printf(" Enter rate : "); scanf("%f",&r); si=(p*t*r)/100; printf(" Simple Interst = %0.2f ", si); return 0; }
7. Write C Program to calculate sqaure root of given number.
#include <stdio.h> #include<math.h> int main() { int num,root; printf(" Enter a number : "); scanf("%d",&num); root=sqrt(num); printf(" Square root of %d = %d ", num , root ); return 0; }
8. Write C Program to display the greater number among any two numbers.
#include <stdio.h> int main() { int num1,num2; printf(" Enter First number : "); scanf("%d",&num1); printf(" Enter Second number : "); scanf("%d",&num2); if(num1>num2) printf("Greater number is %d",num1); else printf("Greater number is %d",num2); return 0; }
9. Write C Program to test whether the input number is even or odd.
#include <stdio.h> int main() { int num,rem; printf(" Enter any number : "); scanf("%d",&num); rem=num%2; if(rem==0) printf(" Given number is even"); else printf("Given number is odd."); return 0; }
10. Write C Program to find greatest number among three numbers
#include <stdio.h> int main() { int num1,num2,num3; printf("Enter three numbers"); scanf("%d%d%d",&num1,&num2,&num3); if(num1>num2&&num1>num3) printf("%d is greatest",num1); else if(num2>num1&&num2>num3) printf("%d is greatest",num2); else printf("%d is greatest",num3); return 0; }
11. Write C Program to find middle number among three numbers
#include <stdio.h> int main() { int num1,num2,num3; printf("Enter three numbers"); scanf("%d%d%d",&num1,&num2,&num3); if(num1>num2&&num1>num3) printf("%d is greatest",num1); else if(num2>num1&&num2>num3) printf("%d is greatest",num2); else printf("%d is greatest",num3); return 0; }
C program to find the multiplication of given number.
#include <stdio.h> int main() { int num,i,ans; printf("Enter any number : "); scanf("%d",&num); for(i=1;i<=10;i++) { ans=num*i; printf("\n %d ",ans); } return 0; }
C program to find the factorial of the given number.
The product of a given positive integer multiplied by all lesser positive integers: The quantity five factorial (5!) = 5x4 x 3 x 2 x 1 = 120.
#include <stdio.h> int main() { int num,i,f=1; printf("Enter any number : "); scanf("%d",&num); for(i=num;i>=1;i--) { f=f*i; } printf("\n Factorial of %d is %d",num,f); return 0; }
Lab-27 Q1: C program to award grade on the basis of mark in Computer: 90+ = A+ 80 to 89 = A 70 to 79 = B+ 60 to 69 = B 50 to 59 = C+ 40 to 49 = C 35 to 39 = D Below 35 = NG
#include <stdio.h> int main() { int mark; printf("Enter Computer mark : "); scanf("%d",&mark); if(mark>=90) printf("Computer Grade = A+"); if(mark>=80&&mark<90) printf("Computer Grade = A"); if(mark>=70&&mark<80) printf("Computer Grade = B+"); if(mark>=60&&mark<70) printf("Computer Grade = B"); if(mark>=50&&mark<60) printf("Computer Grade = C+"); if(mark>=40&mark<50) printf("Computer Grade = C"); if(mark>=35&mark<40) printf("Computer Grade = D"); else printf("Computer Grade = NG"); return 0; }
Lab-27 Q2: C program to display 1 for Sunday, 2 for Monday and so on.
#include <stdio.h> int main() { int choice; printf("Enter your choice : "); scanf("%d",&choice); switch(choice) { case 1: printf("Sunday"); break; case 2: printf("Monday"); break; case 3: printf("Tuesday"); break; case 4: printf("Wednesday"); break; case 5: printf("Thursday"); break; case 6: printf("Friday"); break; case 7: printf("Saturday"); break; default: printf("Wrong Choice"); } return 0; }
Lab-28 Q1: C program to display first ten natural numbers.

for loop

#include <stdio.h> int main() { int i; for(i=1;i<=10;i++) { printf("%d\n",i) ; } return 0; }

while loop

#include <stdio.h> int main() { int i=1; while(i<=10) { printf("%d\n",i) ; i++; } return 0; }

do while loop

#include <stdio.h> int main() { int i=1; do { printf("%d\n",i) ; i++; } while(i<=10); return 0; }
Lab-28 Q2: C program to calculate and display factorial of given number[n!=n*n-1*n-2*....*1] e.g. [5!=5*4*3*2*1=120]
#include <stdio.h> int main() { int i,n,f=1; printf("Enter a number: "); scanf("%d",&n); for(i=n;i>=1;i--) { f=f*i; } printf("Factorial of %d = %d",n,f); return 0; }
Lab-28 Q2: C program to calculate and display factorial of given number[n!=n*n-1*n-2*....*1] e.g. [5!=5*4*3*2*1=120]
#include <stdio.h> int main() { int i,n,f=1; printf("Enter a number: "); scanf("%d",&n); for(i=n;i>=1;i--) { f=f*i; } printf("Factorial of %d = %d",n,f); return 0; }
Lab-29 Q1: C program to display greatest number among ten different numbers using array
#include <stdio.h> int main() { int num[10],i,great; for(i=0;i<10;i++) { printf("Enter num[%d] : ",i+1); scanf("%d",&num[i]); } great=num[0]; for(i=1;i<10;i++) { if(num[i]>great) great=num[i]; } printf("Greatest number = %d",great); return 0; }
Lab-29 Q2: C program to sort five integers numbers in ascending order
#include <stdio.h> int main() { int num[5],i,j,temp; for(i=0;i<5;i++) { printf("Enter num[%d] : ",i+1); scanf("%d",&num[i]); } for(i=0;i<5;i++) { for(j=i+1;j<5;j++) { if(num[i]>num[j]) { temp=num[i]; num[i]=num[j]; num[j]=temp; } } } printf("Sorted in Ascending order\n"); for(i=0;i<5;i++) { printf("%d\n ",num[i]); } return 0; }
Lab-30 Q1: C program to input the age of 20 students and count the number of students having age in between 20 to 25.
#include<stdio.h> int main() { int age[20],i,count=0; for(i=0;i<20;i++) { printf("Enter age[%d] : ",i+1); scanf("%d", &age[i]); } for(i=0;i<20;i++) { if(age[i]>=20 && age[i]<=25) count++; } printf("Total number of students having age in between 20 to 25 is %d ", count); return 0; }
Lab-30 Q2: C program to input 'n' numbers and search whether it is in the list or not.[Program for sequential search].
#include<stdio.h> int main() { int n,num[100],i,searchnum; printf(" Enter array size not more than 100 : "); scanf("%d",&n); for(i=0;i<n;i++) { printf("Enter num[%d] :", i+1); scanf("%d", &num[i]); } printf("Enter a number to be searched "); scanf("%d",&searchnum); for(i=0;i<n;i++) { if(searchnum==num[i]) { printf("%d is in the list", searchnum); break; } } if(n==i) printf(" %d is not in the list", searchnum); return 0; }
Lab-31 Explain strlen(),strupr(),strlwr(),strcat(),strrev(),strcpy() with examples.
String Handling Functions

The C library supports a large number of string handling functions that can be used to carry out many of the string manipulation. The header file #include<string.h> is used for string manipulation functions. Some of the common string manipulation functions are:

1. strlen() function

The strlen() function returns the length of a string which takes the string name as an argument.

Syntax:
l=strlen(str);

where, str is the name of string and l is the length of the string, returned by function.

Example Program of strlen() function
#include<stdio.h> #include<string.h> main() { char str[100]; int l; printf(" Enter a string : "); scanf("%s",str); l=strlen(str); printf(" The length of string is %d ",l); }
2. strrev() function

The strrev() function is used to reverse the given string.

Syntax:
strrev(str);
where str is string to be reversed.
Example Program of strrev() function
#include<stdio.h> #include<string.h> main() { char str[100]; printf(" Enter a string : "); scanf("%s",str); strrev(str); printf(" Reverse of the given string is %s ",str); }
3. strcpy() function

The strcpy() function is used to copy one string into another string.

Syntax:
strcpy(destination,source)
or
strcpy(strl,str2);
where strl,str2 are two strings.
The content of string str2 is copied on string strl.
Example Program of strcpy() function
#include<stdio.h> #include<string.h> main() { char str1[100],str2[100]; printf(" Enter a string : "); scanf("%s",str1); strcpy(str2,str1); printf(" After copying the string is %s ",str2); }
4. strcat() function

The strcat() function is used to join or concatenate one string into another string.

Syntax:
strcat(strl,str2);

where str1,str2 are two strings. The content of string str2 is concatenated with string str1.

Example Program of strcat() function
#include<stdio.h> #include<string.h> main() { char str1[100],str2[100]; printf(" Enter First string : "); scanf("%s",str1); printf(" Enter Second string : "); scanf("%s",str2); strcat(str1,str2); printf(" After concatenating the two string is %s ",str1); }
5. strcmp( ) function

The strcmp() function is used to compare two strings, character by character and stops comparison when there is a difference in the ASCII value or the end of any one string and returns ASCII difference of the character that is integer.

Syntax:
v=strcmp(strl,str2);

Where, str1 and str2 are two strings to be compared and v is a value returned by the function which is either zero(equal), positive value(descending) or negative value(ascending).

Example Program of strcmp() function
#include<stdio.h> #include<string.h> main() { char str1[100],str2[100]; int v; printf(" Enter First string : "); scanf("%s",str1); printf(" Enter Second string : "); scanf("%s",str2); v=strcmp(str1,str2); if(v==0) printf(" Both strings are same. "); else if(v>0) printf(" %s comes after %s ",str1,str2); else printf(" %s comes before %s ",str1,str2); }
6. strlwr() function

The strlwr() function is used to convert upper case characters of string into lower case characters.

Syntax:
strlwr (str);
where str is string to be converted into lower case characters.
Example Program of strlwr() function
#include<stdio.h> #include<string.h> main() { char str[100]; printf(" Enter a string : "); scanf("%s",str); strlwr(str); printf(" The string in lowercase is %s ",str); }
7. strupr() function

The strupr() function is used to convert lower case characters of string into upper case characters.

Syntax:
strupr (str);
where str is string to be converted into upper case characters.
Example Program of strupr() function
#include<stdio.h> #include<string.h> main() { char str[100]; printf(" Enter a string : "); scanf("%s",str); strupr(str); printf(" The string in uppercase is %s ",str); }