SlideShare a Scribd company logo
Common Problems
Solving using C Language
Instructor:
Arghodeep Paul
Firmware Engineer at BitBible Technologies Pvt. Ltd.
Content Author: Arghodeep Paul
License: OpenSource
Date: 01 August 2021
IF ELSE:
1. Check whether the user entered number is positive.
#include<stdio.h>
int main(){
int number;
printf("Enter a number:");
scanf("%d",&number);
if(number>0){
printf("%d is a positive number!",number);
}
else{
printf("%d is a negative number",number);
}
return 0;
}
2. Write a program to check whether a number is even
or odd.
#include<stdio.h>
int main(){
int num;
printf("Enter a number:");
scanf("%d",&num);
if(num%2==0){
printf("%d is an even number!",num);
}
else{
printf("%d is an odd number",num);
}
return 0;
}
FOR LOOP:
3. Write a program to display first 10 natural numbers
#include <stdio.h>
int main()
{
int i;
printf("The first 10 natural numbers are:n");
for (i=1;i<=10;i++)
{
printf("%d ",i);
}
printf("n");
return 0;
}
4. Write a program to find biggest among two numbers.
#include<stdio.h>
int main()
{
int n1,n2,sum;
printf("nEnter 1st number : ");
scanf("%d",&n1);
printf("nEnter 2nd number : ");
scanf("%d",&n2);
if(n1 > n2)
printf("n1st number is greatest.");
else
printf("n2nd number is greatest.");
return 0;
}
WHILE LOOP:
5. Write a program to display natural numbers upto a
given range.
#include <stdio.h>
int main()
{
int num,i;
printf("Enter a number : ");
scanf("%d", &num);
i = 0;
while (i <= num)
{
printf("n%d",i);
i++;
}
return 0;
}
6. Write a program to display sum of first 5 natural
number.
#include <stdio.h>
int main()
{
int num, i, sum = 0;
printf("Enter a positive number : ");
scanf("%d", &num);
i = 0;
while (i <= num)
{
sum = sum + i;
i++;
}
printf(" n Sum of first %d natural number is : %d", num,
sum);
return 0;
}
7. Write a program to generate the following series 1 4
9 16 …. 100
#include<stdio.h>
int main()
{
int i;
for(i=1;i<=10;i++)
{
printf("n%d",i*i);
}
return 0;
}
8. Write a program to find out sum of first 10 natural
numbers
int main()
{
int i, sum = 0;
i = 0;
while (i <= 10)
{
sum = sum + i;
i++;
}
printf(" n Sum of first 10 natural number is : %d", sum);
return 0;
}
9. Write a program to read 10 numbers and find their
sum and average
#include <stdio.h>
int main()
{
int i,n,sum=0;
float avg;
printf("Input the 10 numbers : n");
for (i=1;i<=10;i++)
{
printf("Number-%d :",i);
scanf("%d",&n);
sum +=n;
}
avg=sum/10.0;
printf("The sum of 10 no is : %dnThe Average
is : %fn",sum,avg);
return 0;
}
10. Find cube of the number upto a given integer
#include <stdio.h>
int main()
{
int i,ctr;
printf("Input number of terms : ");
scanf("%d", &ctr);
for(i=1;i<=ctr;i++)
{
printf("Number is : %d and cube of the %d is :%d n",i,i,
(i*i*i));
}
return 0;
}
11. Compute multiplication table of a given integer
#include <stdio.h>
int main()
{
int j,n;
printf("Input the number (Table to be calculated) : ");
scanf("%d",&n);
printf("nn");
for(j=1;j<=10;j++)
{
printf("%d X %d = %d n",n,j,n*j);
}
return 9;
}
12. Display n number of multiplication table vertically
#include <stdio.h>
int main()
{
int j,i,n;
printf("Input upto the table number starting from 1 : ");
scanf("%d",&n);
printf("Multiplication table from 1 to %d n",n);
for(i=1;i<=10;i++)
{
for(j=1;j<=n;j++)
{
if (j<=n-1)
printf("%dx%d = %d, ",j,i,i*j);
else
printf("%dx%d = %d",j,i,i*j);
}
printf("n");
}
return 0;
}
13. Check whether the entered year is leap year or not.
#include <stdio.h>
int main() {
int year;
printf("Enter a year: ");
scanf("%d", &year);
// leap year if perfectly divisible by 400
if (year % 400 == 0) {
printf("%d is a leap year.", year);
}
// not a leap year if divisible by 100
// but not divisible by 400
else if (year % 100 == 0) {
printf("%d is not a leap year.", year);
}
// leap year if not divisible by 100
// but divisible by 4
else if (year % 4 == 0) {
printf("%d is a leap year.", year);
}
// all other years are not leap years
else {
printf("%d is not a leap year.", year);
}
return 0;
}
14. Find out the sum of odd numbers upto a given
range.
#include <stdio.h>
int main()
{
int i, n, sum=0;
/* Input range to find sum of odd numbers */
printf("Enter upper limit: ");
scanf("%d", &n);
/* Find the sum of all odd number */
for(i=1; i<=n; i+=2)
{
sum += i;
}
printf("Sum of odd numbers = %d", sum);
return 0;
}
15. Write a program to print the series 0,1,3,6,10 upto
N
#include<stdio.h>
int main()
{
int i,j=1,k=1,n;
printf("nEnter number of terms:");
scanf("%d",&n);
printf("n");
printf(“%dt”,0);
for(i=0;i<n;i++)
{
printf("%dt",k);
j=j+1;
k=k+j;
}
return 0;
}
16. Find factorial of a user given number
#include <stdio.h>
int main()
{
int c, n, f = 1;
printf("Enter a number to calculate its factorial:");
scanf("%d", &n);
for (c = 1; c <= n; c++)
f = f * c;
printf("Factorial of %d = %dn", n, f);
return 0;
}
PATTERN:
17. Write a program to generate a pyramid pattern.
#include <stdio.h>
int main() {
int i, space, rows, k = 0;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for (i = 1; i <= rows; ++i, k = 0) {
for (space = 1; space <= rows - i; ++space)
{printf(" ");
}
while (k != 2 * i - 1) {
printf("* ");
++k;
}
printf("n");
}
return 0;
}
18. Write a program to generate a ring angle like
pattern with numbers.
#include <stdio.h>
int main() {
int i, j, rows;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for (i = 1; i <= rows; ++i)
{for (j = 1; j <= i; ++j) {
printf("%d ", j);
}
printf("n");
}
return 0;
}
FUNCTION:
19. Create a user defined function addition() to add
two user input numbers.
#include<stdio.h>
int main() {
int num1, num2, res;
printf("nEnter the two numbers : ");
scanf("%d %d", &num1, &num2);
//Call Function Sum With Two Parameters
res = addition(num1, num2);
printf("nAddition of two number is : %d",res);
return (0);
}
int addition(int num1, int num2)
{int num3;
num3 = num1 + num2;
return (num3);
}
20. Find maximum of two numbers entered by user
using function.
#include <stdio.h>
int max(int num1, int num2);
int main()
{
int num1, num2, maximum;
printf("Enter any two numbers: ");
scanf("%d%d", &num1, &num2);
maximum = max(num1, num2);
printf("nMaximum = %dn", maximum);
return 0;
}
int max(int num1, int num2)
{
return (num1 > num2 ) ? num1 : num2;
}
21. Find out average of two numbers using function.
#include <stdio.h>
float average(int x, int
y){return (float)(x + y)/2;
}
int main(){
int number1, number2;
float avg;
printf("Enter the first number: ");
scanf("%d",&number1);
printf("Enter the second number: ");
scanf("%d",&number2);
avg = average(number1, number2);
printf("The Average of %d and %d
is: %.2f",number1,number2,avg);
return 0;
}
ARRAY:
22. Write a program to store elements in an array and
print.
#include <stdio.h>
void main()
{
int arr[10];
int i;
printf("nnRead and Print elements of an array:n");
printf(" n");
printf("Input 10 elements in the array :n");
for(i=0; i<10; i++)
{
printf("element - %d : ",i);
scanf("%d", &arr[i]);
}
printf("nElements in array are: ");
for(i=0; i<10; i++)
{
printf("%d ", arr[i]);
}
printf("n");
}
23. Store elements upto n in an array and show in
reversed order.
#include <stdio.h>
#define MAX_SIZE 100
int main()
{
int arr[MAX_SIZE];
int size, i;
printf("Enter size of the array: ");
scanf("%d", &size);
printf("Enter elements in array: ");
for(i=0; i<size; i++)
{
scanf("%d", &arr[i]);
}
printf("nArray in reverse order: ");
for(i = size-1; i>=0; i--)
{
printf("%dt", arr[i]);
}
return 0;
}
24. Find the sum of all elements in an array.
#include <stdio.h>
void main()
{
int a[100];
int i, n, sum=0;
printf("nnFind sum of all elements of array:n");
printf(" n");
printf("Input the number of elements to be stored in the
array :");
scanf("%d",&n);
printf("Input %d elements in the array :n",n);
for(i=0;i<n;i++)
{
printf("element - %d : ",i);
scanf("%d",&a[i]);
}
for(i=0; i<n; i++)
{
sum += a[i];
}
printf("Sum of all elements stored in the array is : %dnn",
sum);
}
25. Find out the smallest element in an array.
#include <stdio.h>
int main()
{
int arr[] = {25, 11, 7, 75, 56};
int length = sizeof(arr)/sizeof(arr[0]);
int min = arr[0];
for (int i = 0; i < length; i++) {
//Compare elements of array with min
if(arr[i] < min)
min = arr[i];
}
printf("Smallest element present in given array: %dn", min);
return 0;
}
Thank You….. :)
Sincerely…
Arghodeep Paul

More Related Content

DOCX
Practical write a c program to reverse a given number
PDF
C Programming Example
PPTX
C Programming Example
DOCX
Practical write a c program to reverse a given number
DOC
Basic c programs updated on 31.8.2020
PDF
Data Structure using C
DOC
C program to check leap year
DOCX
SaraPIC
Practical write a c program to reverse a given number
C Programming Example
C Programming Example
Practical write a c program to reverse a given number
Basic c programs updated on 31.8.2020
Data Structure using C
C program to check leap year
SaraPIC

What's hot (19)

DOCX
B.Com 1year Lab programs
DOCX
Practical File of C Language
PDF
88 c-programs
DOCX
C programs
DOCX
Core programming in c
DOCX
Program flowchart
DOCX
C Programming
PDF
programs
PDF
Progr3
PDF
The solution manual of c by robin
DOCX
C lab manaual
DOCX
Best C Programming Solution
DOCX
Let us C (by yashvant Kanetkar) chapter 3 Solution
PDF
Computer programming subject notes. Quick easy notes for C Programming.Cheat ...
DOC
C basics
PDF
c-programming-using-pointers
DOCX
DataStructures notes
PPSX
C programming array & shorting
PPT
All important c programby makhan kumbhkar
B.Com 1year Lab programs
Practical File of C Language
88 c-programs
C programs
Core programming in c
Program flowchart
C Programming
programs
Progr3
The solution manual of c by robin
C lab manaual
Best C Programming Solution
Let us C (by yashvant Kanetkar) chapter 3 Solution
Computer programming subject notes. Quick easy notes for C Programming.Cheat ...
C basics
c-programming-using-pointers
DataStructures notes
C programming array & shorting
All important c programby makhan kumbhkar
Ad

Similar to Common problems solving using c (20)

DOC
C-programs
PDF
PCA-2 Programming and Solving 2nd Sem.pdf
DOCX
PCA-2 Programming and Solving 2nd Sem.docx
PDF
Programming in C Lab
DOCX
DOCX
DOC
C important questions
PDF
C- Programming Assignment practice set 2 solutions
PDF
The solution manual of programming in ansi by Robin
DOCX
PDF
Simple C programs
PDF
C Programming lab
PDF
DOCX
Programming fundamentals
PPTX
Najmul
PPTX
Introduction to Basic C programming 02
DOCX
Fuzail_File_C.docx
PDF
C lab programs
C-programs
PCA-2 Programming and Solving 2nd Sem.pdf
PCA-2 Programming and Solving 2nd Sem.docx
Programming in C Lab
C important questions
C- Programming Assignment practice set 2 solutions
The solution manual of programming in ansi by Robin
Simple C programs
C Programming lab
Programming fundamentals
Najmul
Introduction to Basic C programming 02
Fuzail_File_C.docx
C lab programs
Ad

More from ArghodeepPaul (12)

PDF
Microprocessor questions converted
PDF
Windows script host
PDF
Windows batch scripting
PDF
C operators
PDF
C taking user input
PDF
C storage classes
PDF
C datatypes
PDF
C variables and constants
PDF
C program structure
PDF
Computer programming tools and building process
PDF
Algorithm pseudocode flowchart program notes
PDF
notes on Programming fundamentals
Microprocessor questions converted
Windows script host
Windows batch scripting
C operators
C taking user input
C storage classes
C datatypes
C variables and constants
C program structure
Computer programming tools and building process
Algorithm pseudocode flowchart program notes
notes on Programming fundamentals

Recently uploaded (20)

DOCX
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
PPTX
Geodesy 1.pptx...............................................
PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PDF
Automation-in-Manufacturing-Chapter-Introduction.pdf
PPTX
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
PPTX
web development for engineering and engineering
PPT
Introduction, IoT Design Methodology, Case Study on IoT System for Weather Mo...
PPTX
Sustainable Sites - Green Building Construction
PPTX
OOP with Java - Java Introduction (Basics)
PPTX
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
PPTX
Current and future trends in Computer Vision.pptx
PPTX
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
PPTX
CYBER-CRIMES AND SECURITY A guide to understanding
PDF
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
PDF
Operating System & Kernel Study Guide-1 - converted.pdf
PPTX
CH1 Production IntroductoryConcepts.pptx
PPTX
additive manufacturing of ss316l using mig welding
PDF
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
PPT
Mechanical Engineering MATERIALS Selection
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
Geodesy 1.pptx...............................................
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
Automation-in-Manufacturing-Chapter-Introduction.pdf
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
web development for engineering and engineering
Introduction, IoT Design Methodology, Case Study on IoT System for Weather Mo...
Sustainable Sites - Green Building Construction
OOP with Java - Java Introduction (Basics)
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
Current and future trends in Computer Vision.pptx
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
CYBER-CRIMES AND SECURITY A guide to understanding
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
Embodied AI: Ushering in the Next Era of Intelligent Systems
Operating System & Kernel Study Guide-1 - converted.pdf
CH1 Production IntroductoryConcepts.pptx
additive manufacturing of ss316l using mig welding
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
Mechanical Engineering MATERIALS Selection

Common problems solving using c

  • 1. Common Problems Solving using C Language Instructor: Arghodeep Paul Firmware Engineer at BitBible Technologies Pvt. Ltd. Content Author: Arghodeep Paul License: OpenSource Date: 01 August 2021
  • 2. IF ELSE: 1. Check whether the user entered number is positive. #include<stdio.h> int main(){ int number; printf("Enter a number:"); scanf("%d",&number); if(number>0){ printf("%d is a positive number!",number); } else{ printf("%d is a negative number",number); } return 0; }
  • 3. 2. Write a program to check whether a number is even or odd. #include<stdio.h> int main(){ int num; printf("Enter a number:"); scanf("%d",&num); if(num%2==0){ printf("%d is an even number!",num); } else{ printf("%d is an odd number",num); } return 0; }
  • 4. FOR LOOP: 3. Write a program to display first 10 natural numbers #include <stdio.h> int main() { int i; printf("The first 10 natural numbers are:n"); for (i=1;i<=10;i++) { printf("%d ",i); } printf("n"); return 0; }
  • 5. 4. Write a program to find biggest among two numbers. #include<stdio.h> int main() { int n1,n2,sum; printf("nEnter 1st number : "); scanf("%d",&n1); printf("nEnter 2nd number : "); scanf("%d",&n2); if(n1 > n2) printf("n1st number is greatest."); else printf("n2nd number is greatest."); return 0; }
  • 6. WHILE LOOP: 5. Write a program to display natural numbers upto a given range. #include <stdio.h> int main() { int num,i; printf("Enter a number : "); scanf("%d", &num); i = 0; while (i <= num) { printf("n%d",i); i++; }
  • 7. return 0; } 6. Write a program to display sum of first 5 natural number. #include <stdio.h> int main() { int num, i, sum = 0; printf("Enter a positive number : "); scanf("%d", &num); i = 0; while (i <= num) { sum = sum + i; i++;
  • 8. } printf(" n Sum of first %d natural number is : %d", num, sum); return 0; } 7. Write a program to generate the following series 1 4 9 16 …. 100 #include<stdio.h> int main() { int i; for(i=1;i<=10;i++) { printf("n%d",i*i); } return 0; }
  • 9. 8. Write a program to find out sum of first 10 natural numbers int main() { int i, sum = 0; i = 0; while (i <= 10) { sum = sum + i; i++; } printf(" n Sum of first 10 natural number is : %d", sum);
  • 10. return 0; } 9. Write a program to read 10 numbers and find their sum and average #include <stdio.h> int main() { int i,n,sum=0; float avg; printf("Input the 10 numbers : n"); for (i=1;i<=10;i++) { printf("Number-%d :",i); scanf("%d",&n); sum +=n; } avg=sum/10.0; printf("The sum of 10 no is : %dnThe Average is : %fn",sum,avg); return 0;
  • 11. } 10. Find cube of the number upto a given integer #include <stdio.h> int main() { int i,ctr; printf("Input number of terms : "); scanf("%d", &ctr); for(i=1;i<=ctr;i++) { printf("Number is : %d and cube of the %d is :%d n",i,i, (i*i*i));
  • 12. } return 0; } 11. Compute multiplication table of a given integer #include <stdio.h> int main() { int j,n; printf("Input the number (Table to be calculated) : "); scanf("%d",&n); printf("nn"); for(j=1;j<=10;j++) { printf("%d X %d = %d n",n,j,n*j); } return 9; }
  • 13. 12. Display n number of multiplication table vertically #include <stdio.h> int main() { int j,i,n; printf("Input upto the table number starting from 1 : "); scanf("%d",&n); printf("Multiplication table from 1 to %d n",n); for(i=1;i<=10;i++) { for(j=1;j<=n;j++) { if (j<=n-1) printf("%dx%d = %d, ",j,i,i*j); else
  • 14. printf("%dx%d = %d",j,i,i*j); } printf("n"); } return 0; } 13. Check whether the entered year is leap year or not. #include <stdio.h> int main() { int year; printf("Enter a year: "); scanf("%d", &year);
  • 15. // leap year if perfectly divisible by 400 if (year % 400 == 0) { printf("%d is a leap year.", year); } // not a leap year if divisible by 100 // but not divisible by 400 else if (year % 100 == 0) { printf("%d is not a leap year.", year); } // leap year if not divisible by 100 // but divisible by 4 else if (year % 4 == 0) { printf("%d is a leap year.", year); } // all other years are not leap years else { printf("%d is not a leap year.", year); } return 0; }
  • 16. 14. Find out the sum of odd numbers upto a given range. #include <stdio.h> int main() { int i, n, sum=0; /* Input range to find sum of odd numbers */ printf("Enter upper limit: "); scanf("%d", &n); /* Find the sum of all odd number */ for(i=1; i<=n; i+=2) { sum += i; } printf("Sum of odd numbers = %d", sum); return 0; }
  • 17. 15. Write a program to print the series 0,1,3,6,10 upto N #include<stdio.h> int main() { int i,j=1,k=1,n; printf("nEnter number of terms:"); scanf("%d",&n); printf("n"); printf(“%dt”,0); for(i=0;i<n;i++) { printf("%dt",k); j=j+1; k=k+j; } return 0; }
  • 18. 16. Find factorial of a user given number #include <stdio.h> int main() { int c, n, f = 1; printf("Enter a number to calculate its factorial:"); scanf("%d", &n); for (c = 1; c <= n; c++) f = f * c; printf("Factorial of %d = %dn", n, f); return 0; }
  • 19. PATTERN: 17. Write a program to generate a pyramid pattern. #include <stdio.h> int main() { int i, space, rows, k = 0; printf("Enter the number of rows: "); scanf("%d", &rows); for (i = 1; i <= rows; ++i, k = 0) { for (space = 1; space <= rows - i; ++space) {printf(" "); } while (k != 2 * i - 1) { printf("* "); ++k; } printf("n"); } return 0; }
  • 20. 18. Write a program to generate a ring angle like pattern with numbers. #include <stdio.h> int main() { int i, j, rows; printf("Enter the number of rows: "); scanf("%d", &rows); for (i = 1; i <= rows; ++i) {for (j = 1; j <= i; ++j) { printf("%d ", j); } printf("n"); } return 0; }
  • 21. FUNCTION: 19. Create a user defined function addition() to add two user input numbers. #include<stdio.h> int main() { int num1, num2, res; printf("nEnter the two numbers : "); scanf("%d %d", &num1, &num2); //Call Function Sum With Two Parameters res = addition(num1, num2); printf("nAddition of two number is : %d",res); return (0); }
  • 22. int addition(int num1, int num2) {int num3; num3 = num1 + num2; return (num3); } 20. Find maximum of two numbers entered by user using function. #include <stdio.h> int max(int num1, int num2); int main() { int num1, num2, maximum; printf("Enter any two numbers: "); scanf("%d%d", &num1, &num2); maximum = max(num1, num2); printf("nMaximum = %dn", maximum); return 0; } int max(int num1, int num2)
  • 23. { return (num1 > num2 ) ? num1 : num2; } 21. Find out average of two numbers using function. #include <stdio.h> float average(int x, int y){return (float)(x + y)/2; } int main(){ int number1, number2; float avg; printf("Enter the first number: "); scanf("%d",&number1); printf("Enter the second number: "); scanf("%d",&number2); avg = average(number1, number2); printf("The Average of %d and %d is: %.2f",number1,number2,avg); return 0; }
  • 24. ARRAY: 22. Write a program to store elements in an array and print. #include <stdio.h> void main() { int arr[10]; int i; printf("nnRead and Print elements of an array:n"); printf(" n"); printf("Input 10 elements in the array :n"); for(i=0; i<10; i++) { printf("element - %d : ",i); scanf("%d", &arr[i]); }
  • 25. printf("nElements in array are: "); for(i=0; i<10; i++) { printf("%d ", arr[i]); } printf("n"); }
  • 26. 23. Store elements upto n in an array and show in reversed order. #include <stdio.h> #define MAX_SIZE 100 int main() { int arr[MAX_SIZE]; int size, i; printf("Enter size of the array: "); scanf("%d", &size); printf("Enter elements in array: "); for(i=0; i<size; i++) { scanf("%d", &arr[i]); } printf("nArray in reverse order: "); for(i = size-1; i>=0; i--) { printf("%dt", arr[i]); } return 0; }
  • 27. 24. Find the sum of all elements in an array. #include <stdio.h> void main() { int a[100]; int i, n, sum=0; printf("nnFind sum of all elements of array:n"); printf(" n"); printf("Input the number of elements to be stored in the array :"); scanf("%d",&n); printf("Input %d elements in the array :n",n);
  • 28. for(i=0;i<n;i++) { printf("element - %d : ",i); scanf("%d",&a[i]); } for(i=0; i<n; i++) { sum += a[i]; } printf("Sum of all elements stored in the array is : %dnn", sum); }
  • 29. 25. Find out the smallest element in an array. #include <stdio.h> int main() { int arr[] = {25, 11, 7, 75, 56}; int length = sizeof(arr)/sizeof(arr[0]); int min = arr[0]; for (int i = 0; i < length; i++) { //Compare elements of array with min if(arr[i] < min) min = arr[i]; } printf("Smallest element present in given array: %dn", min); return 0; }