C Programming Codes
13.4K subscribers
139 photos
2 videos
12 links
C Programming Codes || Quizzes || DSA

Learn along with the community

Any queries
admin - @Pradeep_saii
Download Telegram
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;
}
👍41🙏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.
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
Program : Write a function to find maximum of two numbers.
#include <iostream>

using namespace std;
int findMax(int n1 , int n2){
if(n1 > n2){
return n1;
}
return n2;
}

int main() {
int num1,num2;
cout << "Enter any two different numbers:"<<endl;
cin >> num1 >> num2;
int max = findMax(num1,num2);
cout << "Maximum of "<< num1 << " and " << num2 << " is " << max;
return 0;
}
👍51
Pass by Value Program
#include <iostream>
using namespace std;

void passByValue(int x) {
x = 20;
}

int main() {
int a = 10;
cout << "Value of a before function call: " << a << endl;
passByValue(a);
cout << "Value of a after function call: " << a << endl;
return 0;
}
👍4
Pass by Reference
#include <iostream>
using namespace std;

void passByReference(int& x) {
x = 20;
}

int main() {
int a = 10;
cout << "Value of a before function call: " << a << endl;
passByReference(a);
cout << "Value of a after function call: " << a << endl;
return 0;
}
👍52
Method Overloading
cpp
#include <iostream>
using namespace std;

int area(int side) {
return side * side;
}

int area(int length, int breadth) {
return length * breadth;
}

int main() {
int side,length,breadth;
cout << "Enter a side length of a square:" << endl;
cin >> side;
cout << "Enter length and breadth of rectangle:" << endl;
cin >> length >> breadth;
cout << "Area of square: " << area(side) << endl;
cout << "Area of rectangle: " << area(length, breadth) << endl;
return 0;
}
8👍1
Program : Largest of Three numbers using functions.
#include <iostream>
using namespace std;

int largestOfThree(int a, int b, int c) {
int largest = a;
if(b > largest)
largest = b;
if(c > largest)
largest = c;
return largest;
}

int main() {
int first, second, third;
cout << "Enter any three numbers: ";
cin >> first >> second >> third;

int large = largestOfThree(first, second, third);
cout << "Largest number among " << first << ", " << second << ", "
<< third << " is: " << large << endl;

return 0;
}
👍43🔥1
Program : Check case of inputted character.
#include <iostream>
using namespace std;


void checkCase(char ch) {
if(ch >= 'a' && ch <= 'z'){
cout << "Entered character is in lower case" << endl;
}
else if(ch >= 'A' && ch <= 'Z'){
cout << "Entered character is in upper case" << endl;
}
else{
cout << "Invalid character entered" << endl;
}
}

int main() {
cout << "Enter any character: ";
char ch;
cin >> ch;
checkCase(ch);
return 0;
}
👍2
Program : Nth Fibonacci Number
#include <iostream>
using namespace std;
int fibonacci(int n) {
int a = 0, b = 1, temp;
if (n == 0) return a;
for (int count = 2; count <= n; count++) {
temp = b;
b = a + b;
a = temp;
}
return b;
}
int main() {
int n;
cout << "Enter the value of n: ";
cin >> n;
cout << n << "th Fibonacci number is: " << fibonacci(n) << endl;
return 0;
}
3👍3
Program : Nth Fibonacci Number
#include <iostream>
using namespace std;
int fibonacci(int n) {
int a = 0, b = 1, temp;
if (n == 0) return a;
for (int count = 2; count <= n; count++) {
temp = b;
b = a + b;
a = temp;
}
return b;
}
int main() {
int n;
cout << "Enter the value of n: ";
cin >> n;
cout << n << "th Fibonacci number is: " << fibonacci(n) << endl;
return 0;
}
👍21
Program: Counting Occurrences of a digit in a number.
#include <iostream>
using namespace std;

int countOccurrences(int number, int targetDigit) {
int count = 0;
while (number > 0) {
int lastDigit = number % 10;
if (lastDigit == targetDigit) {
count++;
}
number /= 10;
}
return count;
}

int main() {
int number, targetDigit;
cout << "Enter a number: ";
cin >> number;
cout << "Enter the digit to count occurrences of: ";
cin >> targetDigit;
int result = countOccurrences(number, targetDigit);
cout << "The digit " << targetDigit << " appears " << result << " times in " << number << "." << endl;

return 0;
}
👍32🔥2
Program: Reversing of a number.
#include <iostream>
using namespace std;

int reverseNumber(int num) {
int reversedNum = 0;
while (num > 0) {
int rem = num % 10;
reversedNum = reversedNum * 10 + rem;
num /= 10;
}
return reversedNum;
}

int main() {
int num;
cout << "Enter the number to reverse: ";
cin >> num;
int reversedNum = reverseNumber(num);
cout << "Reverse of " << num << " is " << reversedNum << endl;
return 0;
}
👍6🔥4👏1
Program : Prime or Not
#include <iostream>
using namespace std;

bool isPrime(int num) {
if (num < 2) {
return false;
}
for (int i = 2; i * i <= num; i++) {
if (num % i == 0) {
return false;
}
}
return true;
}

int main() {
int num;
cout << "Enter any number: ";
cin >> num;
if (isPrime(num)) {
cout << num << " is a prime number" << endl;
} else {
cout << num << " is not a prime number" << endl;
}
return 0;
}
👍53
Program : Armstrong or Not
#include <iostream>
#include <cmath>
using namespace std;

bool isArmstrong(int num) {
int originalNum = num, sum = 0, digits = to_string(num).length();
while (num > 0) {
int rem = num % 10;
sum += pow(rem, digits);
num /= 10;
}
return originalNum == sum;
}

int main() {
int num;
cout << "Enter any number: ";
cin >> num;

if (isArmstrong(num)) {
cout << num << " is an Armstrong number." << endl;
} else {
cout << num << " is not an Armstrong number." << endl;
}
return 0;
}
👍52
Program : Finding Maximum element in an Array.
#include <iostream>
using namespace std;

int findMax(int arr[], int size) {
int maxElem = arr[0];
for(int i = 1; i < size; i++) {
if(maxElem < arr[i]) {
maxElem = arr[i];
}
}
return maxElem;
}

int main() {
int arr[] = {10, 5, 1, 3, 6};
int size = sizeof(arr) / sizeof(arr[0]);
int maxElem = findMax(arr, size);
cout << "Maximum Element : " << maxElem << endl;
return 0;
}
👍81
Program: Reversing of an Array.
#include <iostream>
#include <vector>
using namespace std;

void reverseArray(vector<int>& arr) {
int n = arr.size();
for (int i = 0; i < n / 2; i++) {
swap(arr[i], arr[n - 1 - i]);
}
}

int main() {
vector<int> arr = {1, 2, 3, 4, 5, 6};
reverseArray(arr);

for (size_t i = 0; i < arr.size(); i++) {
cout << arr[i] << " ";
}
cout << endl;

return 0;
}
👍43
Program: Searching for first occurrence of a character in a string.
#include <iostream>
using namespace std;

int searchInStr(string str, char ch) {
for (int i = 0; i < str.length(); i++) {
if (str[i] == ch) {
return i;
}
}
return -1;
}

int main() {
string str = "Java Programming";
char ch;
cout << "Enter character to search: ";
cin >> ch;
int res = searchInStr(str, ch);
if (res == -1) {
cout << "Character not found in the String" << endl;
} else {
cout << "Character is at index " << res << endl;
}
return 0;
}
👍51
Program: Searching for a character in specified range.
#include <iostream>
#include <string>

using namespace std;

int searchInStr(const string &str, char ch, int start, int end) {
for (int i = start; i <= end; i++) {
if (str[i] == ch) {
return i;
}
}
return -1;
}

int main() {
string str = "Java Programming";
char ch;
int start, end;

cout << "Enter character to search: ";
cin >> ch;

cout << "Enter the range,\n";
cout << "Enter the start index: ";
cin >> start;
cout << "Enter the end index: ";
cin >> end;

int res = searchInStr(str, ch, start, end);

if (res == -1) {
cout << "Character not found in the specified range" << endl;
} else {
cout << "Character is at index " << res << endl;
}

return 0;
}
👍2
Program: Finding minimum element in an array.
#include <iostream>
using namespace std;

int findMin(int arr[], int size) {
int min = arr[0];
for (int i = 1; i < size; i++) {
if (arr[i] < min) {
min = arr[i];
}
}
return min;
}

int main() {
int arr[] = {12, 32, 23, 11, 39};
int size = sizeof(arr) / sizeof(arr[0]);
int minNumber = findMin(arr, size);
cout << "Minimum number in array: " << minNumber << endl;
return 0;
}
👍32🔥1