0% found this document useful (0 votes)
26 views

C programming Lab report

C programming Lab report

Uploaded by

alt.b9-5ov294pr
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

C programming Lab report

C programming Lab report

Uploaded by

alt.b9-5ov294pr
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

Thames International College

Bachelors in computer application (BCA)


C- Programming
CASC-151

Lab Report

Submitted by: Submitted to:


Prashant Shakya Nischeet Ratna
BCA 2nd Semester Tuladhar
1. Write a program to print “Hello Programming
Class”. 
Program -
#include<stdio.h>
main()
{
printf("hello programming class");
}
Output –

2. Write a program to define any variable and


print it.
Program –
#include<stdio.h>
main(){
int a;
int b;
a = 5;
b = 8;
printf("%d",a);
}

Output -

Page|2
3. WAP to do simple arithmetic
operations. 
Program - include<stdio.h>
main(){
int a, b, c;
a = 9;
b = 8;
c = a + b;
printf("sum is %d\n",c);
c = a - b;
printf("Diffrence is %d\n",c);
c = a * b;
printf("Product is %d\n",c);
c = a / b;
printf("Quotient is %d\n",c);
c=a%b
}

Output –

4. Write a program to add two numbers given by the


user.

Program – #include<stdio.h>
main(){
float a, b ,c;
printf("Enter First value\n");
scanf("%f", &a);
printf("Enter Second value\n");
scanf("%f", &b);

Page|3
c = a + b;
printf("The sum of %.2f and %.2f is %.2f",a ,b ,c);
}

Output-

Page|4
Output –

5. Write a program to display the name given by the


user. Program -
#include<stdio.h>
main(){
char name[20];
printf("Enter your name\n");
scanf("%s", name);
printf("your name is %s",name);
}
Output –

Page|5
6. Write a program to check a
condition.
Program –
#include<stdio.h>
main(){
float marks;
printf("enter your marks in C Programming\n");
scanf("%f", &marks);

if(marks > 20) {// == > < >= <= !=


printf("you are passed");
}
else{
printf("you are failed");
}}

Output -

7. Write a program to display the greatest number according to the entered


one.
Program – #include<stdio.h>
main(){
int a, b, c;
printf("enter First number\n");
scanf("%d", &a);
printf("enter Second number\n");
scanf("%d", &b);
printf("enter Third number\n");
scanf("%d", &c);
if (a>b && a>c){
printf("a is the greater number");

Page|6
}
else if (b>a && b>c){
printf("b is the greater number");
}
else {
printf("c is the greater number");
}
}
Output -

Page|7
8. WAP to find largest and smallest number from three
numbers. 

Program –
#include<stdio.h>
main(){
int a, b, c;
printf("Enter the first no a:\n");
scanf("%d",&a);
printf("Enter the second no b:\n");
scanf("%d",&b);
printf("enter the third no c:\n");
scanf("%d",&c);
if(a>b &&a>c){
printf("a is the largest no.\n");
}
else if(b>a && b>c){
printf("b is the largest no.");
}
else{
printf("c is the largest no.");
}
}
Output –

9. Write a program to display grade according to


marks. 
Program –
#include<stdio.h>
main(){
float marks;
printf("enter your marks\n");
scanf("%f", &marks);

Page|8
if(marks <=100 && marks >90){
printf("Your Grade is A");
}
else if(marks <=90 && marks >80){

Page|9
printf("Your Grade is A-");
}
else if(marks <=80 && marks >70){
printf("Your Grade is B");
}
else if(marks <=70 && marks >60){
printf("Your Grade is B-");
}
else {
printf("No Grade");
}
}
Output –

10. Write a program to make a restaurant


menu.Program –
#include<stdio.h>
main()
{
int item;
printf("welcome to our resturant\n");
printf("enter code of your choice\n");
printf("Pizza- 105\t Momo- 203\t Sandwitch- 450\n");
scanf("%d", &item);
switch(item)
{
case 105 :
printf("your Pizza is getting ready\n");
printf("wait for a while, Thank you!");
break;
case 203 :
printf("your Momo is getting ready\n");

Page|10
printf("wait for a while, Thank you!");

Page|11
break; case
405 :
printf("your Sandwitch is getting ready\n");
printf("wait for a while, Thank you!");
break;
default:
printf("Please choose another item\n");
}
}
Output –

11. Write a program to display 1 to 1000 using


Loop.Program –
#include<stdio.h>
main(){
int i=1;
do{
printf("%d\n",i);
i++;
}while(i<=1000);
}
Output –

Page|12
12. Write a progam to find the sum of n natural
numbers. Program –
#include<stdio.h>
main(){
int n, i, sum = 0;

printf("Enter any Number for sum\n");


scanf("%d", &n);
for(i=1; i<=n; i++){
sum += i; // sum = sum + i;
}
printf("Sum of %d number is %d", n, sum);
}
Output -

13. Write a program to find the number if it is odd or


even.
Program –

#include<stdio.h>
main(){
int n;
printf("Enter any whole number\n");
scanf("%d", &n);
if (n %2 != 0)
printf("the number %d is odd", n);
else
printf("The number %d is even", n);
}

Page|13
Output -

14. Write a program to find factoraial of a


number. Program –
#include<stdio.h>
main(){
int n, i, sum = 1;
printf("Enter any Number for Factorial\n");
scanf("%d", &n);
for(i=1; i<=n; i++){
sum *= i; // sum = sum *3 i;
}
printf("Factorial of %d number is %d", n, sum);
}
Output –

Page|14
15. Write a program to find if the given number is prime number or
not. Program –
#include<stdio.h>
main() {
int n, i, x=0;
printf("Enter a Whole number\n");
scanf("%d", &n);
for(i=2; i<n; i++){
if(n % i == 0){
x=1;
break;
}
}
if(x == 0)
printf("The number %d is Prime Number", n);
else
printf("The number %d is Not Prime Number", n);
}
Output –

16. Write a program to find the reverse of the given


number. Program – #include<stdio.h>
main(){
int n, rev=0, rem;
printf("Enter an Integer value with more than one digit.");
scanf("%d", &n);
while(n != 0){
rem = n % 10;
rev = rev * 10 + rem;
n /= 10;
}
printf("The reversed value is %d", rev);

Page|15
}

Page|16
Output -

17. Write a program to find the Fibonacci series upto n


term. Program –
#include<stdio.h>
main(){
int n, i;
int a = 0, b = 1;
int next = a + b;
printf("enter the number of Terms\n");
scanf("%d", &n);
printf("The Fibonacci Series is:\n");
printf("%d, %d", a, b);
for(i=3; i<=n; ++i){
printf(", %d", next);
a = b;
b = next;
next = a+b;
}
}
Output -

Page|17
18. Write a program to create a half pyramid with n number of
rows.Program –
#include<stdio.h>
main(){
int i, j, rows;
printf("Enter number of rows\n");
scanf("%d", &rows);
for(i=1; i<= rows; ++i){
for(j=1; j<=i; ++j){
printf("* ");
}
printf("\n");
}
}
Output –

19. WAP to swap two variable.


#include<stdio.h>
main(){
int a,b, c;
a=10;
b=20;
//Before swaping
printf("a=%d\n",a);
printf("b=%d",b);
//swaping variable
c=a;
a=b;
b=c;
//after swaping

Page|18
printf("a=%d\n",a);
printf("b=%d",b);
}

20. WAP to calculate sum of indivisuals digit of number.


#include<stdio.h>
#include<conio.h>
main(){
int r,n ,s=0;
printf("Enter the number\n");
scanf("%d",&n);
while(n!=0){
r=n%10;
s=s+r;
n=n/10;
}
printf("the sum is %d",s);
}

21. WAP to find wheather the given number is palindrome or not.

#include<stdio.h>
main(){
int i, rev=0,n ,r,a;
printf("Enter the number:");
scanf("%d",&n);
a=n;
while(n!=0){
r=n%10;
rev=rev*10+r;

Page|19
n=n/10;
}
printf("%d\n",a);
printf("%d\n",rev);

if(rev==a){
printf("The given number %d is Palindrime\n",a);
}
else{
printf("The given number %d is not Palindrime\n",a);
}
}

22. WAP to caculate sum to N terms using function.

#include<stdio.h>
int sum(int n);
main(){
int s,num;
printf("Enter the number of term you want to sum:\n");
scanf("%d",&num);
s=sum(num);
printf("the sum of %d term is%d",num,s);

}
int sum(int n){
if(n!=0){
return n+sum(n-1);
}else{
return n;
}

Page|20
23. WAP to pass string to the function.

#include<stdio.h>
showString(char str[]);
main(){
char string[10];
printf("Enter the string:\n");
gets(string);
showString(string);
printf("Enter the string:\n");
gets(string);
showString(string);

}
showString(char str[]){

printf("the string you have entered is:\n %s\n",str);

24. WAP to say wheather the given character is vowel or consonant.


#include<stdio.h>
main(){

char letter;

Page|21
int islowercasevowel, isuppercasevowel;

printf("Enter the letter\n");


scanf("%c",&letter);

islowercasevowel=(letter=='a'|| letter=='e'||letter=='i' || letter== 'o'|| letter=='u');


isuppercasevowel=(letter=='A'|| letter=='E'|| letter=='I'|| letter=='O'|| letter=='U');

if(islowercasevowel||isuppercasevowel){
printf("%c is vowel",letter);
}
else{
printf("%c is consonant", letter);
}
}

25. WAP to count number of consonant vowel and space in given array.
#include<stdio.h>
#include<string.h>
main(){
int v, c,i, s;
v=c=s=0;
char strings[20];

printf("Enter the string you want:\n");


gets(strings);
int n=strlen(strings);
for(i=0;i<n;i++){
if(strings[i]=='a'||strings[i]=='e'||strings[i]=='i'||strings[i]=='o'||strings[i]=='u'){
v++;
}
else if(strings[i]==' '){
s++;
}
else{
c++;
}
}
printf("The number of vowel letter is %d\n",v);

Page|22
printf("The number of consonant letter is %d\n",c);
printf("The number of space is %d\n",s);
}

26. WAP to find smallest and largest numcber in an array.


#include<stdio.h>
main(){
int arr[10];
int n,i,min ,max;
printf("Enter the number of element in array:\n");
scanf("%d",&n);
//getting input from user
printf("Enter the element of array:\n");
for(i=0;i<n;i++){
printf("\nEnter the element %d:",i+1);
scanf("%d",&arr[i]);
}
//showing array
printf("The array of number you have given is:\n");
for(i=0;i<n;i++){
printf("%d,",arr[i]);
}
//finding lowest and highest number
min=max=arr[0];
for(i=0;i<n;i++){
if(arr[i]>max){
max=arr[i];
}
if(arr[i]<min){
min=arr[i];
}
}
printf("\nThe largest valus is %d",max);
printf("\nThe smallest value is %d",min);
}

Page|23
27. WAP to arrange array of numbers in descsending order.
#include<stdio.h>
main(){
int i,j,temp,n;
char arr[20];
printf("Enter how many number you want to enter:\n");
scanf("%d",&n);
//taking array of number
printf("Enter the array of number:\n");
for(i=0;i<n;i++){
printf("Enter number %d:\n",i+1);
scanf("%d",&arr[i]);
}
//display array of number
for(i=0;i<n;i++){
printf("%d,",arr[i]);
}
//sorting array

for(i=0;i<n;i++){
for(j=i+1;j<n;j++){
if(arr[i]<arr[j]){
temp=arr[j];
arr[j]=arr[i];
arr[i]=temp;
}
}
}

Page|24
//display array
printf("\nThe sorted array is:\n");
for(i=0;i<n;i++){
printf("%d,",arr[i]);
}

28. WAP to enter the info of employee and display whose salary is greater thn 50000.
#include<stdio.h>
#include<conio.h>
struct employee{
int emplid;
char name[10];
int salary;
};
main(){
int i;
struct employee E[5];
//to enter info
for(i=0;i<2;i++){
printf("Enter your employee id:\n");
scanf("%d",&E[i].emplid);
printf("Enter your name:\n");
scanf("%s",&E[i].name);
printf("Enter your salary:\n");

Page|25
scanf("%d",&E[i].salary);
}

//to display info


for(i=0;i<2;i++){
if(E[i].salary>50000){
printf("\nEmpid:%d Name:%s salary:%d",E[i].emplid,E[i].name,E[i].salary);
}
}

29. WAP to enter the information of student and display them.


#include<stdio.h>
struct date{
int day;
char month[23];
int year;

};
struct student{
int roll;
char name[30];
float marks;
struct date doj;

};
main(){

int n,i;
struct student std[40];

printf("Enter the number of student\n");

Page|26
scanf("%d",&n);
for(i=0;i<n;i++){
printf("Enter the roll number\n");
scanf("%d",&std[i].roll);
printf("Enter the name\n");
scanf("%s",std[i].name);
printf("Enter the marks\n");
scanf("%f",&std[i].marks);
printf("E")
}
printf("you have entered\n");
for(i=0;i<n;i++){
printf("name:%s\troll:%d\tmarks:%.f\n",std[i].name,std[i].roll,std[i].marks);
}
}

30. WAP to store the information of student in file.


#include<stdio.h>
#include<stdlib.h>
struct student{
int roll;
char name[10];
float marks;
char temp;
};
main(){
struct student std[50];

Page|27
int i,n;
FILE *fp;
fp=fopen("thames student.txt","w");
printf("Enter the number of student:\n ");
scanf("%d",&n);
for(i=0;i<n;i++){
printf("enter a roll number \n");
scanf("%d",&std[i].roll);
scanf("%c",&std[i].temp);
printf("enter a name\n");
gets(std[i].name);
printf("enter the marks\n");
scanf("%f",&std[i].marks);
fwrite(&std[i],sizeof(std[i]),1,fp);
}
fclose(fp);
}

Page|28

You might also like