C Language
C Language
C is a
structured programming language developed by Dennis Ritchie at Bell
Laboratories in 1972. Since C is a structured programming language, a program
in C language can be divided into small logical and functional modules. C++
language is an expanded version of C.
Character set:
The character
set consists of alphabet, digit or special symbols used to response
information.
Character sets
used in C
Alphabet |
Uppercase (
ie A, B..............Z) Lowercase (
ie a, b ................z) |
Digits |
0, 1, 2
................... 8, 9 |
Special
symbols |
+, -, *, ( )
, etc. |
Characteristics of C Language
- It is a small programming language
that consumes less space.
- It has enough number of inbuilt
functions and operators.
Keyword:
The character
set consists of alphabet, digit or special symbols used to response
information.
There are 32 characters in C:
Auto, If, While, For, Int, float. etc.
Data type:
Data type
refers to the type of data or variable which is to be stored and used in the
program.
List of data types
- Integer (int) = 2 bytes
- character (char) = 1 byte
- float (float) = 4 bytes
- double (long integer) = 8 bytes
Difference between QBASIC and C-Language
Q-BASIC |
C- Language |
It is a high
level language without feature of low level language. |
It is high level
language with some features of low level language. |
It is mostly
used for designing application software. |
It is mostly
used for designing system software. |
Operators
Operators are
the symbols which are used to compute values and test multiple operations. The
data on which the operators are performed are known as operands.
Types of
operator
- Arithmetic operator: The operators which perform
mathematical expressions like addition, subtraction, multiplication and
division.
- Relational operator: Relational operators are symbols
used to compare two different expressions. The result of the comparison is
either True or False.
- Logical operator: Logical operators performs tests
on multiple relation. Example: A < B and A > B
- Unary operator: C also supports two very important
operators called increment and decrement operators.
Loop
A loop causes a
section of a program to be repeated a certain number of times. The repetition
continues while the condition set for it remains true. When the condition
becomes false, the loop ends the control is passed to the statement following
the loop.
Variable
Those value
which changes during program execution is called variable.Example: A = 5 where
A is a variable
Constant
Those values
which can't change during the program execution is called constant. Example: A
= 10 where 10 is a constant
Rule for naming variables in C
- Space is not allowed in variable
name.
- Both upper and lower case letters
are permitted.
Printf: This statement is used to display the
message on the screen.
Scarf: This statement is used to accept a value.
Control structure of c
Branching with if
syntax:
if (condition)
{
statement;
}
else if (condition)
{
statements;
}
..............
else
{
statements
}
Example
/*check if the
given number is positive, negative or zero*/
#include
main()
{
int h;
system("cls");
printf("Enter an integer;");
scanf("%d", &h);
if(h>0)
Printf("The number is positive");
else if(h=0)
printf("The number is zero");
else
printf("The number is negative");
For control
syntax:
for(initialize,
condition, increment)
{
statement block
}
Example
#include
main()
{
int x;
for(x=1;x<=10;x++)
{
printf("%d\n",x);
}
While control
syntax
while(condition)
{
statement block
}
Example:
#include
main()
{
int x;
x=1;
while(x<=10)
{
printf("%d\n",x);
x++;
}
Do control
syntax
do
{
statement block;
}while(condition)
Example:
#include
main()
{
int x; sum=0;
do
{
printf("enter number");
scanf("%d",&x);
sum=sum+x;
}
while(x!=0);
{
printf("The sum of entered numbers:%d",sum);
}
#include <stdio.h>
int main() {
int sum = 0;
printf("Odd numbers from 1 to 20 are:\n");
for (int i = 1; i <= 20; i++)
{
if (i % 2 != 0)
{
printf("%d ", i);
sum += i;
}
}
printf("\nSum of odd numbers from 1 to 20: %d\n", sum);
}
int main()
{
for (int i = 1; i <= 20; i++)
{
printf("%d ", i);
}
return 0;
}
3. write a c program to input two different number and display greatest.
#include <stdio.h>
int main()
{
int num1, num2;
printf("Enter the first number: ");
scanf("%d", &num1);
printf("Enter the second number: ");
scanf("%d", &num2);
if (num1 > num2)
{
printf("The greatest number is: %d\n", num1);
}
else
{
printf("The greatest number is: %d\n", num2);
}
return 0;
}
4. write a c program to input three different number and display greatest.
#include <stdio.h>
int main()
{
int num1, num2, num3;
printf("Enter the first number: ");
scanf("%d", &num1);
printf("Enter the second number: ");
scanf("%d", &num2);
printf("Enter the third number: ");
scanf("%d", &num3);
if (num1 > num2 && num1 > num3)
{
printf("The greatest number is: %d\n", num1);
}
else if (num2 > num1 && num2 > num3)
{
printf("The greatest number is: %d\n", num2);
}
else
{
printf("The greatest number is: %d\n", num3);
}
return 0;
}
5. write a c program to input temperature in Celsius and convert it into Fahrenheit
#include <stdio.h>
int main()
{
float celsius, fahrenheit;
printf("Enter temperature in Celsius: ");
scanf("%f", &celsius);
fahrenheit = (celsius * 9/5) + 32;
printf("Temperature in Fahrenheit: %.2f\n", fahrenheit);
return 0;
}
6. write a c program to input temperature in Fahrenheit and convert it into Celsius.
#include <stdio.h>
int main() {
float fahrenheit, celsius;
printf("Enter temperature in Fahrenheit: ");
scanf("%f", &fahrenheit);
celsius = (fahrenheit - 32) * 5 / 9;
printf("Temperature in Celsius: %.2f\n", celsius);
return 0;
}
7. write a c program to input two different number and display smallest.
#include <stdio.h>
int main()
{
int num1, num2, smallest;
printf("Enter the first number: ");
scanf("%d", &num1);
printf("Enter the second number: ");
scanf("%d", &num2);
if (num1 < num2) {
smallest = num1;
} else {
smallest = num2;
}
printf("The smallest number is: %d\n", smallest);
return 0;
}
8. write a c program to input three different number and display smallest.
#include <stdio.h>
int main()
{
int num1, num2, num3, smallest;
printf("Enter the first number: ");
scanf("%d", &num1);
printf("Enter the second number: ");
scanf("%d", &num2);
printf("Enter the third number: ");
scanf("%d", &num3);
if (num1 < num2 && num1 < num3)
{
smallest = num1;
}
else if (num2 < num1 && num2 < num3)
{
smallest = num2;
}
else
{
smallest = num3;
}
printf("The smallest number is: %d\n", smallest);
return 0;
}
9. write a c program to input three different number and display middle number.
#include <stdio.h>
int main()
{
int num1, num2, num3, middle;
printf("Enter the first number: ");
scanf("%d", &num1);
printf("Enter the second number: ");
scanf("%d", &num2);
printf("Enter the third number: ");
scanf("%d", &num3);
if ((num1 > num2 && num1 < num3) || (num1 < num2 && num1 > num3))
{
middle = num1;
}
else if ((num2 > num1 && num2 < num3) || (num2 < num1 && num2 > num3))
{
middle = num2;
}
else
{
middle = num3;
}
printf("The middle number is: %d\n", middle);
return 0;
}
11. write a c program to display number in following Fibonacci series.
1, 1,2,3,5,8,13,......10th term.
#include <stdio.h>
int main()
{
int n = 10;
int a = 1, b = 1, c;
printf("%d, %d", a, b);
for (int i = 3; i <= n; i++) {
c = a + b;
printf(", %d", c);
a = b;
b = c;
}
printf("\n");
return 0;
}
12. write a c program to input principal time rate and calculate simple interest and total amount.
#include <stdio.h>
int main()
{
float p, t, r, si, ta;
printf("Enter the principal amount: ");
scanf("%f", &p);
printf("Enter the time (in years): ");
scanf("%f", &t);
printf("Enter the rate of interest (in percentage): ");
scanf("%f", &r);
si = (p * t * r) / 100;
ta = p + si;
printf("Simple Interest: %.2f\n", si);
printf("Total Amount: %.2f\n", ta);
return 0;
}
13. Write a program using C lan age to generate the first 25 even numbers between 100 and 200.
#include <stdio.h>
int main()
{
int i, count = 0;
printf("The first 25 even numbers between 100 and 200 are:\n");
for (i= 100; count < 25; i += 2)
{
printf("%d ", i);
count++;
}
printf("\n");
return 0;
}
14. Write a program using C language to input an integer then check whether the integer is even or odd.
#include<stdio.h>
#include<conio.h>
int main()
{
int n;
printf("Enter any number: ");
scanf("%d", &n);
if(n%2==0)
printf("%d is even number", n);
else
printf("%d is odd number", n);
return 0;
}
15. Write a program in 'C' language to check whether the supplied number is divisible by 5 or not.
#include<stdio.h>
#include<conio.h>
void main()
{
int a;
printf("Enter any number");
scanf("%d", &a);
if (a % 5 == 0)
{
printf("It is Divisible by 5");
}
else
{
printf("It is not Divisible by 5");
}
getch( );
}
16. To check input number is prime or composite.
#include <stdio.h>
int main()
{
int n,i,c=0;
printf ("Type any number ");
scanf ("%d",&n);
for (i=2;i<=n-1;i++)
{
if(n%i==0) c++;
}
if (c==0)
printf("The number is prime. ");
else
printf("The number iscomposite. ");
return 0;
}
17.Write a C program to input multi digit number and display its sum.
[hint 457 = 4+5+7 = 16]
#include <stdio.h>
#include <conio.h>
void main()
{
int n,r,s=0;
clrscr();
printf("Type any one integer ");
scanf("%d",&n);
while (n!=0)
{
r=n%10;
s=s+r;
n=n/10;
}
printf("Sum of individual digits= %d",s);
getch();
}
18. Write a c program that asks length & breadth of a room and calculates its area and perimeter.
#include <stdio.h>
int main()
{
float length, breadth, area, perimeter;
// Input length and breadth of the room
printf("Enter the length of the room: ");
scanf("%f", &length);
printf("Enter the breadth of the room: ");
scanf("%f", &breadth);
area = length * breadth;
perimeter = 2 * (length + breadth);
printf("Area of the room: %.2f square units\n", area);
printf("Perimeter of the room: %.2f units\n", perimeter);
return 0;
}
19. Write a C program that asks any one integer and displays its reverse.
#include <stdio.h>
int main()
{
int num, reverse = 0, remainder;
printf("Enter an integer: ");
scanf("%d", &num);
while (num != 0)
{
remainder = num % 10;
reverse = reverse * 10 + remainder;
num /= 10;
}
printf("Reversed number: %d\n", reverse);
return 0;
}
20. Write a C program to display Series 1 2 4 8 ... upto 10th terms.
#include <stdio.h>
#include <conio.h>
void main()
{
clrscr();
int n=1,c;
for (c=1;c<=10;c++)
{
printf ("%d ",n);
n=n*2;
}
getch();
}
21. Write a c program to input marks obtain in your one subject and check whether you are pass or fail.
#include <stdio.h>
#include <conio.h>
void main()
{
int a;
clrscr();
printf ("Type your marks ");
scanf ("%d",&a);
if(a>=40)
{
printf ("You are Pass");
}
else
{
printf ("You are Fail");
}
getch();
}
22. Write a C program to Calculate area and volume of a room.
#include <stdio.h>
#include <conio.h>
void main()
{
clrscr();
int l,b,h,a,v;
printf ("Type length, breadth and
height ");
scanf ("%d%d%d",&l,&b,&h);
a=l*b;
v=l*b*h;
printf("\nArea=%d",a);
printf ("\nVolume=%d",v);
getch();
}
Comments
Post a Comment