First Year ITP Practise Programs 10.1.24 For Internal Lab
First Year ITP Practise Programs 10.1.24 For Internal Lab
First Year ITP Practise Programs 10.1.24 For Internal Lab
#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
#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:
#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:
#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
#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
#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
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
#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
#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
#include <stdio.h>
int main() {
int n, i;
unsigned long long fact = 1;
printf("Enter an integer: ");
scanf("%d", &n);
#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:
Sum=55
#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:
sum=14
#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:
reverse=5432
#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
big=a[0];
for(i=1;i<n;i++)
if (a[i]>big)
big=a[i];
printf(“biggest element is %d\n”,big);
}
Output:
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;
}
Output:
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
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
#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 c: 2686784
Value of c: 2
#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:
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
#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:
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);
printf(“a=%d b=%d”,a,b);
}
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);
printf(“a=%d b=%d”,a,b);
}