[go: up one dir, main page]

0% found this document useful (0 votes)
29 views11 pages

Comp. Skill Unit - II

Ok887

Uploaded by

hilisov227
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views11 pages

Comp. Skill Unit - II

Ok887

Uploaded by

hilisov227
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

1. Program to Display "Hello, World!

"
#include <stdio.h>

int main() {

// printf() displays the string inside quotation

printf("Hello, World!");

return 0;

2. C Program to Print an Integer (Entered by the User)

#include <stdio.h>

int main() {

int number;

printf("Enter an integer: ");

// reads and stores input

scanf("%d", &number);

// displays output

printf("You entered: %d", number);

return 0;

3. C Program to Add Two Integers

#include <stdio.h>
int main()
{
int number1, number2, sum;

printf(" Enter two integer values \n ");


scanf("%d %d", &number1, &number2);

sum = number1 + number2;

printf(" Sum of the two integer values is %d", sum);


return 0;
}
4. C Program to Find Average of Two Numbers
#include <stdio.h>

int main()
{
int number1, number2;
float res;

printf("Enter the First Number to find = ");


scanf("%d",&number1);

printf("Enter the Second Number to find = ");


scanf("%d",&number2);

int sm = number1 + number2;

res = sum/2; //(float)sum/2;

printf("The Sum of %d and %d = %d\n", number1, number2, sm);


printf("The Average of %d and %d = %.2f\n", number1, number2, res);

return 0;
}

5. C Program to Calculate Cube of a Number


/* C Program to find Cube of a Number */

#include<stdio.h>

int main()
{
int number, cube;

printf(" \n Please Enter any integer Value : ");


scanf("%d", &number);

cube = number * number * number;

printf("\n Cube of a given number %d is = %d", number, cube);

return 0;
}
In ‘C’ programming conditional statements are possible with the help of the
following two constructs:

1. If statement
if (condition)
instruction;
The condition evaluates to either true or false. True is always a non-zero
value, and false is a value that contains zero. Instructions can be a single
instruction or a code block enclosed by curly braces { }.

1.PROGRAM:

#include<stdio.h>
int main()
{
int num1=1;
int num2=2;
if(num1<num2) //test-condition
{
printf("num1 is smaller than num2");
}
return 0;
}

C Program to Check Odd or Even using IF Condition


#include<stdio.h>

int main()
{
int number;

printf(" Enter an int value to check \n");


scanf("%d", &number);

if ( number%2 == 0 ) //Check whether remainder is 0 or not


printf("Given number %d is EVEN NUMBER \n", number);

else
printf("Given number %d is ODD NUMBER \n", number);

return 0;
}
C divides the operators into the following groups:

 Arithmetic operators
 Assignment operators
 Comparison operators
 Logical operators
 Bitwise operators

Arithmetic Operators
Operator Name Description Example

+ Addition Adds together two values x+y

- Subtraction Subtracts one value from another x-y

* Multiplication Multiplies two values x*y

/ Division Divides one value by another x/y

% Modulus Returns the division remainder x%y

++ Increment Increases the value of a variable ++x


by 1

-- Decrement Decreases the value of a variable --x


by 1

Assignment Operators
Assignment operators are used to assign values to variables.

In the example below, we use the assignment operator (=) to assign the
value 10 to a variable called x:

Example
int x = 10;
Operator Example Same as

= x=5 x=5

+= x += 3 x=x+3

-= x -= 3 x=x-3

*= x *= 3 x=x*3

/= x /= 3 x=x/3

%= x %= 3 x=x%3

&= x &= 3 x=x&3

|= x |= 3 x=x|3

^= x ^= 3 x=x^3

>>= x >>= 3 x = x >> 3

<<= x <<= 3 x = x << 3

 Relational Operators Or Comparison operators

C has six relational operators that can be used to formulate a Boolean


expression for making a decision and testing conditions, which returns true or
false :

< less than

<= less than or equal to

> greater than

>= greater than or equal to

== equal to

!= not equal to
Logical Operators
&& Logical Returns true if both statements are true x < 5 && x <
and 10

|| Logical or Returns true if one of the statements is x < 5 || x < 4


true

! Logical Reverse the result, returns false if the !(x < 5 && x <
not result is true 10)

The If-Else statement

The if-else is statement is an extended version of If. The general form of if-
else is as follows:

if (test-expression)
{
True block of statements
}
Else
{
False block of statements
}
Statements;
Program:

#include<stdio.h>
int main()
{
int num=19;
if(num<10)
{
printf("The value is less than 10");
}
else
{
printf("The value is greater than 10");
}
return 0;
}
C Program to Find Largest of Two Numbers using Else If Statement

/* C Program to Find Largest of Two numbers */


#include <stdio.h>
int main()
{
int a, b;
printf("Please Enter Two different values\n");
scanf("%d %d", &a, &b);

if(a > b)
{
printf("%d is Largest\n", a);
}
else if (b > a)
{
printf("%d is Largest\n", b);
}
else
{
printf("Both are Equal\n");
}

return 0;
}
C Program to find Largest of Three numbers using Else If Statement
#include <stdio.h>
int main()
{
int a, b, c;
printf("Please Enter three different values\n");
scanf("%d %d %d", &a, &b, &c);
if (a > b && a > c)
{
printf("\n%d is Highest Among both %d and %d", a, b, c);
}
else if (b > a && b > c)
{
printf("\n%d is Highest Among both %d and %d", b, a, c);
}
else if (c > a && c > b)
{
printf("\n%d is Highest Among both %d and %d", c, a, b);
}
else
{
printf("\nEither any two values or all the three values are equal");
}
return 0;
}
#include <stdio.h>
int main()
{
int age;
printf("Enter your age:");
scanf("%d",&age);
if(age >=18)
{
/* This statement will only execute if the
* above condition (age>=18) returns true
*/
printf("You are eligible for voting");
}
else
{
/* This statement will only execute if the
* condition specified in the "if" returns false.
*/
printf("You are not eligible for voting");
}
return 0;
}
C Nested If..else statement
Syntax of Nested if else statement:
if(condition) {
//Nested if else inside the body of "if"
if(condition2) {
//Statements inside the body of nested "if"
}
else {
//Statements inside the body of nested "else"
}
}
else {
//Statements inside the body of "else"
}
PROGRAM:
#include<stdio.h>
int main()
{
int marks=83;
if(marks>75){
printf("First class");
}
else if(marks>65){
printf("Second class");
}
else if(marks>55){
printf("Third class");
}
else{
printf("Fourth class");
}
return 0;
}

PROGRAM:
#include<stdio.h>
int main()
{
int marks;
printf("Enter you subject Marks:\n");
scanf("%d",&marks);

if(marks >= 50)


{
printf("Congratulations\n"); //s1
printf("You cleared the subject"); //s2
}
else
{
printf("You Failed\n");//s3
printf("Better Luck Next Time");//s4
}
return 0;
}
C Program to Print Even Numbers from 1 to N
/* C Program to Print Even Numbers from 1 to N using For Loop and If */

#include<stdio.h>

int main()
{
int i, number;

printf("\n Please Enter the Maximum Limit Value : ");


scanf("%d", &number);

printf("\n Even Numbers between 1 and %d are : \n", number);


for(i = 1; i <= number; i++)
{
if ( i % 2 == 0 )
{
printf(" %d\t", i);
}
}

return 0;
}

// Program to relate two integers using =, > or < symbol

#include <stdio.h>
int main() {
int number1, number2;
printf("Enter two integers: ");
scanf("%d %d", &number1, &number2);

//checks if the two integers are equal.


if(number1 == number2) {
printf("Result: %d = %d",number1,number2);
}

//checks if number1 is greater than number2.


else if (number1 > number2) {
printf("Result: %d > %d", number1, number2);
}

//checks if both test expressions are false


else {
printf("Result: %d < %d",number1, number2);
}
return 0;
}

Program to calculate the grade of the student according to the


specified marks.
1. #include <stdio.h>
2. int main()
3. {
4. int marks;
5. printf("Enter your marks?");
6. scanf("%d",&marks);
7. if(marks > 85 && marks <= 100)
8. {
9. printf("Congrats ! you scored grade A ...");
10. }
11. else if (marks > 60 && marks <= 85)
12. {
13. printf("You scored grade B + ...");
14. }
15. else if (marks > 40 && marks <= 60)
16. {
17. printf("You scored grade B ...");
18. }
19. else if (marks > 30 && marks <= 40)
20. {
21. printf("You scored grade C ...");
22. }
23. else
24. {
25. printf("Sorry you are fail ...");
26. }
27. }

You might also like