0% found this document useful (0 votes)
3 views

C Code

c+++code if else
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)
3 views

C Code

c+++code if else
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/ 29

CODE’S C++

#include <iostream>

using namespace std;

1, int main()
{
int num1 = 55, num2 = 24, mod;
mod =num1 % num2;

cout << "The Remainder is:" << mod;

2, int a=6, b=2;


cout << (a<b)<<endl;
cout << (a<=b)<<endl;
cout << (a>b)<<endl;
cout << (a>=b)<<endl;
cout << (a==b)<<endl;
cout << (a!=b)<<endl;

3, cout << "((3==3) || (3<5))=" <<((3==3) || (3<5)) << endl;


cout << "((3>3) || (3<5)="<<((3>3) || (3<5))<<endl;
cout << "((3!=) || (3>5)="<<((3!=3) || (3>5))<<endl;

4, cout << "((3==3) && (3<5))=" <<((3==3) && (3<5)) << endl;
cout << "((3>3) && (3<5)="<<((3>3) && (3<5))<<endl;
cout << "((3!=) && (3>5)="<<((3!=3) && (3>5))<<endl;

5, int a=5;
int b = a++; //assign -> update
cout << b<<endl;//5
cout << a<<endl;//6

6, /int a =5;
int b =++a; // update -> assign
cout <<b<< endl; //5
cout <<a<< endl; //6
7, int age;
cout<<"Enter your age";
cin>>age;
if(age<=0)
{cout<<"Invalid Age"<<endl;}
else{
cout<<age
}
return 0;
}

8, int score;
cout<<"Enter your Marks:";
cin>>score;

if(score >=90){
cout<< "Grade A" <<endl;
}
else if(score>=80 && score < 90){
cout<<"Grade B" << endl;
}
else if (score>=70 && score < 80){
cout << "Grade C" << endl;
}
else{
cout<<"Fail"<<endl;
}
return 0;

9, int number;
cout<<"Enter a Number:";
cin>>number;

if(number>=0){
cout<<"Positive Number"<<endl;
}
else{
cout<< "Negative Number"<<endl;
return 0;
10, #include <iostream>

using namespace std;

int main ()

{
cout << "size of int is:" << sizeof(int)<< "Bytes" << endl;
cout << "size of char is:" << sizeof(char)<< "Bytes" << endl;
cout << "size of float is:" << sizeof(float)<< "Bytes" << endl;
cout << "size of double is:" << sizeof(double)<< "Bytes" << endl;
cout << "size of bool is:" << sizeof(bool)<< "Bytes" << endl;

return 0 ;

11, #include <iostream>

using namespace std;

int main()

{ int num;
cout << "Enter a Number"<< endl;
cin >> num;
int cube = num * num * num;

cout<< "your entered number : " << num <<endl;


cout<< "your entered number cube is : " << cube <<endl;

return 0;
}
12, #include <iostream>

using namespace std;

int main()

{
float eng,maths,phy,sum,per;
cout << " Enter Marks for english:" <<endl;
cin >> eng;
cout << "Enter marks for Maths:" <<endl;
cin>> maths;
cout << " Enter marks for physics:"<<endl;
cin >> phy;
sum = eng+maths+phy;
cout << "\n Total marks:" << sum;
per = (sum/300)*100;
cout << "\n percentage:" << per << "%" <<endl;

return 0;
}

Today Class 11/5/2024

13, #include <iostream>

using namespace std;

int main()
{
int age=45;
if(age>18){
cout<<"Eligiable to vote";
}
else{
cout<<"Not Eligible to vote";
}
return 0;
}

LAB MANUAL
14, (Greatest Number)
#include <iostream>

using namespace std;

int main()
{
int Num1,Num2,Num3;
cout<<"Enter First Number:";
cin>>Num1;
cout<<"Enter Second Number:";
cin>>Num2;
cout<<"Enter Third Number:";
cin>>Num3;

if(Num1>Num2 && Num1>Num3){

cout<<Num1 <<"iS Greatest!";


}
else if(Num2>Num1 && Num1>Num3){

cout<<Num2<< "Is Greatest!";


}

else{

cout<<"Number 3 is Greatest";
}

return 0;
}

15, (Variable Swapping)


#include <iostream>

using namespace std;

int main()
{
int a,b,temp;
cout<<"Enter first variable:";
cin>>a;
cout<<"Enter Second variable";
cin>>b;
temp=b; //2 step
b=a; //3 step
a=temp;// 4 step

cout<<"After swapping:" <<endl;


cout<<"variable 1:"<<a<<endl;
cout<<"Variable 2:"<<b<<endl;

return 0;
}

16, (Leap Year)


#include <iostream>

using namespace std;

int main()
{
int year;
cout<<"Enter Leap Year:"<<endl;
cin>>year;

if(year&4==0){
cout<<year<<"It is a leap year.";
}
else{
cout<<"It is no a leap year";
}
return 0;
}

17, (Positive Number and Negative Number)


#include <iostream>

using namespace std;

int main()
{
int num1;
cout<<"Enter Number:"<<endl;
cin>>num1;
if(num1>0){
cout<<"Positive Number";
}
else{
cout<<"Negative Number";
}
return 0;
}

MID TASK

18,(Even and Odd Number)


#include <iostream>

using namespace std;

int main()
{
int num;
cout<<"Enter Number:"<<endl;
cin>>num;
if(num%2==0){
cout<<"Even Number"<<endl;
}
else{
cout<<"Odd Number"<<endl;
} return 0;
}

19, (Number is Less Than 10 and Greater than 100)

#include <iostream>

using namespace std;

int main()
{
int num;
cout<<"Enter the Number"<<endl;
cin>>num;

if(num<10 || num>100){
cout<<"The number is either less than 10 or greater than 100"<<endl;
}
else{
cout<<"The number is between 10 and 100"<<endl;
}
return 0;
}

20, (Age is Greater than 60 and You are a Member, and You are Eligible for Discount)
#include <iostream>

using namespace std;

int main()
{
int age;
char isMember;

cout<<"Enter Your age:"<<endl;


cin>>age;

cout<<"Are you member? (Y/N):"<<endl;


cin>>isMember;

if (age>=60 && (isMember == 'Y' || isMember == 'y')){


cout<<"You are eligible for discount"<<endl;
}
else if (age>=60 && (isMember == 'N' || isMember == 'y')){
cout<<"You are not eligible for discount"<<endl;
}
else{
cout<<"You are not eligible for discount"<<endl;
}
return 0;
}

21, (Grading System)


#include <iostream>

using namespace std;

int main()
{
int score;
cout<<"Enter score:"<<endl;
cin>>score;

if(score<0 || score>100){
cout<<"Invalid input.Score should be between 0 to 100"<<endl;
}
else if(score>=90){
cout<<"Grade A";
}
else if(score>=80 && score<90){
cout<<"Grade B";
}
else if(score>=70 && score<80){
cout<<"Grade C";
}
else if(score>=60 && score<70){
cout<<"Grade D";
}
else{
cout<<"Grade F"<<endl;
}
return 0;
}

Today Class 19/11/2024

22, Grading System(Nested If) Lab 9

#include <iostream>

using namespace std;

int main()
{
int marks= 50;
if(marks>0 && marks<=100){
if(marks>=90)
cout<<"Grade A"<<endl;
else if (marks>=80)
cout<<"Grade B"<<endl;
else if (marks>=70)
cout<<"Grade C"<<endl;
else
cout<<"Fail!!"<<endl;

}
else{
cout<<"Marks Should be Between 0-100";
}
return 0;
}

23, Homework Task

24, Print Hello World 100 times (For statement)

#include <iostream>

using namespace std;

int main()
{
int marks= 50;
if(marks>0 && marks<=100){
if(marks>=90)
cout<<"Grade A"<<endl;
else if (marks>=80)
cout<<"Grade B"<<endl;
else if (marks>=70)
cout<<"Grade C"<<endl;
else
cout<<"Fail!!"<<endl;

}
else{
cout<<"Marks Should be Between 0-100";
}
return 0;
}

25, Odd Numbers (For Statement)

#include <iostream>

using namespace std;

int main()
{
for(int i= 1; i<=100; i+=2){
cout<< i << "Odd Numbers" << endl;
}

return 0;
}

26, Even Numbers (For Statement)

#include <iostream>

using namespace std;

int main()
{
for(int i= 0; i<=100; i+=2){
cout<< i << "Odd Numbers" << endl;
}

return 0;
}

27, (Single Line Print 1 till 10 numbers)

#include <iostream>

using namespace std;

int main()
{
for (int i = 1; i <= 10; i++) {
cout << i << " ";
}

cout << endl;

return 0;
}

28, Sum of Numbers 1 to 5 (For Statement)

#include <iostream>

using namespace std;

int main()
{
int sum= 0;
for(int i=1; i<=5; i++){
sum+=1;
}
cout<<"Sum of Number's from 1 to 5 is:" << sum<<endl;
return 0;
}

29, Odd Number’s (While Loop)

#include <iostream>

using namespace std;

int main()
{
int i = 1;

while(i<=15){
cout<< i << " ";
i+=2;
}
return 0;
}

30, (Guess Secret Number While Loop)


#include <iostream>

using namespace std;

int main()
{
int secretnumber = 6;
int guess;

cout<<"Guess The secret Number between 1 to 10:";


cin>> guess;

while(guess != secretnumber){
cout<<"Wrong Guess! Try again:"<<endl;
cin>> guess;
}
cout<< "Congratulates! You guess the secret Number"<<endl;
return 0;
}

31, Secret Number Code(While,For)

#include <iostream>

using namespace std;

int main()
{
int secretnumber = 6;
int guess;

cout<<"Guess The secret Number between 1 to 10 ";


cout<<"You have three attemps :";
cin>> guess;
if (guess < secretnumber) {
cout << "Two attempt left: ";
}
else if (guess > secretnumber) {
cout << "1 attempt left: ";
}
else{
cout<<"Your attempt is finished"<<endl;
}

while(guess != secretnumber){
cout<<"Wrong Guess! Try again:";
cin>> guess;
}
cout<< "Congratulates! You guess the secret Number"<<endl;
return 0;
}

31, Area Length Parameter of Rectangle (Lab Mannuel)

#include <iostream>

using namespace std;

int main()
{
int length,width,area,parameter;

cout<<"Enter Length:"<<endl;
cin>>length;
cout<<"Enter Width:"<<endl;
cin>>width;

area=length*width;
parameter=(2*(length+width));
cout<<"Area of rectangle: "<<area<<endl;
cout<<"parameter of rectangle: "<<parameter<<endl;
return 0;
}

32, (Vowel)
#include <iostream>

using namespace std;

int main()
{
char alpha;
cout<<"Enter Vowel"<<endl;
cin>>alpha;

if(alpha=='a' || alpha=='e' || alpha=='i' || alpha=='o' || alpha=='u'){


cout<<"Its Vowel"<<alpha<<endl;

}
else{
cout<<"Its Not Vowel: "<<alpha<<endl;
}
return 0;
}

33, Odd and Even number

#include <iostream>

using namespace std;

int main()
{
int number;
cout<<"Enter Number"<<endl;
cin>>number;

if(number%2==0){
cout<<"Even Number"<<endl;
}
else{
cout<<"Odd Number"<<endl;
}
return 0;
}

34, Case Statement: Months Task (Final Paper)


#include <iostream>
using namespace std;

int main()
{
int month;
cout << "Enter month number (1-12): ";
cin >> month;

switch(month) {
case 1:
cout << "January" << endl;
break;
case 2:
cout << "February" << endl;
break;
case 3:
cout << "March" << endl;
break;
case 4:
cout << "April" << endl;
break;
case 5:
cout << "May" << endl;
break;
case 6:
cout << "June" << endl;
break;
case 7:
cout << "July" << endl;
break;
case 8:
cout << "August" << endl;
break;
case 9:
cout << "September" << endl;
break;
case 10:
cout << "October" << endl;
break;
case 11:
cout << "November" << endl;
break;
case 12:
cout << "December" << endl;
break;
default:
cout << "Invalid month number!" << endl;
}

return 0;
}

35, Simple Calculator Switch Statement


#include <iostream>
using namespace std;

int main()
{
float num1,num2;
char op;
cout<<"Enter first number"<<endl;
cin>>num1;
cout<<"Enter second number"<<endl;
cin>>num2;
cout<<"Enter operator(+,-,*,/):"<<endl;
cin>>op;

switch(op){

case '+':
cout<<num1+num2<<endl;
break;
case '-':
cout<<num1-num2<<endl;
break;
case '*':
cout<<num1*num2<<endl;
break;
case '/':
cout<<num1/num2<<endl;
break;

default:
cout<<"Matches None"<<endl;
}

return 0;
}

Lab Manuel Exercise

36, Grade input A To F display comments Switch Statement (LAB Mannuel)

#include <iostream>
using namespace std;

int main()
{
char grade;
cout<<"Enter Grade (A-F): "<<endl;
cin>>grade;
grade=toupper(grade);

switch(grade){

case 'A':
cout<<"Excellent Work! Keep it Up!"<<endl;
break;
case 'B':
cout<<"Great Job You are doing well"<<endl;
break;
case 'C':
cout<<"Good Effort! You can do even better"<<endl;
break;
case 'D':
cout<<"Very Good! Never Leave the Hope"<<endl;
break;
case 'E':
cout<<"Good Try! You can try more"<<endl;
break;
case 'F':
cout<<"Ohh No!! Better luck next time"<<endl;
break;

default:
cout<<"invalid"<<endl;
}

return 0;
}

37, Area of Circle,Square,Triangle With the help of Switch Statement:(Lab Mannuel)

#include <iostream>
#include <cmath>
using namespace std;

int main()
{
int choice;
float area;

//Display Menu for Shape Collection


cout<<"Calculate area of Different Shapes: "<<endl;
cout<<"Select Options: "<<endl;
cout<<"1, Circle"<<endl;
cout<<"2, Square"<<endl;
cout<<"3, Triangle"<<endl;
cin>>choice;

switch(choice){
case 1:
float radius;
cout<<"Enter radius"<<endl;
cin>>radius;
area=M_PI*radius*radius;
cout<<"Area of circle is: "<<area<<endl;
break;
case 2:
float side;
cout<<"Enter the side length of square: ";
cin>>side;
area = side*side;
cout<<"The area of the square is:" <<area<<endl;
break;
case 3:
float height,base;
cout<<"Enter height: "<<endl;
cin>>height;
cout<<"Enter base: "<<endl;
cin>>base;
area = 0.5* height * base;
cout<<"The Area of the triangle is:"<<area<<endl;
break;

default:
cout<<"invalid input"<<endl;
}
return 0;
}

37, Factorial of a Number (Lab Manuel) Important question


#include <iostream>

using namespace std;

int main()
{
int num,fact=1;
cout<<"Enter a positive integer:";
cin>>num;

for(int i = num; i>0; i--){


fact = fact*i;
}
cout<<fact;
return 0;
}

38, Prime Number(Lab Manuel) important question


#include <iostream>

using namespace std;

int main() {
int num, i, count = 0;
cout << "Enter a number: ";
cin >> num;

for (i = 2; i < num; i++) {


if (num % i == 0) {
count = count + 1;
break;
}
}

if (num <= 1) {
cout << "Not a prime number." << endl;
}
else if (count == 0) {
cout << "Prime number." << endl;
}
else {
cout << "Not a prime number." << endl;
}

return 0;
}

39, Fibonacci Series (Lab Manuel) Important Question


#include <iostream>

using namespace std;

int main() {
int n, t1 = 0, t2 = 1, nextTerm;

cout << "Enter the number of terms: ";


cin >> n;

cout << "Fibonacci Series: ";

for (int i = 1; i <= n; ++i) {


cout << t1 << " ";
nextTerm = t1 + t2;
t1 = t2;
t2 = nextTerm;
}

return 0;
}

40, Write a program to draw the following pattern(Lab Manuel)important Question


1
12
123
1234
12345
#include <iostream>

using namespace std;

int main() {
int rows;
cout << "Enter Number of Rows: ";
cin >> rows;

for (int i = 1; i <= rows; ++i) {

for (int j = 1; j <= i; ++j) {


cout << j << " ";
}
cout << endl;
}

return 0;
}

41 , 12/17/2024 Array Topics


Example

#include <iostream>

using namespace std;

int main()
{
string a[2]={"Hello","BSAI"};
cout<<a[0];
cout<<a[1];

42, Example 2
#include <iostream>

using namespace std;

int main()
{
string cars[4]={"Volvo","BMW","Ford","Mazda"};
cars[0]="Audi";
cout<<cars[0];
cout<<cars[1];
return 0;
}

43, Example 3

#include <iostream>

using namespace std;

int main()
{
string cars[5]={"Volvo","BMW","Ford","Mazda","Audi"};
for(int i = 0; i < 5; i++){
cout << cars[i]<<"\n";
}

return 0;
}

44, Example 4

#include <iostream>

using namespace std;

int main()
{
string cars[5]={"Volvo","BMW","Ford","Mazda","Audi"};
for(int i = 0; i < 5; i++){
cout << i << "="<<cars[i]<<"\n";
}

return 0;
}

45,Example 5
#include <iostream>

using namespace std;

int main()
{
int myNumbers[5] = {10,20,30,40,50};
for(int i = 0; i < 5; i++){
cout<<myNumbers[i]<<"\n";
}

return 0;
}

46,Example 6
#include <iostream>

using namespace std;

int main()
{
string cars[]={"Volvo","BMW","Ford"};
cout<<cars[0]<<endl;
cout<<cars[1]<<endl;
cout<<cars[2]<<endl;

return 0;
}

47 Example 7
#include <iostream>

using namespace std;

int main()
{
int n;
cout<<"Enter Number of array elements:";
cin>>n;

int array[n];

cout<<"Enter value of array elements:";

for (int i=0; i<n; i++)


cin>>array[i];

for(int i=0; i<n; i++)


cout<<array[i]<<" ";

return 0;
}

48, Lab Task Of Array


49, Write a program to add two arrays.(Lab Task)

#include <iostream>
using namespace std;

int main() {
int a[2] = {0,1};
int b[2] = {2,3};
int c[2];

c[0] = a[0] + b[0];


c[1] = a[1] + b[1];

cout<< "Resultant Array:" << c[0] <<" " <<c[1]<<endl;


return 0;
}

50, User input addition (addition)

#include <iostream>
using namespace std;

int main() {
int n;

cout<<"enter number of elements for array"<<endl;


cin>>n;

int arr1[n],arr2[n],sum[n];

cout<<"Enter elements of the first array: ";


for (int i=0; i<n; i++)
cin>>arr1[i];

cout<<"Enter elements of the second array: ";


for (int i=0; i<n; i++)
cin>>arr2[i];

for (int i=0; i<n; i++)


sum[i]=arr1[i]+arr2[i];

cout<<"Result of addition of both arrays is: ";


for (int i=0; i<n; i++)
cout<<sum[i]<<" ";
cout<<endl;

return 0;
}

51,Multiplication Table Y/N


#include <iostream>

using namespace std;

int main() {
int number;
char choice;

do {
// Ask the user for the number
cout << "Enter a number to generate its multiplication table: ";
cin >> number;

// Display the multiplication table for the entered number


cout << "Multiplication Table for " << number << ":\n";
for (int i = 1; i <= 10; i++) {
cout << number << " x " << i << " = " << number * i << endl;
}

// Ask the user if they want to continue or exit


cout << "Do You Want To Continue (Y/N): ";
cin >> choice;

} while (choice == 'Y' || choice == 'y'); // Continue if user enters 'Y' or 'y'

cout << "Program Exiting... Goodbye!" << endl;

return 0;
}

52,Factorial of Number (While Loop)


#include <iostream>

using namespace std;

int main() {
int num, factorial = 1;

// Ask the user to enter a number


cout << "Enter a number to find its factorial: ";
cin >> num;
// Check if the number is negative, as factorial is not defined for negative numbers
if (num < 0) {
cout << "Factorial is not defined for negative numbers." << endl;
} else {
int i = 1;

// Using a while loop to calculate the factorial


while (i <= num) {
factorial *= i; // Multiply the factorial by the current value of i
i++; // Increment i
}

// Display the result


cout << "Factorial of " << num << " is: " << factorial << endl;
}

return 0;
}

53, 2-D array Example


#include <iostream>

using namespace std;

int main()
{
int arr[3][3]={{1,2,3},{4,5,6},{7,8,9}};

return 0;
}

54, 2-D and 3-D array Example

#include <iostream>

using namespace std;

int main()
{

int arr2D[3][3] = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
int arr3D[2][2][3] = {
{{1, 2, 3}, {3, 4, 5}},
{{1, 2, 4}, {3, 4, 5}}
};

return 0;
}

55, Print Matrix 2*2


#include <iostream>
using namespace std;

int main() {
int matrix1[2][2];
int matrix2[2][2];
int result[2][2];

cout << "\nInput First (2x2) Matrix:\n";


for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
cout << "Enter the value of row " << (i + 1) << ", column " << (j + 1) << ": ";
cin >> matrix1[i][j];
}
}

cout << "\nInput Second (2x2) Matrix:\n";


for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
cout << "Enter the value of row " << (i + 1) << ", column " << (j + 1) << ": ";
cin >> matrix2[i][j];
}
}

for (int i = 0; i < 2; i++) {


for (int j = 0; j < 2; j++) {
result[i][j] = matrix1[i][j] + matrix2[i][j];
}
}

cout << "\nFirst Matrix:\n";


for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
cout << matrix1[i][j] << " ";
}
cout << endl;
}

cout << "\nSecond Matrix:\n";


for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
cout << matrix2[i][j] << " ";
}
cout << endl;
}

cout << "\nSum of the Matrices (Result Matrix):\n";


for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
cout << result[i][j] << " ";
}
cout << endl;
}

return 0;
}

56, Its Vowel (For Practice Exercise)


#include <iostream>

using namespace std;

int main()
{
char alpha;
cout<<"Enter Vowel: "<<endl;
cin>>alpha;

if(alpha=='a' || alpha=='e' || alpha=='i' || alpha=='o' || alpha=='u'){


cout<<"Its Vowel: "<<alpha<<endl;
}
else if(alpha=='A' || alpha=='E' || alpha=='I' || alpha=='O' || alpha=='U'){
cout<<"Its Vowel: "<<alpha<<endl;

}
else{
cout<<"Its Not Vowel: "<<alpha<<endl;
}
return 0;
}

57, Its Vowel Switch Case (For Practice Exercise)


#include <iostream>
using namespace std;
int main() {
char alpha;
cout << "Enter a character: ";
cin >> alpha;

switch(alpha) {
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
cout << "It's a Vowel: " << alpha << endl;
break;
default:
cout << "It's Not a Vowel: " << alpha << endl;
}

return 0;
}

58,

You might also like