In: Computer Science
c statement to declare variables of primitive data types, and assign values to the variables
ans:
#include <stdio.h>
int main(){
int a=10;
float b=1.5;
char c='a';
char d[5]="hello";
double e=1.9393;
printf("a=%d\n",a);
printf("b=%f\n",b);
printf("c=%c\n",c);
printf("d=%s\n",d);
printf("e=%lf\n",e);
return 0;
}
Use common control structure in c, such as if, switch, break, for, while statements
ans:
#include <stdio.h>
int main(){
int i=0;
while(i<10){
if(i%2==0){
printf("even");
}else{
printf("odd");
}i++;
}
char a='d';
for(int i=0;i<10;i++)printf("%d ",i);
switch(a){
case 'a':
printf("\na is printed\n");break;
case 'd':
printf("\nd is printed\n");break;
default:
printf("\ndefault one is printed\n");
}
}
Write a c program that takes command line parameters, and perform some computation based on the command line input.
ans: which is product of 2 numbers
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]){
int a=atoi(argv[1]);
int b=atoi(argv[2]);
printf("%d x %d = %d\n",a,b,a*b);
return 0;
}
Write c statements to generate random number within a certain range
ans:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(int argc, char *argv[]){
int max_num=56;
int min_num=12;
srand(time(0));
int random_num_range = (rand() % (max_num - min_num+ 1)) + min_num;
printf("%d\n",random_num_range);
return 0;
}
Write a c statement to declare array variables of primitive data types, and assign values to the elements of array type variables.
ans:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(int argc, char *argv[]){
int a[4]={1,2,5,4};
double b[3]={1.2,4.5,8.9};
char c[3]={'a','b','d'};
float d[4]={3.4,2.4,5.6,1.2};
return 0;
}
Write a c statement to loop through an array to find a certain element, or the sum, maximum, minimum values of all array elements.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(int argc, char *argv[]){
int a[6]={4,6,9,2,3,8};
int min_index=0,max_index=0,sum=0;
for(int i=0;i<6;i++){
if(a[i]<a[min_index]){
min_index=i;
}
if(a[i]>a[max_index]){
max_index=i;
}
sum=sum+a[i];
}
printf("minimum element: %d\n",a[min_index]);
printf("maximum element: %d\n",a[max_index]);
printf("sum of element: %d\n",sum);
}
Read user input using scanf() function, and output contents of variables using printf() function.
ans:
#include <stdio.h>
int main(int argc, char *argv[]){
printf("Enter number:");
int n;
scanf("%d",&n);
printf("You enter number is %d\n",n);
return 0;
}
Declare pointer type of variables, and set a pointer to the address of a variable, or certain element of an array.
ans:
#include <stdio.h>
int main(int argc, char *argv[]){
int a[4]={1,2,5,8};
int *ptr;
ptr=a;
for(int i=0;i<4;i++){
printf("%d\n",ptr[i]);
}
}
Declare an array assign values to the entries in the array, display the values stored in the array.
Program using 2d arrays.
ans:
#include <stdio.h>
int main(int argc, char *argv[]){
int a[2][3]={{1,4,6},{6,8,9}};
for(int i=0;i<2;i++){
for(int j=0;j<3;j++){
printf("%d ",a[i][j]);
}printf("\n");
}
}