Programming for Problem Solving-Basics of C Programming
1. Simple C Program (Basic Structure)
#include <stdio.h>
#include<conio.h>
int main()
{
printf("Welcome to Information Technology Dept.);
return 0;
getch();
}
Output
Welcome to Information Technology Dept.
2. Program Using return 0
#include <stdio.h>
#include<conio.h>
int main()
{
printf("Program ends with return");
return 0;
getch();
}
Output
Program ends with return
3. Print Name
#include <stdio.h>
#include<conio.h>
int main()
{
printf("My name is James");
return 0;
getch();
}
Output
My name is James
4. Print Multiple Lines
#include <stdio.h>
#include<conio.h>
int main() {
printf("C Programming\nBasics");
return 0;
getch();
}
Output
C Programming
Basics
5. Program with Comments
#include <stdio.h>
#include<conio.h>
int main() {
// This is a comment
printf("Comments example");
return 0;
getch();
}
Output
Comments example
6. Integer Variable
#include <stdio.h>
#include<conio.h>
int main() {
int a = 10;
printf("%d", a);
return 0;
getch();
}
Output
10
7. Float Variable
#include <stdio.h>
#include<conio.h>
int main() {
float x = 3.5;
printf("%f", x);
return 0;
getch();
}
Output
3.500000
8. Character Variable
#include <stdio.h>
#include<conio.h>
int main() {
char ch = 'A';
printf("%c", ch);
return 0;
getch();
}
Output
A
9. Multiple Variables
#include <stdio.h>
#include<conio.h>
int main() {
int a = 5, b = 10;
printf("%d %d", a, b);
return 0;
getch();
}
Output
5 10
10. User Input Integer
#include <stdio.h>
#include<conio.h>
int main()
{
int n;
scanf("%d", &n);
printf("%d", n);
return 0;
getch();
}
Output
5
11. User Input Float
#include <stdio.h>
#include<conio.h>
int main()
{
float f;
scanf("%f", &f);
printf("%f", f);
return 0;
getch();
}
Output
2.500000
12. Size of Data Types
#include <stdio.h>
#include<conio.h>
int main()
{
printf("%d", sizeof(int));
return 0;
getch();
}
Output
4
13. Constant Variable
#include <stdio.h>
#include<conio.h>
int main()
{
const int a = 10;
printf("%d", a);
return 0;
getch();
}
Output
10
14. Sum Using Variables
#include <stdio.h>
#include<conio.h>
int main()
{
int a = 3, b = 7;
printf("%d", a + b);
return 0;
getch();
}
Output
10
15. Swap Using Temporary Variable
#include <stdio.h>
#include<conio.h>
int main()
{
int a = 5, b = 10, t;
t = a;
a = b;
b = t;
printf("%d %d", a, b);
return 0;
getch();
}
Output
10 5
16. Arithmetic Operators
#include <stdio.h>
#include<conio.h>
int main()
{
int a = 10, b = 5;
printf("%d", a + b);
return 0;
getch();
}
Output
15
17. Subtraction Operator
#include <stdio.h>
#include<conio.h>
int main()
{
int a = 10, b = 4;
printf("%d", a - b);
return 0;
getch();
}
Output
6
18. Multiplication Operator
#include <stdio.h>
#include<conio.h>
int main() {
int a = 3, b = 4;
printf("%d", a * b);
return 0;
getch();
}
Output
12
19. Division Operator
#include <stdio.h>
#include<conio.h>
int main()
{
int a = 10, b = 2;
printf("%d", a / b);
return 0;
getch();
}
Output
5
20. Modulus Operator
#include <stdio.h>
#include<conio.h>
int main()
{
int a = 10, b = 3;
printf("%d", a % b);
return 0;
getch();
}
Output
1
21. Relational Operator
#include <stdio.h>
#include<conio.h>
int main()
{
int a = 5, b = 5;
printf("%d", a == b);
return 0;
getch();
}
Output
1
22. Logical AND
#include <stdio.h>
#include<conio.h>
int main()
{
int a = 1, b = 1;
printf("%d", a && b);
return 0;
getch();
}
Output
1
23. Logical OR
#include <stdio.h>
#include<conio.h>
int main()
{
int a = 0, b = 1;
printf("%d", a || b);
return 0;
getch();
}
Output
1
24. Increment Operator
#include <stdio.h>
#include<conio.h>
int main()
{
int a = 5;
a++;
printf("%d", a);
return 0;
getch();
}
Output
6
25. Assignment Operator
#include <stdio.h>
#include<conio.h>
int main()
{
int a = 10;
a += 5;
printf("%d", a);
return 0;
getch();
}
Output
15