CLab (1)
CLab (1)
CLab (1)
#include<stdio.h>
int main(){
printf("Welcome to Programming");
printf("\n Abhishek Prasad");
// \n Used To next Line
return 0;
}
2. Write a program to calculate the sum and product of
two numbers entered by the user using operators.
#include<stdio.h>
int main(){
// Declaration of Variable
int FNum,SNum;
scanf("%d",&FNum);
scanf("%d",&SNum);
return 0;
}
3.Write a program to calculate the average of three
numbers entered by the user
#include<stdio.h>
int main(){
// Declaration of Variable
int FNum,SNum,TNum;
float avg;
printf("Enter First No.: ");
scanf("%d",&FNum);
printf("Enter Second No.: ");
scanf("%d",&SNum);
printf("Enter Third No.: ");
scanf("%d",&TNum);
// Calculate of average
avg=(FNum+SNum+TNum)/3;
printf("Average Of %d , %d and %d is %.2f",FNum,SNum,avg);
return 0;
}
4.Write a program to swap two numbers without
using a third variable
#include<stdio.h>
int main(){
// Declaration of Variable
int FNum=10,SNum=12;
printf("Before Swaping No. are :\n");
printf("FNum= %d SNum= %d\n",FNum,SNum);
// Operation of Swaping
FNum=FNum+SNum;
SNum=FNum-SNum;
FNum=FNum-SNum;
printf("After Swaping No. are :\n");
printf("FNum= %d SNum= %d\n",FNum,SNum);
return 0;
}
5.Write a program to determine the largest of three
numbers using nested if-else statements
#include<stdio.h>
int main(){
// Declaration of Variable
int FNum=10,SNum=12,TNum=16;
if(FNum>SNum){
if(FNum>TNum)
else
else if(SNum>FNum){
if(SNum>TNum)
else
return 0;
}
6.Write a program to check whether a number entered
by the user is even or odd using an if-else statement.
#include<stdio.h>
int main(){
int Num;
printf("Enter Any No.: ");
scanf("%d",&Num);
if(Num%2==0)
printf("%d is Even No.",Num);
else
printf("%d is Odd No.",Num);
return 0;
}
7. Write a program to display a grade based on marks
entered by the user using a switch-case statement
#include<stdio.h>
int main(){
int marks;
scanf("%d",&marks);
switch(marks/10){
case 1:
case 2:
case 3:printf("Fail");break;
return 0;
}
8.Write a program to find the factorial of a
number entered by the user using a for loop.
#include<stdio.h>
int main(){
int Num,i,Fac=1;
printf("Enter Any No.: ");
scanf("%d",&Num);
for(i=1;i<=Num;i++){
Fac*=i;
}
printf("Factorial of %d is %d ",Num,Fac);
return 0;
}
9. Write a program to check whether a
number is prime using a for loop
#include<stdio.h>
int main(){
int Num=7,i,count=0;
for(i=1;i<=(Num/2);i++){
if(Num%i==0)
count++;
}
if(count==1)
printf("%d is prime no.",Num);
else
printf("%d is not a prime no.",Num);
return 0;
}
10. Write a program to print the Fibonacci series
up to a given number using a while loop
#include<stdio.h>
int main(){
int FNum=0,SNum=1,c,i=3;
printf("Fibonacci Series Upon 5\n");
printf("%d %d ",FNum,SNum);
while(i<=5){
c=FNum+SNum;
printf("%d ",c);
FNum=SNum;
SNum=c;
i++;
}
return 0;
}
11. Write a program to calculate the sum of
digits of a number using a do-while loop
#include<stdio.h>
int main(){
int Num=123,sum=0,cal;
while(Num!=0){
cal=Num%10;
sum+=cal;
Num/=10;
}
printf("Sum of Digit is %d",sum);
return 0;
}
13. Write a program to find the greatest common
divisor (GCD) of two numbers using a while loop
#include<stdio.h>
int main(){
int Fnum=36,Snum=60,max,Cgcd=1,i=1;
max=Fnum>Snum?Fnum:Snum;
while(i<=max){
if(Fnum%i==0&&Snum%i==0){
Cgcd=i;
i++;
return 0;
}
14.Write a program to find the largest and smallest
numbers in a one-dimensional array of size 10 entered
by the user
#include<stdio.h>
int main(){
int arr[]={1,2,3,4,5,6,7,8,9,10};
int max,min,i;
max=min=arr[0];
for(i=0;i<=9;i++){
if(max>arr[i])
max=arr[i];
else if(min<arr[i])
min=arr[i];
}
printf("max=%d min=%d",max,min);
return 0;
}
15.Write a program to calculate the sum of all
elements in a one-dimensional array
#include<stdio.h>
int main(){
int arr[]={1,2,3,4,5,6,7,8,9,10};
int sum=0,i;
for(i=0;i<=9;i++){
//sum each array no.
sum+=arr[i];
}
printf("sum=%d",sum);
return 0;
}
16.Write a program to reverse the elements of
an array entered by the user
#include<stdio.h>
int main(){
int arr[]={1,2,3,4,5,6,7,8,9,10};
int size,i;
// cal. Size of array
size=sizeof(arr)/sizeof(arr[0]);
for(i=size-1;i>=0;i--){
printf("arr[%d]=%d\n",i,arr[i]);
}
return 0;
}
17.Write a program to search for an element in a
one-dimensional array using linear search
#include<stdio.h>
int main(){
int arr[]={1,2,3,4,5,6,7,8,9,10};
int i,search=6,find=0;
for(i=0;i<=9;i++){
if(arr[i]==search){
find=1;
break;
}
}
if(find==1)
printf("%d is present in a array”, search);
else
printf("%d is not present in a array",search);
return 0;
}
18.Write a program to calculate the transpose of a
3x3 matrix entered by the user
#include<stdio.h>
int main(){
int arr[3][3]={{1,2,3},{4,5,6},{7,8,9}};
int i,j;
printf(“Matrix are \n”);
for(i=0;i<=2;i++){
for(j=0;j<=2;j++){
printf("%d ",arr[i][j]);
}
printf("\n");
}
printf("after Transpose of 3X3 matrix
are \n");
//row
for(i=0;i<=2;i++){
//column
for(j=0;j<=2;j++){
printf("%d ",arr[j][i]);
}
printf("\n");
}
return 0;
}
19.Write a program to add two matrices entered
by the user
#include<stdio.h>
int main(){
int arr[3][3];
int i,j,sum=0;
//input array
//row
for(i=0;i<=2;i++){
//column
for(j=0;j<=2;j++){
printf("Enter Arr[%d][%d] no: ",i,j);
scanf("%d",&arr[i][j]);
}
}
printf("Matrices is \n");
for(i=0;i<=2;i++){
for(j=0;j<=2;j++){
printf("%d ",arr[i][j]);
}
printf("\n");
}
printf("Calculation\n");
//calculation
for(i=0;i<=2;i++){
for(j=0;j<=2;j++){
sum+=arr[i][j];
}
}
printf("sum is %d ",sum);
return 0;
}
20. Write a program to demonstrate pointer
arithmetic by printing the addresses of consecutive
elements in an array
#include<stdio.h>
int main(){
int arr[]={1,2,3};
int *Parr=arr;
int i;
for(i=0;i<=2;i++){
printf("value is %d and its Address
is%d\n",*Parr,Parr);
++Parr;
}
return 0;
}
21.Write a program to swap two numbers
using pointers
#include<stdio.h>
int main(){
// Declaration of Variable
int FNum=10,SNum=12;
//Assign First variable address
int *PFNum=&FNum;
//Assign Second variable address
int *PSNum=&SNum;
printf("Before Swaping No. are :\n");
printf("FNum= %d SNum=
%d\n",*PFNum,*PSNum);
// Operation of Swaping
*PFNum=*PFNum+*PSNum;
*PSNum=*PFNum-*PSNum;
*PFNum=*PFNum-*PSNum;
printf("After Swaping No. are :\n");
printf("FNum= %d SNum=
%d\n",*PFNum,*PSNum);
return 0;
}
22.Write a program to count the occurrences of a
given number in an array using pointers
#include<stdio.h>
int main(){
int arr[]={1,3,3};
int *Parr=arr;
int i,search=3,count=0;
for(i=0;i<=2;i++){
if(search==*Parr)
count++;
Parr++;
}
printf("It is present in %d times",count);
return 0;
}
23.Write a program to concatenate two strings
without using built-in string functions
#include<stdio.h>
int main(){
char Fstr[50]="abhishek";
char Sstr[10]=" prasad";
printf("First String is %s\n",Fstr);
printf("Second string is %s\n",Sstr);
int i,Strlen=0;
//for count the length of Fstr
for(i=0;Fstr[i]!='\0';i++){
Strlen++;
}
//i decrement because last element is
null that why i did that
Strlen--;
for(i=0;Sstr[i]!='\0';i++){
//adding Second string into First
Fstr[++Strlen]=Sstr[i];
}
Fstr[++Strlen]='\0';
printf("After Concantenate String is
%s",Fstr);
return 0;
}
24.Write a program to find the length of a string
without using built-in string functions
#include<stdio.h>
int main(){
char str[50]="abhishek";
int i,Strlen=0;
//for count the length of str
for(i=0;str[i]!='\0';i++){
Strlen++;
}
printf("String is %s and its length is
%d",str,Strlen);
return 0;
}
25.Write a program to reverse a string entered
by the user
#include<stdio.h>
#include<string.h>
int main(){
char str[50];
int i;
printf("Enter any String : ");
scanf("%s",str);
//for count the length of str
printf("after Reverse String is : ");
for(i=strlen(str)-1;i>=0;i--){
printf("%c",str[i]);
}
return 0;
}
26.Write a program to compare two strings
entered by the user using built-in string functions
#include<stdio.h>
int main(){
char Fstr[50];
char Sstr[50];
int i;
printf("Enter First String : ");
scanf("%s",Fstr);
printf("Enter Second String : ");
scanf("%s",Sstr);
if(strcmp(Fstr,Sstr)==0)
printf("%s and %s are equal",Fstr,Sstr);
else
printf("%s and %s are not equal",Fstr,Sstr);
return 0;
}
27.Write a program to define a structure to store
details of a student (name, roll number, marks)
and print these details
#include<stdio.h>
#include<string.h>
struct StuDetail{
char name[50];
int roll;
int marks;
};
int main(){
struct StuDetail Strvar;
strcpy(Strvar.name,"abhishek");
Strvar.roll=13;
Strvar.marks=234;
printf("Student Details\n");
printf("name : %s\n",Strvar.name);
printf("roll : %d\n",Strvar.roll);
printf("marks: %d",Strvar.marks);
return 0;
}