Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

C Programming Fundamentals, Study notes of Computer Programming

A comprehensive overview of the fundamental concepts and techniques in c programming. It covers various data types, operators, control structures, and functions in the c language. The document aims to serve as a valuable resource for students and learners who are new to c programming or looking to deepen their understanding of the language. It includes detailed explanations, code examples, and exercises to reinforce the concepts. By studying this document, users can gain a solid foundation in c programming and be well-equipped to tackle more advanced topics and projects.

Typology: Study notes

2023/2024

Uploaded on 06/10/2024

saipavan-khandavalli
saipavan-khandavalli 🇮🇳

1 / 37

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Assignment 01 - Algorithms (17/01/2024)
- 21BEC7130, K.Saipavan
1.Write an Algorithm to find Fibonacci series up to “n”.
A. Step 1: START
Step 2: Take input , say n.
Step 3: Initialize term1 = 0,term2 = 1,sum = 0,count = 0.Step 4: Calculate
sum = term1+term2. Print sum.
Step 5: Make term1 = term2,term2 = sum
Step 6: Increment count.Repeat from step 3 to step 5 untilcount becomes n.
Step 7 : STOP
2Write an Algorithm to find whether a give number is prime or not.
A. Step 1: START
Step 2: Take input,say num.
Step 3: Initialize i=2.
Step 4: Calculate num%i.If “i” is equal to zero,print given number is
Prime number, go to Step 6.
Step 5: If i is not equal to zero,increment i,repeat the process from
Step 4.
Step 6: STOP
2. Write an Algorithm to find whether a given number is Armstrong
number or not.
A. Step 1: START
Step 2: Take input,say a.
Step 3: Initialize count = 0,sum = 0.
Step 4: Calculate rem = a%10 , sum = sum+(rem)*(rem)*(rem),
a=a/10,
Step 5: Repeat Step 4 until “a” becomes zero. If a is equal to sum
Print given number is Armstrong Number.
Step 6: STOP
3. Write an Algorithm to find whether a given number is even or not.
A. Step 1 : START
Step 2 : Take input, say number.
Step 3 : Calculate result = number%2.
Step 4 : If result is equal to zero print Even Number.
Step 5 : STOP
4. Write an Algorithm to find lcm of two numbers.
A. Step 1 : START
Step 2 : Take inputs two numbers , say num1 and num2.
Step 3 : If num2 is equal to zero, print num1 as lcm.
Step 4 : Calculate num1 = num1/num2 if num1 is greater than num2. Repeat this
until num2 becomes zero.
Step 5 : STOP.
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25

Partial preview of the text

Download C Programming Fundamentals and more Study notes Computer Programming in PDF only on Docsity!

Assignment 01 - Algorithms (17/01/2024)

- 21BEC7130, K.Saipavan

1.Write an Algorithm to find Fibonacci series up to “n”.

A. Step 1: START

Step 2: Take input , say n.

Step 3: Initialize term1 = 0,term2 = 1,sum = 0,count = 0.Step 4: Calculate

sum = term1+term2. Print sum.

Step 5: Make term1 = term2,term2 = sum

Step 6: Increment count.Repeat from step 3 to step 5 untilcount becomes n.

Step 7 : STOP

2 Write an Algorithm to find whether a give number is prime or not.

A. Step 1: START

Step 2: Take input,say num.

Step 3: Initialize i=2.

Step 4: Calculate num%i.If “i” is equal to zero,print given number is

Prime number, go to Step 6.

Step 5: If i is not equal to zero,increment i,repeat the process from

Step 4.

Step 6: STOP

2. Write an Algorithm to find whether a given number is Armstrong

number or not.

A. Step 1: START

Step 2: Take input,say a.

Step 3: Initialize count = 0,sum = 0.

Step 4: Calculate rem = a%10 , sum = sum+(rem)(rem)(rem),

a=a/10,

Step 5: Repeat Step 4 until “a” becomes zero. If a is equal to sum

Print given number is Armstrong Number.

Step 6: STOP

3. Write an Algorithm to find whether a given number is even or not.

A. Step 1 : START

Step 2 : Take input, say number.

Step 3 : Calculate result = number%2.

Step 4 : If result is equal to zero print Even Number.

Step 5 : STOP

4. Write an Algorithm to find lcm of two numbers.

A. Step 1 : START

Step 2 : Take inputs two numbers , say num1 and num2.

Step 3 : If num2 is equal to zero, print num1 as lcm.

Step 4 : Calculate num1 = num1/num2 if num1 is greater than num2. Repeat this

until num2 becomes zero.

Step 5 : STOP.

Assignment 02 - Operators & Datatypes in C (24/01/2024)

- 21BEC7130, K.Saipavan

Program to demonstrate Operators in C

#include <stdio.h> int main(){ int a,b; printf("Enter an integer value for variable a : "); scanf("%d ",&a); printf("\nEnter an integer value for variable b : "); scanf("%d ",&b); //Arithmetic Operators printf("\nPlus(+) operator : a+b = %d",a+b); printf("\nMinus(-) operator : a-b = %d",a-b); printf("\nMultiply() operator : ab = %d",ab); printf("\nDivide(/) operator : a/b = %d",a/b); printf("\nModulus(%) operator : a%b = %d",a%b); printf("\nUnary Plus(+) operator : +a = %d",+a); printf("\nUnary Minus(-) operator : -b = %d",-b); printf("\nPost Increment(++) operator : a++ = %d",a++); printf("\nPre Increment(++) operator : ++a = %d",++a); printf("\nPost Decrement(--) operator : b-- = %d",b--); printf("\nPre Decrement(--) operator : --b = %d",--b); //Relational Operators printf("\nLess than or equal to(<=) : a<=b = %d",a<=b); printf("\nGreater than or equal to(>=) : a>=b = %d",a>=b); printf("\nLess than(<) : a<b = %d",a<b); printf("\nGreater than(>) : a>b = %d",a>b); printf("\nEqual to(==) : a==b = %d",a==b); printf("\nNot Equal to(!=) : a!=b = %d",a!=b); //Logical Operators printf("\nLogical AND(&&) : (a>b)&&(b!=a) = %d",(a>b)&&(b!=a)); printf("\nLogical OR(||) : (a>b)||(b!=a) = %d",(a>b)||(b!=a)); printf("\nLogical NOT(&&) : !(a>b) = %d",!(b!=a)); //Bitwise Operators printf("\nBitwise AND(a&b) : %d", a&b); printf("\nBitwise OR(a|b) : %d", a|b); printf("\nBitwise XOR(a^b): %d", a^b); printf("\nBitwise First Compliment(~a): %d", ~a); printf("\nBitwise Right Shift(a>>b): %d", a>>b); printf("\nBitwise Left Shift(a<<b): %d", a<<b); //Assignment Operators printf("\nSimple Assignment Operator(a=b) : %d", a=b); printf("\nPlus and Assignment Operator(a+=b) : %d", a+=b); printf("\nMinus and Assignment Operator(a-=b) : %d", a-=b); printf("\nMultiply and Assignment Operator(a= b) : %d", a*=b); printf("\nDivide and Assignment Operator(a/=b): %d", a/=b); printf("\nModulus and Assignment Operator(a%=b) : %d", a%=b); printf("\nAND and Assignment Operator(a&=b) : %d", a&=b); printf("\nOR and Assignment Operator(a|=b) : %d", a|=b); printf("\nRight Shift Assignment Operator(a>>=b) : %d", a>>b); printf("\nLeft Shift Assignment Operator(a<<=b) : %d", a<<b); }

C Program to demonstrate Data Types in C #include <stdio.h> int main(){ in t a; printf("Enter an integer value for variable a : "); scanf("%d ",&a); float f; printf("\nEnter an float value for variable f : "); scanf("%f ",&f); char c; printf("\nEnter a character value for variabe c : "); scanf("%c",&c); double d; printf("\nEnter a double value for variable d : "); scanf("%lf ",&d); short int e; printf("\nEnter a short int value for variable e : "); scanf("%hd ",&e); unsigned short int g; printf("\nEnter an unsigned short int value for variable g : "); scanf("%hu ",&g); unsigned int h; printf("\nEnter an unsigned int value for variable h : "); scanf("%u",&h); long int i; printf("\nEnter a long int value for variable i : "); scanf("%ld ",&i); unsigned long int j; printf("\nEnter an unsigned long int value for variable j : "); scanf("%lu ",&j); long long int k; printf("\nEnter a long long int value for variable k : "); scanf("%lld ",&k); unsigned long long int l; printf("\nEnter a unsigned long long int value for variable l : "); scanf("%llu ",&l); signed char m; printf("\nEnter a signed char value for variable m : "); scanf("%c ",&m); unsigned char y; printf("\nEnter a unsigned char value for variable y : "); scanf("%c ",&y); long double z; printf("\nEnter a long double value for variable z : "); scanf("%Lf ",&z); int sizeint = sizeof(int); int sizechar = sizeof(char); int sizefloat = sizeof(float); int sizedouble = sizeof(double); printf("\nThe size of int data type : %d", sizeint); printf("\nThe size of char data type : %d",sizechar); printf("\nThe size of float data type : %d",sizefloat); printf("\nThe size of double data type : %d",sizedouble);

Input : Output :

Program - 2 : C Program to calculate Electricity bill

#include <stdio.h> //Program to calculate electricity bill int main(){ float units; printf("Enter the value of units: "); scanf("%f",&units); if(units>=300){ printf("\nTotal Bill Amount is %.2f",units2.5); } else if(units>300 & units<400){ printf("\nTotal Bill Amount is %.2f",units2.75); } else if(units>400 & units<600){ printf("\nTotal Bill Amount is %.2f",units3.25); } else{ printf("\nTotal Bill Amount is %.2f",units5); } }

Program and Output Screenshots :

Program - 3 : Program to Calculate Income Tax

#include <stdio.h> //Program to calculate income tax int main(){ float income; printf("Enter the value of income : "); scanf("%f",&income); if(income>=100000){ printf("\nTax Amount to be paid is %.2f",income0.05); } else if(income>100000 & income<=150000){ printf("\nTax Amount to be paid is %.2f",income0.075); } else if(income>150000 & income<300000){ printf("\nTax Amount to be paid is %.2f",income0.10); } else{ printf("\nTax Amount to be paid is %.2f",income0.18); } }

Program and Output Screenshots :

Program - 5 : Program to Calculate Odd or even number

#include <stdio.h> //Program to calculate Odd or even number int main(){ int number; printf("Enter the number : "); scanf("%d",&number); if(number%2 == 0){ printf("\n%d is an even number",number); } else{ printf("\n%d is an odd number",number); } }

Program and Output Screenshots :

break;

case 2:

if(year%4==0){

if(year%100==0 & year%400!=0){

no_of_days = 28;

else{

no_of_days = 29;

else{

no_of_days=28;

break;

default:

printf("Invalid Input");

printf("No of days : %d",no_of_days);

Output :

2.Write a C program that calculates and prints sum of the digits of a given positive integer

using while loop. Take number as input from keyboard. Users need to check whether the sum

of digits is a prime number or not. Hint: If input number is 236 then sum of its digit (i.e.,

2+3+6 = 11) is a prime number.

Program :

#include <stdio.h>

int main() {

int num;

int sum =0,flag=0;

printf("Enter number : ");

scanf("%d",&num);

while(num!=0){

sum = sum + num%10;

num = num/10;

printf("Sum of digits : %d",sum);

int i=2;

while(i<sum){

if(sum%i==0){

flag=1;

break;

i++;

if(flag){

printf("\nSum of digits is not a Prime number");

else{

printf("\nSum of digits is a Prime number");

Output :

a[j]=temp;

printf("\nDescending order:\n");

for(i=0;i<size;i++){

printf("%d \t",a[i]);

Output :

Assignment 6 - Matrices (07/3/2024)

- K.Saipavan ,21BEC

Program to calculate product of given two matrices.

#include <stdio.h>

int main()

{ int R1,R2,C1,C2;

printf("Enter no of rows of Matrix1 : ");

scanf("%d",&R1);

printf("Enter no of columns of Matrix1 : ");

scanf("%d",&C1);

printf("Enter no of rows of Matrix2 : ");

scanf("%d",&R2);

printf("Enter no of columns of Matrix2 : ");

scanf("%d",&C2);

int arr1[R1][C1];

printf("Enter the elements of Matrix 1 : \n");

for(int i=0;i<R1;i++){

for(int j=0;j<C1;j++){

scanf("%d",&arr1[i][j]);

printf("Enter the elements of Matrix 2 : \n");

int arr2[R2][C2];

for(int i=0;i<R2;i++){

for(int j=0;j<C2;j++){

scanf("%d",&arr2[i][j]);

int result[R1][C2];

if (C1 != R2) {

printf("The number of columns in Matrix-1 must be "

"equal to the number of rows in "

"Matrix-2\n");

else{

for (int i = 0; i < R1; i++) {

for (int j = 0; j < C2; j++) {

result[i][j] = 0;

for (int k = 0; k < R2; k++) {

result[i][j] += arr1[i][k] * arr2[k][j];

printf("Product matrix is : \n");

for (int i = 0; i < R1; i++) {

Lab Assignment 07 - Number System Conversions (21/3/2024)

- K.Saipavan , 21BEC

Program : Binary to Decimal

#include <stdio.h>

#include <math.h>

int convert(long long);

int main() {

long long n;

printf("Enter a binary number: ");

scanf("%lld", &n);

printf("%lld in binary = %d in decimal", n, convert(n));

return 0;

int convert(long long n) {

int dec = 0, i = 0, rem;

while (n != 0) {

rem = n % 10;

n /= 10;

dec += rem * pow(2, i);

++i;

return dec;

Output :

Program : Decimal to Binary

#include <stdio.h>

#include <math.h>

long long convert(int);

int main() {

int n;

long long bin;

printf("Enter a decimal number: ");

scanf("%d", &n);

bin = convert(n);

printf("%d in decimal = %lld in binary", n, bin);

return 0;

long long convert(int n) {

long long bin = 0;

int rem, i = 1;

while (n != 0) {

rem = n % 2;

n /= 2;

bin += rem * i;

i *= 10;

return bin;

Output :

Program : Binary to Octal

#include <math.h>

#include <stdio.h>

int convert(long long bin);

int main() {

long long bin;

printf("Enter a binary number: ");

scanf("%lld", &bin);

printf("%lld in binary = %d in octal", bin, convert(bin));

return 0;

int convert(long long bin) {

int oct = 0, dec = 0, i = 0;

while (bin != 0) {

dec += (bin % 10) * pow(2, i);

++i;

bin /= 10;

i = 1;

while (dec != 0) {