[go: up one dir, main page]

0% found this document useful (0 votes)
24 views20 pages

First Year ITP Practise Programs 10.1.24 For Internal Lab

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 20

Simple Programs on Basic Input/Output statements and Arithmetic Operators

Write a program in C to find the total and average of 4 numbers.

#include <stdio.h>
int main() {
int num1,num2,num3,num4,total;
float avg;
printf("Input integer value for num1,num2,num3 and num4:");
scanf("%d%d%d%d",&num1,&num2,&num3,&num4);
total=num1+num2+num3+num4;
avg=total/4;
printf("Total=%d\n",total);
printf("average=%f\n",avg);
return 0;
}

Output:
Input integer value for num1,num2,num3 and num4:55 65 75 85
Total=280
average=70.000000

Write a program in C to compute the simple interest

#include <stdio.h>

int main() {
float principle_amount,rate,simple_interest;
int n;
printf("Input real value for principle_amount:");
scanf("%f",&principle_amount);
printf("Input rate of interest:");
scanf("%f",&rate);
printf("Input no.of years:");
scanf("%d",&n);
simple_interest=(principle_amount*rate*n)/100;
printf("Simple interest=%f\n",simple_interest);
return 0;
}
Output:

Input real value for principle_amount:3000


Input rate of interest:2
Input no.of years:2
Simple interest=120.000000

Write a program in C to find the area of a circle

#include <stdio.h>
#define pi 3.14

int main() {
int r;
float area;
printf("Input value for r:");
scanf("%d",&r);
area=pi*r*r;
printf("area=%f\n",area);
return 0;
}

Output:

Input value for r:5


area=78.500000

Write a program in C to interchange/exchange two numbers

#include <stdio.h>
int main() {
int temp,a,b;
printf("Input value for a and b:");
scanf("%d%d",&a,&b);
printf("Value of a and b before intechange:\n");
printf("a=%d b=%d\n",a,b);
temp=a;
a=b;
b=temp;
printf("Value of a and b after intechange:\n");
printf("a=%d b=%d\n",a,b);
return 0;
}

Output:
Input value for a and b:234 567
Value of a and b before intechange:
a=234 b=567
Value of a and b after intechange:
a=567 b=234

Write a program in C to print the quotient and remainder of a given input number

#include <stdio.h>
int main() {
float quotient;
int remainder,number;
printf("input a value for number:");
scanf("%d",&number);
quotient=number/10;
remainder=number%10;
printf("quotient=%f\n", quotient);
printf("remainder=%d\n",remainder);
return 0;
}

Output:
input a value for number:2345
quotient=234.000000
remainder=5

Simple Programs on different forms of if statements and switch statement

Write a program in C to check whether a number is odd or even

#include <stdio.h>

int main() {
int num;
printf("Input a value for a number:");
scanf("%d",&num);
if (num%2==0)
printf("Even number");
else
printf("odd number");

return 0;
}
Output:
Input a value for a number:20
Even number
Input a value for a number:21
odd number

Write a program in C to find the biggest of 3 numbers

#include <stdio.h>
int main() {
int a,b,c;
printf("input value for a,b,c:");
scanf("%d%d%d",&a,&b,&c);
if ((a>b)&&(a>c))
printf("a is the biggest");
else if ((b>a) && (b>c))
printf("b is the biggest");
else
printf("c is the biggest");
return 0;
}

Output:
input value for a,b,c:10 20 30
c is the biggest
input value for a,b,c:10 30 20
b is the biggest
input value for a,b,c:30 20 10
a is the biggest

Write a program in C to check whether a number is single digit/double digit/triple digit


number
#include <stdio.h>
int main() {
int num;
printf("input value for num:");
scanf("%d",&num);
if (num>0 && num<10)
printf("Single Digit Number");
else if (num>10 && num <100)
printf("Two Digit Number");
else if (num>100 && num <1000)
printf("Three Digit Number");
else
printf("More than 3 digits");
return 0;
}

Output:
input value for num:23
Two Digit Number
input value for num:234
Three Digit Number
input value for num:2345
More than 3 digits
input value for num:6
Single Digit Number

Write a program in C to print branch name based on branch code

#include <stdio.h>
int main() {
int bcode;
printf("input value for branch code:");
scanf("%d",&bcode);
switch(bcode)
{
case 5:printf("cse branch");
break;
case 33:
printf("csm branch");
break;
case 31:
printf("cai branch");
break;
case 4:
printf("ece branch");
break;
default:
printf("branch code not supported");
}

return 0;
}

Output:
input value for branch code:33
csm branch
input value for branch code:4
ece branch
input value for branch code:3
branch code not supported

Write a program in C to print whether accepted character is a vowel or not

#include <stdio.h>
int main() {
char letter;
printf("input a letter:");
scanf("%c",&letter);
switch(letter)
{
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':printf("given letter is vowel, input is small case letter");
break;
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':printf("given letter is vowel, input is upper case letter");
break;
default:
printf("Input is not a letter");
}

return 0;
}

Output:
input a letter:a
given letter is vowel, input is small case letter
input a letter:A
given letter is vowel, input is upper case letter
input a letter:3
Input is not a letter

Simple Programs on different forms of loop statements

Write a program in C to find Fibonacci Series up to n terms


#include <stdio.h>
int main()
{
int i, n;
// initialize first and second terms
int t1 = 0, t2 = 1;
// initialize the next term (3rd term)
int nextTerm = t1 + t2;
// get no. of terms from user
printf("Enter the number of terms: ");
scanf("%d", &n);
// print the first two terms t1 and t2
printf("Fibonacci Series: %d, %d, ", t1, t2);
// print 3rd to nth terms
for (i = 3; i <= n; ++i) {
printf("%d, ", nextTerm);
t1 = t2;
t2 = nextTerm;
nextTerm = t1 + t2;
}
return 0;
}
Output:-
Enter the number of terms: 5
Fibonacci Series: 0, 1, 1, 2, 3

Write a program in C to find factorial of a given number.

#include <stdio.h>
int main() {
int n, i;
unsigned long long fact = 1;
printf("Enter an integer: ");
scanf("%d", &n);

// shows error if the user enters a negative integer


if (n < 0)
printf("Error! Factorial of a negative number doesn't exist.");
else {
for (i = 1; i <= n; ++i) {
fact *= i;
}
printf("Factorial of %d = %llu", n, fact);
}
return 0;
}
Output:-
Enter an integer: 5
Factorial of 5 = 120
Write a program in C to find the sum of n elements

#include <stdio.h>
void main()
{
int i,n,sum=0;
printf(“input value for n:”);
scanf(“%d”,&n);
for(i=1;i<=n;i++)
sum+=i;
printf(“sum=%d\n”,sum);
}
Output:

Input value for n: 10

Sum=55

Write a program in C to find the sum of the digits of a number

#include <stdio.h>
void main()
{
int i,n,rem,sum=0;
printf(“input value for n:”);
scanf(“%d”,&n);
while(n!=0)
{
rem=n%10;
sum=sum+rem;
n=n/10;
}
printf(“sum=%d\n”,sum);
}
Output:

input value for n:2345

sum=14

Write a program in C to find the reverse of the digits of a number

#include <stdio.h>
void main()
{
int i,n,rem,rev=0;
printf(“input value for n:”);
scanf(“%d”,&n);
while(n!=0)
{
rem=n%10;
rev=rev*10+rem;
n=n/10;
}
printf(“reverse=%d\n”,rev);
}

Output:

input value for n:2345

reverse=5432

Simple Programs on arrays(1D,2D)

Write a program in C to read n elements and print n elements

#include<stdio.h>
void main()
{
int i,n,a[10];
printf(“input value for n:”);
scanf(“%d”,&n);
printf(“input n numerical values:”);
for(i=0;i<n;i++)
scanf(“%d”,&a[i]);
printf(“elements are :\n”);
for(i=0;i<n;i++)
printf(“%3d”,a[i]);
}

Output:
Input value for n:5
Input n numerical values:
10 22 35 44 60
Elements are:
10 22 35 44 60

Write a program in C to find the biggest among n elements


#include <stdio.h>
void main()
{
int big,i,n,a[10];
printf(“input value for n:”);
scanf(“%d”,&n);
printf(“input n numerical values:”);
for(i=0;i<=n;i++)
scanf(“%d”,&a[i]);

big=a[0];
for(i=1;i<n;i++)
if (a[i]>big)
big=a[i];
printf(“biggest element is %d\n”,big);
}

Output:

Input value for n:5


Input n numerical values:
10 22 35 44 60
Biggest element is 60

Write a program in C to sort n elements in ascending order


#include <stdio.h>
void main()
{
int temp,i,,j,n,a[10];
printf(“input value for n:”);
scanf(“%d”,&n);
printf(“input n numerical values:”);
for(i=0;i<n;i++)
scanf(“%d”,&a[i]);

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

printf(“sorted elements are :\n”);


for(i=0;i<n;i++)
printf(“%3d”,a[i]);

Output:

Input value for n:5


Input n numerical values:
10 35 22 60 44
Sorted elements are:
10 22 35 44 60

Write a program in C to read elements into 2D array and print elements of 2D array.
#include<stdio.h>
void main()
{
int i,j,m,n,a[10][10];
printf(“input no. of rows :”);
scanf(“%d”,&m);
printf(“input no. of cols :”);
scanf(“%d”,&n);

printf(“input elements:”);
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf(“%d”,&a[i][j]);
printf(“elements are :\n”);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
printf(“%3d”,a[i][j]);
printf(“\n”);
}
}

Output:
Input no. of rows: 3
Input no. of cols:3
Input elements:
123456789
Elements are:
123
456
789

Write a program in C to perform matrix addition.


#include<stdio.h>
void main()
{
int i,j,m,n,a[10][10],b[10][10]sum[10][10];
printf(“Input no. of rows :”);
scanf(“%d”,&m);
printf(“Input no. of cols :”);
scanf(“%d”,&n);
printf(“Enter elements of first matrix :”);
for(i=0;i<m;i++){
for(j=0;i<n;j++){
printf("Enter element a %d%d: ", i + 1, j + 1);
scanf("%d", &a[i][j]);
}
}
printf(“Enter elements of second matrix :”);
for(i=0;i<m;i++){
for(j=0;i<n;j++){
printf("Enter element b %d%d: ", i + 1, j + 1);
scanf("%d", &b[i][j]);
}
}
for (i = 0; i < m; i++)
for (j = 0; j <n; j++) {
sum[i][j] = a[i][j] + b[i][j];
}

// printing the result


printf("\nSum of two matrices: \n");
for (i = 0; i < m; i++)
for (j = 0; j < n; j++) {
printf("%3d ", sum[i][j]);
printf(“\n”);
}
}

Output:
Input no. of rows: 3
Input no. of cols:3
Enter Elements of First Matrix
Enter Element 11:1
Enter Element 12:2
Enter Element 13:3
Enter Element 21:4
Enter Element 22:5
Enter Element 23:6
Enter Element 31:7
Enter Element 32:8
Enter Element 33:9

Enter Elements of Second Matrix


Enter Element 11:1
Enter Element 12:2
Enter Element 13:3
Enter Element 21:4
Enter Element 22:5
Enter Element 23:6
Enter Element 31:7
Enter Element 32:8
Enter Element 33:9

Sum of Two Matrices


246
8 10 12
14 16 18

Simple Programs on Pointers and Structures

#include <stdio.h>

int main()
{
int* pc, c;

c = 22;
printf("Address of c: %p\n", &c);
printf("Value of c: %d\n\n", c); // 22

pc = &c;
printf("Address of pointer pc: %p\n", pc);
printf("Content of pointer pc: %d\n\n", *pc); // 22

c = 11;
printf("Address of pointer pc: %p\n", pc);
printf("Content of pointer pc: %d\n\n", *pc); // 11

*pc = 2;
printf("Address of c: %p\n", &c);
printf("Value of c: %d\n\n", c); // 2
return 0;
}
Output:

Address of c: 2686784
Value of c: 22

Address of pointer pc: 2686784


Content of pointer pc: 22

Address of pointer pc: 2686784


Content of pointer pc: 11

Address of c: 2686784
Value of c: 2

Write a program in C to store information of students using structures

#include <stdio.h>
struct student {
char firstName[50];
int roll;
float marks;
} s[5];

int main() {
int i;
printf("Enter information of students:\n");

// storing information
for (i = 0; i < 5; ++i) {
s[i].roll = i + 1;
printf("\nFor roll number%d,\n", s[i].roll);
printf("Enter first name: ");
scanf("%s", s[i].firstName);
printf("Enter marks: ");
scanf("%f", &s[i].marks);
}
printf("Displaying Information:\n\n");

// displaying information
for (i = 0; i < 5; ++i) {
printf("\nRoll number: %d\n", i + 1);
printf("First name: ");
puts(s[i].firstName);
printf("Marks: %.1f", s[i].marks);
printf("\n");
}
return 0;
}

Output:

Enter information of students:

For roll number1,


Enter first name: abc
Enter marks: 85
For roll number2,
Enter first name: xyz
Enter marks: 75
For roll number3,
Enter first name: def
Enter marks: 66
For roll number4,
Enter first name: alla
Enter marks: 44
For roll number5,
Enter first name: boy
Enter marks: 55

Displaying Information:

Roll number: 1
First name: abc
Marks: 85.0

Roll number: 2
First name: xyz
Marks: 75.0

Roll number: 3
First name: def
Marks: 66.0

Roll number: 4
First name: alla
Marks: 44.0

Roll number: 5
First name: boy
Marks: 55.0

Write a program in C to illustrate string handling functions

#include<stdio.h>
#include<string.h>
main()
{
char a[10],b[10];
int ch,len;
printf("enter str1 ");
scanf("%s",a);
printf("enter str2 ");
scanf("%s",b);
while(1)
{
printf("\n choose ur option");
printf("\n 1.length\n 2.compare\n 3.copy\n 4.concat\n");
printf("enter ur choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1: len=strlen(a);
printf("length is %d\n",len);
break;
case 2:if(strcmp(a,b)==0)
{
printf("both strings are equal\n");
}
else
if(strcmp(a,b)>0)
printf("%s is greater than %s\n",a,b);
else
printf("%s is greater than %s\n",b,a);
break;
case 3: printf(" str1 %s\n",a);
printf("str2 %s\n",b);
strcpy(a,b);
printf("after copy strings are\n");
printf(" str1 %s\n",a);
printf("str2 %s\n",b);
break;
case 4:printf(" str1 %s\n",a);
printf("str2 %s\n",b);
strcat(a,b);
printf(" str1 %s\n",a);
break;
}
}
}
Output:

Enter str1: hello


Enter Str2: world
Choose ur option
1.length
2.compare
3.Copy
4.Concat
Enter ur choice:1
Length is 5

Call by Value
Function with two arguments and no return value:

Below is given an example showing how two arguments are passed from the function call
to the function definition and the function definition does not return any value.
/* program to swap two numbers */
#include <stdio.h>

/* function declaration */
void swap(int,int);

main()
{
int a,b;
clrscr();
printf(“Enter the values of a and b:\n”);
scanf(“%d %d”,&a,&b);

/* function call with arguments and no return value */


swap(a,b);

printf(“a=%d b=%d”,a,b);
}

/* function definition with arguments and no return value */

void swap(a1, b1)


int a1,b1;
{
int temp;
temp=a1;
a1=b1;
b1=temp;

return 0;
}

Explanation:
In the above program though we have written the logic to interchange two
numbers , the two numbers will be interchanged only in the function definition and
the interchanged values is not reflected in the calling program.
Call by Reference:
Function with two arguments and multiple return values:
Below is give an example showing how two arguments are passed from the function call
to the function definition and the function definition returns multiple values.
In this example we pass the address of the arguments from the function call and receive
in pointer arguments in function definition and as a result the changes made are automatically
reflected to the variables/arguments in the calling program.
/* program to swap two numbers */
#include <stdio.h>

/* function declaration */
void swap(int *,int *);

main()
{
int a,b;
clrscr();
printf(“Enter the values of a and b:\n”);
scanf(“%d %d”,&a,&b);

/* function call with arguments and no return value */


swap(&a,&b);

printf(“a=%d b=%d”,a,b);
}

/* function definition with arguments and no return value */

void swap(a1, b1)


int *a1,*b1;
{
int *temp;
*temp=*a1;
*a1=*b1;
*b1=*temp;
}
Explanation:
In the above program the logic that we have written to interchange two
numbers, the two numbers will be interchanged in the calling program.

You might also like