Which individual is often referred to as the "father of C" programming language?
Anonymous Quiz
79%
a) Dennis Ritchie
7%
b) Ken Thompson
8%
c) Bjarne Stroustrup
6%
d) James Gosling
👍19❤9🔥3
#include <stdio.h>
int main() {
int x = 5;
printf("%d", x++);
return 0;
}
👍11🥰10❤5👏3
👍34🤯19❤8👏7👌7😁4🤔1🙏1
Which keyword is used to define a constant in C?
Anonymous Quiz
67%
a) const
6%
b) final
13%
c) define
13%
d) constant
👍26🤯7❤3👏1😁1
#include <stdio.h>
int main() {
int x = 10;
int *ptr = &x;
*ptr = 20;
printf("%d\n", x);
return 0;
}
👍40🤯9❤5👏3👌3
Output of above code?
Anonymous Quiz
33%
A) 10
14%
B) Undefined Behavior
43%
C) 20
10%
D) Compilation Error
🤯36👍31😁7
Printing Hello World
#include <iostream>
int main() {
std::cout << "Hello World";
return 0;
}
👍11❤7
Program to swap values of two variables.
#include <iostream>
int main() {
int a=1;
int b=2;
int temp;
std::cout << "Before Swapping:\n";
std::cout << "a=" << a <<",b=" << b <<"\n";
temp = a;
a=b;
b=temp;
std::cout << "After Swapping:\n";
std::cout << "a=" << a <<",b=" << b <<"\n";
return 0;
}
👍11👏2❤1
Program to convert temperature from Fahrenheit to Celsius.
#include <iostream>
using namespace std;
int main() {
double fahrenheitTemperature;
double temperatureInCelsius;
cout << "Enter your temperature in Fahrenheit:";
cin >> fahrenheitTemperature;
temperatureInCelsius = ((fahrenheitTemperature - 32) * 5) / 9;
cout << "Temperature in celsius : " << temperatureInCelsius;
return 0;
}
👍8❤3🔥2
Program to calculate area of circle.
#include <iostream>
#include <cmath>
using namespace std;
int main() {
double radius;
double area;
const double PI = 3.14;
cout << "Enter the radius of the circle: ";
cin >> radius;
area = PI * pow(radius,2);
cout << "Area of circle: "<< area;
return 0;
}
👍8❤2
Program for rolling dice.
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main() {
int result;
const short maxValue = 6;
const short minValue = 1;
srand(time(nullptr));
result = (rand() % (maxValue - minValue + 1)) + minValue;
cout << result;
return 0;
}
👍12
Program: Swap two numbers without using a third variable.
#include <iostream>
using namespace std;
int main() {
int a, b;
cout << "Enter two numbers: ";
cin >> a >> b;
a = a + b;
b = a - b;
a = a - b;
cout << "After swapping: " << a << " " << b << endl;
return 0;
}
❤5👍3
Program: Check if a number is even or odd using the modulus operator.
#include <iostream>
using namespace std;
int main() {
int num;
cout << "Enter a number: ";
cin >> num;
if (num % 2 == 0)
cout << num << " is Even." << endl;
else
cout << num << " is Odd." << endl;
return 0;
}
👍8❤3
Program: Find the largest of three numbers using if-else.
#include <iostream>
using namespace std;
int main() {
int a, b, c;
cout << "Enter three numbers: ";
cin >> a >> b >> c;
if (a >= b && a >= c)
cout << "Largest number is: " << a << endl;
else if (b >= a && b >= c)
cout << "Largest number is: " << b << endl;
else
cout << "Largest number is: " << c << endl;
return 0;
}
👍6❤2
Program: Check if a given year is a leap year.
#include <iostream>
using namespace std;
int main() {
int year;
cout << "Enter a year: ";
cin >> year;
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
cout << year << " is a Leap Year." << endl;
else
cout << year << " is not a Leap Year." << endl;
return 0;
}
👍10❤1
Program: Implement a calculator using a switch case.
#include <iostream>
using namespace std;
int main() {
char op;
double num1, num2;
cout << "Enter an operator (+, -, *, /): ";
cin >> op;
cout << "Enter two numbers: ";
cin >> num1 >> num2;
switch (op) {
case '+': cout << "Result: " << num1 + num2 << endl; break;
case '-': cout << "Result: " << num1 - num2 << endl; break;
case '*': cout << "Result: " << num1 * num2 << endl; break;
case '/':
if (num2 != 0)
cout << "Result: " << num1 / num2 << endl;
else
cout << "Error! Division by zero." << endl;
break;
default: cout << "Invalid operator!" << endl;
}
return 0;
}
👍7❤1
Program : To calculate factorial of a given number
cpp
#include <iostream>
using namespace std;
int main() {
int num,res=1;
cout << "Enter a positive number:" << endl;
cin >> num;
if(num < 0){
cout << "You entered a negative number" << endl;
}
else if(num == 0){
cout << "0! = " << "1";
}
else{
for(int i = 1;i <= num;i++){
res = res * i;
}
cout << "Factorial of " << num << " is "<<res;
}
return 0;
}
❤4👍4
Program: Given an array with numbers 11,12,13,14,15 calculate the sum of elements in array using range based for loop.
cpp
#include <iostream>
using namespace std;
int main(){
int numbers[] = {11,12,13,14,15};
int sum = 0;
for(int number : numbers){
sum = sum + number;
}
cout << "Sum = "<< sum;
return 0;
}
👍4❤1🙏1
Program : Design a number guessing game using while loop.
Take range of numbers from 1 - 10
hard code any number between 1 and 10
Ask user to guess the number after guessing display number of attempts made to guess the number.
Take range of numbers from 1 - 10
hard code any number between 1 and 10
Ask user to guess the number after guessing display number of attempts made to guess the number.
cpp
#include <iostream>
using namespace std;
int main()
{
int numberToGuess = 4;
int attempts = 0;
int guess = 0;
while (guess != numberToGuess)
{
cout << "Guess the number between 1 and 10:" << endl;
cin >> guess;
attempts++;
if(guess == numberToGuess){
cout << "You guessed it right in " << attempts << " attempts" << endl;
}
}
cout << "Game ended";
return 0;
}
👍8
Program : Sum on n natural numbers using do while loop.
#include <iostream>
using namespace std;
int main() {
int n,sum=0,i=1;
cout << "Enter the value of n:";
cin >> n;
do{
sum = sum + i;
i++;
}while(i <= n);
cout << "Sum of "<< n << " natural numbers :" << sum;
}
❤2👍2