Sei sulla pagina 1di 38

BMHSS, Pathari-2, Morang (Prepared by: TBB) C-Programming Language Grade-XII

C-Programming Language
Introduction Fundamental of C
It is a structured programming language, high speed and easy to understand so it is C - Character sets are letters, digits, and special characters and white spaces that form
also termed as middle level language or intermediate language between high level and low words, numbers and expressions.
level language. It is very powerful programming language because it is used to prepare The following character sets are used in C.
system software as well as application software. It has large numbers of vocabularies and Letters Digits
syntax to write program. It was developed by Dennis Ritchie in 1970, USA. Upper case A to Z 0 to 9
Why to use C? Lower case a to z
a. C is a powerful and flexible language so system software and application software Special characters
can be developed. , Coma . Period
b. C is a popular language preferred by professional as it has wide variety of c- ; Semicolon : Colon
compilers and accessories. ? Question mark ' Apostrophe
c. C is a portable language so can be compiled and run on another system too. " Quotation marks ! Exclamation mark
d. Those words which has special meaning to a language compiler (called keyword or | Vertical bar / Slash
reversed word), can be used to do any task while making program. \ Back slash ~ Tilde
e. C is a modular so subroutines can be reused in other applications or programs. _ Underscore $ Dollar sign
Features of C-Language % Percentage sign & Ampersand
It is a popular language because it has large number of features for programmer to ^ Caret * Asterisk
write medium types of program. Some features of it are given below: - Minus sign + Plus sign
a. It is highly portable language.
< Opening angle > Closing angle
b. It is procedural programming language because program is divided into number of
( Left parenthesis ) Right parenthesis
functions.
[ Left bracket ] Right bracket
c. It inherits or invisible functionality(memory management, CPU register
{ Left brace } Right brace
manipulation, etc.)
d. It is general purpose structured programming language. # Hash
e. It is internationally standardized programming language. White space
Advantages - Blank line - Horizontal Tab - Carriage Return - New line - Form feed
a. It is very easy for debugging, testing and maintaining.
b. It is portable so can be run in any computer. Comments:
c. It is fast for executing. Comments in code can be useful for various purpose such as for providing visual split
d. The statement in C-language are generally short but very powerful. between various parts of the code, gives information about the code that what is going to do,
e. It has only 32 keywords so that easy to remember. etc.
Disadvantages Comments can be written in two Forms:
C-language has not strong disadvantages but has some negligible disadvantages which //single line comments
are given below: /*multi-line comments*/
a. There is no runtime checking. Tokens:
b. As the program extends it is very difficult to fix the bug. Tokens are fundamental parts of C program from which other parts are formed. There are six
c. It may be compile time overhead due to the misplacing and excessive use of types of C tokens that are listed below:
pointers. a) Operators b) Keywords c) Identifiers d) Constants e) strings f) Special symbols
d. It does not use to develop complex type of software now a days. Keywords or reserved words
e. Object oriented programming concept is not included in C.
BMHSS, Pathari-2, Morang (Prepared by: TBB) C-Programming Language Grade-XII
C keeps a small set of keywords for its own use. These keywords cannot be used as Unsigned short integer Unsigned short 2 0 to 65535
identifiers in the program. It has special meaning in the program to a language compiler that int
are pre defined in C programming language. Unsigned long integer Unsigned long int4 0 to 4294967295
Auto Double Int Struct Float Float 4 1.2E-38 to
Break Else Long Switch Double Double 8 2.2e-308 to
Case Enum Register Typedef Long double Long double 10 3.4E-4932 to
Char Extern Return Union Secondary Data type
Const Float Short Unsigned a. Arrays b. Pointers c. Structure (we will study letter)
Continue For Signed Void
Default Goto Sizeof Volatile Variable
Do If Static While A variable is a location of a computer's memory used to store data value using unique
name and can be changed during program execution time. Any variable declared in a program
Identifiers should confirm to the following:
Identifiers are the names given to program unit such as variable, structure, etc. They a. They must always begin with a letter, although some systems permit underscore as
are not defined in programming language but are used to define by the programmer. Some the first character.
basic rules to define identifiers are given below: b. The length of a variable must not be more than 8 characters.
a. First character must be an alphabet underscore then digits or alphabets. c. White space is not allowed.
b. It must consist of only letters, digits and underscore. d. A variable should not be a keyword.
c. Any standard C language keyword cannot be used as identifier name. e. It should not contain any special characters.
d. It should not be contain a space. Valid Variables:
e. It allows both upper case and lower case characters. Sum1, numb, Salary, emp_name, average1, etc.
Valid Identifier: B1, first_name, lastname, etc. Invalid variables:
Invalid Identifier: 1A, int, void, sum-is, first name, etc. 123, (area), 6th , %ans, first name
Data Type in C Types of variable:
Data type can be represented in variety of ways in any programming language, so that 1. Numeric Variable: The variable which stores numeric data only called numeric variable.
we can store data in system. It determines what types of data should be stored. There are two It may be whole number or fractional number.
types of data type in C which are: Ex. Int Salary float average
a. Primary data type and 2. String variable: The variable which stores character data only is called string variable. It
b. Secondary data type. may be character or string.
Ex. Char name, char w
Primary data type Constant
It is the fundamental data types used to build other data types. The fundamental data A constant is the fixed values that do not change during the execution of a program. C
types are given below: supports four types of constants. They are character, string, integer and floating point
constants.
Variable type Keyword Bytes Range Types of constant are :
Character Char 1 -128 to 127 a. Character constant: Character constant is a single character which is enclosed
Unsigned character Unsigned char 1 0 to 255 with singe quotation mark. Eg. 'a' , 'A', '4' , etc.
Integer Int 2 -32768 to 32767 b. String constant: String constant of any number of consecutive characters
Short integer Short int 2 -32768 to 32767 enclosed in double quotation marks whose maximum length is 255 characters. Eg.
Long integer Long int 4 -2147483648 to .647 "it's very easy language", "2*3/5(55-2)", "3400", etc.
Unsigned integer Unsigned int 2 0 to 65535
BMHSS, Pathari-2, Morang (Prepared by: TBB) C-Programming Language Grade-XII
c. Integer constant: An integer constant refers to a sequence of digits that will be }
either positive or negative. There are three types of integers namely, decimal, octal c. Control statement
and hexadecimal. A control statements are used to create special program features such as logical tests,
i. Decimal integer constant: A decimal constant may have the combination of the loops, branches and it also contains other statements.
digits from 0 to 9 and ve or +ve sign. Eg. 123, -56, 0, etc. Example:
ii. Octal integer constant: An octal integer constant can consist of the For(i=1;i<=100;i++)
combination of the digits 0 to 7 with a leading 0. eg. 0, 01, 0675, etc. Operator
iii. Hexadecimal integer constant: A hexadecimal integer should begin with 0x An operator is a symbol which is used to do mathematical or logical operation on data and
and it contains the digits from 0 to 9 and an alphabet A to F. eg. 0x2, 0xgf, etc. variables. C has a rich set of operators which can be classified as follows:
d. Floating point constant: A floating point constant is a base 10 number that a. Arithmetic Operators
contains either a decimal point or an exponent or both. 0, 33, 1.345E+4, 4.22e78, Those symbols which are used in C Language to perform simple arithmetic operation
etc. such as addition, subtraction, multiplication, division, etc. is called arithmetic operators.
` It uses two operands so that is called binary operator also.
Escape sequence Operator Meaning Example
The non printing characters started with slash ( \ ) which uses special characters with output + addition a+b 2+3=5
functions. These are listed below: - subtraction a-b 5-1=4
Escape sequenceMeaning * multiplication a*b 2*3=6
\a Audible alert / division a/b 6/2=3
\b Backspace % modulus a%b 3/2 = 1
\n New line b. Relational operator
\t Horizontal tab It is required to compare the relationship between operands and bring out a decision
\v Vertical tab and program accordingly. It is also called comparison operator because it is used to
\' Single quote compare any two expressions. C supports the following relational operator:
\" Double quote Operator Meaning Examples
\? Question mark < less than a<b
Statement <= is less than or equal to a<=b
Statement causes the computer to carry out some action as per given instructions. It is > greater than a>b
terminated by semicolon ( ; ). The types of statement are given below: >= is greater than or equal to a>=b
a. Expression statement == is equal to a= =b
It consists of an expression followed by a semicolon only. != is not equal to a!=b
Example: c. Logical operator
B = 5; Logical operators are used to give logical value either true of false. C has following
C = d+e; logical operators:
Printf(Enter a number"); Operator Meaning Examples
b. Compound statement && Logical And (a>b)&&(a>c)
A compound statement consists of several individual statements enclosed with a pair of || Logical Or (a>b)||(a>c)
braces { }. It provides capability for embedding statements with in other statements. ! Logical NOT !(a==b)
Example: Logical AND (&&)
{ This operator is used to operate 2 condition or expression with relational operators if
A = l*b both expression are true then displays true otherwise false.
Area = 3.14*r*r Example: a>b && x ==10
BMHSS, Pathari-2, Morang (Prepared by: TBB) C-Programming Language Grade-XII
Logical OR (||) m = 5;
the logical OR is used to combine 2 expression or the condition evaluates to true if any y = m++;
one of the 2 expression is true. here, first ' m ' is assign to y so y = 5 and m = m + 1 =5 +1=6
Example: a<m || a<n Relational Operator
Logical NOT ( ! ) The operator which compare two quantities and depending on their relation and take certain
The logical not operator takes single expression and evaluates to true if the expression decisions.
is false and evaluates to false if the expression is true. These comparisons can be done with the help of relational operators. Example: <, <=, >, >=,
Example: ! (x>=y) ==, != (not equals to).
Assignment Operator Special Operator
It is used to evaluate t an expression on the right of the expression and substitutes it to a. Comma operator: It is used to separate expressions when multiple expressions are
the value or variable on the left of the expression. It is used to assign value of an used in one statement. It always is evaluated left to right and value of the left expression is
expression into variable. descarded.
variable = expression Example: j = (p = 3, p+2); ( where p+2 = 3+2=5 and p is discarded.
Example: x = a+b b. Size of Operatior: It gives the byte size of data type or variable.
Shorthand operator example:
It is the combination of arithmetic and assignment so it first performs arithmetic and int sum;
then only assignment operation. m = sizeof(sum);
Operators Statement with Statement with n = sizeof(int);
shorthand operator assignment operator c. Pointer operator: * operator is also used for multiplication but when used with a
+= a+=1 a = a+1 pointer variable, It is used to get the content of the address pointed by a pointer variable
-= a-=1 a=a1 to a particular memory location.
*= a*1 a = a*1 Format Specifiers in C
/= a/=b a = a/b It tells what type of the data is being processed. The syntax of format specifier is as
%= a%=b a=a%b follows:
Unary Operator %c - specify a character %d - specify a decimal integer
The increment and decrement operators are one of the unary operations which are very %s - specify a string %f - specify a float
useful in C language. The are extensively used in For and While loops. These format specifier is used with printf ( ) and scanf( ).
Syntax: scanf("%d", &a ); asks the user to input an integer and stores it in a.
++ variable name (prefix) printf("the sum of %d and %d is %d",a ,b, sum);
variable name ++ (postfix)
- - variable name (prefix) Basic structure of C programs:
variable name - - (postfix) Documentation section
The increment operator + + adds the value 1 to the current value of operand and Header file inclusion section
decrement operator - - subtract 1 to the current value of operand. Global declaration section of user defined function
Example: main ( ) function section
suppose {
m = 5; .......................................
y = ++m; .......................................
in this case y = m + 1 = 5+1 = 6 }
m=m+1=5+1=6 Sub program section
again if we rewrite the above as function 1
BMHSS, Pathari-2, Morang (Prepared by: TBB) C-Programming Language Grade-XII
function 2
...................................
...................................
function n

Syntax for the header file:


#include <header file> (where # include is a pre processor statement.
Header files uses
stdio.h printf(), scanf(), getchar(), putchar(), gets(), puts(), getc(), putc(),
fopen(), fclose(), feof()
conio.h clrscr(), getch()
ctype.h toupper(), tolower(), isalpha()
math.h pow(), sqrt(), cost(), tan(), sin(), log()
stdlib.h rand(), srand(), exit()
string.h strlen(), strcpy(), strcmp(), strcat(), strlwr(), strupr(), strrev()
Functions:
Formatted I/O
a. Printf(): it is widely used output function. The text which is needed to be shown as
output in the screen is shown by the help of printf() function.
syntax:
printf(control string, arg1, arg2, ..............);
ex. printf("hellow, friend");
printf("\nThe total amount =Rs. %f",ta);
printf("enter any number");
b. Scanf(): It is the most frequently used input function.
syntax:
scanf(control string, arg1, arg2, ................);
Example:
printf("enter any number");
scanf("%d", &a);

printf("enter any character and string");


scanf("%c %s", &w, wo);
Unformatted I/O
a. getchar(): it accept a character only as an input at a time.
b. putchar(): it displays just a character on the screen.
c. gets(): it accepts a string from the user which may have space in between.
d. puts(): it displays string on the output device.
BMHSS, Pathari-2, Morang (Prepared by: TBB) C-Programming Language Grade-XII
C-Programming solved problems scanf("%d %d", &m2, &c2);
/*1. Write a C program to read number of items, rate and calculate total amount.*/ c=(c1+c2)%100;
#include<stdio.h> m=m1+m2 +(c1+c2)/100;
#include<conio.h> printf("\nSum of the two distances is %d m and %d cm", m,c);
main() getch();
{ }
int noi; /*4. Write a C program to add two distances in feet and inches.*/
float r,ta; #include<stdio.h>
printf("\nEnter the number of items: "); #include<conio.h>
scanf("%d",&noi); main()
printf("\nEnter the rate of an item: "); {
scanf("%f",&r); int f1, i1, f2, i2, f, i;
ta=noi*r; printf("\nEnter first distance in feet and inches : ");
printf("\nThe total amount =Rs. %f",ta); scanf("%d %d", &f1, &i1);
getch(); printf("\nEnter second distance in feet and inches : ");
} scanf("%d %d", &f2, &i2);
/*2. Write a C program to convert days into year, months and remaining days.*/ i=(i1+i2)%12;
#include<stdio.h> f=f1+f2 +(i1+i2)/12;
#include<conio.h> printf("\nSum of the two distances %d ft %d in and %d ft %d in is %d feet and %d inches",
main() f1,i1,f2,i2,f,i);
{ getch();
int days, y, m, rd; }
printf("\nEnter the number of days: "); /* 5. Write a C program to add two times in hour, minute and second.*/
scanf("%d",&days); #include<stdio.h>
y = days / 365; #include<conio.h>
rd = days %365; main()
m = rd / 30; {
rd = rd % 30; int h1, m1, s1, h2, m2, s2, h, m, s;
printf("\nThe %d days converted into %d year %d months and %d remaining printf("\nEnter first time in hour, minute and second : ");
days",days, y, m, rd); scanf("%d %d %d", &h1,&m1, &s1);
getch(); printf("\nEnter second time in hour, minute and second : ");
} scanf("%d %d %d", &h2,&m2, &s2);
/*3. Write a C program to add two distances meter and centimeter.*/ s = (s1+s2)%60;
#include<stdio.h> m = (m1+m2 + (s1+s2)/60)% 60;
#include<conio.h> h = h1+h2+ (m1+m2 + (s1+s2)/60)/ 60;
main() printf("\nThe sum of two times is %d hr %d min and %d sec", h,m,s);
{ getch();
int m1, c1, m2, c2, m, c; }
printf("\nEnter first distance in meter and centimeter : "); /* 6. To input a positive integer and find it is a perfect cube number or not.*/
scanf("%d %d", &m1, &c1); #include<stdio.h>
printf("\nEnter second distance in meter and centimeter : "); #include<conio.h>
BMHSS, Pathari-2, Morang (Prepared by: TBB) C-Programming Language Grade-XII
main() {
{ int i, n, m, a,b,c,d, s=0;
int n, i; printf("\nEnter any 5 digit number (or UPTO 5 digits): ");
printf("\nEnter any number to check perfect cube or not : "); scanf("%d", &n);
scanf("%d",&n); m = n;
for(i=1;i<n;i++) a = n % 10; //extract last number of 54879 i.e. 9
{ n = n / 10; //extract number except last number 9 i.e. 5487
if(i*i*i == n) b = n % 10; // extract last number of 5487 i.e. 7
{ n = n / 10; //extract number except last number 7 i.e. 548
printf("\n %d is perfect cube number",n); c = n % 10; // extract last number of 548 i.e. 8
break; n = n / 10; //extract number except last number 8 i.e. 54
} d = n % 10; //extract last number of 54 i.e. 4
} n = n / 10; //extract number except last number 4 i.e. 5
if(i==n) s = a+b+c+d+n;
printf("\n%d number is not perfect cuber number",n); printf("\nThe sum of digits of %d is %d",m,s);
getch(); getch();
} }
/* 7. Write a C program to calculate simple interest and compound interest.*/ /* 9. Write a C program to read a number and display it equivalence in octal and
#include<stdio.h> hexadecimal number.*/
#include<math.h> #include<stdio.h>
#include<conio.h> #include<conio.h>
main() main()
{ {
float p,t,r,si,ci; int n, o, h;
printf("\nEnter principal: "); printf("\nEnter any decimal number : ");
scanf("%f",&p); scanf("%d", &n);
printf("\nEnter time: "); printf("\nOctal of %d is %o", n,n);
scanf("%f",&t); printf("\nHexadecimal of %d is %X",n,n);
printf("\nEnter rate of interest: "); getch();
scanf("%f",&r); }
si = p*t*r/100; /* 10. Write a C program to read a number and check it is Armstrong number or not.
ci = p*(pow(1+r/100,t)-1); [Armstrong number 153 = 13+53+33]*/
printf("\nSimple interest is %.2f",si); #include<stdio.h>
printf("\nCompound interest is %.2f",ci); #include<conio.h>
getch(); main()
} {
/* 8. WAP to read a 5 digit number and find sum of individual digits. i.e. int n, m, b, c, a = 0;
54879=5+4+8+7+9=33.*/ printf("\nEnter any three digit number : ");
#include<stdio.h> scanf("%d", &n);
#include<conio.h> m = n;
main() b = n % 10; //extracting last number from 153 digit number i.e. 3
BMHSS, Pathari-2, Morang (Prepared by: TBB) C-Programming Language Grade-XII
n = n / 10; //extracting number except last digit 3 i.e. 15 else
c = n % 10; // extracting last number from 15 i.e. 5 printf("\nThe inputed word %s is not palindrome", word);
n = n / 10; //extracting number except last number 5 i.e. 1 getch();
a = b*b*b +c*c*c + n*n*n; }
if(a == m) /* 13. Write a C program to check whether user input number is prime or not?*/
printf("\nThe number %d is Armstrong number",m); #include<stdio.h>
else #include<conio.h>
printf("\n The number %d is not Armstrong number",m); main()
getch(); {
} int i, n;
/* 11. Write a program to read a four digit number and display it in reverse order.*/ printf("\nEnter any number : ");
#include<stdio.h> scanf("%d",&n);
#include<conio.h> for(i=2;i<n;i++)
main() {
{ if(n%i==0)
int n, m, b, c, r = 0; {
printf("\nEnter any three digit number : "); printf("\nThe number %d is not prime",n);
scanf("%d", &n); break;
m = n; }
b = n % 10; //extracting last number from 153 digit number i.e. 3 }
n = n / 10; //extracting number except last digit 3 i.e. 15 if(i==n)
c = n % 10; // extracting last number from 15 i.e. 5 printf("\nThe number %d is prime",n);
n = n / 10; //extracting number except last number 5 i.e. 1 getch();
r = b*100+c*10+n; }
printf("\nThe reverse number of %d is %03d ", m, r);//%03 is used to padded zeros for making /*14. Write a C program to read two numbers sum them and display result by using
three digits. pointer.*/
getch(); #include<stdio.h>
} #include<conio.h>
/* 13. Write a C program to check whether user input number is prime or not?*/ main()
#include<stdio.h> {
#include<conio.h> int a, b, *p, *q, sum = 0;
main() printf("\nEnter two numbers: ");
{ scanf("%d %d", &a,&b);
int number, reverse; p = &a;
printf("\nEnter any three digit number : "); q = &b;
scanf("%d",&number); sum = *p + *q;
a = number % 10 ; printf("\nThe sum is %d",sum);
number = number / 10; getch();
b = number % }
if(i == 0)
printf("\nThe inputed word %s is palindrome", word);
BMHSS, Pathari-2, Morang (Prepared by: TBB) C-Programming Language Grade-XII
/*1. For any integer input through the keyboard, write a C program to find out whether it is printf("\n\n");
an odd number or even number.*/ }
#include<stdio.h> getch();
#include<conio.h> return(0);
main() }
{ /*3. Write a program to input any three different numbers and find the middle value.*/
int n,r; #include<stdio.h>
printf("Enter a number "); #include<conio.h>
scanf("%d",&n); #include<process.h>
r=n%2; main()
if(r==0) {
{ int n1,n2,n3;
printf("\n%d is even number",n); printf("Enter first number ");
} scanf("%d",&n1);
else printf("Enter second number ");
{ scanf("%d",&n2);
printf("\n%d is odd number",n); printf("Enter third number ");
} scanf("%d",&n3);
getch(); if(n1==n2 || n1==n3||n2==n3)
return(0); {
} printf("\nPlease, enter the different numbers ");
/*2. Write a program to print the following pattern. getch();
1 //exit(0);
2 2 }
3 3 3 if((n1>n2 && n1<n3)||(n1<n2&&n1>n3))
4 4 4 4 {
5 5 5 5 5 printf("\n%d is middle number",n1);
*/ }
#include<stdio.h> else if((n2>n1&& n2<n3)||(n2<n1&&n2>n3))
#include<conio.h> {
main() printf("\n%d is middle number",n2);
{ }
int a,b; else
printf("the required pattern is "); {
printf("\n"); printf("\n%d is middle number",n3);
for(a=1;a<=5;a++) }
{ getch();
for(b=1;b<=a;b++) return(0);
{ }
printf("%d\t",a);
}
BMHSS, Pathari-2, Morang (Prepared by: TBB) C-Programming Language Grade-XII
/*4. Write a C program to print the following series of cube numbers up to nth term. 1, 8, /*6. Write a C program to print Fibonacci series upto N terms. [Fibonacci series: 1, 1, 2, 3,
27, */ 5, 8, 13, ]*/
#include<stdio.h> #include<stdio.h>
#include<conio.h> #include<conio.h>
main() main()
{ {
int n,i,c; int a,b,f,n,i;
printf("Enter the 'n' th term of the series "); printf("Enter the value of 'n' th term : ");
scanf("%d",&n); scanf("%d",&n);
printf("\nthe required series is "); printf("\n The fibonacci series up to %d th term is :\n\n ",n);
for(i=1;i<=n;i++) a = 0;
{ b = 1;
c=i*i*i; f = 1;
printf("%d\t",c); for(i=1;i<=n;i++)
} {
getch(); printf("%d\t",f);
return(0); f = a + b;
} a=b;
/*5. Write a C program to print multiplication table up to N.*/ b=f;
#include<stdio.h> }
#include<conio.h> getch();
main() }
{ /*7. Write an algorithm to input a number and print it is positive or negative number.
int n,i,j,m; Then, Convert the algorithm into C program code.*/
printf("Enter the last number for the series of multiplication table "); #include<stdio.h>
scanf("%d",&n); #include<conio.h>
main()
for(i=1;i<=n;i++) {
{ int n,r;
printf("\nThe multiplication table of %d is "); printf("Enter a number ");
printf("\n\n"); scanf("%d",&n);
for(j=1;j<=10;j++) if(n>0)
{ {
m=j*i; printf("\n%d is positive number",n);
printf("\n%d * %d =%d",i,j,m); }
} else
} {
getch(); printf("\n%d is negative number",n);
return(0); }
} getch();
return(0);
BMHSS, Pathari-2, Morang (Prepared by: TBB) C-Programming Language Grade-XII
} printf("Enter second number ");
/*9. Write an algorithm to input a number and find whether it is exactly divisible by both 5 scanf("%d",&n2);
and 8 or not. Convert the algorithm into C program code.*/ printf("Enter third number ");
#include<stdio.h> scanf("%d",&n3);
#include<conio.h> if(n1==n2 || n1==n3||n2==n3)
main() {
{ printf("\nPlease, enter the different numbers ");
int n,r1,r2; getch();
printf("Enter a number "); //exit(0);
scanf("%d",&n); }
r1=n%5; s=smallest(n1,n2,n3);
r2=n%8; printf("\nThe smallest number=%d",s);
if(r1==0&&r2==0) getch();
{ return(0);
printf("\n%d is exactly divisible by both 5 and 8",n); }
} int smallest(int n1,int n2,int n3)
else if(r1==0) {
{ int s;
printf("\n%d is exactly divisible by 5 only but not by 8",n); if(n1<n2 && n1<n3)
} {
else if(r2==0) s=n1;
{ }
printf("\n%d is exactly divisible by 8 only but not by 5",n); else if(n2<n1&& n2<n3)
} {
else s=n2;
{ }
printf("\n%d is not divisible by 5 and 8 both",n); else
} {
getch(); s=n3;
return(0); }
} return(s);
/*10. Write a program to input any 3 numbers and find smallest number by using function.*/ }
#include<stdio.h> /*11. Write an algorithm and C program to read a number and find is it exactly divisible by
#include<conio.h> 3 and 5 or not?*/
#include<process.h> #include<stdio.h>
int smallest(int,int,int); #include<conio.h>
main() main()
{ {
int n1,n2,n3,s; int n,r1,r2;
printf("Enter first number "); printf("Enter a number ");
scanf("%d",&n1); scanf("%d",&n);
BMHSS, Pathari-2, Morang (Prepared by: TBB) C-Programming Language Grade-XII
r1=n%3; printf("%d\t",a);
r2=n%5; }
if(r1==0&&r2==0) }
{ getch();
printf("\n%d is exactly divisible by both 3 and 5",n); }
} /*13. Write a program to read two numbers and find the bigger number by using function.*/
else if(r1==0) #include<stdio.h>
{ #include<conio.h>
printf("\n%d is exactly divisible by 3 only but not by 5",n); int bigger(int,int);
} main()
else if(r2==0) {
{ int n1,n2,big;
printf("\n%d is exactly divisible by 5 only but not by 3",n); printf("Enter the first number ");
} scanf("%d",&n1);
else printf("Enter the second number ");
{ scanf("%d",&n2);
printf("\n%d is not divisible by 3 and 5 both",n); big=bigger(n1,n2);
} printf("\nthe bigger number=%d",big);
getch(); getch();
return(0); return(0);
} }
/*12. Write a C program to print all Armstrong number form 1 to 1000*/ int bigger(int a,int b)
#include<stdio.h> {
#include<conio.h> if(a>b)
main() {
{ return(a);
int i,n,a,x,r; }
printf("\n the series of armstrong numbers upto 1000 are "); else
for(i=1;i<=1000;i++) {
{ return(b);
n=i; }
a=i; }
x=0; /*14. Write a program to input the cost price and selling price and calculate the profit or
while(n>0) loss.*/
{ #include<stdio.h>
r=n%10; #include<conio.h>
x=x+r*r*r; main()
n=n/10; {
} float cp,sp,amt;
if(x==a) printf("Enter the cost price of the item ");
{ scanf("%f",&cp);
BMHSS, Pathari-2, Morang (Prepared by: TBB) C-Programming Language Grade-XII
printf("Enter the sales price of the item "); printf("\nApril");
scanf("%f",&sp); break;
if(sp>cp) case 5:
{ printf("\nMay");
amt=sp-cp; break;
printf("\n The profit amount=Rs.%.2f",amt); case 6:
} printf("\nJune");
else if(cp>sp) break;
{ case 7:
amt=cp-sp; printf("\nJuly");
printf("\n The loss amount=Rs.%.2f",amt); break;
} case 8:
else printf("\nAugust");
{ break;
printf("\n There is neither profit nor loss"); case 9:
} printf("\nSemptember");
getch(); break;
return(0); case 10:
} printf("\nOctober");
break;
/*1. Write a C program to read any number and display 1 for January, 2 for February case 11:
.., 12 for December and other for 'wrong input'.*/ printf("\nNovember");
#include<stdio.h> break;
#include<conio.h> case 12:
main() printf("\nDecember");
{ break;
int a; default:
printf("\nEnter a number between 1 to 12 "); printf("\n\nyou entered invalid number. Please enter a number
scanf("%d",&a); between 1 to 12 ");
switch(a) }
{ getch();
case 1: }
printf("\nJanuary"); /*2. The marks obtained by a student in 7 different subjects are entered through the
break; keyboard. The student gets a division as per the following rules: [10]
case 2: Percentage greater or equal to 60 First Division
printf("\nFebruary"); Percentage between 45 and 59 Second Division
break; Percentage between 35 and 44 Third Division
case 3: Percentage less than 35 Fail
printf("\nMarch"); Marks less than 35 in a subject will be declared as Fail
break; */
case 4:
BMHSS, Pathari-2, Morang (Prepared by: TBB) C-Programming Language Grade-XII
#include<stdio.h> {
#include<conio.h> strcpy(div,"Second Division");
#include<string.h> }
main() else if(percent>=35)
{ {
int s1,s2,s3,s4,s5,s6,s7,total; strcpy(div,"Third Division");
float percent; }
char res[10],div[30]; else
printf("\nEnter the marks of first subject "); {
scanf("%d",&s1); strcpy(div,"Fail");
printf("\nEnter the marks of second subject "); }
scanf("%d",&s2); //code to display total, percent, result and division
printf("\nEnter the marks of third subject "); printf("\nTotal marks=%ld",total);
scanf("%d",&s3); printf("\n Percentage =%.2f",percent);
printf("\nEnter the marks of fourth subject "); printf("\n Result= %s",res);
scanf("%d",&s4); printf("\n Division=%s",div);
printf("\nEnter the marks of fifth subject "); getch();
scanf("%d",&s5); }
printf("\nEnter the marks of sixth subject ");
scanf("%d",&s6); /*3. Write a C program to print all Armstrong numbers upto 1000.*/
printf("\nEnter the marks of seventh subject "); #include<stdio.h>
scanf("%d",&s7); #include<conio.h>
//code to find total main()
total=s1+s2+s3+s4+s5+s6+s7; {
//code to find percent and result int a,n,r,x;
if(s1>=35&&s2>=35&&s3>=35&&s4>=35&&s5>=35 &&s6>=35&&s7>=35) printf("The Armstrong number upto 1000 are ");
{ for(a=1;a<=1000;a++)
percent=(float)total*100/700; {
strcpy(res,"Pass"); n=a;
} x=0;
else while(n>0)
{ {
percent=0; r=n%10;
strcpy(res,"Fail"); x=x+r*r*r;
} n=n/10;
//code to find division }
if(percent>=60) if(x==a)
{ {
strcpy(div,"First Division"); printf("%d\t",a);
} }
else if(percent>=45) }
BMHSS, Pathari-2, Morang (Prepared by: TBB) C-Programming Language Grade-XII
getch(); #include<conio.h>
} main()
/*4. Write a program to find out whether it is an odd number or even number. (HSEB {
2062,2068)*/ char msg[100], encod[100], decod[100];
#include<stdio.h> int i;
#include<conio.h> puts("Enter message to encode : ");
main() gets(msg);
{ for(i=0;msg[i]!='\0';i++)
int n,r; {
printf("\nEnter a number "); encod[i] = msg[i] - 10; //any value can be placed.
scanf("%d",&n); }
r=n%2; encod[i] = '\0';
if(r==0) printf("\nEncoded message is :\n");
{ puts(encod);
printf("\n%d is even number",n); for(i=0;encod[i] !='\0';i++)
} {
else decod[i] = encod[i] + 10;//same value should be placed.
{ }
printf("\n%d is odd number",n); printf("\nDecoded message is :\n");
} puts(decod);
getch(); getch();
} }
/*5. Write a C program to show the number in power of 2 up to N.*/ /*7. Write a program to count the number of words in a text.*/
#include<stdio.h> #include<stdio.h>
#include<math.h> #include<conio.h>
#include<conio.h> main()
main() {
{ char msg[100];
long int i, n; int i, w = 0;
long int ans; puts("Enter one paragraph to count words :\n\n ");
printf("\nEnter how many terms : "); gets(msg);
scanf("%d",&n); for(i=0;msg[i]!='\0';i++)
for(i=0;i<=n;i++) {
{ if(msg[i] == ' ')//checks spaces for counting words.
ans = pow(2,i); w ++;
printf("\nThe %d times power of 2 is %ld",i,ans); }
} printf("\nThere are %d words",w+1);//the last word do not have space at last hence +1.
getch(); getch();
} }
/*6. Write a program to encode user input text and decode them.*/ /*8. Write a C program to find the GCD of 4 integers.*/
#include<stdio.h> #include<stdio.h>
BMHSS, Pathari-2, Morang (Prepared by: TBB) C-Programming Language Grade-XII
#include<conio.h> printf("%d, ",a);
main() a = a + 4;
{ }
int i, a, b, c, d; getch();
printf("\nEnter any four numbers : "); }
scanf("%d %d %d %d", &a, &b, &c, &d); /*11. Write a C program to find a peculiar two digit number which is three times the sum of
for(i=a;i>0;i--) its digits.*/
{ #include<stdio.h>
if(a%i == 0 && b % i == 0 && c % i == 0 && d % i == 0) #include<conio.h>
{ main()
printf("\nThe GCD of %d %d %d and %d is %d", a, b, c, d, i); {
break; int x,y,z=0, i;
} for(i=10;i<=99;i++)
} {
getch(); x=i/10;
} y=i%10;
/*9. Write a C program to display the sum of 'n' terms of even numbers.*/ z=3*(x+y);
#include<stdio.h> if(i==z)
#include<conio.h> printf("\nThe peculiar two digit number is : %d\t",i);
main() }
{ getch();
int i, n, evnsum=0; }
printf("\nEnter how many terms : "); /*12. Write a program to input an integer number and checks whether it is prime or not.*/
scanf("%d", &n); #include<stdio.h>
for(i=0;i<=n;i+=2) #include<conio.h>
{ main()
evnsum += i; {
} int n,r,i;
printf("\nThe sum of even numbers upto %d is %d", n, evnsum ); printf("\n Enter a number ");
getch(); scanf("%d",&n);
} for(i=2;i<n;i++)
/*10. Write a C program to print first 10 terms of the following series using FOR loop 1, 5, {
9, 13 */ if(n%i==0)
#include<stdio.h> {
#include<conio.h> printf("%d is not prime",n);
main() break;
{ }
int i, a=1; }
printf("\nThe first 10 terms of the series is : "); if(i==n)
for(i=0;i<10;i++) printf("\n%d is prime",n);
{ getch();
BMHSS, Pathari-2, Morang (Prepared by: TBB) C-Programming Language Grade-XII
} /*1) Write a program to input any 10 numbers in an array and find the total.*/
/*13. A number of "Cats" got together and decided to kill between them 999919 mice. #include<stdio.h>
Every cat killed equal number of "mice". Write a program to find number of cats.*/ #include<conio.h>
#include<stdio.h> main()
#include<conio.h> {
main() int num[10],i,t;
{ printf("\nEnter any 10 numbers ");
int mice=999919,cats; for(i=0;i<10;i++)
printf("\nPossible combinations of\n\nCats\t\tMice\n\n "); {
for(cats=2;cats<=999919;cats++) scanf("%d",&num[i]);
{ }
if(mice%cats==0) printf("\n The numbers are ");
{ for(i=0;i<10;i++)
printf("%d\t\t%d\n",cats, mice); {
} printf("%d ",num[i]);
} }
getch(); t=0;
} for(i=0;i<10;i++)
{
/*14. There are some goats and ducks in a farm. There are 60 eyes and 86 foot in total. t=t+num[i];
Write a program to find number of goats and ducks in the farm.*/ }
#include<stdio.h> printf("\n The total=%d",t);
#include<conio.h> getch();
main() }
{ /*2) Write a program to input the marks of any 20 students in Computer Science and find
int eyes = 60, legs = 86, ducks, ships, animals; the average marks.*/
animals = eyes /2; //every has two eyes i.e. 30 total #include<stdio.h>
printf("\nThe possible combinations is: "); #include<conio.h>
for(ducks = 0; ducks<=30; ducks++) main()
{ {
for(ships =0; ships <=30;ships++) int marks[20],i,t;
{ float avg;
if(ducks*2+ships*4 == 86 && (ducks+ships)*2 == 60) printf("\nEnter the marks of any 20 students in computer science ");
{ for(i=0;i<20;i++)
printf("\nducks = %d and ships = %d having %d eyes and %d foot", ducks, ships, {
(ducks+ships)*2, ducks*2+ships*4); scanf("%d",&marks[i]);
} }
} printf("\n The marks are ");
} for(i=0;i<20;i++)
getch(); {
} printf("%d ",marks[i]);
BMHSS, Pathari-2, Morang (Prepared by: TBB) C-Programming Language Grade-XII
} #define N 1000
t=0; main()
for(i=0;i<20;i++) {
{ int age[N],i,t,n;
t=t+marks[i]; float avg;
} printf("Enter the value of 'n' i.e. number of students ");
avg=t/20; scanf("%d",&n);
printf("\n The average marks in computer science=%f",avg); printf("\nEnter the age of the students ");
getch(); for(i=0;i<n;i++)
} {
/*3) Write a program to input the monthly profit of a bank in a year and find the total and scanf("%d",&age[i]);
monthly average profit of the bank in that year.*/ }
#include<stdio.h> printf("\n The age are ");
#include<conio.h> for(i=0;i<n;i++)
main() {
{ printf("%d ",age[i]);
float profit[12],t,avg; }
int i; t=0;
printf("\nEnter the monthly profit of a bank in a year "); for(i=0;i<n;i++)
for(i=0;i<12;i++) {
{ t=t+age[i];
scanf("%f",&profit[i]); }
} avg=t/n;
printf("\n The monthly profit amount "); printf("\n The average age of students=%f",avg);
for(i=0;i<12;i++) getch();
{ }
printf("%f ",profit[i]); /*5) Write a program to input any 50 numbers in an array and find the sum of positive
} numbers only.*/
t=0; #include<stdio.h>
for(i=0;i<12;i++) #include<conio.h>
{ main()
t=t+profit[i]; {
} int num[50],i,t;
avg=t/12; printf("\nEnter any 50 numbers ");
printf("\n The average profit of a bank in a year=%f",avg); for(i=0;i<50;i++)
getch(); {
} scanf("%d",&num[i]);
/*4) Write a program to input the age of n numbers of students in a class and find the }
average age of the students in that class.*/ printf("\n The numbers are ");
#include<stdio.h> for(i=0;i<50;i++)
#include<conio.h> {
BMHSS, Pathari-2, Morang (Prepared by: TBB) C-Programming Language Grade-XII
printf("%d ",num[i]); /*7) Write a program to input any 50 numbers in an array and find the sum of odd
} numbers only.*/
t=0; #include<stdio.h>
for(i=0;i<50;i++) #include<conio.h>
{ main()
if(num[i]>0) {
{ int num[50],i,t;
t=t+num[i]; printf("\nEnter any 50 numbers ");
} for(i=0;i<50;i++)
} {
printf("\n The sum of positive numbers=%d",t); scanf("%d",&num[i]);
getch(); }
} printf("\n The numbers are ");
/*6) Write a program to input any 50 numbers in an array and find the sum of even for(i=0;i<50;i++)
numbers only.*/ {
#include<stdio.h> printf("%d ",num[i]);
#include<conio.h> }
main() t=0;
{ for(i=0;i<50;i++)
int num[50],i,t; {
printf("\nEnter any 50 numbers "); if(num[i]%2==1)
for(i=0;i<50;i++) {
{ t=t+num[i];
scanf("%d",&num[i]); }
} }
printf("\n The numbers are "); printf("\n The sum of odd numbers only=%d",t);
for(i=0;i<50;i++) getch();
{ }
printf("%d ",num[i]); /*8) Write a program to input any 10 numbers and count how many numbers are
} positive.*/
t=0; #include<stdio.h>
for(i=0;i<50;i++) #include<conio.h>
{ main()
if(num[i]%2==0) {
{ int num[10],i,c;
t=t+num[i]; printf("\nEnter any 10 numbers ");
} for(i=0;i<10;i++)
} {
printf("\n The sum of even numbers only=%d",t); scanf("%d",&num[i]);
getch(); }
} printf("\n The numbers are ");
BMHSS, Pathari-2, Morang (Prepared by: TBB) C-Programming Language Grade-XII
for(i=0;i<10;i++) getch();
{ }
printf("%d ",num[i]); /*10) Write a program to input the marks of any 50 students in a subject and count how
} many students got above the average marks.*/
c=0; #include<stdio.h>
for(i=0;i<10;i++) #include<conio.h>
{ main()
if(num[i]>0) {
{ int marks[50],i,c,t;
c++; float avg;
} printf("\nEnter marks of any 50 students ");
} for(i=0;i<50;i++)
printf("\n The no. of positive numbers =%d",c); {
getch(); scanf("%d",&marks[i]);
} }
/*9)Write a program to input the marks of any 100 students in computer science and count printf("\n The marks are ");
how many students got marks in the range of 70 to 90.*/ for(i=0;i<50;i++)
#include<stdio.h> {
#include<conio.h> printf("%d ",marks[i]);
main() }
{ t=0;
int marks[100],i,c; for(i=0;i<50;i++)
printf("\nEnter the marks 100 students "); {
for(i=0;i<100;i++) t=t+marks[i];
{ }
scanf("%d",&marks[i]); avg=t/50;
} c=0;
printf("\n The marks are "); for(i=0;i<50;i++)
for(i=0;i<100;i++) {
{ if(marks[i]>avg)
printf("%d ",marks[i]); {
} c++;
c=0; }
for(i=0;i<100;i++) }
{ printf("\n The no. of students who got above average marks =%d",c);
if(marks[i]>=70 && marks[i]<=90) getch();
{ }
c++; /*11) Write a program to input marks of any 40 students in a subjects and count how many
} students are passed in Distinction, First Division, Second Division, Third Division and Failed.
} Marks Division
printf("\n The no. of students who got marks in the range of 70 to 90 are=%d",c); >=75 Distinction
BMHSS, Pathari-2, Morang (Prepared by: TBB) C-Programming Language Grade-XII
>=60 First c5++;
>=45 Second }
>=35 Third
Below 35 Fail }
*/ printf("\n The no. of students passed in Distinction=%d",c1);
#include<stdio.h> printf("\n The no. of students passed in First Division=%d",c2);
#include<conio.h> printf("\n The no. of students passed in Second Division=%d",c3);
main() printf("\n The no. of students passed in third Division=%d",c4);
{ printf("\n The no. of students who got failed=%d",c5);
int marks[40],i,c1,c2,c3,c4,c5; getch();
printf("\nEnter marks of any 40 students "); }
for(i=0;i<40;i++) /*12) Write a program to input any 50 numbers in an array and count how many are even
{ and odd numbers are in the list.*/
scanf("%d",&marks[i]); #include<stdio.h>
} #include<conio.h>
printf("\n The marks are "); main()
for(i=0;i<40;i++) {
{ int num[50],i,c1,c2;
printf("%d ",marks[i]); float avg;
} printf("\nEnter any 50 numbers ");
c1=c2=c3=c4=c5=0; for(i=0;i<50;i++)
for(i=0;i<40;i++) {
{ scanf("%d",&num[i]);
if(marks[i]>=75) }
{ printf("\n The numbers are ");
c1++; for(i=0;i<50;i++)
} {
else if(marks[i]>=60) printf("%d ",num[i]);
{ }
c2++; c1=c2=0;
} for(i=0;i<50;i++)
else if(marks[i]>=45) {
{ if(num[i]%2==0)
c3++; {
} c1++;
else if(marks[i]>=35) }
{ else
c4++; {
} c2++;
else }
{ }
BMHSS, Pathari-2, Morang (Prepared by: TBB) C-Programming Language Grade-XII
printf("\n The no. of even numbers =%d",c1); printf("\n the no. of prime numbers in the array=%d",c);
printf("\n the no. of odd numbers=%d",c2); getch();
getch(); }
} /*14) Write a program to input any 50 numbers and count how many numbers are there
/*13) Write a program to input any 50 numbers and count how many are prime numbers.*/ which are exactly divisible by 7.*/
#include<stdio.h> #include<stdio.h>
#include<conio.h> #include<conio.h>
main() main()
{ {
int num[50],i,j,c,r,result=1; int num[50],i,c;
printf("\nEnter any 50 numbers "); printf("\nEnter any 50 numbers ");
for(i=0;i<50;i++) for(i=0;i<50;i++)
{ {
scanf("%d",&num[i]); scanf("%d",&num[i]);
} }
printf("\n The numbers are "); printf("\n The numbers are ");
for(i=0;i<50;i++) for(i=0;i<50;i++)
{ {
printf("%d ",num[i]); printf("%d ",num[i]);
} }
for(i=0;i<50;i++) for(i=0;i<50;i++)
{ {
result=1; if(num[i]%7==0)
for(j=2;j<num[i];j++) {
{ c++;
r=num[i]%j; }
if(r==0) }
{ printf("\n there are %d numbers which are exactly divisible by 7",c);
result=0; getch();
break; }
} /*15) Write a program to input any 50 numbers and count how many numbers are square
} and cube numbers.*/
if(result==0) #include<stdio.h>
{ #include<conio.h>
continue; main()
} {
else int num[50],i,j,ans,result,c1,c2;
{ printf("\nEnter any 50 numbers ");
c++; for(i=0;i<50;i++)
} {
} scanf("%d",&num[i]);
BMHSS, Pathari-2, Morang (Prepared by: TBB) C-Programming Language Grade-XII
} }
printf("\n The numbers are "); printf("\n the no. of square numbers in the array=%d",c1);
for(i=0;i<50;i++) printf("\n the no. of cube numbers in the array=%d",c2);
{ getch();
printf("%d ",num[i]); }
} /*16) Write a program to input any 50 numbers in an array and find the biggest number.*/
//code to count square numbers #include<stdio.h>
for(i=0;i<50;i++) #include<conio.h>
{ main()
result=0; {
for(j=1;j<=num[i];j++) int num[50],i,biggest;
{ printf("\nEnter any 50 numbers ");
ans=j*j; for(i=0;i<50;i++)
if(ans==num[i]) {
{ scanf("%d",&num[i]);
result=1; }
break; printf("\n The numbers are ");
} for(i=0;i<50;i++)
} {
if(result==1) printf("%d ",num[i]);
{ }
c1++; //code to find biggest number
} biggest=num[0];
} for(i=0;i<50;i++)
//code to count dube numbers {
for(i=0;i<50;i++) if(num[i]>biggest)
{ {
result=0; biggest=num[i];
for(j=1;j<=num[i];j++) }
{ }
ans=j*j*j; printf("\n the biggest number=%d",biggest);
if(ans==num[i]) getch();
{ }
result=1; /*17) Write a program to input any 50 numbers in an array and find the smallest number.*/
break; #include<stdio.h>
} #include<conio.h>
} main()
if(result==1) {
{ int num[50],i,smallest;
c2++; printf("\nEnter any 50 numbers ");
} for(i=0;i<50;i++)
BMHSS, Pathari-2, Morang (Prepared by: TBB) C-Programming Language Grade-XII
{ if(marks[i]>h)
scanf("%d",&num[i]); {
} h=marks[i];
printf("\n The numbers are "); }
for(i=0;i<50;i++) }
{ //code to find lowest marks
printf("%d ",num[i]); l=marks[0];
} for(i=0;i<50;i++)
//code to find smallest number {
smallest=num[0]; if(marks[i]<l)
for(i=0;i<50;i++) {
{ l=marks[i];
if(num[i]<smallest) }
{ }
smallest=num[i]; printf("\n the biggest marks=%d",h);
} printf("\n the lowest marks=%d",l);
} getch();
printf("\n the smallest number=%d",smallest); }
getch(); /*19) Write a program to input the age of any 100 students and find the oldest age.*/
} #include<stdio.h>
/*18) Write a program to input the marks of any 100 students in a subject and find the #include<conio.h>
highest and lowest marks.*/ main()
#include<stdio.h> {
#include<conio.h> int age[100],i,oldage;
main() printf("\nEnter the age of 100 students ");
{ for(i=0;i<100;i++)
int marks[100],i,h,l; {
printf("\nEnter marks of 100 students "); scanf("%d",&age[i]);
for(i=0;i<100;i++) }
{ printf("\n The age of 100 students ");
scanf("%d",&marks[i]); for(i=0;i<100;i++)
} {
printf("\n The marks are "); printf("%d ",age[i]);
for(i=0;i<100;i++) }
{ oldage=age[0];
printf("%d ",marks[i]); for(i=0;i<100;i++)
} {
//code to find highest marks if(age[i]>oldage)
h=marks[0]; {
for(i=0;i<50;i++) oldage=age[i];
{ }
BMHSS, Pathari-2, Morang (Prepared by: TBB) C-Programming Language Grade-XII
} /*21) Write a program to input the marks of n numbers of students and sort them in
printf("\n The oldest age=%d",oldage); ascending order.*/
getch(); #include<stdio.h>
} #include<conio.h>
/*20) Write a program to input any 10 numbers in an array and sort them in descending #define N 1000
order.*/ main()
#include<stdio.h> {
#include<conio.h> int num[N],i,j,t,n;
main() printf("Enter the value of 'n' ");
{ scanf("%d",&n);
int num[10],i,j,t; printf("\nEnter any n numbers ");
printf("\nEnter any 10 numbers "); for(i=0;i<n;i++)
for(i=0;i<10;i++) {
{ scanf("%d",&num[i]);
scanf("%d",&num[i]); }
} printf("\n The numbers before sorting are ");
printf("\n The numbers before sorting are "); for(i=0;i<n;i++)
for(i=0;i<10;i++) {
{ printf("%d ",num[i]);
printf("%d ",num[i]); }
} //code to sort
//code to sort for(i=0;i<n;i++)
for(i=0;i<10;i++) {
{ for(j=0;j<n-1;j++)
for(j=0;j<9;j++) {
{ if(num[j]>num[j+1])
if(num[j]<num[j+1]) {
{ t=num[j];
t=num[j]; num[j]=num[j+1];
num[j]=num[j+1]; num[j+1]=t;
num[j+1]=t; }
} }
} }
} printf("\n The numbers in ascending order are ");
printf("\n The numbers after sorting are "); for(i=0;i<n;i++)
for(i=0;i<10;i++) {
{ printf("%d ",num[i]);
printf("%d ",num[i]); }
} getch();
getch(); }
}
BMHSS, Pathari-2, Morang (Prepared by: TBB) C-Programming Language Grade-XII
/*22) Write a program to input the height of n numbers in an array and display the height Then, ask the user to input a mobile number and find it is in the contact list or not.*/
in shortest to tallest form.*/ #include<stdio.h>
#include<stdio.h> #include<conio.h>
#include<conio.h> #include<string.h>
#define N 1000 main()
main() {
{ char mobileno[50][10],srchmobile[10],result;
int height[N],i,j,t,n; int i;
printf("Enter the value of 'n' "); printf("Enter the mobile no. of any 50 students ");
scanf("%d",&n); for(i=0;i<50;i++)
printf("\nEnter the height of n students "); {
for(i=0;i<n;i++) gets(mobileno[i]);
{ }
scanf("%d",&height[i]); printf("Enter the mobile no. which you want to search ");
} gets(srchmobile);
printf("\n The height of the students before sorting are "); //code to search
for(i=0;i<n;i++) for(i=0;i<50;i++)
{ {
printf("%d ",height[i]); if(strcmpi(srchmobile,mobileno[i])==0)
} {
//code to sort result='y';
for(i=0;i<n;i++) break;
{ }
for(j=0;j<n-1;j++) }
{ if(result=='y')
if(height[j]<height[j+1]) {
{ printf("\n Yes, the mobile no. which you are searching is in the list ");
t=height[j]; }
height[j]=height[j+1]; else
height[j+1]=t; {
} printf("\n No, the mobile no. which you are searching is not in the list ");
} }
} getch();
printf("\n The height of the students from shortest to tallest form "); return(0);
for(i=0;i<n;i++) }
{ /*24) A college published the entrance result of the successful candidates for the
printf("%d ",height[i]); admission in BBA. The symbol no. of the successful candidates are as follows:
} 12001 12005 12009 12104 12204
getch(); 12250
} 12305 12555 12560 12570 12600
/*23) Write a program to input mobile number of any 50 students in a class. 12601
BMHSS, Pathari-2, Morang (Prepared by: TBB) C-Programming Language Grade-XII
Now, make a program to input a symbol no. and your program should find the given symbol #include<conio.h>
no. is in the list or not.*/ main()
#include<stdio.h> {
#include<conio.h> int m[2][3]={{4,8,-2},{3,4,0}};
main() int r,c;
{ printf("The required matrix is ");
int printf("\n");
sn[]={12001,12005,12009,12104,12204,12250,12305,12555,12560,12570,12600,12601}; for(r=0;r<2;r++)
int i,x; {
char found; for(c=0;c<3;c++)
printf("\n The list of successful candidates are "); {
for(i=0;i<12;i++) printf("%d\t",m[r][c]);
{ }
printf("\n %d",sn[i]); printf("\n");
} }
printf("\nEnter the symbol no. which you wnat to search "); getch();
scanf("%d",&x); }
//code to search /*26) Write a program to input a 2*3 matrix and display it properly.*/
for(i=0;i<12;i++) #include<stdio.h>
{ #include<conio.h>
if(x==sn[i]) main()
{ {
found=1; int m[2][3],r,c;
break; printf("Enter the elements of the matrix ");
} for(r=0;r<2;r++)
} {
if(found==1) for(c=0;c<3;c++)
{ {
printf("\n Yes, the symbol no. %d is in the list of successful students ",x); scanf("%d",&m[r][c]);
} }
else }
{ printf("The required matrix is ");
printf("\n No, the symbol no. %d is not in the list of successful students ",x); printf("\n");
} for(r=0;r<2;r++)
getch(); {
} for(c=0;c<3;c++)
/*25) Write a program to store the following matrix and display it properly. {
4 8 -2 printf("%d\t",m[r][c]);
3 4 0 }
*/ printf("\n");
#include<stdio.h> }
BMHSS, Pathari-2, Morang (Prepared by: TBB) C-Programming Language Grade-XII
getch(); }
} printf("\n");
/*27) Write a program to input any two matrices of 3*4 size and find their sum.*/ }
#include<stdio.h> //code to add matrices
#include<conio.h> for(r=0;r<3;r++)
main() {
{ for(c=0;c<4;c++)
int m1[3][4],m2[3][4],m3[3][4],r,c; {
printf("Enter the elements of the first matrix "); m3[r][c]=m1[r][c]+m2[r][c];
for(r=0;r<3;r++) }
{ }
for(c=0;c<4;c++) printf("The sum of two matrices is ");
{ printf("\n");
scanf("%d",&m1[r][c]); for(r=0;r<3;r++)
} {
} for(c=0;c<4;c++)
printf("Enter the elements of the second matrix "); {
for(r=0;r<3;r++) printf("%d\t",m3[r][c]);
{ }
for(c=0;c<4;c++) printf("\n");
{ }
scanf("%d",&m2[r][c]); getch();
} }
} /*28) Write a program to input any two matrices of 2*3 size and find their difference.*/
printf("The first matrix is "); #include<stdio.h>
printf("\n"); #include<conio.h>
for(r=0;r<3;r++) main()
{ {
for(c=0;c<4;c++) int m1[2][3],m2[2][3],m3[2][3],r,c;
{ printf("Enter the elements of the first matrix ");
printf("%d\t",m1[r][c]); for(r=0;r<2;r++)
} {
printf("\n"); for(c=0;c<3;c++)
} {
printf("The second matrix is "); scanf("%d",&m1[r][c]);
printf("\n"); }
for(r=0;r<3;r++) }
{ printf("Enter the elements of the second matrix ");
for(c=0;c<4;c++) for(r=0;r<2;r++)
{ {
printf("%d\t",m2[r][c]); for(c=0;c<3;c++)
BMHSS, Pathari-2, Morang (Prepared by: TBB) C-Programming Language Grade-XII
{ }
scanf("%d",&m2[r][c]); getch();
} }
} /*29) Write a program to input any two matrices of 3*3 sizes and find their product.*/
printf("The first matrix is "); #include<stdio.h>
printf("\n"); #include<conio.h>
for(r=0;r<2;r++) main()
{ {
for(c=0;c<3;c++) int m1[3][3],m2[3][3],m3[3][3],r,c,i,s;
{ printf("Enter the elements of the first matrix ");
printf("%d\t",m1[r][c]); for(r=0;r<3;r++)
} {
printf("\n"); for(c=0;c<3;c++)
} {
printf("The second matrix is "); scanf("%d",&m1[r][c]);
printf("\n"); }
for(r=0;r<2;r++) }
{ printf("Enter the elements of the second matrix ");
for(c=0;c<3;c++) for(r=0;r<3;r++)
{ {
printf("%d\t",m2[r][c]); for(c=0;c<3;c++)
} {
printf("\n"); scanf("%d",&m2[r][c]);
} }
//code to find difference of the matrices }
for(r=0;r<2;r++) printf("The first matrix is ");
{ printf("\n");
for(c=0;c<3;c++) for(r=0;r<3;r++)
{ {
m3[r][c]=m1[r][c]-m2[r][c]; for(c=0;c<3;c++)
} {
} printf("%d\t",m1[r][c]);
printf("The difference of two matrices is "); }
printf("\n"); printf("\n");
for(r=0;r<2;r++) }
{ printf("The second matrix is ");
for(c=0;c<3;c++) printf("\n");
{ for(r=0;r<3;r++)
printf("%d\t",m3[r][c]); {
} for(c=0;c<3;c++)
printf("\n"); {
BMHSS, Pathari-2, Morang (Prepared by: TBB) C-Programming Language Grade-XII
printf("%d\t",m2[r][c]); {
} int m1[2][2],m2[2][2],m3[2][2],r,c,ch,s,i;
printf("\n"); printf("***********MENU ***********");
} printf("\n1.Sum of the matrices");
//code to multply matrices printf("\n2.Difference of the matrices");
for(r=0;r<3;r++) printf("\n3.Product of the matrices");
{ printf("\n4.Quit");
for(c=0;c<3;c++) printf("\n\nEnter your choice ");
{ scanf("%d",&ch);
s=0; switch(ch)
for(i=0;i<3;i++) {
{ case 1:
s=s+m1[r][i]*m2[i][c]; printf("Enter the elements of the first matrix ");
} for(r=0;r<2;r++)
m3[r][c]=s; {
} for(c=0;c<2;c++)
} {
printf("The product of two matrices is "); scanf("%d",&m1[r][c]);
printf("\n"); }
for(r=0;r<3;r++) }
{ printf("Enter the elements of the second matrix ");
for(c=0;c<3;c++) for(r=0;r<2;r++)
{ {
printf("%d\t",m3[r][c]); for(c=0;c<2;c++)
} {
printf("\n"); scanf("%d",&m2[r][c]);
} }
getch(); }
} printf("The first matrix is ");
/*30) Write a program to display the following menu and prompt the user to input his/her printf("\n");
choice and do the task accordingly in for(r=0;r<2;r++)
two matrices of 2*2 size. {
1. Sum of the matrices for(c=0;c<2;c++)
2. Difference of the matrices {
3. Product of the matrices printf("%d\t",m1[r][c]);
4. Quit }
*/ printf("\n");
#include<stdio.h> }
#include<conio.h> printf("The second matrix is ");
#include<process.h> printf("\n");
main() for(r=0;r<2;r++)
BMHSS, Pathari-2, Morang (Prepared by: TBB) C-Programming Language Grade-XII
{ }
for(c=0;c<2;c++) }
{ printf("The first matrix is ");
printf("%d\t",m2[r][c]); printf("\n");
} for(r=0;r<2;r++)
printf("\n"); {
} for(c=0;c<2;c++)
//code to find sum {
for(r=0;r<2;r++) printf("%d\t",m1[r][c]);
{ }
for(c=0;c<2;c++) printf("\n");
{ }
m3[r][c]=m1[r][c]+m2[r][c]; printf("The second matrix is ");
} printf("\n");
} for(r=0;r<2;r++)
printf("The sum of two matrices is "); {
printf("\n"); for(c=0;c<2;c++)
for(r=0;r<2;r++) {
{ printf("%d\t",m2[r][c]);
for(c=0;c<2;c++) }
{ printf("\n");
printf("%d\t",m3[r][c]); }
} //code to find difference
printf("\n"); for(r=0;r<2;r++)
} {
break; for(c=0;c<2;c++)
case 2: {
printf("Enter the elements of the first matrix "); m3[r][c]=m1[r][c]-m2[r][c];
for(r=0;r<2;r++) }
{ }
for(c=0;c<2;c++) printf("The difference of two matrices is ");
{ printf("\n");
scanf("%d",&m1[r][c]); for(r=0;r<2;r++)
} {
} for(c=0;c<2;c++)
printf("Enter the elements of the second matrix "); {
for(r=0;r<2;r++) printf("%d\t",m3[r][c]);
{ }
for(c=0;c<2;c++) printf("\n");
{ }
scanf("%d",&m2[r][c]); break;
BMHSS, Pathari-2, Morang (Prepared by: TBB) C-Programming Language Grade-XII
case 3: {
printf("Enter the elements of the first matrix "); s=0;
for(r=0;r<2;r++) for(i=0;i<2;i++)
{ {
for(c=0;c<2;c++) s=s+m1[r][i]*m2[i][c];
{ }
scanf("%d",&m1[r][c]); m3[r][c]=s;
} }
} }
printf("Enter the elements of the second matrix "); printf("The product of two matrices is ");
for(r=0;r<2;r++) printf("\n");
{ for(r=0;r<2;r++)
for(c=0;c<2;c++) {
{ for(c=0;c<2;c++)
scanf("%d",&m2[r][c]); {
} printf("%d\t",m3[r][c]);
} }
printf("The first matrix is "); printf("\n");
printf("\n"); }
for(r=0;r<2;r++) break;
{ case 4:
for(c=0;c<2;c++) exit(0);
{ default:
printf("%d\t",m1[r][c]); printf("\n your choice is invalid ");
} printf("\n Please, enter your choice between 1 to 4 ");
printf("\n"); }
} getch();
printf("The second matrix is "); }
printf("\n"); /*1. Write a C program to calculate factorial of user inputed number by using function.*/
for(r=0;r<2;r++) #include<stdio.h>
{ #include<conio.h>
for(c=0;c<2;c++) long int factorial(int);
{ main()
printf("%d\t",m2[r][c]); {
} int n;
printf("\n"); long int f;
} printf("\nEnter a number ");
//code to find product scanf("%d",&n);
for(r=0;r<2;r++) f=factorial(n);
{ printf("\nthe factorial value=%ld",f);
for(c=0;c<2;c++) getch();
BMHSS, Pathari-2, Morang (Prepared by: TBB) C-Programming Language Grade-XII
} /*3. Write a C program to calculate factorial upto user input number by using function.*/
long int factorial(int n) #include<stdio.h>
{ #include<conio.h>
int i; long int factorial(int);
long int f; main()
f=1; {
for(i=n;i>=1;i--) int n,i;
{ long int f;
f=f*i; printf("\nEnter a number ");
} scanf("%d",&n);
return(f); for(i=1;i<=n;i++)
} {
/*2. What is recursive function? Write a program to calculate the factorial of an integer f=factorial(i);
using recursion. (HSEB 2068)*/ printf("\nthe factorial value of %d =%ld",i,f);
#include<stdio.h> }
#include<conio.h> getch();
long int factorial(int); }
main() long int factorial(int n)
{ {
int n; int i;
long int f; long int f;
printf("\nEnter a number "); f=1;
scanf("%d",&n); for(i=n;i>=1;i--)
f=factorial(n); {
printf("\nthe factorial value=%ld",f); f=f*i;
getch(); }
} return(f);
long int factorial(int n) }
{ /*4. Write a C program to pass out number of elements and sort them in ascending order
by using function.*/
long int f; #include<stdio.h>
if(n==0) #include<conio.h>
{ #define N 1000
f=1; void sort(int[],int);
} main()
else {
{ int num[N],n,i;
f=n*factorial(n-1); printf("\nEnter the value of 'n' ");
} scanf("%d",&n);
return(f); printf("\nEnter different number ");
} for(i=0;i<n;i++)
BMHSS, Pathari-2, Morang (Prepared by: TBB) C-Programming Language Grade-XII
{ printf("\n%d is even number",n);
scanf("%d",&num[i]); }
} else
//code to call function {
sort(num,n); printf("\n%d is odd number",n);
//display in ascending order }
printf("\nThe numbers in ascending order "); getch();
for(i=0;i<n;i++) }
{ int evenodd(int n)
printf(" %d",num[i]); {
} int r;
getch(); r=n%2;
} if(r==0)
void sort(int x[N],int n) {
{ return (0);
int i,j,t; }
for(i=0;i<n;i++) else
{ {
for(j=0;j<n-1;j++) return(1);
{ }
if(x[j]>x[j+1]) }
{ /*6. Write a C program to compound interest by using function.*/
t=x[j]; #include<stdio.h>
x[j]=x[j+1]; #include<conio.h>
x[j+1]=t; float compoundinterest(float,float,float);
} main()
} {
} float p,t,r,ci;
} printf("\nEnter the principle amount ");
/*5. Write a C program to check user input number is even or odd by using function.*/ scanf("%f",&p);
#include<stdio.h> printf("\nEnter the time ");
#include<conio.h> scanf("%f",&t);
int evenodd(int); printf("\nEnter the rate of interest ");
main() scanf("%f",&r);
{ ci=compoundinterest(p,t,r);
int n,ans; printf("\nThe compound interest=Rs. %.2f",ci);
printf("\nEnter a number "); getch();
scanf("%d",&n); }
ans=evenodd(n); float compoundinterest(float p,float t,float r)
if(ans==0) {
{ float amt;
BMHSS, Pathari-2, Morang (Prepared by: TBB) C-Programming Language Grade-XII
#include<conio.h>
#define N 500
return(amt); int sumofeven(int [],int);
} int sumofodd(int [],int);
main()
/*7. Write a C program to calculate sum of even numbers by using recursion.*/ {
#include<stdio.h> int i,num[N],sum1,sum2,n;
#include<conio.h> printf("Enter the value of 'n' ");
long int sumofeven(int); scanf("%d",&n);
main() printf("\nEnter any %d numbers ",n);
{ for(i=0;i<n;i++)
int n; {
long int s; scanf("%d",&num[i]);
printf("\nEnter last number of the series "); }
scanf("%d",&n); sum1=sumofeven(num,n);
s=sumofeven(n); sum2=sumofodd(num,n);
printf("\nthe sum of even numbers upto %d = %ld",n,s); printf("\nThe sum of even numbers=%d",sum1);
getch(); printf("\nthe sum of odd numbers=%d",sum2);
} getch();
long int sumofeven(int n) }
{ int sumofeven(int x[N],int n)
{
long int ans; int i,s=0;
int i; for(i=0;i<n;i++)
if(n%2==1) {
{ if(x[i]%2==0)
n=n-1; {
} s=s+x[i];
if(n<=0) }
{ }
ans=0; return(s);
} }
else int sumofodd(int x[N],int n)
{ {
ans=n+sumofeven(n-2); int i,s=0;
} for(i=0;i<n;i++)
return(ans); {
} if(x[i]%2==1)
/*8. Write a C program to read N numbers and calculate sum of even numbers and sum {
of odd numbers by using function.*/ s=s+x[i];
#include<stdio.h> }
BMHSS, Pathari-2, Morang (Prepared by: TBB) C-Programming Language Grade-XII
} {
return(s); int n;
} long int f;
/*9. Write a recursive C program to calculate sum of odd numbers upto N.*/ printf("\nEnter a number ");
#include<stdio.h> scanf("%d",&n);
#include<conio.h> printf("\nThe required series is ");
long int sumofodd(int); series(n);
main() getch();
{ }
int n; void series(int n)
long int s; {
printf("\nEnter last number of the series "); int i;
scanf("%d",&n); for(i=1;i<=n;i=i+4)
s=sumofodd(n); {
printf("\nthe sum of even numbers upto %d = %ld",n,s); printf("%d\t",i);
getch(); }
} }
long int sumofodd(int n) /*11. Write a C program to calculate HCF / GCD of 3 user inputed numbers by using
{ function.*/
#include<stdio.h>
long int ans; #include<conio.h>
int i; int HCF(int,int,int);
if(n%2==0) main()
{ {
n=n-1; int n1,n2,n3,ans;
} printf("\nEnter first number ");
if(n<=0) scanf("%d",&n1);
{ printf("\nEnter second number ");
ans=0; scanf("%d",&n2);
} printf("\nEnter third number ");
else scanf("%d",&n3);
{ ans=HCF(n1,n2,n3);
ans=n+sumofodd(n-2); printf("\nthe HCF/GCD of given numbers=%d",ans);
} getch();
return(ans); }
} int HCF(int a,int b,int c)
/*10. Write a C program to print series 1, 4, 9, upto N by using function.*/ {
#include<stdio.h> int i,r1,r2,r3,ans,smallest;
#include<conio.h> if(a<b && a<c)
void series(int); {
main() smallest=a;
BMHSS, Pathari-2, Morang (Prepared by: TBB) C-Programming Language Grade-XII
} int i,r1,r2,r3,ans,x,smallest;
else if(b<a&& b<c) if(a<b && a<c)
{ {
smallest=b; smallest=a;
} }
else else if(b<a&& b<c)
{ {
smallest=c; smallest=b;
} }
for(i=1;i<=smallest;i++) else
{ {
r1=a%i; smallest=c;
r2=b%i; }
r3=c%i; x=a*b*c;
if(r1==0&&r2==0&&r3==0) for(i=x;i>=1;i--)
{ {
ans=i; r1=i%a;
} r2=i%b;
} r3=i%c;
return(ans); if(r1==0&&r2==0&&r3==0)
} {
/*12. Write a C program to to calculate LCM of user inputed three numbers by using ans=i;
function.*/ }
#include<stdio.h> }
#include<conio.h> return(ans);
int LCM(int,int,int); }
main() /*13. Write a C program to find area of right angle triangle whose height and base are input
{ by user also using function.*/
int n1,n2,n3,ans; #include<stdio.h>
printf("\nEnter first number "); #include<conio.h>
scanf("%d",&n1); float areaoftriangle(float ,float);
printf("\nEnter second number "); main()
scanf("%d",&n2); {
printf("\nEnter third number "); float h,b,a;
scanf("%d",&n3); printf("\nEnter the height of triangle ");
ans=LCM(n1,n2,n3); scanf("%f",&h);
printf("\nthe LCM of given numbers=%d",ans); printf("\nEnter the base of triangle ");
getch(); scanf("%f",&b);
} a=areaoftriangle(h,b);
int LCM(int a,int b,int c) printf("\nthe area of triangle=%f",a);
{ getch();
BMHSS, Pathari-2, Morang (Prepared by: TBB) C-Programming Language Grade-XII
} int a,b,c,d,e,f;
float areaoftriangle(float h,float b) printf("\nEnter values of a, b, c for first equation ax^2+bx+c=0 : ");
{ scanf("%d %d %d", &a, &b, &c);
float a; printf("\nEnter values of d, e, f for second equation dx^2+ex+f=0 : ");
a=0.5*b*h; scanf("%d %d %d", &d, &e, &f);
return (a); equation_adder(a, b, c, d, e, f);
} getch();
/*14. Write a C program to find area of triangle whose 3 sides are input by user. [Hint: area }
= sqrt(s*(s-a)(s-b)(s-c)), void equation_adder(int p, int q, int r, int s, int t, int u)
where s = (a+b+c)/2 ]*/ {
#include<stdio.h> int x, y, z;
#include<conio.h> x = p + s;
#include<math.h> y = q + t;
float areaoftriangle(float ,float,float); z = r + u;
main() printf("\nThe addition of %dx^2+%dx+%d = 0 and %dx^2+%dx+%d = 0 is %dx^2=%dx+%d =
{ 0",p,q,r,s,t,u,x,y,z);
float side1,side2,side3,a; }
printf("\nEnter the length of the first side of triangle ");
scanf("%f",&side1); Lab 4 Finished
printf("\nEnter the length of the second side of triangle ");
scanf("%f",&side2);
printf("\nEnter the length of the third side of triangle ");
scanf("%f",&side3);
a=areaoftriangle(side1,side2,side3);
printf("\nthe area of triangle=%f",a);
getch();
}
float areaoftriangle(float a,float b,float c)
{
float s,ar;
s=(a+b+c)/2;
ar=sqrt(s*(s-a)*(s-b)*(s-c));
return (ar);
}
/*15. Write a C program to read two equation ax2+bx+c = 0 and dx2+ex+f=0 and sum
them by using function.*/
#include<stdio.h>
#include<conio.h>
void equation_adder(int, int, int, int, int, int);
main()
{

Potrebbero piacerti anche