Professional Documents
Culture Documents
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(){
scanf("%d",&rollno);
scanf("%d",&marks);
printf("Marks of roll no %d is %d",rollno,marks);
Sample output:
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);
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);
Sample Output:
Enter first number: 21
Enter second number: 5
The sum of 21 and 5 is 26
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);
Sample Output:
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(){
Sample Output:
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);
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
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;
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);
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