Comp. Skill Unit - II
Comp. Skill Unit - II
"
#include <stdio.h>
int main() {
printf("Hello, World!");
return 0;
#include <stdio.h>
int main() {
int number;
scanf("%d", &number);
// displays output
return 0;
#include <stdio.h>
int main()
{
int number1, number2, sum;
int main()
{
int number1, number2;
float res;
return 0;
}
#include<stdio.h>
int main()
{
int 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;
}
int main()
{
int 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
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
== equal to
!= not equal to
Logical Operators
&& Logical Returns true if both statements are true x < 5 && x <
and 10
! Logical Reverse the result, returns false if the !(x < 5 && x <
not result is true 10)
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
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);
#include<stdio.h>
int main()
{
int i, number;
return 0;
}
#include <stdio.h>
int main() {
int number1, number2;
printf("Enter two integers: ");
scanf("%d %d", &number1, &number2);