You are on page 1of 7

CSE100 Fundamentals of Computer Programming

Classroom Activity 1: Fundamentals of C Programming

 Topics covered: Basics of C Programming


 Software to be used: Dev C++, Any other C Compiler

Roll No. AU2140054 Name: Jineet Shah Section No.: 3

1. Writing first C Program.

Code:

#include<stdio.h>

int main(){

printf("Hello world");

return 0;

Sample Output:

Hello world

2. C program to read roll number and marks from user and display it on screen.

Code:

#include<stdio.h>

int main(){

int rollno, marks;

printf("Enter your roll no.: ");

scanf("%d",&rollno);

printf("Enter your marks: ");

scanf("%d",&marks);
printf("Marks of roll no %d is %d",rollno,marks);

Sample output:

Enter your roll no.: 2140054

Enter your marks: 78

Marks of roll no 2140054 is 78

3. C program to read and display roll number and percentage from user. (Use of int and float
variables). [Hint: Consider Roll as int like 101, 102; and percentage as float like 80.50]
Code:
#include<stdio.h>
int main(){
int rollno;
float perc;
printf("Enter your roll no.: ");
scanf("%d",&rollno);
printf("Enter your perc: ");
scanf("%f",&perc);

printf("Marks of roll no %d is %f",rollno,perc);


}

Sample Output:
Enter your roll no.: 2140054
Enter your perc: 79.5
Marks of roll no 2140054 is 79.500000

4. C program to read two numbers from user and display addition on screen. [Use of basic arithmetic
operators].
Code:
#include<stdio.h>
int main(){
int n1,n2;
printf("Enter first number: ");
scanf("%d",&n1);
printf("Enter second number: ");
scanf("%d",&n2);

int sum = n1+n2;


printf("The sum of %d and %d is %d",n1,n2,sum);
}

Sample Output:
Enter first number: 21
Enter second number: 5
The sum of 21 and 5 is 26

5. C program to Calculate Simple Interest based on given values. [ SI = P*R*N/100 ]

Code:
#include<stdio.h>
int main(){
int p,n;
float r;
printf("Enter Principle amount: ");
scanf("%d",&p);
printf("Enter rate of interest: ");
scanf("%f",&r);
printf("Enter Time duration: ");
scanf("%d",&n);

printf("The simple interest is: %f",((p*r*n)/100));


}

Sample Output:

Enter Principle amount: 10000


Enter rate of interest: 8
Enter Time duration: 10
The simple interest is: 8000.000000

6. C program to print your name and address (Static program, don’t read any data from user).

Code:

int main(){
printf("Name : JINEET SHAH \n");
printf("Address: Naranpura");
}
Sample Output:
Name : JINEET SHAH
Address: Naranpura

7. C program to read and print your name and address. (Read name and address from user).

Code:
#include<stdio.h>
int main(){

char name[25], add[100];


printf("Enter your name: ");
gets(name);
printf("Enter your add: ");
gets(add);
printf("Name : %s \n", name);
printf("Address: %s", add);
}

Sample Output:

Enter your name: Jineet Shah


Enter your add: Naranpura, Ahmedabad
Name : Jineet Shah
Address: Naranpura, Ahmedabad

8. Reading two float numbers, assign sum to integer variable (Data type conversion).

Code:
#include<stdio.h>
int main(){
float n1,n2;
printf("Enter the first number: ");
scanf("%f", &n1);
printf("Enter the second number: ");
scanf("%f", &n2);

int sum = n1+n2;


printf("%d",sum);
}
Sample Output:

Enter the first number: 6.87


Enter the second number: 12.67
19

9. Read and Print Grade of a student. (Demonstrate use character variable in C program.)

Code:
#include<stdio.h>
int main(){
char grade;
printf("Enter your grade: ");
scanf("%c",&grade);

printf("grade is %c",grade);
}

Sample Output:
Enter your grade: C
grade is C

10. C program to print ASCII value of a given character.

Code:
#include<stdio.h>
int main(){
char charac;
printf("Enter a character: ");
scanf("%c",&charac);
int ascii_val;
ascii_val=charac;
printf("The ASCII value of %c is %d.",charac,ascii_val);
}

Sample Output:
Enter a character: C
The ASCII value of C is 67.

11. Use of printf function with formatting (Use of \t, \n, format specifier like %.2f, etc..).

Code:
#include<stdio.h>
int main(){
float a,b,c;
printf("Enter the first number: ");
scanf("%f",&a);
printf("Enter the second number: ");
scanf("%f",&b);

c=a+b;

printf("\tThe sum of %f and %f is %f \n",a,b,c);


printf("\tThe sum of %.2f and %.2f is %.2f",a,b,c);
}

Sample Output:
Enter the first number: 14
Enter the second number: 35.3
The sum of 14.000000 and 35.299999 is 49.299999
The sum of 14.00 and 35.30 is 49.30

12. Simple Calculator using C program. Show use of arithmetic operators on two given numbers.

Code:
#include<stdio.h>
int main(){
int n1,n2,choice;
printf("Enter the first number: ");
scanf("%d",&n1);
printf("Enter the second number: ");
scanf("%d",&n2);

printf("***** Choose a operation: *****\n 1. Addition \n 2. Subtraction \n 3. Multiplication \n 4.


Division \n 5. Remainder \n ");
scanf("%d",&choice);

if (choice==1){
printf("%d + %d = %d",n1,n2,(n1+n2));
}else if (choice==2){
printf("%d - %d = %d",n1,n2,(n1-n2));
}else if (choice==3){
printf("%d * %d = %d",n1,n2,(n1*n2));
}else if (choice==4){
printf("%d / %d = %d",n1,n2,(n1/n2));
}else if (choice==5){
printf("%d %c %d = %d",n1,'%',n2,(n1%n2));
}else{
printf("Wrong input");
}
}

Sample Output:
Enter the first number: 33
Enter the second number: 15
***** Choose a operation: *****
1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Remainder
5
30 % 11 = 8

You might also like