Professional Documents
Culture Documents
Demo Programs-Module I
Demo Programs-Module I
Programming (Lab)
(Contents as per the syllabus)
1. Arithmetic Operators
// Working of arithmetic operators
#include <stdio.h>
int main()
{
int a = 9,b = 4, c;
c = a+b;
printf("a+b = %d \n",c);
c = a-b;
printf("a-b = %d \n",c);
c = a*b;
printf("a*b = %d \n",c);
c = a/b;
printf("a/b = %d \n",c);
c = a%b;
printf("Remainder when a divided by b = %d \n",c);
return 0;
}
return 0;
}
3. C Assignment Operators
An assignment operator is used for assigning a value to a variable. The most common
assignment operator is =
c = a; // c is 5
printf("c = %d\n", c);
c += a; // c is 10
printf("c = %d\n", c);
c -= a; // c is 5
printf("c = %d\n", c);
c *= a; // c is 25
printf("c = %d\n", c);
c /= a; // c is 5
printf("c = %d\n", c);
c %= a; // c = 0, c=c%a;
printf("c = %d\n", c);
return 0;
}
4. C Relational Operators
// Working of relational operators
#include <stdio.h>
int main()
{
int a = 5, b = 5, c = 10;
return 0;
}
5. C Logical Operators
An expression containing logical operator returns either 0 or 1 depending upon whether
expression results true or false. Logical operators are commonly used in decision making in C
programming.
#include <stdio.h>
int main()
{
int a = 5, b = 5, c = 10, result;
return 0;
}
6. C Bitwise Operators
https://www.programiz.com/c-programming/bitwise-operators
void main()
{
float x,y;
clrscr();
printf("Enter x: ");
scanf("%f", &x);
y = x < 0 ? x*x+4 : x*x-4;
printf("f(%f) = %f", x, y);
getch();
}
1. #include <stdio.h>
2. int main()
3. {
4.
5. int x=4;
6. if(x%2==0)
7. {
8. printf("The number x is even");
9. }
10. else
11. printf("The number x is not even");
12. return 0;
13. }
9. Expressions and if –else loop
1. #include <stdio.h>
2. int main()
3. {
4. int x = 4;
5. int y = 9;
6. if ( (x <6) || (y>10))
7. {
8. printf("Condition is true");
9. }
10. else
11. printf("Condition is false");
12. return 0;
13. }
10.Expressions and if –else loop
1. #include<stdio.h>
2. #include<string.h>
3. int main()
4. {
int main() {
char op;
double first, second;
printf("Enter an operator (+, -, *, /): ");
scanf("%c", &op);
printf("Enter two operands: ");
scanf("%lf %lf", &first, &second);
switch (op) {
case '+':
printf("%.1lf + %.1lf = %.1lf", first, second, first + second);
break;
case '-':
printf("%.1lf - %.1lf = %.1lf", first, second, first - second);
break;
case '*':
printf("%.1lf * %.1lf = %.1lf", first, second, first * second);
break;
case '/':
printf("%.1lf / %.1lf = %.1lf", first, second, first / second);
break;
// operator doesn't match any case constant
return 0;
}
13. C Program to check whether an alphabet is vowel or Consonant
#include <stdio.h>
#include <conio.h>
int main(){
char c;
printf("Enter a Character: ");
scanf("%c", &c);
/* Check if input alphabet is member of set{A,E,I,O,U,a,e,i,o,u} */
if(c == 'a' || c == 'e' || c =='i' || c=='o' || c=='u' || c=='A'
|| c=='E' || c=='I' || c=='O' || c=='U'){
printf("%c is a Vowel\n", c);
} else {
printf("%c is a Consonant\n", c);
}
getch();
return 0;
}
14. Write a C program to enter month number and print number of days in month.
/**
* C program to print number of days in a month
*/
#include <stdio.h>
int main()
{
int month;
return 0;
}
15. Write a C program to generate Fibonacci series up to n value using for loop.
#include <stdio.h>
int main() {
int i, n;
return 0;
}
16. Write a C program to find the factorial of the given number using for loop.
#include <stdio.h>
int main() {
int n, i;
unsigned long long fact = 1;
printf("Enter an integer: ");
scanf("%d", &n);
return 0;
}
17. Write a C program to print total number of digits in the given number using while loop.
#include <stdio.h>
int main() {
long long n;
int count = 0;
printf("Enter an integer: ");
scanf("%lld", &n);
int main()
{
int Number, Reminder, Product;
return 0;
}
#include <stdio.h>
int main()
{
int A, B, C;
return 0;
}
20. Write the answer for the following.
// An example of implicit conversion
#include<stdio.h>
int main()
{
int x = 10; // integer x
char y = 'a'; // character c
int main()
{
double x = 1.2;
return 0;
}
23. Write a c program for bit-wise operator – complete the below given program
#include <stdio.h>
void main()
{
long number, tempnum;
Output :
24. Write a C Program finds sum of first 50 natural numbers using for loop.
#include <stdio.h>
void main()
{
int num, sum = 0;
int main () {
/* do loop execution */
do {
if( a == 15) {
/* skip the iteration */
a = a + 1;
continue;
}
} while( a < 20 );
return 0;
1. #include <stdio.h>
2. int main()
3. {
4. int i, j, k;
5. for(i=0;i<10;i++)
6. {
7. for(j=0;j<5;j++)
8. {
9. for(k=0;k<3;k++)
10. {
11. printf("%d %d %d\n",i,j,k);
12. if(j == 3)
13. {
14. goto out;
15. }
16. }
17. }
18. }
#include <stdio.h>
int main() {
int i;
double number, sum = 0.0;
return 0;
}